Beispiel #1
0
 /// <summary>
 /// Compares the contents of two IDataReader instances, optionally creating DataTables for further inspection of the data.
 /// </summary>
 /// <param name="tableName">The name of the table for which the data comparison was performed.</param>
 /// <param name="expectedData">A <see cref="IDataReader"/> containing the expected data.</param>
 /// <param name="actualData">A <see cref="IDataReader"/> containing the actual data.</param>
 /// <param name="uniqueConstraintFieldNames">An array of the names of the primary key fields to use as the basis for identifying corresponding rows between the two readers.</param>
 /// <param name="isPrimaryKey"></param>
 /// <param name="dataCopyMode">Indicates whether or not a <see cref="DataTable"/> should be created for each <see cref="IDataReader"/> (expected and actual) and the data copied in while processing the readers.</param>
 /// <returns>The results of the comparison.</returns>
 public DataComparerResults Compare(
     string tableName,
     IDataReader expectedData,
     IDataReader actualData,
     string[] uniqueConstraintFieldNames,
     bool isPrimaryKey,
     DataCopyMode dataCopyMode)
 {
     return(Compare(tableName, expectedData, actualData, uniqueConstraintFieldNames, isPrimaryKey, dataCopyMode, null));
 }
Beispiel #2
0
        /// <summary>
        /// Compares the contents of two IDataReader instances, ignoring the specified columns, optionally creating DataTables for further inspection of the data.
        /// </summary>
        /// <param name="tableName">The name of the table for which the data comparison was performed.</param>
        /// <param name="expectedData">A <see cref="IDataReader"/> containing the expected data.</param>
        /// <param name="actualData">A <see cref="IDataReader"/> containing the actual data.</param>
        /// <param name="uniqueConstraintFieldNames">An array of the names of the primary key fields to use as the basis for identifying corresponding rows between the two readers.</param>
        /// <param name="isPrimaryKey"></param>
        /// <param name="dataCopyMode">Indicates whether or not a <see cref="DataTable"/> should be created for each <see cref="IDataReader"/> (expected and actual) and the data copied in while processing the readers.</param>
        /// <param name="additionalFieldsToIgnore">Fields to ignore for the current comparison (mismatches in data values will not be counted as failures).</param>
        /// <returns>The results of the comparison.</returns>
        public DataComparerResults Compare(
            string tableName,
            IDataReader expectedData,
            IDataReader actualData,
            string[] uniqueConstraintFieldNames,
            bool isPrimaryKey,
            DataCopyMode dataCopyMode,
            string[] additionalFieldsToIgnore)
        {
            //Initialize an empty array if the parameter is null.
            additionalFieldsToIgnore = additionalFieldsToIgnore ?? new string[0];

            var dataComparerResults = new DataComparerResults {
                TableName = tableName
            };

            this.dataCopyMode = dataCopyMode;

            // Prepare data tables, if necessary
            if (dataCopyMode == DataCopyMode.All || dataCopyMode == DataCopyMode.DifferencesOnly)
            {
                DataTable expectedTable = InitializeDataTable(expectedData, uniqueConstraintFieldNames, isPrimaryKey);
                expectedTable.TableName          = "ExpectedDataTable";
                tablesByReader[expectedData]     = expectedTable;
                dataComparerResults.ExpectedData = expectedTable;

                DataTable actualTable = InitializeDataTable(actualData, uniqueConstraintFieldNames, isPrimaryKey);
                actualTable.TableName          = "ActualDataTable";
                tablesByReader[actualData]     = actualTable;
                dataComparerResults.ActualData = actualTable;
            }

            primaryKeyFields = new List <string>(uniqueConstraintFieldNames);

            InitializeColumnNamesFromReader(dataComparerResults.ColumnNames, actualData);

            VerifyThatAllNonTestColumnsFromExpectedDataArePresent(
                dataComparerResults.TableName,
                dataComparerResults.ColumnNames,
                expectedData,
                additionalFieldsToIgnore);

            // Initialize counts
            rowCountsByReader[expectedData] = 0;
            rowCountsByReader[actualData]   = 0;

            int[] expectedPKOrdinals =
                (from field in uniqueConstraintFieldNames
                 select expectedData.GetOrdinal(field)).ToArray();

            actualPKOrdinals =
                (from field in uniqueConstraintFieldNames
                 select actualData.GetOrdinal(field)).ToArray();

            DbDataReaderComparer comparer = new DbDataReaderComparer(expectedPKOrdinals, actualPKOrdinals);

            CompareAndDisposeDataReaders(comparer, dataComparerResults, actualData, expectedData, additionalFieldsToIgnore);

            // Set counts
            dataComparerResults.ExpectedRowCount = rowCountsByReader[expectedData];
            dataComparerResults.ActualRowCount   = rowCountsByReader[actualData];

            return(dataComparerResults);
        }