public static SqlSnapshotCollection MakeCollection() { var collection = new SqlSnapshotCollection(Config.ConnectionString); collection.ConfigureSchema("Chess"); //this loads table definitions direct from the database schema. In theory no further config should be required collection.LoadSchemaOverrides(typeof(SchemaObjectMother).Assembly); //In practice, however, some extra configuration settings are needed return(collection); }
public async void DatabaseChangesAreFlaggedFluent() { //Arrange var collection = new SqlSnapshotCollection(Config.ConnectionString); collection.ConfigureSchema("Chess"); //this loads table definitions direct from the database schema. In theory no further config should be required //In practice, however, some extra configuration settings are needed collection.DefineTable("[Chess].[Rating]") .IsUnpredictable("RatingDate") //RatingDate is not defaulted to GETDATE() or any of the automatic "get the time" variants. This was deliberate, to illustrate that we can alter the automated decisions. .Sort("RatingDate") //* In a similar vein, a GUID was used as the table key for [Chess].[Rating] to illustrate that GUIDs can be handled, however, when they are the primary key they can .Sort("RatingSourceId"); //* randomise the order rows are returned, so we need to force a sort on other fields to make the differences repeatable. collection.DefineTable("[Chess].[Player]") .Sort("Name"); // As above, we need to sort player data because it has a GUID for a primary key, randomising the order that rows are returned. collection.DefineTable("[Chess].[AuditPlayer]") .IsReference("PlayerId", "[Chess].[Player]", "PlayerId") //The audit table does not have the player ID defined as a foreign key, so no relationship is defined automatically. We define it manually to make the keys consistent. .IsUnpredictable("AuditStartDate") //* These dates will match the update timestamp. They are set by a trigger, not a default .IsUnpredictable("AuditEndDate") //* otherwise unpredictability would have been set automatically. .Local("AuditStartDate") //* And they are... .Local("AuditEndDate"); //* local dates collection.DefineTable("[Chess].[AuditRating]") .IsReference("PlayerId", "[Chess].[Player]", "PlayerId") //The audit table does not have the player ID defined as a foreign key, so no relationship is defined automatically. We define it manually to make the keys consistent. .IsReference("RatingId", "[Chess].[Rating]", "RatingId") //The audit table does not have the Rating Id defined as a foreign key, so no relationship is defined automatically. We define it manually to make the keys consistent. .IsUnpredictable("AuditStartDate") //* These dates will match the update timestamp. They are set by a trigger, not a default .IsUnpredictable("AuditEndDate") //* otherwise unpredictability would have been set automatically. .IsUnpredictable("RatingDate") //* .Local("AuditStartDate") //* And they are... .Local("AuditEndDate") //* local dates .Local("RatingDate"); //* var builder = collection.Snapshot("before"); var entities = new ChessContext(Config.ConnectionString); //Act await _om.AddPlayerAsync("Bill", (ChessObjectMother.ChessDotCom, 1801), (ChessObjectMother.Lichess, 1992)); await _om.AddPlayerAsync("Ted", (ChessObjectMother.ChessDotCom, 1836), (ChessObjectMother.Lichess, 1918)); collection.Snapshot("after"); //Assert var output = new Output(); collection.ReportChanges("before", "after", output); output.Report.Verify(); }
public void SchemaOverridesCannotBeAppliedAfterSnapshot() { //Arrange var collection = new SqlSnapshotCollection(DbController.ConnectionString); collection.ConfigureSchema("Test"); collection.Snapshot("Test"); //Act Action action = () => collection.DefineTable("[Test].[A_Main]").IsPredictable("MainId"); //Assert action.Should().Throw <ConfigurationCannotBeChangedException>(); }
public void SchemaOverridesCannotBeLoadedFromAssemblyAfterSnapshot() { //Arrange var collection = new SqlSnapshotCollection(DbController.ConnectionString); collection.ConfigureSchema("Test"); collection.Snapshot("Test"); //Act Action action = () => collection.LoadSchemaOverrides(GetType().Assembly); //Assert action.Should().Throw <ConfigurationCannotBeChangedException>(); }
public void SchemaOverridesCanBeLoadedFromAssembly() { //Arrange var collection = new SqlSnapshotCollection(DbController.ConnectionString); collection.ConfigureSchema("Test"); //Act collection.LoadSchemaOverrides(GetType().Assembly); //Assert var output = new Output(); collection.GetSchemaReport(output, true); output.Report.Verify(); }
public void SchemaOverridesCanBeAppliedDirectly() { //Arrange var collection = new SqlSnapshotCollection(DbController.ConnectionString); collection.ConfigureSchema("Test"); //Act collection.DefineTable("[Test].[A_Main]").IsPredictable("MainId"); //Assert var output = new Output(); collection.GetSchemaReport(output, true); output.Report.Verify(); }
public void SnapshotReportCanBeExtracted() { //Arrange var collection = new SqlSnapshotCollection(DbController.ConnectionString); collection.ConfigureSchema("Test"); collection.Snapshot("Test"); //Act var output = new Output(); collection.GetSnapshotReport("Test", output); //Assert output.Report.Verify(); }
public void SchemaConfigurationIsConsistent() { //Arrange var collection = new SqlSnapshotCollection(Config.ConnectionString); //Act collection.ConfigureSchema("Chess"); //this loads table definitions direct from the database schema. In theory no further config should be required //Assert var output = new Output(); output.WrapLine("Default schema"); output.WriteLine(); collection.GetSchemaReport(output, true); output.Report.Verify(); }
public void AutomaticDataSnapshotCanBeAvoided() { //Arrange var collection = new SqlSnapshotCollection(DbController.ConnectionString); collection.ConfigureSchema("Test"); var builder = collection.Snapshot("Test", SnapshotOptions.NoAutoSnapshot); var row = builder.AddNewRow("[Test].[A_Main]"); row["MainId"] = 101; row["CreatedDate"] = DateTime.Parse("2020-10-16 08:16"); row["Comment"] = "This row was added manually, not snapped from the database. This column isn't even in the database."; //Act var output = new Output(); collection.GetSnapshotReport("Test", output); //Assert output.Report.Verify(); }