public void SubstitutionsCanBeDisabled()
        {
            //Arrange
            var beforeData = new[]
            {
                new { Name = "Test 1", A = 5, B = 6, C = 7 },
                new { Name = "Test 2", A = 5, B = 6, C = 7 },
                new { Name = "Test 3", A = 3, B = 7, C = 6 },
            };
            var afterData = new[]
            {
                new { Name = "Test 1", A = 5, B = 6, C = 7 },
                new { Name = "Test 2", A = 8, B = 6, C = 3 },
                new { Name = "Test 4", A = 10, B = 7, C = 6 },
            };

            var beforeBuilder = _collection.NewSnapshot("before");

            beforeData.ToSnapshotTable(beforeBuilder, "Table", "Name");
            var afterBuilder = _collection.NewSnapshot("after");

            afterData.ToSnapshotTable(afterBuilder, "Table");
            _collection.DefineTable("Table").IsUnpredictable("A").IsUnpredictable("B").IsUnpredictable("C");

            var output = new Output();

            //Act
            SnapshotComparer.ReportDifferences(_collection, _collection.GetSnapshot("before"), _collection.GetSnapshot("after"), output, ChangeReportOptions.NoSubs);

            //Assert
            output.Report.Verify();
        }
Beispiel #2
0
        public void SubstitutionsCanBeDisabledInDiffReports()
        {
            //Arrange
            var snapshots = new SnapshotCollection();

            snapshots.DefineTable("Customer")
            .PrimaryKey("Id")
            .CompareKey("Surname")
            .IsUnpredictable("Id");
            snapshots.DefineTable("Address")
            .PrimaryKey("AddressId")
            .IsUnpredictable("AddressId")
            .IsReference("CustomerId", "Customer", "Id");

            var customers = new[]
            {
                new { Id = 1, Surname = "S1", FirstName = "F1", Age = 40 },
                new { Id = 2, Surname = "S2", FirstName = "F2", Age = 45 },
                new { Id = 3, Surname = "S3", FirstName = "F3", Age = 50 },
            };

            var addresses = new[]
            {
                new { AddressId = 102, CustomerId = 1, House = 15, Street = "Line 1", PostCode = "code1" },
                new { AddressId = 193, CustomerId = 2, House = 99, Street = "Line 2", PostCode = "code2" },
                new { AddressId = 6985, CustomerId = 3, House = 8000, Street = "Line 3", PostCode = "code3" }
            };

            {
                var builder = snapshots.NewSnapshot("Before");
                customers.ToSnapshotTable(builder, "Customer");
                addresses.ToSnapshotTable(builder, "Address");
            }

            customers[1] = new { Id = 2, Surname = "S2", FirstName = "F2Edited", Age = 32 };
            addresses[2] = new { AddressId = 6985, CustomerId = 2, House = 8001, Street = "Line 3", PostCode = "code3" };

            {
                var builder2 = snapshots.NewSnapshot("After");
                customers.ToSnapshotTable(builder2, "Customer");
                addresses.ToSnapshotTable(builder2, "Address");
            }

            //Act/Assert
            var output = new Output();

            snapshots.ReportChanges("Before", "After", output, ChangeReportOptions.NoSubs);
            output.Report.Verify();
        }
Beispiel #3
0
        public void TablesAreDefined()
        {
            //Arrange
            var snapshots = new SnapshotCollection();

            //Act
            snapshots.DefineTable("Customer").PrimaryKey("Id").CompareKey("Surname");
            snapshots.DefineTable("Address").PrimaryKey("AddressId");

            //Assert
            var output = new Output();

            snapshots.GetSchemaReport(output);
            output.Report.Verify();
        }
Beispiel #4
0
        public void SnapshotRowsInMissingTablesAreNull()
        {
            //Arrange
            var snapshots = new SnapshotCollection(GetType(), t => t != typeof(Snapshots.TestDefinitions.TableDef));

            snapshots.DefineTable("Customer")
            .PrimaryKey("Id")
            .CompareKey("Surname")
            .IsUnpredictable("Id");

            var builder    = snapshots.NewSnapshot("Before");
            var rowBuilder = builder.AddNewRow("Customer");

            rowBuilder["Id"]      = 1;
            rowBuilder["Surname"] = "surname";

            var snapshot = snapshots.GetSnapshot("Before");

            //Act
            var row    = snapshot.Rows("Customer").First();
            var result = snapshot.GetRow(new SnapshotRowKey(row, snapshots.GetTableDefinition("Customer")), "Wrong");

            //Assert
            result.Should().BeNull();
        }
