public void ShouldThrowErrorIfIdentityValueIsNotSupplied()
        {
            var testingDatabaseService = new TestingDatabaseService();

            testingDatabaseService.CreateTestDatabase();

            var dataGateway = new TestingDataGateway(testingDatabaseService);

            dataGateway.DropTable();
            dataGateway.CreateSingleSurrogateKeyTable();

            var dtos = new[]
            {
                new SampleSurrogateKey
                {
                    Pk           = 100,
                    TextValue    = "JJ",
                    IntValue     = 100,
                    DecimalValue = 100.99m
                }
            };

            var exception = Assert.Throws <ArgumentException>(() =>
            {
                dataGateway.ExecuteWithConnection(conn =>
                {
                    BulkLoaderFactory.Create()
                    .InsertWithOptions("sample", conn, true, dtos)
                    .Execute();
                });
            });

            exception.Message.Should().Contain(@"must be called when ""keepIdentityColumnValue"" is True.");
        }
        public void ShouldNotBatchWhenOptionSelected()
        {
            var testingDatabaseService = new TestingDatabaseService();

            testingDatabaseService.CreateTestDatabase();

            var dataGateway = new TestingDataGateway(testingDatabaseService);

            dataGateway.DropTable();
            dataGateway.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 sqlBulkCopyUtilitySpy = new SqlBulkCopyUtilitySpy();

            dataGateway.ExecuteWithConnection(conn =>
            {
                new BulkLoader(sqlBulkCopyUtilitySpy)
                .InsertWithOptions("sample", conn, true, dtos)
                .IdentityColumn(c => c.Pk)
                .SetBatchSize(1)
                .NoBatch()
                .Execute();
            });

            sqlBulkCopyUtilitySpy.BulkCopyCalled.Should().Be(1);

            var databaseDtos = dataGateway.GetAllSampleSurrogateKey().ToArray();

            var firstDto = databaseDtos.First(x => x.TextValue == "JJ");

            firstDto.IntValue.Should().Be(100);
            firstDto.DecimalValue.Should().Be(100.99m);

            var secondDto = databaseDtos.First(x => x.TextValue == "ZZ");

            secondDto.IntValue.Should().Be(999);
            secondDto.DecimalValue.Should().Be(123.45m);
        }
