public void Should_put_only_private_keys_into_where_clause()
        {
            var entityProfile = new SimpleEntityProfile();
            var elements      = new List <TestEntity>
            {
                new TestEntity {
                    RecordId = "rec-01"
                }
            };
            var commands = _testService.Generate(elements, entityProfile, CancellationToken.None);

            Assert.NotNull(commands);
            Assert.AreEqual(1, commands.Count);
            var commandResult = commands.First();

            Assert.NotNull(commandResult.Command);

            // pattern should match "id" whatever builder put the "id" column in any order where "id" = @param1;  or where ""value"=@param1, "id"=@param2;
            var pattern = @"where\s+(""\w+""\s*=\s*@param\d+\s*,\s*)*""{0}""\s*=";

            Assert.IsTrue(Regex.IsMatch(commandResult.Command, string.Format(pattern, "id"), RegexOptions.IgnoreCase));
            Assert.IsFalse(Regex.IsMatch(commandResult.Command, string.Format(pattern, "value"), RegexOptions.IgnoreCase));
            Assert.IsFalse(Regex.IsMatch(commandResult.Command, string.Format(pattern, "record_id"), RegexOptions.IgnoreCase));
            Assert.IsFalse(Regex.IsMatch(commandResult.Command, string.Format(pattern, "sensor_id"), RegexOptions.IgnoreCase));
        }
Exemple #2
0
        public async Task SetUp()
        {
            var truncateTableCmd   = "truncate \"unit_tests\".\"simple_test_entity\";";
            var resetIdSequenceCmd = "ALTER SEQUENCE \"unit_tests\".\"simple_test_entity_id_seq\" RESTART WITH 1;";

            await using var connection = new NpgsqlConnection(_configuration.ConnectionString);
            await using var command    = new NpgsqlCommand($"{truncateTableCmd}{resetIdSequenceCmd}", connection);
            await connection.OpenAsync();

            await command.ExecuteNonQueryAsync();

            var bulkServiceOptions = new BulkServiceOptions();

            _entityProfile = new SimpleEntityProfile();
            bulkServiceOptions.AddEntityProfile <TestEntity>(_entityProfile);

            var insertCommandBuilder = new InsertSqlCommandBuilder(NullLoggerFactory.Instance);
            var updateCommandBuilder = new Mock <IUpdateSqlCommandBuilder>().Object;
            var deleteCommandBuilder = new SimpleDeleteSqlCommandBuilder(NullLoggerFactory.Instance);
            var upsertCommandBuilder = new Mock <IUpsertSqlCommandBuilder>().Object;

            _testService = new NpgsqlCommandsBulkService(
                bulkServiceOptions,
                NullLoggerFactory.Instance,
                insertCommandBuilder,
                updateCommandBuilder,
                deleteCommandBuilder,
                upsertCommandBuilder);
        }
Exemple #3
0
        public void Should_calculate_value_of_an_int_property()
        {
            var entityProfile = new SimpleEntityProfile();
            var propInfo      = entityProfile.Properties[nameof(TestEntity.Id)];

            Assert.NotNull(propInfo);

            var item = new TestEntity {
                Id = 13
            };
            var result = propInfo.GetPropertyValue(item);

            Assert.AreEqual(13, result);
        }
Exemple #4
0
        public void Should_calculate_value_of_a_string_property()
        {
            var entityProfile = new SimpleEntityProfile();
            var propInfo      = entityProfile.Properties[nameof(TestEntity.RecordId)];

            Assert.NotNull(propInfo);

            var item = new TestEntity {
                RecordId = "rec-01"
            };
            var result = propInfo.GetPropertyValue(item);

            Assert.AreEqual("rec-01", result);
        }
Exemple #5
0
        public void Should_include_table_name()
        {
            var entityProfile = new SimpleEntityProfile();
            var elements      = new List <TestEntity>
            {
                new TestEntity {
                    RecordId = "rec-01"
                }
            };
            var commands = _testService.Generate(elements, entityProfile, CancellationToken.None);

            Assert.NotNull(commands);
            Assert.AreEqual(1, commands.Count);
            var commandResult = commands.First();

            Assert.NotNull(commandResult.Command);

            Assert.IsTrue(commandResult.Command.ToLowerInvariant().StartsWith("insert into \"unit_tests\".\"simple_test_entity\""));
        }
Exemple #6
0
        public void Should_close_command_with_semicolon()
        {
            var entityProfile = new SimpleEntityProfile();
            var elements      = new List <TestEntity>
            {
                new TestEntity {
                    RecordId = "rec-01"
                }
            };
            var commands = _testService.Generate(elements, entityProfile, CancellationToken.None);

            Assert.NotNull(commands);
            Assert.AreEqual(1, commands.Count);
            var commandResult = commands.First();

            Assert.NotNull(commandResult.Command);

            Assert.IsTrue(commandResult.Command.EndsWith(";"));
        }