Beispiel #5
0
 public void SetUp()
 {
     _collection = new SnapshotCollection();
     _collection.DefineTable("Test").CompareKey("Key");
     _snapshot = _collection.NewSnapshot("TestSnapshot");
     _row      = _snapshot.AddNewRow("Test");
 }
Beispiel #6
0
        public void ReportChangesThrowsWhenAfterSnapshotNotPresent()
        {
            //Arrange/Act
            var snapshots = new SnapshotCollection(GetType(), t => t != typeof(Snapshots.TestDefinitions.TableDef));

            snapshots.DefineTable("Customer")
            .PrimaryKey("Id")
            .CompareKey("Surname")
            .IsUnpredictable("Id");

            var customers = new[]
            {
                new { Id = 1, Surname = "S1", FirstName = "F1", Age = 40 },
            };
            var builder = snapshots.NewSnapshot("Before");

            customers.ToSnapshotTable(builder, "Customer");

            var output = new Output();

            //Assert
            Action action = () => snapshots.ReportChanges("Before", "After", output);

            action.Should().Throw <SnapshotNotFoundException>().Where(x => x.SnapshotName == "After");
        }
 public void SetUp()
 {
     _collection = new SnapshotCollection();
     _collection.DefineTable(TableName).PrimaryKey(IdCol).PrimaryKey(NameCol);
     _tableDef        = _collection.GetTableDefinition(TableName);
     _snapshotBuilder = _collection.NewSnapshot("Test");
 }
Beispiel #8
0
        public static void Define(SnapshotCollection collection, SchemaStructure schema)
        {
            foreach (var table in schema.Tables)
            {
                var tableDef = collection.DefineTable(table.Name);
                foreach (var column in table.Columns.Where(c => c.InPrimaryKey))
                {
                    tableDef.PrimaryKey(column.Name);
                    if (column.IsUnpredictable)
                    {
                        tableDef.IsUnpredictable(column.Name);
                    }

                    if (column.IsUtcDateTime)
                    {
                        tableDef.Utc(column.Name);
                    }

                    if (column.IsLocalDateTime)
                    {
                        tableDef.Local(column.Name);
                    }
                }

                foreach (var reference in table.References)
                {
                    tableDef.IsReference(reference.ReferencingColumnNames.First(), $"[{reference.ReferencedSchema}].[{reference.ReferencedTable}]", reference.ReferencedColumnNames.First());
                }
            }
        }
 internal TestData()
 {
     _collection = new SnapshotCollection();
     Definer     = _collection.DefineTable(TableName);
     Builder     = _collection.NewSnapshot("Test");
     Snapshot    = _collection.GetSnapshot("Test");
     NewRow();
 }
Beispiel #10
0
        public void SetUp()
        {
            _collection = new SnapshotCollection();
            _collection.DefineTable(CompareKeyTableName).CompareKey(NameCol).PrimaryKey(IdCol);
            _collection.DefineTable(PrimaryKeyTableName).PrimaryKey(IdCol);
            _collection.DefineTable(CompoundKeyTableName).PrimaryKey(IdCol).PrimaryKey(NameCol);
            var builder = _collection.NewSnapshot("Test");

            for (var n = 1; n <= 10; n++)
            {
                var ckRow = builder.AddNewRow(CompareKeyTableName);
                var pkRow = builder.AddNewRow(PrimaryKeyTableName);
                var cpRow = builder.AddNewRow(CompoundKeyTableName);
                PopulateRows(n, ckRow, pkRow, cpRow);
            }

            _snapshot = _collection.GetSnapshot("Test");
        }
        public void DateDiagnosticsAreReported()
        {
            //Arrange
            var collection = new SnapshotCollection();

            var beforeDates = new[]
            {
                DateTime.Parse("2020-10-11 10:35"),
                DateTime.Parse("2020-10-11 10:36"),
                DateTime.Parse("2020-10-11 10:37"),
                DateTime.Parse("2020-10-11 10:38"),
            };

            var afterDates = new[]
            {
                DateTime.Parse("2020-10-11 10:35"),
                DateTime.Parse("2020-10-11 10:36"),
                DateTime.Parse("2020-10-11 10:39"),
                DateTime.Parse("2020-10-11 10:40"),
                DateTime.Parse("2020-10-11 10:41"),
            };

            var beforeBuilder = collection.NewSnapshot("before");

            collection.DefineTable("Dates").PrimaryKey("Key").IsUnpredictable("Date");

            beforeDates.Select((bd, ix) => new { Key = ix, Date = bd, Other = "o", OtherDate = DateTime.MinValue }).ToSnapshotTable(beforeBuilder, "Dates");
            var deleteRow = beforeBuilder.AddNewRow("Dates");

            deleteRow["Key"]       = 100;
            deleteRow["Date"]      = DateTime.Parse("2020-10-18 11:19");
            deleteRow["Other"]     = "o";
            deleteRow["OtherDate"] = DateTime.MinValue;

            var afterBuilder = collection.NewSnapshot("after");

            afterDates.Select((bd, ix) => new { Key = ix, Date = bd, Other = "a", OtherDate = DateTime.MaxValue }).ToSnapshotTable(afterBuilder, "Dates");

            var before      = collection.GetSnapshot("before");
            var after       = collection.GetSnapshot("after");
            var differences = SnapshotDifferenceAnalyser.ExtractDifferences(collection, before, after);

            var output = new Output();

            //Act
            var ranges = new List <NamedTimeRange>
            {
                new NamedTimeRange(beforeDates[0], beforeDates[3], "before"),
                new NamedTimeRange(afterDates[2], afterDates[3], "after"),
            };

            DateDiagnosticReporter.Report(output, ranges, before, after, differences);

            //Assert
            output.Report.Verify();
        }