Exemple #3
0
        public void ShouldRespectRenamedFields()
        {
            var testingDatabaseService = new TestingDatabaseService();

            testingDatabaseService.CreateTestDatabase();

            var dataGateway = new TestingDataGateway(testingDatabaseService);

            dataGateway.DropTable();
            dataGateway.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
                }
            };

            dataGateway.ExecuteWithConnection(conn =>
            {
                BulkLoaderFactory.Create()
                .InsertWithOptions("sample", conn, true, dtos)
                .IdentityColumn(c => c.Pk)
                .With(c => c.TextValueExtra, "TextValue")
                .With(c => c.IntValueExtra, "IntValue")
                .With(c => c.DecimalValueExtra, "DecimalValue")
                .Execute();
            });

            var databaseDtos = dataGateway.GetAllSampleSurrogateKey().ToArray();

            var firstDto = databaseDtos.First(x => x.TextValue == "JJ");

            firstDto.Pk.Should().Be(100);
            firstDto.IntValue.Should().Be(100);
            firstDto.DecimalValue.Should().Be(100.99m);

            var secondDto = databaseDtos.First(x => x.TextValue == "ZZ");

            secondDto.Pk.Should().Be(200);
            secondDto.IntValue.Should().Be(999);
            secondDto.DecimalValue.Should().Be(123.45m);
        }
        public void ShouldRespectTheWithoutOption()
        {
            var testingDatabaseService = new TestingDatabaseService();

            testingDatabaseService.CreateTestDatabase();

            var dataGateway = new TestingDataGateway(testingDatabaseService);

            dataGateway.DropTable();
            dataGateway.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
                }
            };

            dataGateway.ExecuteWithConnection(conn =>
            {
                BulkLoaderFactory.Create()
                .InsertWithOptions("Sample", conn, true, dtos)
                .Without("DecimalValue")
                .Without(t => t.IntValue)
                .Execute();
            });

            var databaseDtos = dataGateway.GetAllSampleSurrogateKey().ToArray();

            var firstDto = databaseDtos.First(x => x.TextValue == "JJ");

            firstDto.Pk.Should().Be(100);
            firstDto.IntValue.Should().BeNull();
            firstDto.DecimalValue.Should().BeNull();

            var secondDto = databaseDtos.First(x => x.TextValue == "ZZ");

            secondDto.Pk.Should().Be(200);
            secondDto.IntValue.Should().BeNull();
            secondDto.DecimalValue.Should().BeNull();
        }
        public void ShouldNotInsertPrimaryKeyWhenKeepIdentityOptionIsFalse()
        {
            var testingDatabaseService = new TestingDatabaseService();

            testingDatabaseService.CreateTestDatabase();

            var dataGateway = new TestingDataGateway(testingDatabaseService);

            dataGateway.DropTable();
            dataGateway.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
                }
            };

            dataGateway.ExecuteWithConnection(conn =>
            {
                BulkLoaderFactory.Create()
                .InsertWithOptions("sample", conn, false, dtos)
                .IdentityColumn(c => c.Pk)
                .Execute();
            });

            var databaseDtos = dataGateway.GetAllSampleSurrogateKey().ToArray();

            var firstDto = databaseDtos.First(x => x.TextValue == "JJ");

            firstDto.Pk.Should().NotBe(100);
            firstDto.IntValue.Should().Be(100);
            firstDto.DecimalValue.Should().Be(100.99m);

            var secondDto = databaseDtos.First(x => x.TextValue == "ZZ");

            secondDto.Pk.Should().NotBe(200);
            secondDto.IntValue.Should().Be(999);
            secondDto.DecimalValue.Should().Be(123.45m);
        }
Exemple #6
0
        public void ShouldUseDefaultBulkCopyUtilityWhenNoConstructorParams()
        {
            var testingDatabaseService = new TestingDatabaseService();

            testingDatabaseService.CreateTestDatabase();

            var dataGateway = new TestingDataGateway(testingDatabaseService);

            dataGateway.DropTable();
            dataGateway.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
                }
            };

            dataGateway.ExecuteWithConnection(conn =>
            {
                new BulkLoader()
                .InsertWithOptions("sample", conn, true, dtos)
                .IdentityColumn(c => c.Pk)
                .Execute();
            });

            var databaseDtos = dataGateway.GetAllSampleSurrogateKey().ToArray();

            var firstDto = databaseDtos.First(x => x.TextValue == "JJ");

            firstDto.IntValue.Should().Be(100);
            firstDto.DecimalValue.Should().Be(100.99m);

            var secondDto = databaseDtos.First(x => x.TextValue == "ZZ");

            secondDto.IntValue.Should().Be(999);
            secondDto.DecimalValue.Should().Be(123.45m);
        }
        public void ShouldHaveAccessToRenameRules()
        {
            var testingDatabaseService = new TestingDatabaseService();

            testingDatabaseService.CreateTestDatabase();

            var dataGateway = new TestingDataGateway(testingDatabaseService);

            dataGateway.DropTable();
            dataGateway.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 bulkLoader = BulkLoaderFactory.Create()
                             .InsertWithOptions("Sample", null, true, dtos)
                             .With(c => c.TextValueExtra, "TextValue")
                             .With(c => c.IntValueExtra, "IntValue")
                             .With(c => c.DecimalValueExtra, "DecimalValue");

            var renameRules = bulkLoader.GetRenameRules();

            renameRules.Keys.Count().Should().Be(3);
            renameRules.First(x => x.Key == "TextValueExtra").Value.Should().Be("TextValue");
            renameRules.First(x => x.Key == "IntValueExtra").Value.Should().Be("IntValue");
            renameRules.First(x => x.Key == "DecimalValueExtra").Value.Should().Be("DecimalValue");
        }