Ejemplo n.º 1
0
        public void CompareKeyTableKeysAreExtracted()
        {
            //Act
            var keys = SnapshotKeyExtractor.GetKeys(_snapshot, _collection.GetTableDefinition(CompareKeyTableName));

            //Assert
            var output = new Output();
            var rep    = keys.MakeKeyReport();

            output.FormatTable(rep);
            output.Report.Verify();
        }
 public void SetUp()
 {
     _collection = new SnapshotCollection();
     _collection.DefineTable(TableName).PrimaryKey(IdCol).PrimaryKey(NameCol);
     _tableDef        = _collection.GetTableDefinition(TableName);
     _snapshotBuilder = _collection.NewSnapshot("Test");
 }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
0
        public RowBuilder AddNewRow(string tableName)
        {
            var table = _collection.GetTableDefinition(tableName);

            if (table == null)
            {
                throw new UndefinedTableInSnapshotException(tableName);
            }

            return(new RowBuilder(table, _snapshot));
        }
        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));
        }
Ejemplo n.º 6
0
        public void ExcludedColumnsAreNotGenerated()
        {
            //Arrange
            var collection = new SnapshotCollection();
            var schema     = SchemaStructureLoader.Load(DbController.Server, DbController.TestDbName, "Test");

            SnapshotTableDefiner.Define(collection, schema);
            var definitions = SnapshotDefinitionLoader.Load(GetType());

            collection.ApplyDefinitions(definitions);
            var table = schema.Tables.Single(t => t.Name == "[Test].[B_Related]");

            //Act
            var select = SnapshotTableSelectBuilder.Build(table, collection.GetTableDefinition(table.Name));

            //Assert
            select.Should().Be("SELECT * FROM [Test].[B_Related] WHERE [Name] LIKE 'First%'");
        }
Ejemplo n.º 7
0
        public void BasicSelectIsBuilt()
        {
            //Arrange
            var collection = new SnapshotCollection();
            var schema     = SchemaStructureLoader.Load(DbController.Server, DbController.TestDbName, "Test");

            SnapshotTableDefiner.Define(collection, schema);
            var definitions = SnapshotDefinitionLoader.Load(GetType());

            collection.ApplyDefinitions(definitions);
            var table = schema.Tables.First();

            //Act
            var select = SnapshotTableSelectBuilder.Build(table, collection.GetTableDefinition(table.Name));

            //Assert
            select.Should().Be("SELECT * FROM [Test].[A_Main]");
        }
Ejemplo n.º 8
0
        public static void Make(string connectionString, SnapshotBuilder builder, IEnumerable <SchemaStructure> schemas,
                                SnapshotCollection snapshotCollection)
        {
            var schemasOrdered = schemas.OrderBy(s => s.Name);

            foreach (var schema in schemasOrdered)
            {
                foreach (var table in schema.Tables)
                {
                    var definition = snapshotCollection.GetTableDefinition(table.Name);
                    if (definition?.ExcludeFromComparison ?? false)
                    {
                        continue;
                    }

                    SnapshotData(connectionString, builder, table, definition);
                }
            }
        }
        public void TablesCanBeAutomaticallyDefined()
        {
            //Arrange
            var data = new[]
            {
                new { A = 1, B = "Bee", C = 5.5 },
                new { A = 2, B = "Cee", C = 6.6 },
                new { A = 3, B = "Dee", C = 7.7 },
                new { A = 4, B = "Eee", C = 8.8 },
                new { A = 5, B = "Eff", C = 9.9 },
            };

            var snapshot = _collection.NewSnapshot("Test");

            //Act
            data.ToSnapshotTable(snapshot, "Data", "A", "B");

            //Assert
            var output = new Output();
            var table  = _collection.GetTableDefinition("Data");

            _collection.GetSchemaReport(output, true);
            output.Report.Verify();
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Scan the differences in a set of table <see cref="SnapshotTableDifferences"/> for columns flagged as referencing rows in the snapshot. If the
        /// referenced row is present, but does not contain any differences, it will be returned as an additional reference. This method reads the differences,
        /// but will not make any changes.
        /// </summary>
        /// <param name="collection">The snapshot collection. Needed for table definitions.</param>
        /// <param name="tableDiffs">The difference set that will be analysed</param>
        /// <returns>An <see cref="AdditionalReferencedRows"/> instance containing the referenced row details. Only rows not currently in the difference set
        /// will be included.</returns>
        public static AdditionalReferencedRows GetMissingRows(SnapshotCollection collection, IReadOnlyCollection <SnapshotTableDifferences> tableDiffs)
        {
            var allKeysByReferencedTable = GeyKeysByReferencedTable(collection, tableDiffs)
                                           .Where(rk => rk.KeyValue != null)
                                           .GroupBy(rk => new { rk.ReferencedTableDefinition.TableName, rk.ColumnName })
                                           .ToList();

            var requiredTableNotPresent = allKeysByReferencedTable
                                          .Where(g => !tableDiffs.Any(td => td.TableDefinition.TableName == g.Key.TableName))
                                          .ToList();

            var missingKeys = allKeysByReferencedTable.Join(tableDiffs, kg => kg.Key.TableName, td => td.TableDefinition.TableName,
                                                            (kg, td) => new
            {
                TableDefinition = kg.Key.TableName,
                KeyField        = kg.Key.ColumnName,
                MissingRows     = kg.Where(k => !td.RowDifferences.Any(row => IsMatchByKeyValue(row, k)))
                                  .Select(k => k.KeyValue).ToList()
            })
                              .Concat(requiredTableNotPresent.Select(r => new
            {
                TableDefinition = r.Key.TableName,
                KeyField        = r.Key.ColumnName,
                MissingRows     = allKeysByReferencedTable.Where(g => g.Key.TableName == r.Key.TableName)
                                  .SelectMany(g => g.Select(k => k.KeyValue))
                                  .ToList()
            }))
                              .GroupBy(req => req.TableDefinition)
                              .Select(g => new AdditionalReferencedRows.RequiredTableRows(collection.GetTableDefinition(g.Key), g.SelectMany(r => r.MissingRows.Select(mr => new AdditionalReferencedRows.RowRequest(r.KeyField, mr))).ToList()))
                              .ToList();

            return(new AdditionalReferencedRows(missingKeys));
        }
Ejemplo n.º 11
0
        public void PrimaryKeyCanBeSet()
        {
            //Act
            _definer.PrimaryKey("Key1");

            //Assert
            _collection.GetTableDefinition(TestTableName)
            .PrimaryKeys
            .Single()
            .Should().Be("Key1");
        }