Beispiel #12
0
        public void GetSnapshotReportThrowsForMissingSnapshot()
        {
            //Arrange
            var snapshots = new SnapshotCollection();

            snapshots.DefineTable("Customer")
            .PrimaryKey("Id")
            .CompareKey("Surname")
            .IsUnpredictable("Id");
            snapshots.DefineTable("Address")
            .PrimaryKey("AddressId")
            .IsUnpredictable("AddressId")
            .IsReference("CustomerId", "Customer", "Id");

            //Act/Assert
            var    output = new Output();
            Action test   = () => snapshots.GetSnapshotReport("Before", output);

            test.Should().Throw <SnapshotNotFoundException>();
        }
Beispiel #13
0
        public void SelectedSnapshotContentsCanBeReported()
        {
            //Arrange
            var snapshots = new SnapshotCollection();

            snapshots.DefineTable("Customer")
            .PrimaryKey("Id")
            .CompareKey("Surname")
            .IsUnpredictable("Id");
            snapshots.DefineTable("Address")
            .PrimaryKey("AddressId")
            .IsUnpredictable("AddressId")
            .IsReference("CustomerId", "Customer", "Id");

            var customers = new[]
            {
                new { Id = 1, Surname = "S1", FirstName = "F1", Age = 40 },
                new { Id = 2, Surname = "S2", FirstName = "F2", Age = 45 },
                new { Id = 3, Surname = "S3", FirstName = "F3", Age = 50 },
            };

            var addresses = new[]
            {
                new { AddressId = 102, CustomerId = 1, House = 15, Street = "Line 1", PostCode = "code1" },
                new { AddressId = 193, CustomerId = 2, House = 99, Street = "Line 2", PostCode = "code2" },
                new { AddressId = 6985, CustomerId = 3, House = 8000, Street = "Line 3", PostCode = "code3" }
            };

            {
                var builder = snapshots.NewSnapshot("Before");
                customers.ToSnapshotTable(builder, "Customer");
                addresses.ToSnapshotTable(builder, "Address");
            }

            //Act/Assert
            var output = new Output();

            snapshots.GetSnapshotReport("Before", output, "Address");
            output.Report.Verify();
        }
Beispiel #14
0
        public void SnapshotShouldNotConsiderExcludedTables()
        {
            //Arrange
            var before = MakeSnapshot("Test", 1, 1, MakeValueSet(5));
            var after  = MakeSnapshot("Test2", 1, 1, MakeValueSet(6));

            _collection.DefineTable(TableName).ExcludeFromComparison();

            //Act
            var result = SnapshotDifferenceAnalyser.Match(_collection, before, after);

            //Assert
            result.Should().BeTrue(); //it can only be a match if the table is ignored
        }
        public void SetUp()
        {
            _collection = new SnapshotCollection();
            _collection.DefineTable(TestTableName).CompareKey("Id");
            _table    = _collection.GetTableDefinition(TestTableName);
            _snapshot = _collection.NewSnapshot("Test");

            //Borrow a column config so that we can test with it.
            void ExtractColumnDefForTest(ColumnConfig config)
            {
                _cc = config;
            }

            var report = new[] { "T" }.AsReport(rep => rep.AddColumn(t => t, ExtractColumnDefForTest));
        }
