Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Sejoslaw/KD.FakeDb

Repository files navigation

KD.FakeDb

Service Status
AppVeyor Build status
Travis Build Status

In-memory Fake Database. Made specially for Unit Tests.

PROJECTS:

Project Name / Namespace Name Description
KD.FakeDb Main project. Contains core interfaces and default implementation.
KD.FakeDb.Connection Contains generic definition for parsing existing Database to IFakeDatabase.
KD.FakeDb.Connection.MSSQL Contains implementation for parsing Microsoft SQL (MSSQL) Database to IFakeDatabase.
KD.FakeDb.Connection.MySQL Contains implementation for parsing MySQL Database to IFakeDatabase.
KD.FakeDb.Converter.DataSet Extension method for IFakeDatabase to convert it to and from System.Data.DataSet.
KD.FakeDb.Export Contains generic definition of IFakeDatabase's Exporter.
KD.FakeDb.Export.Files IFakeDatabase's Exporter pre-configured for exporting to Files.
KD.FakeDb.Export.Files.CSV Contains implementation for exporting IFakeDatabase to CSV file.
KD.FakeDb.Factory Factory which should be used to dynamically create new IFakeDatabase.
KD.FakeDb.Linq Linq methods for IFakeDatabase.
KD.FakeDb.Serialization Contains abstract definition for parsing IFakeDatabase.
KD.FakeDb.Serialization.DataSet System.Data.DataSet Serialization for IFakeDatabase.
KD.FakeDb.Serialization.JSON JSON Serialization for IFakeDatabase.
KD.FakeDb.Serialization.XML XML Serialization for IFakeDatabase.
KD.FakeDb.XUnitTests Tests made with xUnit Framework.
KD.FakeDb.XUnitTests.Connection Tests with xUnit Framework made specially for different connections.

DEPENDENCY DIAGRAM (Where "Externals" are .NET Core libraries and Project-Required libraries. For example: "Newtonsoft.Json" for "KD.FakeDb.Serialization.JSON"):

DONE:

  • Added Factory for static and dynamic Fake Database creation.
  • Finished including remade Linq methods (Almost any basic FakeDb object implements IEnumerable).
  • Added importing / exporting IFakeDatabase to / from XML.
  • Added importing / exporting IFakeDatabase to / from JSON.
  • Added importing / exporting IFakeDatabase to / from System.Data.DataSet.
  • Added converting existing MySQL Database to IFakeDatabase.
  • Added converting existing Microsoft SQL (MSSQL) Database to IFakeDatabase.
  • Added exporting IFakeDatabase to CSV File Format.

TODO:

  • Add Events (OnCreate, OnUpdate, OnDelete, etc.) as an extension.
  • Add exporting IFakeDatabase to Excel File Format.
  • Add importing / exporting IFakeDatabase to / from CRM.
  • Add support for IFakeDatabase to read SQL Query.
  • Add support for reading other Databases (for instance: Oracle, PostreSQL, MongoDB, DB2, Microsoft Access, Cassandra, Redis, Elasticsearch, SQLite, MariaDB, Sybase, Teradata, Firebird, Derby, etc.).
  • Add support for Entity Framework to generate MDF file from IFakeDatabase.
  • Add Proxy Generator for given IFakeDatabase. In source and .exe file.

TUTORIALS:

  1. Create new Fake Database
  // New Fake Database should be created using Factory
  IFakeDatabase database = FakeDatabaseFactory.NewDatabase("New Test Database Name");

