public void Should_put_all_private_keys_into_where_clause()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.Id)
            .ThatIsPrivateKey();
            entityProfile.HasProperty <TestEntity, string>(x => x.RecordId)
            .ThatIsPrivateKey();
            var elements = new List <TestEntity>
            {
                new TestEntity {
                    Id = 12, RecordId = "rec-01", SensorId = "sen-0"
                }
            };
            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+(\"id\"\\s*=\\s*\\d+|\\s*\"record_id\"\\s*=\\s*@param_record_id_\\d+)\\s*(and\\s*\"id\"\\s*=\\s*\\d+|\\s+and\\s*\"record_id\"\\s*=\\s*@param_record_id_\\d+)";

            Assert.IsTrue(Regex.IsMatch(commandResult.Command, pattern, RegexOptions.IgnoreCase));
        }
        public void Should_return_two_parameters_for_id_and_value()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.Id)
            .ThatIsPrivateKey();
            entityProfile.HasProperty <TestEntity, int>(x => x.IntValue)
            .ThatIsPrivateKey();

            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(0, commandResult.SqlParameters.Count);
        }
Esempio n. 3
0
        public void HasProperty_should_pass_binary_expressions()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            Assert.DoesNotThrow(() => entityProfile.HasProperty <TestEntity, int>("id", x => x.Id + x.IntValue));
            Assert.DoesNotThrow(() => entityProfile.HasProperty <TestEntity, int>("value", x => x.Id + 10));
        }
        public void Should_not_put_column_that_was_not_mapped()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.Id)
            .ThatIsPrivateKey();
            entityProfile.HasProperty <TestEntity, string>(x => x.RecordId);
            entityProfile.ToTable("test_entity", "custom");

            var elements = new List <TestEntity>
            {
                new TestEntity {
                    Id = 12, RecordId = "rec-01", SensorId = "sens-01", IntValue = 127
                },
            };
            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("sensor_id"));
        }
        public void Should_build_params_for_string_id()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.IntValue);
            entityProfile.HasProperty <TestEntity, string>(x => x.RecordId)
            .ThatIsPrivateKey();
            var elements = new List <TestEntity>
            {
                new TestEntity {
                    IntValue = 12, RecordId = "rec-01", SensorId = "sen-0"
                },
                new TestEntity {
                    IntValue = 13, RecordId = "rec-02", SensorId = "sen-1"
                }
            };
            var result = _testService.Generate(elements, entityProfile, CancellationToken.None);

            Assert.NotNull(result);
            var commandResult = result.FirstOrDefault();

            Assert.NotNull(commandResult);
            Assert.NotNull(commandResult.SqlParameters);
            Assert.AreEqual(2, commandResult.SqlParameters.Count);

            Assert.IsTrue(Regex.IsMatch(commandResult.Command, DELETE_PARAM_PATTERN, RegexOptions.IgnoreCase));
        }
Esempio n. 6
0
        public void Should_not_put_constraint_columns_into_update_set_clause()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.Id)
            .ThatIsPrivateKey();
            entityProfile.HasPropertyAsPartOfUniqueConstraint <TestEntity, string>(x => x.RecordId);
            entityProfile.HasUniqueConstraint("business_identity");

            entityProfile.HasProperty <TestEntity, string>(x => x.SensorId);
            entityProfile.ToTable("test_entity", "custom");

            var elements = new List <TestEntity>
            {
                new TestEntity {
                    Id = 12, RecordId = "rec-01", SensorId = "sens-01", IntValue = 127
                },
            };
            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 upsertSetPattern = "(set\\s+\"record_id\"\\s*=\\s*(\"\\w+\".)?\"\\w+\".\"\\w+\"\\s*)|,\\s*\"record_id\"\\s*=";

            Assert.IsFalse(Regex.IsMatch(commandResult.Command, upsertSetPattern, RegexOptions.IgnoreCase));
        }
        public void Should_build_correct_id_property_name()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.IntValue);
            entityProfile.HasProperty <TestEntity, string>(x => x.RecordId)
            .ThatIsPrivateKey();
            var elements = new List <TestEntity>
            {
                new TestEntity {
                    IntValue = 12, RecordId = "rec-01", SensorId = "sen-0"
                },
                new TestEntity {
                    IntValue = 13, RecordId = "rec-02", SensorId = "sen-1"
                }
            };
            var result = _testService.Generate(elements, entityProfile, CancellationToken.None);

            Assert.NotNull(result);
            var commandResult = result.FirstOrDefault();

            Assert.NotNull(commandResult);

            Assert.IsTrue(commandResult.Command.Contains("where \"record_id\" in"));
        }
        public void Should_match_insert_cmd_without_returning_clause()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.Id)
            .ThatIsPrivateKey();
            entityProfile.HasPropertyAsPartOfUniqueConstraint <TestEntity, string>(x => x.RecordId);
            entityProfile.HasUniqueConstraint("business_identity");

            entityProfile.HasProperty <TestEntity, string>(x => x.SensorId);
            entityProfile.ToTable("test_entity", "custom");

            var elements = new List <TestEntity>
            {
                new TestEntity {
                    Id = 12, RecordId = "rec-01", SensorId = "sens-01", IntValue = 127
                },
            };
            var commands = _testService.Generate(elements, entityProfile, CancellationToken.None);

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

            Assert.NotNull(commandResult.Command);

            // Check that the whole command still matches the upsert pattern
            Assert.IsTrue(Regex.IsMatch(commandResult.Command, UpsertConsts.UPSERT_PATTERN, RegexOptions.IgnoreCase));

            // Check that there is no returning clause in the result command
            Assert.IsFalse(commandResult.Command.Contains("returning"));
        }