Beispiel #16
0
        public void GetSnapshotReturnsTheRequestedSnapshot()
        {
            //Arrange/Act
            var snapshots = new SnapshotCollection(GetType(), t => t != typeof(Snapshots.TestDefinitions.TableDef));

            snapshots.DefineTable("Customer")
            .PrimaryKey("Id")
            .CompareKey("Surname")
            .IsUnpredictable("Id");

            var builder = snapshots.NewSnapshot("Before");

            //Assert
            snapshots.GetSnapshot("Before").Name.Should().Be("Before");
        }
Beispiel #17
0
        public void SubstitutionsCanBeSuppressed()
        {
            //Arrange
            var collection = new SnapshotCollection();

            collection.DefineTable("TheTable")
            .PrimaryKey("Id")
            .IsUnpredictable("Id");

            void AddRow(SnapshotBuilder snapshot, int id, string name)
            {
                var add = snapshot.AddNewRow("TheTable");

                add["Id"]   = id;
                add["Name"] = name;
            }

            void AddData(SnapshotBuilder snapshot, string names)
            {
                var id = 1;

                foreach (var name in names.Split(','))
                {
                    AddRow(snapshot, id++, name);
                }
            }

            var beforeBuilder = collection.NewSnapshot("Before");

            AddData(beforeBuilder, "A,B,C");

            var afterBuilder = collection.NewSnapshot("After");

            AddData(afterBuilder, "D,E,F");

            var before = collection.GetSnapshot("Before");
            var after  = collection.GetSnapshot("After");
            var diffs  = SnapshotDifferenceAnalyser.ExtractDifferences(collection, before, after, ChangeReportOptions.NoSubs);

            //Act
            var result = SnapshotDifferenceSorter.SortDifferences(collection, diffs.TableDifferences.ToList());

            //Assert
            var output = new Output();

            result.Report(output);
            output.Report.Verify();
        }
Beispiel #18
0
        public TableDefiner DefineTable(string tableName)
        {
            lock (_lock)
            {
                if (_snapshotTaken)
                {
                    throw new ConfigurationCannotBeChangedException();
                }

                if (!_collectionConfigured)
                {
                    ConfigureCollection();
                }
                return(_collection.DefineTable(tableName));
            }
        }
Beispiel #19
0
        public void SnapshotThrowsWhenUndefinedTableUsed()
        {
            //Arrange/Act
            var snapshots = new SnapshotCollection(GetType(), t => t != typeof(Snapshots.TestDefinitions.TableDef));

            snapshots.DefineTable("Customer")
            .PrimaryKey("Id")
            .CompareKey("Surname")
            .IsUnpredictable("Id");

            var builder = snapshots.NewSnapshot("Before");

            //Assert
            Action action = () => builder.AddNewRow("Wrong");

            action.Should().Throw <UndefinedTableInSnapshotException>().Where(x => x.TableName == "Wrong");
        }
Beispiel #20
0
        public void AppliedDefinitionsAreMerged()
        {
            //Arrange
            var definitionSet = SnapshotDefinitionLoader.Load(GetType());
            var snapshots     = new SnapshotCollection();

            snapshots.DefineTable("LocalTable").CompareKey("Name");

            //Act
            snapshots.ApplyDefinitions(definitionSet);

            //Assert
            var output = new Output();

            snapshots.GetSchemaReport(output, true);
            output.Report.Verify();
        }
Beispiel #21
0
        public void ExcludedTablesAreNotAddedToSnapshot()
        {
            //Arrange
            var collection = new SnapshotCollection();
            var schema     = SchemaStructureLoader.Load(DbController.Server, DbController.TestDbName, "Test");
            var builder    = collection.NewSnapshot("Test");

            SnapshotTableDefiner.Define(collection, schema);
            collection.DefineTable("[Test].[B_Related]").ExcludeFromComparison();

            //Act
            DbSnapshotMaker.Make(DbController.ConnectionString, builder, new [] { schema }, collection);

            //Assert
            var output = new Output();

            collection.GetSnapshotReport("Test", output);
            output.Report.Verify();
        }
