Ejemplo n.º 1
0
        public static IDictionary <string, long> CountRecords(this IDAC dac, IEnumerable <string> tableNames = null)
        {
            var result = new Dictionary <string, long>();

            if (tableNames == null)
            {
                tableNames = dac.GetSchemaCached().Tables.Select(table => table.Name);
            }

            if (!tableNames.Any())
            {
                return(result);
            }

            var sqlBuilder = dac.CreateSQLBuilder();

            foreach (var table in tableNames.WithDescriptions())
            {
                if (table.Index > 0)
                {
                    sqlBuilder.NewLine().Emit("UNION ALL").NewLine();
                }
                sqlBuilder.Emit("SELECT '{0}', COUNT(1) FROM {1}", table.Item, SQLBuilderCommand.TableName(table.Item));
            }

            dac
            .ExecuteQuery(((Object)sqlBuilder).ToString())
            .Rows
            .Cast <DataRow>()
            .ForEach(row => result.Add(row.Get <string>(0), row.Get <long>(1)));
            return(result);
        }
Ejemplo n.º 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");
        }