Esempio n. 9
0
        public void Should_match_upsert_cmd_of_one_element()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(entity => entity.Id)
            .ThatIsAutoGenerated()
            .ThatIsPrivateKey();
            entityProfile.HasPropertyAsPartOfUniqueConstraint <TestEntity, string>(entity => entity.RecordId);
            entityProfile.HasPropertyAsPartOfUniqueConstraint <TestEntity, string>(entity => entity.SensorId);
            entityProfile.HasProperty <TestEntity, int>(entity => entity.IntValue);

            var elements = new List <TestEntity>
            {
                new TestEntity {
                    RecordId = "rec-01", SensorId = "sens-01", IntValue = 127
                },
            };
            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(Regex.IsMatch(commandResult.Command, UpsertConsts.UPSERT_PATTERN, RegexOptions.IgnoreCase));
        }
        public void Should_match_delete_cmd_without_returning_clause()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.Id)
            .ThatIsPrivateKey();
            entityProfile.HasProperty <TestEntity, string>(x => x.RecordId);
            entityProfile.HasProperty <TestEntity, string>(x => x.SensorId);
            entityProfile.ToTable("test_entity", "custom");

            var elements = new List <TestEntity>
            {
                new TestEntity {
                    Id = 12, RecordId = "rec-01", SensorId = "sens-01", IntValue = 127
                },
            };
            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(Regex.IsMatch(commandResult.Command, DELETE_PATTERN, RegexOptions.IgnoreCase));
        }
Esempio n. 11
0
        public void Should_match_constraint_name_in_on_conflict_clause()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.Id)
            .ThatIsPrivateKey();
            entityProfile.HasPropertyAsPartOfUniqueConstraint <TestEntity, string>(x => x.RecordId);
            entityProfile.HasUniqueConstraint("business_identity");

            entityProfile.HasProperty <TestEntity, string>(x => x.SensorId);
            entityProfile.ToTable("test_entity", "custom");

            var elements = new List <TestEntity>
            {
                new TestEntity {
                    Id = 12, RecordId = "rec-01", SensorId = "sens-01", IntValue = 127
                },
            };
            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 onConflictClausePattern = "on\\s+conflict\\s+(\\(\\s*\"\\w+\"\\s*\\)|on\\s+constraint\\s+\"\\w+\")";

            Assert.IsTrue(Regex.IsMatch(commandResult.Command, onConflictClausePattern, RegexOptions.IgnoreCase));
        }
Esempio n. 12
0
        public void HasProperty_should_not_pass_duplicates()
        {
            var i             = Math.Round((decimal)5 / 2, MidpointRounding.ToPositiveInfinity);
            var entityProfile = new EntityProfile(typeof(TestEntity));

            Assert.DoesNotThrow(() => entityProfile.HasProperty <TestEntity, int>(x => x.Id));
            Assert.Throws <TypeMappingException>(() => entityProfile.HasProperty <TestEntity, int>(x => x.Id));
        }
