public void ForeignKeysDropCreateScriptProviderReturnsNotNullNotEmptyScriptsTest()
 {
     string DATABASE_NAME = _testDatabaseProvider.GenerateUniqueDatabaseName("TestReplication_ForeignConstraints");
     _testDatabaseProvider.CreateTestDatabase(DATABASE_NAME);
     IForeignKeysDropCreateScriptProvider foreignKeysDropCreateScriptProvider = new ForeignKeysDropCreateScriptProvider(_connectionString, new SqlCommandFactory());
     try
     {
         Student student;
         using (var testContext = new TestContext(_connectionString + $";Database={DATABASE_NAME}"))
         {
             var standard = new Standard() { StandardName = "Standard" };
             testContext.Standards.Add(standard);
             student = testContext.Students.Add(new Student()
             {
                 Standard = standard,
                 Weight = 70,
                 Height = 178,
                 StudentName = "Jan Halama",
                 DateOfBirth = DateTime.Now,
                 Photo = new byte[] { 0, 1, 2, 3 }
             });
             testContext.SaveChanges();
         }
         var scriptContainer = foreignKeysDropCreateScriptProvider.GenerateScripts(DATABASE_NAME);
         Assert.NotNull(scriptContainer);
         Assert.True(!string.IsNullOrEmpty(scriptContainer.DropScript));
         Assert.True(!string.IsNullOrEmpty(scriptContainer.CreateScript));
     }
     finally
     {
         _testDatabaseProvider.DropTestDatabase(DATABASE_NAME);
     }
 }
 public void ReplicationBasicTest()
 {
     string SOURCE_DATABASE_NAME = _testDatabaseProvider.GenerateUniqueDatabaseName("TestReplication_Source");
     string TARGET_DATABASE_NAME = _testDatabaseProvider.GenerateUniqueDatabaseName("TestReplication_Target");
     const string SCHEMA = "dbo";
     _testDatabaseProvider.CreateTestDatabase(SOURCE_DATABASE_NAME);
     try
     {
         _testDatabaseProvider.CreateTestDatabase(TARGET_DATABASE_NAME);
         try
         {
             Student student;
             using (var testContext = new TestContext(_connectionString + $";Database={SOURCE_DATABASE_NAME}"))
             {
                 var standard = new Standard() { StandardName = "Standard" };
                 testContext.Standards.Add(standard);
                 student = testContext.Students.Add(new Student()
                 {
                     Standard = standard,
                     Weight = 70,
                     Height = 178,
                     StudentName = "Jan Halama",
                     DateOfBirth = DateTime.Now,
                     Photo = new byte[] { 0, 1, 2, 3 }
                 });
                 testContext.SaveChanges();
             }
             using (var testContext = new TestContext(_connectionString + $";Database={TARGET_DATABASE_NAME}"))
             {
                 testContext.Standards.Select(s => s.StandardId > 0);
                 testContext.Students.Select(s => s.StudentId > 0);
                 testContext.SaveChanges();
             }
             IReplicationStrategy replicationStrategy = new Strategies.TableSnapshotReplicationStrategy(_connectionString, _connectionString, _logMock.Object, _foreignKeysDropCreateScriptProvider, new SqlCommandFactory());
             replicationStrategy.Replicate(new ReplicationArticle() { SourceDatabaseName = SOURCE_DATABASE_NAME, TargetDatabaseName = TARGET_DATABASE_NAME, ArticleName = "Standards", SourceSchema = SCHEMA, TargetSchema = SCHEMA, ArticleType = DataContracts.Enums.eArticleType.TABLE });
             replicationStrategy.Replicate(new ReplicationArticle() { SourceDatabaseName = SOURCE_DATABASE_NAME, TargetDatabaseName = TARGET_DATABASE_NAME, ArticleName = "Students", SourceSchema = SCHEMA, TargetSchema = SCHEMA, ArticleType = DataContracts.Enums.eArticleType.TABLE });
             using (var testContext = new TestContext(_connectionString + $";Database={TARGET_DATABASE_NAME}"))
             {
                 Assert.True(testContext.Students.Any(s => s.StudentId == student.StudentId && s.StudentName == s.StudentName && s.Standard.StandardId == student.Standard.StandardId), "Target db not as expected after replication");
             }
         }
         finally
         {
             _testDatabaseProvider.DropTestDatabase(TARGET_DATABASE_NAME);
         }
     }
     finally
     {
         _testDatabaseProvider.DropTestDatabase(SOURCE_DATABASE_NAME);
     }
 }
 public void ReplicationTest100kRows()
 {
     string SOURCE_DATABASE_NAME = _testDatabaseProvider.GenerateUniqueDatabaseName("TestReplication_Source");
     string TARGET_DATABASE_NAME = _testDatabaseProvider.GenerateUniqueDatabaseName("TestReplication_Target");
     const string SCHEMA = "dbo";
     _testDatabaseProvider.CreateTestDatabase(SOURCE_DATABASE_NAME);
     try
     {
         _testDatabaseProvider.CreateTestDatabase(TARGET_DATABASE_NAME);
         try
         {
             using (var testContext = new TestContext(_connectionString + $";Database={SOURCE_DATABASE_NAME}"))
             {
                 var standard = new Standard() { StandardName = "Standard" };
                 standard = testContext.Standards.Add(standard);
                 testContext.SaveChanges();
                 List<Student> students2Add = new List<Student>();
                 for (int i = 0; i < 100000; i++)
                 {
                     students2Add.Add(new Student()
                     {
                         StandardId = standard.StandardId,
                         Weight = 70 + i,
                         Height = 178 + i,
                         StudentName = $"Student {i}",
                         DateOfBirth = DateTime.Now,
                         Photo = new byte[] { 0, 1, 2, 3 }
                     });
                 }
                 testContext.BulkInsert(students2Add);
                 testContext.SaveChanges();
             }
             using (var testContext = new TestContext(_connectionString + $";Database={TARGET_DATABASE_NAME}"))
             {
                 var standard = new Standard() { StandardName = "Standard" };
                 standard = testContext.Standards.Add(standard);
                 testContext.SaveChanges();
                 List<Student> students2Add = new List<Student>();
                 for (int i = 0; i < 100000; i++)
                 {
                     students2Add.Add(new Student()
                     {
                         StandardId = standard.StandardId,
                         Weight = 0,
                         Height = 0,
                         StudentName = $"Student old {i}",
                         DateOfBirth = DateTime.Now,
                         Photo = new byte[] { 0, 1, 2, 3 }
                     });
                 }
                 testContext.BulkInsert(students2Add);
                 testContext.SaveChanges();
             }
             IReplicationStrategy replicationStrategy = new Strategies.TableSnapshotReplicationStrategy(_connectionString, _connectionString, _logMock.Object, _foreignKeysDropCreateScriptProvider, new SqlCommandFactory());
             replicationStrategy.Replicate(new ReplicationArticle() { SourceDatabaseName = SOURCE_DATABASE_NAME, TargetDatabaseName = TARGET_DATABASE_NAME, ArticleName = "Standards", SourceSchema = SCHEMA, TargetSchema = SCHEMA, ArticleType = DataContracts.Enums.eArticleType.TABLE });
             replicationStrategy.Replicate(new ReplicationArticle() { SourceDatabaseName = SOURCE_DATABASE_NAME, TargetDatabaseName = TARGET_DATABASE_NAME, ArticleName = "Students", SourceSchema = SCHEMA, TargetSchema = SCHEMA, ArticleType = DataContracts.Enums.eArticleType.TABLE });
             using (var testContext = new TestContext(_connectionString + $";Database={TARGET_DATABASE_NAME}"))
             {
                 Assert.Equal(100000, testContext.Students.Count());
                 for (int i = 0; i < 100000; i += 99)
                 {
                     string expectedStudentName = $"Student {i}";
                     Assert.True(null != testContext.Students.Single(s => s.StudentName == expectedStudentName && s.Weight == 70 + i && s.Height == 178 + i), $"Student {i} not found");
                 }
             }
         }
         finally
         {
             _testDatabaseProvider.DropTestDatabase(TARGET_DATABASE_NAME);
         }
     }
     finally
     {
         _testDatabaseProvider.DropTestDatabase(SOURCE_DATABASE_NAME);
     }
 }
 public void ReplicationForeignKeyWithUpdateOnLastRowTest()
 {
     string SOURCE_DATABASE_NAME = _testDatabaseProvider.GenerateUniqueDatabaseName("TestReplication_Source");
     string TARGET_DATABASE_NAME = _testDatabaseProvider.GenerateUniqueDatabaseName("TestReplication_Target");
     const string SCHEMA = "dbo";
     const int STUDENT_COUNT = 100;
     _testDatabaseProvider.CreateTestDatabase(SOURCE_DATABASE_NAME);
     try
     {
         _testDatabaseProvider.CreateTestDatabase(TARGET_DATABASE_NAME);
         try
         {
             using (var testContext = new TestContext(_connectionString + $";Database={SOURCE_DATABASE_NAME}"))
             {
                 var standard = new Standard() { StandardName = "Standard" };
                 testContext.Standards.Add(standard);
                 testContext.SaveChanges();
                 List<Student> students2Add = new List<Student>();
                 for (int i = 0; i < STUDENT_COUNT; i++)
                 {
                     students2Add.Add(new Student()
                     {
                         StandardId = standard.StandardId,
                         Weight = 70 + i,
                         Height = 178 + i,
                         StudentName = $"Student {i}",
                         DateOfBirth = DateTime.Now,
                         Photo = new byte[] { 0, 1, 2, 3 }
                     });
                 }
                 testContext.BulkInsert(students2Add);
                 testContext.SaveChanges();
             }
             using (var testContext = new TestContext(_connectionString + $";Database={TARGET_DATABASE_NAME}"))
             {
                 testContext.Standards.Select(s => s.StandardId > 0);
                 testContext.Students.Select(s => s.StudentId > 0);
                 testContext.SaveChanges();
             }
             IReplicationStrategy replicationStrategy = new Strategies.TableWithIncKeyUpdateLastRowReplicationStrategy(_connectionString, _connectionString, _logMock.Object, new SqlCommandFactory());
             replicationStrategy.Replicate(new ReplicationArticle() { SourceDatabaseName = SOURCE_DATABASE_NAME, TargetDatabaseName = TARGET_DATABASE_NAME, ArticleName = "Standards", SourceSchema = SCHEMA, TargetSchema = SCHEMA, ArticleType = DataContracts.Enums.eArticleType.TABLE });
             replicationStrategy.Replicate(new ReplicationArticle() { SourceDatabaseName = SOURCE_DATABASE_NAME, TargetDatabaseName = TARGET_DATABASE_NAME, ArticleName = "Students", SourceSchema = SCHEMA, TargetSchema = SCHEMA, ArticleType = DataContracts.Enums.eArticleType.TABLE });
             Student sourceDatabaseLastStudent;
             using (var testContext = new TestContext(_connectionString + $";Database={SOURCE_DATABASE_NAME}"))
             {
                 int studentMaxId = testContext.Students.Max(s => s.StudentId);
                 sourceDatabaseLastStudent = testContext.Students.Single(c => c.StudentId == studentMaxId);
                 sourceDatabaseLastStudent.StudentName = "Jan Halama";
                 testContext.SaveChanges();
             }
             replicationStrategy.Replicate(new ReplicationArticle() { SourceDatabaseName = SOURCE_DATABASE_NAME, TargetDatabaseName = TARGET_DATABASE_NAME, ArticleName = "Standards", SourceSchema = SCHEMA, TargetSchema = SCHEMA, ArticleType = DataContracts.Enums.eArticleType.TABLE });
             replicationStrategy.Replicate(new ReplicationArticle() { SourceDatabaseName = SOURCE_DATABASE_NAME, TargetDatabaseName = TARGET_DATABASE_NAME, ArticleName = "Students", SourceSchema = SCHEMA, TargetSchema = SCHEMA, ArticleType = DataContracts.Enums.eArticleType.TABLE });
             using (var testContext = new TestContext(_connectionString + $";Database={TARGET_DATABASE_NAME}"))
             {
                 Assert.Equal(STUDENT_COUNT, testContext.Students.Count());
                 for (int i = 0; i < STUDENT_COUNT - 1; i++)
                 {
                     string expectedStudentName = $"Student {i}";
                     Assert.True(null != testContext.Students.Single(s => s.StudentName == expectedStudentName && s.Weight == 70 + i && s.Height == 178 + i), $"Student {i} not found");
                 }
                 int studentMaxId = testContext.Students.Max(s => s.StudentId);
                 var lastStudent = testContext.Students.Single(c => c.StudentId == studentMaxId);
                 Assert.Equal(sourceDatabaseLastStudent.StudentId, lastStudent.StudentId);
                 Assert.Equal(sourceDatabaseLastStudent.StudentName, lastStudent.StudentName);
             }
         }
         finally
         {
             _testDatabaseProvider.DropTestDatabase(TARGET_DATABASE_NAME);
         }
     }
     finally
     {
         _testDatabaseProvider.DropTestDatabase(SOURCE_DATABASE_NAME);
     }
 }
 public void TestGetTableInfoForTableWithForeignKeyColumn()
 {
     string DATABASE_NAME = _testDatabaseProvider.GenerateUniqueDatabaseName("TestReplication");
     const string SCHEMA = "dbo";
     try
     {
         using (var testContext = new TestContext(_connectionString + $";Database={DATABASE_NAME}"))
         {
             testContext.Standards.Select(s => s.StandardId > 0);
             testContext.Students.Select(s => s.StudentId > 0);
             testContext.SaveChanges();
         }
         Table table = _testedTableSchemaAnalyzer.GetTableInfo(DATABASE_NAME, SCHEMA, "Students");
         Assert.True(table.Columns.Count(c => c.Name == "StandardId" && c.IsForeignKey) == 1, "Foreign key column expected");
         Assert.True(table.Columns.Count(c => c.IsForeignKey) == 1, "Only one foreign key expected");
     }
     finally
     {
         _testDatabaseProvider.DropTestDatabase(DATABASE_NAME);
     }
 }