1.1. Add data to Fake Database

  public void Add_some_data_to_FakeDatabase()
  {
      // Create new Fake Database
      IFakeDatabase db = FakeDatabaseFactory.NewDatabase("Test Database");
      
      // Create and return new Fake Table named "Accounts"
      IFakeTable accTable = db.AddTable("Accounts");
      
      // Add few Fake Columns to Fake Table
      // Specify Fake Columns Name and Type of data which can be added to Column
      accTable.AddColumn("AccountId", typeof(Guid));
      accTable.AddColumn("FirstName", typeof(string));
      accTable.AddColumn("LastName", typeof(string));
      accTable.AddColumn("CountryName", typeof(string));
      
      // Add few Fake Rows to Fake Table
      
      // Fake Row is created and returned dynamically
      // You can add values to cells using Column Names
      // 0 is an index of a new Fake Row
      // If Fake Row with index 0 does not exists it will be created in real time; otherwise it will be returned
      IFakeRow fakeRow = accTable.GetRow(0); 
      fakeRow["AccountId"] = Guid.NewGuid();
      fakeRow["FirstName"] = "Krzysztof";
      fakeRow["LastName"] = "Dobrzynski";
      fakeRow["CountryName"] = "Poland";
      
      // You can also fill Cells using right Cell index
      fakeRow = accTable.GetRow(1);
      fakeRow[0] = Guid.NewGuid();
      fakeRow[1] = "Esteban";
      fakeRow[2] = "Hulio";
      fakeRow[3] = "Spain";
      
      fakeRow = accTable.GetRow(2);
      fakeRow["AccountId"] = Guid.NewGuid();
      fakeRow["FirstName"] = "A";
      fakeRow["LastName"] = "B";
      fakeRow["CountryName"] = "C";
      
      //...
  }
  1. Write Fake Database to XML (JSON looks similar)
  public void Try_to_write_Database_to_XML()
  {
      IFakeDatabase db = FakeDatabaseData.GetDatabaseWithData(); // Method used in tests to create new Fake Database and fill it with random data.
      FileStream fileStream = new FileStream("db.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
      using (XmlWriter writer = XmlWriter.Create(fileStream))
      {
          var serializer = new FakeDbSerializer<XmlReader, XmlWriter>() // Serializer with given Xml Parameters, used to read / write Fake Database
          {
              Database = db, // Database which will be written to file
              Configuration = new FakeDbXMLByColumnConfiguration() // Default configuration used to read / write Fake Database to / from XML File.
          };
          serializer.WriteDatabase(writer); // Write Fake Database to file
      }
  }
  1. Read Fake Database from XML (JSON looks similar)
  public void Try_to_read_Database_from_XML()
  {
      FileStream fileStream = new FileStream("db.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
      var serializer = new FakeDbSerializer<XmlReader, XmlWriter>() // Serializer with given Xml Parameters, used to read / write Fake Database
      {
          // Database property is not specified because it will be created dynamically
          Configuration = new FakeDbXMLByColumnConfiguration() // Default configuration used to read / write Fake Database to / from XML File.
      };
      serializer.ReadDatabase(XDocument.Load(fileStream).CreateReader()); // Reads Fake Database and save it in serializer property
      
      IFakeDatabase db = serializer.Database; // Database readed from XML file
  }
  1. Read data from outside Database (MySQL, MSSQL, etc.). Used Database is MSSQL.
  public void Test_if_database_was_mapped_to_Fake()
  {
      var dbConn = new DatabaseConnectionMSSQL() // Connection used to connect to MSSQL Database
      {
          Database = FakeDatabaseFactory.NewDatabase("Name_which_will_be_replaced_after_mapping"), // Fake Database must be given to Connector
          Connection = new SqlConnection() // Connection used to read data from Database
          {
              ConnectionString = $"" +
                  $"Server=mssql6.gear.host;" +
                  $"Database=testdb49;" +
                  $"User Id=testdb49;" +
                  $"Password=;"
          }
      };

      dbConn.ToFake("testdb49"); // This weird name is an actual Database Name. Database with this Name will be mapped to Connectors Fake Database.

      IFakeDatabase fakeDb = dbConn.Database; // Fake Database filled with data taken from MSSQL Database.
  }
  1. Convert IFakeDatabase to System.Data.DataSet
  using KD.FakeDb.Converter.DataSet;
  
  //...
  
  public void Test_if_IFakeDatabase_was_converted_to_DataSet()
  {
      IFakeDatabase fakeDb = FakeDatabaseFactory.NewDatabase("New Test Database Name"); // New Fake Database
      
      //...Fill Fake Database with some data...
      
      System.Data.DataSet dataSet = fakeDb.ToDataSet(); // Convert Fake Database to DataSet
      
      //...
  }
  1. Convert System.Data.DataSet to IFakeDatabase
  using KD.FakeDb.Converter.DataSet;
  
  //...
  
  public void Test_if_DataSet_was_converted_to_IFakeDatabase(System.Data.DataSet dataSet) // DataSet which will be converted to IFakeDatabase
  {
      // Create new IFakeDatabase
      IFakeDatabase fakeDatabase = FakeDatabaseFactory.NewDatabase("Some random Database name that will be replaced after fill from DataSet.");
      
      // Fill IFakeDatabase with DataSet values
      dataSet.ToFakeDatabase(fakeDatabase);
      
      //...Now IFakeDatabase is filled with values readed from DataSet...
  }
  1. Export IFakeDatabase to CSV file
  public void Export_to_CSV(IFakeDatabase fakeDb) // Fake Database which will be exported to CSV file
  {
      // Create new Exporter and specify the Configuration which should use
      var exporter = new FakeDbExporter<FileStream>()
      {
          Configuration = new FakeDbExporterCSVConfiguration()
      };
      
      // Create Stream for File
      // INFO: Exporter will require Write permission, otherwise it will thrown Exception
      FileStream fileStream = new FileStream("db.csv", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
      
      // Export given Fake Database with specified FileStream
      // INFO: Stream will be closed after exporting
      exporter.Export(fileStream, fakeDb);
  }