Esempio n. 13
0
        public void HasProperty_should_set_type_mapping_exception_properties()
        {
            var i             = Math.Round((decimal)5 / 2, MidpointRounding.ToPositiveInfinity);
            var entityProfile = new EntityProfile(typeof(TestEntity));

            Assert.DoesNotThrow(() => entityProfile.HasProperty <TestEntity, int>(x => x.Id));
            var ex = Assert.Throws <TypeMappingException>(() => entityProfile.HasProperty <TestEntity, int>(x => x.Id));

            Assert.AreEqual(entityProfile.EntityType, ex.EntityType);
            Assert.AreEqual(nameof(TestEntity.Id), ex.PropertyName);
        }
Esempio n. 14
0
        public async Task SimpleDelete_should_delete_elements_with_multiple_pk()
        {
            var bulkServiceOptions = new BulkServiceOptions();
            var profile            = new EntityProfile(typeof(TestEntity));

            profile.HasProperty <TestEntity, int>(x => x.Id)
            .ThatIsPrivateKey()
            .ThatIsAutoGenerated();
            profile.HasProperty <TestEntity, string>(x => x.RecordId)
            .ThatIsPrivateKey();
            profile.HasProperty <TestEntity, int>(x => x.Value);
            profile.ToTable("simple_test_entity", "unit_tests");
            bulkServiceOptions.AddEntityProfile <TestEntity>(profile);

            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);

            var elements = new List <TestEntity>
            {
                new TestEntity {
                    RecordId = "rec-01", SensorId = "sens-01", Value = 127
                },
                new TestEntity {
                    RecordId = "rec-02", SensorId = "sens-01", Value = 128
                },
                new TestEntity {
                    RecordId = "rec-01", SensorId = "sens-02", Value = 227
                },
            };

            await using var connection = new NpgsqlConnection(_configuration.ConnectionString);
            await _testService.InsertAsync(connection, elements, CancellationToken.None);

            await _testService.DeleteAsync(connection, elements, CancellationToken.None);

            var countOfRows = await _testUtils.HowManyRowsWithIdsAsync(_entityProfile, new[] { elements[0].Id, elements[1].Id, elements[2].Id });

            Assert.AreEqual(0, countOfRows);
        }
Esempio n. 15
0
        public void HasProperty_should_pass_nullable_properties()
        {
            var entityProfile = new EntityProfile(typeof(EntityWithNullableProperty));

            Assert.DoesNotThrow(() => entityProfile.HasProperty <EntityWithNullableProperty, bool?>
                                    ("value", x => x.NullableBool));
        }
Esempio n. 16
0
        public void HasProperty_should_pass_constant_expressions()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            Assert.DoesNotThrow(()
                                => entityProfile.HasProperty <TestEntity, int>("id", x => 10));
        }
        public void Should_set_date_column_types()
        {
            var entityProfile   = new EntityProfile(typeof(EntityWithDateTime));
            var propertyProfile = entityProfile.HasProperty <EntityWithDateTime, Date>(x => x.Date);

            Assert.AreEqual(NpgsqlDbType.Date, propertyProfile.DbColumnType);
        }
        public void Should_not_include_after_insert_columns_into_update_returning_clause()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.Id)
            .ThatIsPrivateKey()
            .MustBeUpdatedAfterInsert();
            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 returning "id";  or returning ""value", "id";
            var pattern = @"returning\s+(""\w+""\s*,\s*)*""id""";

            Assert.IsFalse(Regex.IsMatch(commandResult.Command, pattern, RegexOptions.IgnoreCase));
        }
