115 lines
4.5 KiB
C#
115 lines
4.5 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Data.SqlClient;
|
||
|
|
using DatabaseUnitTesting.Utilities;
|
||
|
|
using DatabaseUnitTesting.Utilities.Results;
|
||
|
|
|
||
|
|
namespace DatabaseUnitTesting
|
||
|
|
{
|
||
|
|
public class DatabaseModificationTester : IDisposable
|
||
|
|
{
|
||
|
|
|
||
|
|
private readonly DatabaseComparer _dataComparer;
|
||
|
|
private SqlTransaction _transaction;
|
||
|
|
private SqlConnection _connection;
|
||
|
|
private readonly string _snapshotName;
|
||
|
|
private readonly DatabaseAdapter _databaseAdapter;
|
||
|
|
private bool _activeSnapshot = false;
|
||
|
|
/// <summary>
|
||
|
|
/// Used to establish the objects required to create a unit test running compares between a stored XML database file and an SQL transaction.
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="connection"></param>
|
||
|
|
/// <param name="databaseName"></param>
|
||
|
|
/// <param name="snapshotName"></param>
|
||
|
|
public DatabaseModificationTester(SqlConnection connection, string databaseName, string snapshotName)
|
||
|
|
{
|
||
|
|
_connection = connection;
|
||
|
|
_snapshotName = snapshotName;
|
||
|
|
_dataComparer = new DatabaseComparer(connection, databaseName, snapshotName);
|
||
|
|
_databaseAdapter = new DatabaseAdapter(connection);
|
||
|
|
_databaseAdapter.CreateSnapshot(databaseName, snapshotName);
|
||
|
|
_activeSnapshot = true;
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// establishes the command transaction to be compared and rolled back on end
|
||
|
|
/// </summary>
|
||
|
|
/// <returns></returns>
|
||
|
|
public SqlTransaction BeginTestTransaction()
|
||
|
|
{
|
||
|
|
if (_transaction != null)
|
||
|
|
EndTestTransaction();
|
||
|
|
|
||
|
|
return (_transaction = _connection.BeginTransaction());
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// Once the compare is complete this method cleans up, rollsback, and disposes the transaction
|
||
|
|
/// </summary>
|
||
|
|
public void EndTestTransaction()
|
||
|
|
{
|
||
|
|
if (_transaction == null)
|
||
|
|
throw new InvalidOperationException("Transaction does not exist");
|
||
|
|
|
||
|
|
_dataComparer.CleanUp();
|
||
|
|
_transaction.Rollback();
|
||
|
|
_transaction.Dispose();
|
||
|
|
_transaction = null;
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// Method to write XML file to use during unit testing. Writes differences between current database and the completed transaction.
|
||
|
|
/// The file generated here should be manually verified
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="filename"></param>
|
||
|
|
public void WriteDiffsToXml(string filename)
|
||
|
|
{
|
||
|
|
XmlFileAdapter.Write(filename, _dataComparer.GenerateDifferences(_transaction));
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// Return true/false based on compare of transaction and stored XML expected test results.
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="filename"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public bool CompareDiffsToXml(string filename)
|
||
|
|
{
|
||
|
|
return XmlFileAdapter.Read(filename).Equals(_dataComparer.GenerateDifferences(_transaction));
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool AreEqual()
|
||
|
|
{
|
||
|
|
return _dataComparer.GenerateDifferences(_transaction).TableCount == 0;
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// Enables test to ignore defined columns in database tables
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="schemaName"></param>
|
||
|
|
/// <param name="objectName"></param>
|
||
|
|
/// <param name="columnName"></param>
|
||
|
|
public void AddColumnToIgnore(string schemaName, string objectName, string columnName)
|
||
|
|
{
|
||
|
|
_dataComparer.AddColumnToIgnore(schemaName, objectName, columnName);
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// Enables test to ignore defined columns in database tables
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="schema1"></param>
|
||
|
|
/// <param name="name1"></param>
|
||
|
|
/// <param name="columnNames"></param>
|
||
|
|
public void AddColumnsToIgnore(string schema1, string name1, List<string> columnNames)
|
||
|
|
{
|
||
|
|
_dataComparer.AddColumnsToIgnore(schema1, name1, columnNames);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void AddObjectComparison(string schemaName, string tableName)
|
||
|
|
{
|
||
|
|
_dataComparer.AddObjectComparison(schemaName, tableName, schemaName, tableName);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Dispose()
|
||
|
|
{
|
||
|
|
if (_activeSnapshot)
|
||
|
|
{
|
||
|
|
_activeSnapshot = false;
|
||
|
|
_databaseAdapter.DropSnapshot(_snapshotName);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|