Beispiel #22
0
        public void ExcludedColumnsAreNotAddedToSnapshot()
        {
            //Arrange
            var collection = new SnapshotCollection();
            var schema     = SchemaStructureLoader.Load(DbController.Server, DbController.TestDbName, "Test");
            var builder    = collection.NewSnapshot("Test");

            SnapshotTableDefiner.Define(collection, schema);
            collection.DefineTable("[Test].[B_Related]").Exclude("Name");

            //Act
            DbSnapshotMaker.Make(DbController.ConnectionString, builder, new [] { schema }, collection);

            //Assert
            var output = new Output();

            output.WrapLine("The Name column should not be present in [Test].[B_Related] below:");
            output.WriteLine();
            collection.GetSnapshotReport("Test", output);
            output.Report.Verify();
        }
Beispiel #23
0
        public void DifferenceDataIsSortedDescending()
        {
            //Arrange
            var collection = new SnapshotCollection();

            collection.DefineTable("TheTable").PrimaryKey("Id").SortDescending("Name");

            void AddRow(SnapshotBuilder snapshot, int id, string name)
            {
                var add = snapshot.AddNewRow("TheTable");

                add["Id"]   = id;
                add["Name"] = name;
            }

            var beforeBuilder = collection.NewSnapshot("Before");

            AddRow(beforeBuilder, 1, "Alice");
            AddRow(beforeBuilder, 2, "Xavier");
            AddRow(beforeBuilder, 3, "Zed");

            var afterBuilder = collection.NewSnapshot("After");

            AddRow(afterBuilder, 1, "Alice+");
            AddRow(afterBuilder, 2, "Xavier+");
            AddRow(afterBuilder, 3, "Zed+");

            var before = collection.GetSnapshot("Before");
            var after  = collection.GetSnapshot("After");
            var diffs  = SnapshotDifferenceAnalyser.ExtractDifferences(collection, before, after);

            //Act
            var result = SnapshotDifferenceSorter.SortDifferences(collection, diffs.TableDifferences.ToList());

            //Assert
            var output = new Output();

            result.Report(output);
            output.Report.Verify();
        }
Beispiel #24
0
 public void SetUp()
 {
     _collection = new SnapshotCollection();
     _collection.DefineTable(TableName).PrimaryKey(IdCol).PrimaryKey(NameCol);
 }
Beispiel #25
0
        public void DifferenceDataIsSortedByMultipleColumns()
        {
            //Arrange
            var collection = new SnapshotCollection();

            collection.DefineTable("TheTable")
            .PrimaryKey("Id")
            .Sort("ColAsc1")
            .SortDescending("ColDesc1")
            .Sort("ColAsc2")
            .SortDescending("ColDesc2");

            void AddRow(SnapshotBuilder snapshot, int id, string asc1, string desc1, string asc2, string desc2)
            {
                var add = snapshot.AddNewRow("TheTable");

                add["Id"]       = id;
                add["ColAsc1"]  = asc1;
                add["ColDesc1"] = desc1;
                add["ColAsc2"]  = asc2;
                add["ColDesc2"] = desc2;
            }

            void AddData(SnapshotBuilder snapshot, string ascData, string descData)
            {
                var id = 1;

                foreach (var colAsc1 in ascData.Split(','))
                {
                    foreach (var colDesc1 in descData.Split(','))
                    {
                        foreach (var colAsc2 in ascData.Split(','))
                        {
                            foreach (var colDesc2 in descData.Split(','))
                            {
                                AddRow(snapshot, id++, colAsc1, colDesc1, colAsc2, colDesc2);
                            }
                        }
                    }
                }
            }

            var beforeBuilder = collection.NewSnapshot("Before");

            AddData(beforeBuilder, "A,B,C", "C,B,A");

            var afterBuilder = collection.NewSnapshot("After");

            AddData(afterBuilder, "A+,B+,C+", "C+,B+,A+");

            var before = collection.GetSnapshot("Before");
            var after  = collection.GetSnapshot("After");
            var diffs  = SnapshotDifferenceAnalyser.ExtractDifferences(collection, before, after);

            //Act
            var result = SnapshotDifferenceSorter.SortDifferences(collection, diffs.TableDifferences.ToList());

            //Assert
            var output = new Output();

            result.Report(output);
            output.Report.Verify();
        }
Beispiel #26
0
 public void SetUp()
 {
     _collection = new SnapshotCollection();
     _collection.DefineTable("Test").CompareKey("Id");
 }
Beispiel #27
0
 public void SetUp()
 {
     _collection = new SnapshotCollection();
     _definer    = _collection.DefineTable(TestTableName);
 }