예제 #1
0
        private void AssertRowsInternal(IDAC dac, string tableName, IEnumerable <IEnumerable <object> > expectedRows, bool anyOrder)
        {
            var tableRows = dac.Select(tableName).Rows.Cast <DataRow>().Select(r => r.ItemArray as IEnumerable <object>);

            if (anyOrder)
            {
                expectedRows = expectedRows.OrderByAll();
                tableRows    = tableRows.OrderByAll();
            }
            NUnitTool.AssertSame2DArrays(expectedRows, tableRows, tableName);
        }
예제 #2
0
        private void AssertSameTableDataInternal(IDAC source, IDAC dest, string tableName, bool primaryKeyMustMatch)
        {
            var sourceTableSchema = source.GetSchemaCached()[tableName];
            var destTableSchema   = dest.GetSchemaCached()[tableName];

            Assert.AreNotEqual(DBKeyType.None, sourceTableSchema.PrimaryKey.KeyType, "Tables without primary keys are not supported");
            Assert.AreEqual(sourceTableSchema.PrimaryKey.KeyType, destTableSchema.PrimaryKey.KeyType, "Table primary key types do not match");

            var sourceData = source.Select(tableName).Rows.Cast <DataRow>().Select(r => r.ItemArray.Select(Object.SanitizeObject).ToArray()).ToArray();
            var destData   = dest.Select(tableName).Rows.Cast <DataRow>().Select(r => r.ItemArray.Select(Object.SanitizeObject).ToArray()).ToArray();

            var primaryKeysNotInDest   = new IEnumerable <object> [0];
            var primaryKeysNotInSource = new IEnumerable <object> [0];

            // Validate data by primary keys
            if (primaryKeyMustMatch)
            {
                // Gather data into primary key look up
                var sourceDataDict = sourceTableSchema.ConvertDataToMultiKeyDictionary(sourceData);
                var destDataDict   = destTableSchema.ConvertDataToMultiKeyDictionary(destData);

                // Validate that primary keys map exactly
                primaryKeysNotInDest   = sourceDataDict.Keys.Except(destDataDict.Keys, new EnumerableSequenceEqualComparer <object>()).ToArray();
                primaryKeysNotInSource = destDataDict.Keys.Except(sourceDataDict.Keys, new EnumerableSequenceEqualComparer <object>()).ToArray();

                // Print data missing from dest (by PK)
                if (primaryKeysNotInDest.Any())
                {
                    Console.WriteLine("Primary key missing from dest:");
                    foreach (var key in primaryKeysNotInDest.Select(k => k.ToArray()))
                    {
                        Console.WriteLine("\tPK:({0}) Data:{1}".FormatWith(key.ToDelimittedString(", "), sourceDataDict[key].ToDelimittedString(", ")));
                    }
                }
                Console.WriteLine();

                // Print data missing from source (by PK)
                if (primaryKeysNotInSource.Any())
                {
                    Console.WriteLine("Primary key missing from source:");
                    foreach (var key in primaryKeysNotInSource.Select(k => k.ToArray()))
                    {
                        Console.WriteLine("\tPK:({0}) Data:{1}".FormatWith(key.ToDelimittedString(", "), sourceDataDict[key].ToDelimittedString(", ")));
                    }
                }
                Console.WriteLine();
            }

            var primaryKeyColCount  = sourceTableSchema.PrimaryKeyColumns.Length;
            var sourceDataToCompare = primaryKeyMustMatch ? sourceData : sourceData.Select(rowArr => rowArr.Skip(primaryKeyColCount).ToArray()).ToArray();
            var destDataToCompare   = primaryKeyMustMatch ? destData : destData.Select(rowArr => rowArr.Skip(primaryKeyColCount).ToArray()).ToArray();

            // print missing data
            var rowsNotInDestByValue   = sourceDataToCompare.Except(destDataToCompare, new EnumerableSequenceEqualComparer <object>()).ToArray();
            var rowsNotInSourceByValue = destDataToCompare.Except(sourceDataToCompare, new EnumerableSequenceEqualComparer <object>()).ToArray();

            // Print data missing from dest
            if (rowsNotInDestByValue.Any())
            {
                Console.WriteLine(NUnitTool.Convert2DArrayToString("Data missing from Dest", rowsNotInDestByValue));
            }
            Console.WriteLine();

            // Print data missing from source
            if (rowsNotInSourceByValue.Any())
            {
                Console.WriteLine(NUnitTool.Convert2DArrayToString("Data missing from Source", rowsNotInSourceByValue));
            }
            Console.WriteLine();

            // Assert primary keys are present in both
            if (primaryKeyMustMatch)
            {
                NUnitTool.IsEmpty(primaryKeysNotInDest, "Dest missing primary keys");
                NUnitTool.IsEmpty(primaryKeysNotInSource, "Source missing primary keys");
            }

            NUnitTool.IsEmpty(rowsNotInDestByValue, "Dest missing data");
            NUnitTool.IsEmpty(rowsNotInSourceByValue, "Source missing data");
        }