Exemple #7
0
        public void Should_exclude_autogenerated_columns()
        {
            var entityProfile = new SimpleEntityProfile();
            var elements      = new List <TestEntity>
            {
                new TestEntity {
                    RecordId = "rec-01"
                }
            };
            var commands = _testService.Generate(elements, entityProfile, CancellationToken.None);

            Assert.NotNull(commands);
            Assert.AreEqual(1, commands.Count);
            var commandResult = commands.First();

            Assert.NotNull(commandResult.Command);

            Assert.IsFalse(commandResult.Command.Contains("(\"id\""));
        }
        public void Should_not_build_params_for_int_id()
        {
            var entityProfile = new SimpleEntityProfile();
            var elements      = new List <TestEntity>
            {
                new TestEntity {
                    Id = 10, RecordId = "rec-01", SensorId = "sens-02", IntValue = 12
                }
            };
            var commands = _testService.Generate(elements, entityProfile, CancellationToken.None);

            Assert.NotNull(commands);
            Assert.AreEqual(1, commands.Count);
            var commandResult = commands.First();

            Assert.NotNull(commandResult.Command);
            Assert.NotNull(commandResult.SqlParameters);
            Assert.IsEmpty(commandResult.SqlParameters);
        }
        public void Should_include_scheme_and_table_name()
        {
            var entityProfile = new SimpleEntityProfile();
            var elements      = new List <TestEntity>
            {
                new TestEntity {
                    RecordId = "rec-01"
                }
            };
            var commands = _testService.Generate(elements, entityProfile, CancellationToken.None);

            Assert.NotNull(commands);
            Assert.AreEqual(1, commands.Count);
            var commandResult = commands.First();

            Assert.NotNull(commandResult.Command);
            var pattern = @"delete\s+from\s+""unit_tests"".""simple_test_entity""\s+";

            Assert.IsTrue(Regex.IsMatch(commandResult.Command, pattern, RegexOptions.IgnoreCase));
        }
Exemple #10
0
        public void Should_return_correct_parameters_types()
        {
            var entityProfile = new SimpleEntityProfile();
            var elements      = new List <TestEntity>
            {
                new TestEntity {
                    RecordId = "rec-01", SensorId = "sens-02", IntValue = 12
                }
            };
            var commands = _testService.Generate(elements, entityProfile, CancellationToken.None);

            Assert.NotNull(commands);
            Assert.AreEqual(1, commands.Count);
            var commandResult = commands.First();

            Assert.NotNull(commandResult.Command);
            Assert.NotNull(commandResult.SqlParameters);

            Assert.AreEqual(2, commandResult.SqlParameters.Count);
            Assert.IsTrue(commandResult.SqlParameters.All(p => p.NpgsqlDbType == NpgsqlDbType.Text));
        }
        public void Should_exclude_autogenerated_columns_from_returning_clause()
        {
            var entityProfile = new SimpleEntityProfile();
            var elements      = new List <TestEntity>
            {
                new TestEntity {
                    RecordId = "rec-01"
                }
            };
            var commands = _testService.Generate(elements, entityProfile, CancellationToken.None);

            Assert.NotNull(commands);
            Assert.AreEqual(1, commands.Count);
            var commandResult = commands.First();

            Assert.NotNull(commandResult.Command);

            // pattern should not match "id" whatever builder put the "id" column returning "id";  or returning ""value", "id";
            var pattern = @"returning\s+(""\w+""\s*,\s*)*""id""";

            Assert.IsFalse(Regex.IsMatch(commandResult.Command, pattern, RegexOptions.IgnoreCase));
        }
        public void Should_return_correct_parameters_values()
        {
            var entityProfile = new SimpleEntityProfile();
            var elements      = new List <TestEntity>
            {
                new TestEntity {
                    Id = 10, RecordId = "rec-01", SensorId = "sens-02", IntValue = 12
                }
            };
            var commands = _testService.Generate(elements, entityProfile, CancellationToken.None);

            Assert.NotNull(commands);
            Assert.AreEqual(1, commands.Count);
            var commandResult = commands.First();

            Assert.NotNull(commandResult.Command);
            Assert.NotNull(commandResult.SqlParameters);

            Assert.AreEqual(2, commandResult.SqlParameters.Count);
            Assert.NotNull(commandResult.SqlParameters.FirstOrDefault(p => p.Value.ToString() == "rec-01"));
            Assert.NotNull(commandResult.SqlParameters.FirstOrDefault(p => p.Value.ToString() == "sens-02"));
            Assert.AreEqual(0, commandResult.SqlParameters.Where(p => p.NpgsqlDbType == NpgsqlDbType.Integer).ToList().Count);
        }