public void ShouldMarkForDeleteWhenCustomFieldName() { var helper = new MergeQueryObjectTestHelper(); helper.DataService.DropTable(); helper.DataService.CreateCompositeKeyTable(); helper.DataService.Insert(new SampleCompositeKeyDto { Pk1 = 3, Pk2 = "B", TextValue = "AA", IntValue = 1, DecimalValue = 1 }); var dtos = new[] { new SampleCompositeKeyDto { Pk1 = 1, Pk2 = "A", TextValue = "JJ", IntValue = 100, DecimalValue = 100.99m }, new SampleCompositeKeyDto { Pk1 = 2, Pk2 = "B", TextValue = "ZZ", IntValue = 999, DecimalValue = 123.45m } }; var request = new MergeRequest <SampleCompositeKeyDto> { DataToMerge = dtos, TargetTableName = "dbo.Sample", UseRealTempTable = false, PrimaryKeyExpression = t => new object[] { t.Pk1, t.Pk2 }, KeepPrimaryKeyInInsertStatement = true, WhenNotMatchedDeleteBehavior = DeleteBehavior.MarkIsDelete, WhenNotMatchedDeleteFieldName = "IsRemovable" }; helper.DataService.Merge(request); var sourceDtos = helper.DataService .GetAllSampleDtos <SampleCompositeKeyDto>() .ToArray(); sourceDtos.Length.Should().Be(3); var firstDto = sourceDtos.First(x => x.Pk1 == 3); firstDto.IsDeleted.Should().BeFalse(); firstDto.IsRemovable.Should().BeTrue(); }
public void ShouldLogErrors() { var helper = new MergeQueryObjectTestHelper(); helper.DataService.DropTable(); helper.DataService.CreateSingleSurrogateKeyTable(); var dtos = new[] { new SampleSurrogateKey { TextValue = "JJ", IntValue = 100, DecimalValue = 100.99m } }; var request = new MergeRequest <SampleSurrogateKey> { DataToMerge = dtos, TargetTableName = "dbo.Sample_DOESNOTEXIST", UseRealTempTable = false, PrimaryKeyExpression = t => new object[] { t.Pk }, KeepPrimaryKeyInInsertStatement = false }; var errorLogger = new List <string>(); request.ErrorLogger = info => errorLogger.Add(info); Assert.Throws <SqlException> (() => { helper.DataService.Merge(request); }); errorLogger.Count.Should().Be(1); errorLogger.Single(x => x.Contains("Invalid object name 'dbo.Sample_DOESNOTEXIST'")).Should().NotBeNull(); }
public void ShouldMergeWhenBulkLoadUsesRenamedFields() { var helper = new MergeQueryObjectTestHelper(); helper.DataService.DropTable(); helper.DataService.CreateSingleSurrogateKeyTable(); var dtos = new[] { new SampleSurrogateKeyDifferentNamesDto { Pk = 100, TextValueExtra = "JJ", IntValueExtra = 100, DecimalValueExtra = 100.99m }, new SampleSurrogateKeyDifferentNamesDto { Pk = 200, TextValueExtra = "ZZ", IntValueExtra = 999, DecimalValueExtra = 123.45m } }; var request = new MergeRequest <SampleSurrogateKeyDifferentNamesDto> { DataToMerge = dtos, TargetTableName = "dbo.Sample", UseRealTempTable = false, PrimaryKeyExpression = t => new object[] { t.Pk }, KeepPrimaryKeyInInsertStatement = false, WhenNotMatchedDeleteBehavior = DeleteBehavior.Delete, OnMergeUpdateActive = false, BulkLoaderOptions = t => t.With(c => c.TextValueExtra, "TextValue") .With(c => c.IntValueExtra, "IntValue") .With(c => c.DecimalValueExtra, "DecimalValue") }; helper.DataService.Merge(request); var sourceDtos = helper.DataService.GetAllSampleDtos <SampleSurrogateKey>().ToArray(); var firstDto = sourceDtos.First(x => x.TextValue == "JJ"); firstDto.Pk.Should().BeGreaterThan(0); firstDto.IntValue.Should().Be(100); firstDto.DecimalValue.Should().Be(100.99m); var secondDto = sourceDtos.First(x => x.TextValue == "ZZ"); secondDto.Pk.Should().BeGreaterThan(0); secondDto.IntValue.Should().Be(999); secondDto.DecimalValue.Should().Be(123.45m); }
public void ShouldIncludePrimaryKeyInUpdate() { var helper = new MergeQueryObjectTestHelper(); helper.DataService.DropTable(); helper.DataService.CreateCompositeKeyTable(); var dtos = new[] { new SampleCompositeKeyDto { Pk1 = 1, Pk2 = "A", TextValue = "JJ", IntValue = 100, DecimalValue = 100.99m }, new SampleCompositeKeyDto { Pk1 = 2, Pk2 = "B", TextValue = "ZZ", IntValue = 999, DecimalValue = 123.45m } }; var request = new MergeRequest <SampleCompositeKeyDto> { DataToMerge = dtos, TargetTableName = "dbo.Sample", UseRealTempTable = false, PrimaryKeyExpression = t => new object[] { t.Pk1, t.Pk2 }, KeepPrimaryKeyInInsertStatement = true }; helper.DataService.Merge(request); var sampleDtos = helper.DataService.GetAllSampleDtos <SampleCompositeKeyDto>().ToArray(); var firstDto = sampleDtos.First(x => x.Pk1 == 1); firstDto.Pk2.Should().Be("A"); firstDto.TextValue.Should().Be("JJ"); firstDto.IntValue.Should().Be(100); firstDto.DecimalValue.Should().Be(100.99m); var secondDto = sampleDtos.First(x => x.Pk1 == 2); secondDto.Pk2.Should().Be("B"); secondDto.TextValue.Should().Be("ZZ"); secondDto.IntValue.Should().Be(999); secondDto.DecimalValue.Should().Be(123.45m); }
public void ShouldKeepIdentityColumnsWhenFlagged() { var helper = new MergeQueryObjectTestHelper(); helper.DataService.DropTable(); helper.DataService.CreateSingleSurrogateKeyTable(); var dtos = new[] { new SampleSurrogateKey { Pk = 100, TextValue = "JJ", IntValue = 100, DecimalValue = 100.99m }, new SampleSurrogateKey { Pk = 200, TextValue = "ZZ", IntValue = 999, DecimalValue = 123.45m } }; var request = new MergeRequest <SampleSurrogateKey> { DataToMerge = dtos, TargetTableName = "dbo.Sample", UseRealTempTable = false, PrimaryKeyExpression = t => new object[] { t.Pk }, KeepPrimaryKeyInInsertStatement = true, KeepIdentityColumnValueOnInsert = true }; helper.DataService.Merge(request); var sourceDtos = helper.DataService.GetAllSampleDtos <SampleSurrogateKey>().ToArray(); var firstDto = sourceDtos.First(x => x.Pk == 100); firstDto.TextValue.Should().Be("JJ"); firstDto.IntValue.Should().Be(100); firstDto.DecimalValue.Should().Be(100.99m); var secondDto = sourceDtos.First(x => x.Pk == 200); secondDto.TextValue.Should().Be("ZZ"); secondDto.IntValue.Should().Be(999); secondDto.DecimalValue.Should().Be(123.45m); }
public void ShouldExecuteWithDefinedTempTableType(bool useRealTempTable) { var helper = new MergeQueryObjectTestHelper(); helper.DataService.DropTable(); helper.DataService.CreateSingleSurrogateKeyTable(); var dtos = new[] { new SampleSurrogateKey { TextValue = "JJ", IntValue = 100, DecimalValue = 100.99m } }; var request = new MergeRequest <SampleSurrogateKey> { DataToMerge = dtos, TargetTableName = "dbo.Sample", UseRealTempTable = useRealTempTable, PrimaryKeyExpression = t => new object[] { t.Pk }, KeepPrimaryKeyInInsertStatement = false }; var infoLogger = new List <string>(); request.InfoLogger = info => infoLogger.Add(info); helper.DataService.Merge(request); if (useRealTempTable == false) { infoLogger.Any(x => x.Contains("##MergeObjectTemp")).Should().BeTrue(); } if (useRealTempTable == true) { infoLogger.Any(x => x.Contains("dbo.MergeObjectTemp")).Should().BeTrue(); } var sourceDtos = helper.DataService.GetAllSampleDtos <SampleSurrogateKey>().ToArray(); var firstDto = sourceDtos.First(x => x.TextValue == "JJ"); firstDto.IntValue.Should().Be(100); firstDto.DecimalValue.Should().Be(100.99m); }
public void ShouldMergeDtoWithDerivedColumns() { var helper = new MergeQueryObjectTestHelper(); helper.DataService.DropTable(); helper.DataService.CreateSingleSurrogateKeyTable(); var dtos = new[] { new SampleSurrogateKeyWithDerivedColumns { TextValue = "JJ", IntValue = 100, DecimalValue = 100.99m }, new SampleSurrogateKeyWithDerivedColumns { TextValue = "ZZ", IntValue = 999, DecimalValue = 123.45m } }; var request = new MergeRequest <SampleSurrogateKeyWithDerivedColumns> { DataToMerge = dtos, TargetTableName = "dbo.Sample", UseRealTempTable = false, PrimaryKeyExpression = t => new object[] { t.Pk }, ColumnsToExcludeExpressionOnUpdate = t => new object[] { t.ExtraColumn }, ColumnsToExcludeExpressionOnInsert = t => new object[] { t.ExtraColumn }, KeepPrimaryKeyInInsertStatement = false }; helper.DataService.Merge(request); var sourceDtos = helper.DataService.GetAllSampleDtos <SampleSurrogateKey>().ToArray(); var firstDto = sourceDtos.First(x => x.TextValue == "JJ"); firstDto.IntValue.Should().Be(100); firstDto.DecimalValue.Should().Be(100.99m); var secondDto = sourceDtos.First(x => x.TextValue == "ZZ"); secondDto.IntValue.Should().Be(999); secondDto.DecimalValue.Should().Be(123.45m); }
public void ShouldIncludePrimaryKeyInUpdate() { var helper = new MergeQueryObjectTestHelper(); helper.DataService.DropTable(); helper.DataService.CreateCompositeKeyTable(); var dtos = new[] { new SampleCompositeKeyDto { Pk1 = 1, Pk2 = "A", TextValue = "JJ", IntValue = 100, DecimalValue = 100.99m } }; var customSqlCommandCalled = 0; var request = new MergeRequest <SampleCompositeKeyDto> { DataToMerge = dtos, TargetTableName = "dbo.Sample", UseRealTempTable = false, PrimaryKeyExpression = t => new object[] { t.Pk1, t.Pk2 }, KeepPrimaryKeyInInsertStatement = true, ExecuteSql = (connection, sql, req) => { customSqlCommandCalled++; connection.Execute(sql, req.SqlCommandTimeout); } }; helper.DataService.Merge(request); var sampleDtos = helper.DataService.GetAllSampleDtos <SampleCompositeKeyDto>().ToArray(); customSqlCommandCalled.Should().Be(1); var firstDto = sampleDtos.First(x => x.Pk1 == 1); firstDto.Pk2.Should().Be("A"); firstDto.TextValue.Should().Be("JJ"); firstDto.IntValue.Should().Be(100); firstDto.DecimalValue.Should().Be(100.99m); }
public void ShouldLogMajorEvents() { var helper = new MergeQueryObjectTestHelper(); helper.DataService.DropTable(); helper.DataService.CreateSingleSurrogateKeyTable(); var dtos = new[] { new SampleSurrogateKey { TextValue = "JJ", IntValue = 100, DecimalValue = 100.99m } }; var request = new MergeRequest <SampleSurrogateKey> { DataToMerge = dtos, TargetTableName = "dbo.Sample", UseRealTempTable = false, PrimaryKeyExpression = t => new object[] { t.Pk }, KeepPrimaryKeyInInsertStatement = false }; var infoLogger = new List <string>(); request.InfoLogger = info => infoLogger.Add(info); helper.DataService.Merge(request); infoLogger.Single(x => x.Contains("SELECT TOP(0)")).Should().NotBeNull(); infoLogger.Single(x => x.Contains("Bulk loading data to temp table")).Should().NotBeNull(); infoLogger.Single(x => x.Contains("CREATE CLUSTERED INDEX [IdxPrimaryKey] ON")).Should().NotBeNull(); infoLogger.Single(x => x.Contains("MERGE INTO CTE_T AS T")).Should().NotBeNull(); infoLogger.Single(x => x.Contains("DROP TABLE")).Should().NotBeNull(); }
public void ShouldUpdateOnlyWhenFlagged() { var helper = new MergeQueryObjectTestHelper(); helper.DataService.DropTable(); helper.DataService.CreateSingleSurrogateKeyTable(); helper.DataService.Insert(new SampleSurrogateKey() { Pk = 1, TextValue = "JJ", IntValue = 100, DecimalValue = 100.99m }); helper.DataService.Insert(new SampleSurrogateKey { Pk = 2, TextValue = "ZZ", IntValue = 999, DecimalValue = 123.45m }); helper.DataService.Insert(new SampleSurrogateKey { Pk = 3, TextValue = "AA", IntValue = 1, DecimalValue = 1 }); var dtos = new[] { new SampleSurrogateKey { Pk = 1, TextValue = "zz", IntValue = 0, DecimalValue = 0 }, new SampleSurrogateKey { Pk = 2, TextValue = "zz", IntValue = 0, DecimalValue = 0 }, new SampleSurrogateKey { Pk = 5, TextValue = "INSERT", IntValue = 99, DecimalValue = 99.1m } }; var request = new MergeRequest <SampleSurrogateKey> { DataToMerge = dtos, TargetTableName = "dbo.Sample", UseRealTempTable = false, PrimaryKeyExpression = t => new object[] { t.Pk }, KeepIdentityColumnValueOnInsert = true, KeepPrimaryKeyInInsertStatement = true, WhenNotMatchedDeleteBehavior = DeleteBehavior.None, OnMergeUpdateActive = false, OnMergeInsertActive = true }; helper.DataService.Merge(request); var sourceDtos = helper.DataService.GetAllSampleDtos <SampleSurrogateKey>().ToArray(); var firstDto = sourceDtos.First(x => x.Pk == 1); firstDto.TextValue.Should().Be("JJ"); firstDto.IntValue.Should().Be(100); firstDto.DecimalValue.Should().Be(100.99m); var secondDto = sourceDtos.First(x => x.Pk == 2); secondDto.TextValue.Should().Be("ZZ"); secondDto.IntValue.Should().Be(999); secondDto.DecimalValue.Should().Be(123.45m); var thirdDto = sourceDtos.First(x => x.TextValue == "INSERT"); thirdDto.IntValue.Should().Be(99); thirdDto.DecimalValue.Should().Be(99.1m); }
public void ShouldUpdateOnlyWhenFlagged() { var helper = new MergeQueryObjectTestHelper(); helper.DataService.DropTable(); helper.DataService.CreateCompositeKeyTable(); helper.DataService.Insert(new SampleCompositeKeyDto { Pk1 = 1, Pk2 = "A", TextValue = "JJ", IntValue = 100, DecimalValue = 100.99m }); helper.DataService.Insert(new SampleCompositeKeyDto { Pk1 = 2, Pk2 = "B", TextValue = "ZZ", IntValue = 999, DecimalValue = 123.45m }); helper.DataService.Insert(new SampleCompositeKeyDto { Pk1 = 3, Pk2 = "B", TextValue = "AA", IntValue = 1, DecimalValue = 1 }); var dtos = new[] { new SampleCompositeKeyDto { Pk1 = 1, Pk2 = "A", TextValue = "zz", IntValue = 999, DecimalValue = 999m } }; var request = new MergeRequest <SampleCompositeKeyDto> { DataToMerge = dtos, TargetTableName = "dbo.Sample", UseRealTempTable = false, PrimaryKeyExpression = t => new object[] { t.Pk1, t.Pk2 }, KeepPrimaryKeyInInsertStatement = true, WhenNotMatchedDeleteBehavior = DeleteBehavior.None, OnMergeUpdateActive = true }; helper.DataService.Merge(request); var sourceDtos = helper.DataService.GetAllSampleDtos <SampleCompositeKeyDto>().ToArray(); var firstDto = sourceDtos.First(x => x.Pk1 == 1); firstDto.Pk2.Should().Be("A"); firstDto.TextValue.Should().Be("zz"); firstDto.IntValue.Should().Be(999); firstDto.DecimalValue.Should().Be(999); var secondDto = sourceDtos.First(x => x.Pk1 == 2); secondDto.Pk2.Should().Be("B"); secondDto.TextValue.Should().Be("ZZ"); secondDto.IntValue.Should().Be(999); secondDto.DecimalValue.Should().Be(123.45m); var thirdDto = sourceDtos.First(x => x.Pk1 == 3); thirdDto.Pk2.Should().Be("B"); thirdDto.TextValue.Should().Be("AA"); thirdDto.IntValue.Should().Be(1); thirdDto.DecimalValue.Should().Be(1); }
public void ShouldUpdateFieldsWithMerge() { var helper = new MergeQueryObjectTestHelper(); helper.DataService.DropTable(); helper.DataService.CreateSingleSurrogateKeyTable(); var dtos = new[] { new SampleSurrogateKey { TextValue = "JJ", IntValue = 100, DecimalValue = 100.99m }, new SampleSurrogateKey { TextValue = "ZZ", IntValue = 999, DecimalValue = 123.45m } }; var request = new MergeRequest <SampleSurrogateKey> { DataToMerge = dtos, TargetTableName = "dbo.Sample", UseRealTempTable = false, PrimaryKeyExpression = t => new object[] { t.Pk }, KeepPrimaryKeyInInsertStatement = false }; helper.DataService.Merge(request); var firstSetOfInsertedDtos = helper.DataService.GetAllSampleDtos <SampleSurrogateKey>().ToArray(); var firstInsertDto = firstSetOfInsertedDtos.First(x => x.TextValue == "JJ"); firstInsertDto.IntValue.Should().Be(100); firstInsertDto.DecimalValue.Should().Be(100.99m); var secondInsertDto = firstSetOfInsertedDtos.First(x => x.TextValue == "ZZ"); secondInsertDto.IntValue.Should().Be(999); secondInsertDto.DecimalValue.Should().Be(123.45m); firstInsertDto.TextValue = "1"; firstInsertDto.IntValue = 2; firstInsertDto.DecimalValue = 3; secondInsertDto.TextValue = "2"; secondInsertDto.IntValue = 3; secondInsertDto.DecimalValue = 4; request.DataToMerge = firstSetOfInsertedDtos; helper.DataService.Merge(request); var secondSetOfUpdateDtos = helper.DataService.GetAllSampleDtos <SampleSurrogateKey>().ToArray(); var firstUpdateDto = secondSetOfUpdateDtos.First(x => x.TextValue == "1"); firstUpdateDto.IntValue.Should().Be(2); firstUpdateDto.DecimalValue.Should().Be(3); var secondUpdateDto = secondSetOfUpdateDtos.First(x => x.TextValue == "2"); secondUpdateDto.IntValue.Should().Be(3); secondUpdateDto.DecimalValue.Should().Be(4); }
public void ShouldAllowForPartialUpdateWithPartialObject() { var helper = new MergeQueryObjectTestHelper(); helper.DataService.DropTable(); helper.DataService.CreateCompositeKeyTable(); var dtos = new[] { new SampleCompositeKeyDto { Pk1 = 1, Pk2 = "A", TextValue = "JJ", IntValue = 100, DecimalValue = 100.99m } }; var request = new MergeRequest <SampleCompositeKeyDto> { DataToMerge = dtos, TargetTableName = "dbo.Sample", UseRealTempTable = false, PrimaryKeyExpression = t => new object[] { t.Pk1, t.Pk2 }, KeepPrimaryKeyInInsertStatement = true, WhenNotMatchedDeleteBehavior = DeleteBehavior.Delete }; helper.DataService.Merge(request); var sampleDtos = helper.DataService.GetAllSampleDtos <SampleCompositeKeyDto>().ToArray(); var firstDto = sampleDtos.First(x => x.Pk1 == 1); firstDto.Pk2.Should().Be("A"); firstDto.TextValue.Should().Be("JJ"); firstDto.IntValue.Should().Be(100); firstDto.DecimalValue.Should().Be(100.99m); firstDto.IsDeleted.Should().BeFalse(); //** var dtos2 = new[] { new SampleCompositeKeyPartialUpdateDto { Pk1 = 1, Pk2 = "A", TextValue = "JJ_Partial" } }; var request2 = new MergeRequest <SampleCompositeKeyPartialUpdateDto> { DataToMerge = dtos2, TargetTableName = "dbo.Sample", UseRealTempTable = false, PrimaryKeyExpression = t => new object[] { t.Pk1, t.Pk2 }, KeepPrimaryKeyInInsertStatement = true, WhenNotMatchedDeleteBehavior = DeleteBehavior.None, OnMergeUpdateActive = true }; helper.DataService.Merge(request2); var sampleDtos2 = helper.DataService.GetAllSampleDtos <SampleCompositeKeyDto>().ToArray(); var firstDto2 = sampleDtos2.First(x => x.Pk1 == 1); firstDto2.Pk2.Should().Be("A"); firstDto2.TextValue.Should().Be("JJ_Partial"); firstDto2.IntValue.Should().Be(100); firstDto2.DecimalValue.Should().Be(100.99m); firstDto2.IsDeleted.Should().BeFalse(); }
public void ShouldAllInsertPrimaryKeyWhenSourceTableHasIdentity() { var helper = new MergeQueryObjectTestHelper(); helper.DataService.DropTable(); helper.DataService.CreateSingleSurrogateKeyTable(); var dtos = new[] { new SampleSurrogateKeyDifferentNamePrimaryKeyDto { PkPrimaryKey = 100, TextValue = "JJ", IntValue = 100, DecimalValue = 100.99m }, new SampleSurrogateKeyDifferentNamePrimaryKeyDto { PkPrimaryKey = 200, TextValue = "ZZ", IntValue = 999, DecimalValue = 123.45m } }; var request = new MergeRequest <SampleSurrogateKeyDifferentNamePrimaryKeyDto> { DataToMerge = dtos, TargetTableName = "dbo.Sample", UseRealTempTable = true, PrimaryKeyExpression = t => new object[] { t.PkPrimaryKey }, KeepPrimaryKeyInInsertStatement = true, KeepIdentityColumnValueOnInsert = true, WhenNotMatchedDeleteBehavior = DeleteBehavior.None, BulkLoaderOptions = t => t.With(c => c.PkPrimaryKey, "Pk") }; helper.DataService.Merge(request); //** var dtosSecondRun = new[] { new SampleSurrogateKeyDifferentNamePrimaryKeyDto { PkPrimaryKey = 100, TextValue = "JJ", IntValue = 100, DecimalValue = 999.99m }, new SampleSurrogateKeyDifferentNamePrimaryKeyDto { PkPrimaryKey = 300, TextValue = "ZZ", IntValue = 999, DecimalValue = 333.33m } }; var requestSecondRun = new MergeRequest <SampleSurrogateKeyDifferentNamePrimaryKeyDto> { DataToMerge = dtosSecondRun, TargetTableName = "dbo.Sample", UseRealTempTable = true, PrimaryKeyExpression = t => new object[] { t.PkPrimaryKey }, KeepPrimaryKeyInInsertStatement = true, KeepIdentityColumnValueOnInsert = true, WhenNotMatchedDeleteBehavior = DeleteBehavior.None, BulkLoaderOptions = t => t.With(c => c.PkPrimaryKey, "Pk") }; helper.DataService.Merge(requestSecondRun); var sourceDtos = helper.DataService.GetAllSampleDtos <SampleSurrogateKey>().ToArray(); var firstDto = sourceDtos.First(x => x.TextValue == "JJ"); firstDto.Pk.Should().Be(100); firstDto.IntValue.Should().Be(100); firstDto.DecimalValue.Should().Be(999.99m); var secondDto = sourceDtos.First(x => x.TextValue == "ZZ"); secondDto.Pk.Should().Be(200); secondDto.Pk.Should().BeGreaterThan(0); secondDto.IntValue.Should().Be(999); secondDto.DecimalValue.Should().Be(123.45m); }