Esempio n. 19
0
        public void HasProperty_should_not_pass_constant_expressions_without_column()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));
            var ex            = Assert.Throws <ArgumentException>(() => entityProfile.HasProperty <TestEntity, int>(x => 10));

            Assert.AreEqual("column", ex.ParamName);
        }
        public void Should_return_true_given_nullable_double_member()
        {
            var entityProfile = new EntityProfile(typeof(EntityWithNullableProperty));
            var propInfo      = entityProfile.HasProperty <EntityWithNullableProperty, double?>(entity => entity.NullableDouble);

            Assert.IsFalse(propInfo.IsDynamicallyInvoked());
        }
        public void Should_return_true_given_date_member()
        {
            var entityProfile = new EntityProfile(typeof(EntityWithDateTime));
            var propInfo      = entityProfile.HasProperty <EntityWithDateTime, Date>("date", entity => entity.Date);

            Assert.IsTrue(propInfo.IsDynamicallyInvoked());
        }
        public void Should_return_true_given_double_member()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));
            var propInfo      = entityProfile.HasProperty <TestEntity, double>(entity => entity.DoubleValue);

            Assert.IsFalse(propInfo.IsDynamicallyInvoked());
        }
        public void Should_return_true_given_converted_enum()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));
            var propInfo      = entityProfile.HasProperty <TestEntity, int?>(entity => (int?)entity.Enumeration);

            Assert.IsTrue(propInfo.IsDynamicallyInvoked());
        }
        public void Should_return_true_given_string_member()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));
            var propInfo      = entityProfile.HasProperty <TestEntity, string>(entity => entity.RecordId);

            Assert.IsTrue(propInfo.IsDynamicallyInvoked());
        }
        public void Should_return_true_given_simple_enum_member()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));
            var propInfo      = entityProfile.HasProperty <TestEntity, SomeEnum>(entity => entity.Enumeration);

            Assert.IsFalse(propInfo.IsDynamicallyInvoked());
        }
Esempio n. 26
0
        public async Task Should_delete_elements_with_multiple_pk()
        {
            var bulkServiceOptions = new BulkServiceOptions();
            var profile            = new EntityProfile(typeof(TestEntity));

            profile.HasProperty <TestEntity, int>(x => x.Id)
            .ThatIsPrivateKey()
            .ThatIsAutoGenerated();
            profile.HasProperty <TestEntity, string>(x => x.RecordId)
            .ThatIsPrivateKey();
            profile.HasProperty <TestEntity, int>(x => x.Value);
            profile.ToTable("simple_test_entity", "unit_tests");
            bulkServiceOptions.AddEntityProfile <TestEntity>(profile);

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

            _testService = new NpgsqlCommandsBulkService(
                bulkServiceOptions,
                NullLoggerFactory.Instance,
                insertCommandBuilder,
                updateCommandBuilder,
                deleteCommandBuilder,
                upsertCommandBuilder);

            var elements = new List <TestEntity>
            {
                new TestEntity {
                    RecordId = "rec-01", SensorId = "sens-01", Value = 127
                },
                new TestEntity {
                    RecordId = "rec-02", SensorId = "sens-01", Value = 128
                },
                new TestEntity {
                    RecordId = "rec-01", SensorId = "sens-02", Value = 227
                },
            };

            await using var connection = new NpgsqlConnection(_configuration.ConnectionString);
            var ex = Assert.ThrowsAsync <SqlBulkExecutionException <TestEntity> >(() => _testService.DeleteAsync(connection, elements, CancellationToken.None));

            Assert.IsTrue(ex.InnerException is ArgumentException);
        }
        public void Should_not_allow_generate_cmd_without_private_key()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.Id);
            entityProfile.HasProperty <TestEntity, string>(x => x.RecordId);
            entityProfile.HasProperty <TestEntity, string>(x => x.SensorId);
            entityProfile.ToTable("test_entity", "custom");

            var elements = new List <TestEntity>
            {
                new TestEntity {
                    Id = 12, RecordId = "rec-01", SensorId = "sens-01", IntValue = 127
                },
            };

            Assert.Throws <SqlGenerationException>(() => _testService.Generate(elements, entityProfile, CancellationToken.None));
        }
        public void Upsert_should_not_pass_entities_without_unique_constraint()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.Id);
            entityProfile.ToTable("test_entity", "custom");
            var elements = new List <TestEntity>();

            Assert.Throws <ArgumentException>(() => _testService.Generate(elements, entityProfile, CancellationToken.None));
        }
        public void Should_throw_exception_for_empty_elements()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.Id).ThatIsPrivateKey();
            entityProfile.ToTable("test_entity", "custom");
            var elements = new List <TestEntity>();

            Assert.Throws <ArgumentException>(() => _testService.Generate(elements, entityProfile, CancellationToken.None));
        }
Esempio n. 30
0
        public void HasProperty_should_map_to_custom_column_name()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, string>("RecordID", x => x.RecordId);

            Assert.IsNotEmpty(entityProfile.Properties);
            Assert.IsNotNull(entityProfile.Properties.First().Value);
            Assert.AreEqual("RecordID", (string)entityProfile.Properties.First().Value.DbColumnName);
        }