DataLinkTests.cs :  » Persistence-Frameworks » FileHelpers-Library » FileHelpersTests » DataLink » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Persistence Frameworks » FileHelpers Library 
FileHelpers Library » FileHelpersTests » DataLink » DataLinkTests.cs
#if ! MINI

using System;
using System.Data.OleDb;
using FileHelpers;
using FileHelpers.DataLink;
using NUnit.Framework;

namespace FileHelpersTests.DataLink{
  [TestFixture]
  public class DataLinks
  {
    FileDataLink mLink;


    #region "  FillRecordOrders  "

    
    protected void FillRecordOrders(object rec, object[] fields)
    {
      OrdersFixed record = (OrdersFixed) rec;

      record.OrderID = (int) fields[0];
      record.CustomerID = (string) fields[1];
      record.EmployeeID = (int) fields[2];
      record.OrderDate = (DateTime) fields[3];
      record.RequiredDate = (DateTime) fields[4];
      if (fields[5] != DBNull.Value)
        record.ShippedDate = (DateTime) fields[5];
      else
        record.ShippedDate = DateTime.MinValue;
      record.ShipVia = (int) fields[6];
      record.Freight = (decimal) fields[7];

    }

    #endregion

    [Test]
    public void OrdersDbToFile()
    {
      AccessStorage storage = new AccessStorage(typeof(OrdersFixed), @"..\data\TestData.mdb");
      storage.SelectSql = "SELECT * FROM Orders";
      storage.FillRecordCallback = new FillRecordHandler(FillRecordOrders);

      mLink = new FileDataLink(storage);
      mLink.ExtractToFile(@"..\data\temp.txt");
      int extractNum = mLink.LastExtractedRecords.Length;

      OrdersFixed[] records = (OrdersFixed[]) mLink.FileHelperEngine.ReadFile(@"..\data\temp.txt");

      Assert.AreEqual(extractNum, records.Length);
    }


    [Test]
    public void OrdersDbToFileEasy()
    {
      AccessStorage storage = new AccessStorage(typeof(OrdersFixed), @"..\data\TestData.mdb");
      storage.SelectSql = "SELECT * FROM Orders";
      storage.FillRecordCallback = new FillRecordHandler(FillRecordOrders);

      OrdersFixed[] records = (OrdersFixed[]) FileDataLink.EasyExtractToFile(storage,@"..\data\temp.txt");
      
      int extractNum = records.Length;

      records = (OrdersFixed[]) CommonEngine.ReadFile(typeof(OrdersFixed), @"..\data\temp.txt");

      Assert.AreEqual(extractNum, records.Length);
    }

    
    private void FillRecordCustomers(object rec, object[] fields)
    {
      CustomersVerticalBar record = (CustomersVerticalBar) rec;

      record.CustomerID = (string) fields[0];
      record.CompanyName = (string) fields[1];
      record.ContactName = (string) fields[2];
      record.ContactTitle = (string) fields[3];
      record.Address = (string) fields[4];
      record.City = (string) fields[5];
      record.Country = (string) fields[6];
    }

    [Test]
    public void CustomersDbToFile()
    {
      AccessStorage storage = new AccessStorage(typeof (CustomersVerticalBar), @"..\data\TestData.mdb");
      storage.SelectSql =  "SELECT * FROM Customers";
      storage.FillRecordCallback = new FillRecordHandler(FillRecordCustomers);

      mLink = new FileDataLink(storage);
      mLink.ExtractToFile(@"..\data\temp.txt");
      int extractNum = mLink.LastExtractedRecords.Length;

      CustomersVerticalBar[] records = (CustomersVerticalBar[]) mLink.FileHelperEngine.ReadFile(@"..\data\temp.txt");

      Assert.AreEqual(extractNum, records.Length);
    }

    private object FillRecord(object[] fields)
    {
      CustomersVerticalBar record = new CustomersVerticalBar();

      record.CustomerID = (string) fields[0];
      record.CompanyName = (string) fields[1];
      record.ContactName = (string) fields[2];
      record.ContactTitle = (string) fields[3];
      record.Address = (string) fields[4];
      record.City = (string) fields[5];
      record.Country = (string) fields[6];

      return record;
    }



    #region "  GetInsertSql  "

    protected string GetInsertSqlCust(object record)
    {
      CustomersVerticalBar obj = (CustomersVerticalBar) record;

      return String.Format("INSERT INTO CustomersTemp (Address, City, CompanyName, ContactName, ContactTitle, Country, CustomerID) " +
        " VALUES ( \"{0}\" , \"{1}\" , \"{2}\" , \"{3}\" , \"{4}\" , \"{5}\" , \"{6}\"  ); ",
        obj.Address,
        obj.City,
        obj.CompanyName,
        obj.ContactName,
        obj.ContactTitle,
        obj.Country,
        obj.CustomerID
        );

    }

    #endregion

    [Test]
    public void CustomersFileToDb()
    {
      AccessStorage storage = new AccessStorage(typeof(CustomersVerticalBar), @"..\data\TestData.mdb");
      storage.InsertSqlCallback = new InsertSqlHandler(GetInsertSqlCust);

      mLink = new FileDataLink(storage);
      ClearData(((AccessStorage) mLink.DataStorage).AccessFileName, "CustomersTemp");

      int count = Count(((AccessStorage) mLink.DataStorage).AccessFileName, "CustomersTemp");
      Assert.AreEqual(0, count);

      mLink.InsertFromFile(@"..\data\UpLoadCustomers.txt");

      count = Count(((AccessStorage) mLink.DataStorage).AccessFileName, "CustomersTemp");
      Assert.AreEqual(91, count);

      ClearData(((AccessStorage) mLink.DataStorage).AccessFileName, "CustomersTemp");
    }


      
    protected object FillRecordOrder(object[] fields)
    {
      OrdersFixed record = new OrdersFixed();

      record.OrderID = (int) fields[0];
      record.CustomerID = (string) fields[1];
      record.EmployeeID = (int) fields[2];
      record.OrderDate = (DateTime) fields[3];
      record.RequiredDate = (DateTime) fields[4];
      if (fields[5] != DBNull.Value)
        record.ShippedDate = (DateTime) fields[5];
      else
        record.ShippedDate = DateTime.MinValue;
      record.ShipVia = (int) fields[6];
      record.Freight = (decimal) fields[7];

      return record;
    }

    
    #region "  GetInsertSql  "

    protected string GetInsertSqlOrder(object record)
    {
      OrdersFixed obj = (OrdersFixed) record;

      return String.Format("INSERT INTO OrdersTemp (CustomerID, EmployeeID, Freight, OrderDate, OrderID, RequiredDate, ShippedDate, ShipVia) " +
        " VALUES ( \"{0}\" , \"{1}\" , \"{2}\" , \"{3}\" , \"{4}\" , \"{5}\" , \"{6}\" , \"{7}\"  ) ",
        obj.CustomerID,
        obj.EmployeeID,
        obj.Freight,
        obj.OrderDate,
        obj.OrderID,
        obj.RequiredDate,
        obj.ShippedDate,
        obj.ShipVia
        );

    }

    #endregion

    [Test]
    public void OrdersFileToDb()
    {

      AccessStorage storage = new AccessStorage(typeof(OrdersFixed), @"..\data\TestData.mdb");
      storage.InsertSqlCallback = new InsertSqlHandler(GetInsertSqlOrder);

      mLink = new FileDataLink(storage);
      ClearData(((AccessStorage) mLink.DataStorage).AccessFileName, "OrdersTemp");

      int count = Count(((AccessStorage) mLink.DataStorage).AccessFileName, "OrdersTemp");
      Assert.AreEqual(0, count);

      mLink.InsertFromFile(@"..\data\UpLoadOrders.txt");

      count = Count(((AccessStorage) mLink.DataStorage).AccessFileName, "OrdersTemp");
      Assert.AreEqual(830, count);

      ClearData(((AccessStorage) mLink.DataStorage).AccessFileName, "OrdersTemp");
    }

    private const string AccessConnStr = @"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Database Password=;Data Source=""<BASE>"";Password=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk Transactions=1;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=False";

    public void ClearData(string fileName, string table)
    {
      string conString = AccessConnStr.Replace("<BASE>", fileName);
      OleDbConnection conn = new OleDbConnection(conString);
      OleDbCommand cmd = new OleDbCommand("DELETE FROM " + table, conn);
      conn.Open();
      cmd.ExecuteNonQuery();
      conn.Close();
      int count = Count(((AccessStorage) mLink.DataStorage).AccessFileName, "OrdersTemp");
    }

    public int Count(string fileName, string table)
    {
      string conString = AccessConnStr.Replace("<BASE>", fileName);
      OleDbConnection conn = new OleDbConnection(conString);
      OleDbCommand cmd = new OleDbCommand("SELECT COUNT (*) FROM " + table, conn);
      conn.Open();
      int res = (int) cmd.ExecuteScalar();
      conn.Close();
      return res;
    }

  }
}

#endif
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.