Beispiel #1
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_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));
        }
        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_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"));
        }
Beispiel #5
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));
        }
        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));
        }
        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_when_elements_collection_items_are_all_null()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

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

            Assert.Throws <ArgumentException>(() => _testService.Generate(elements, entityProfile, CancellationToken.None));
        }
Beispiel #9
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);
        }
Beispiel #10
0
        public void Should_not_throw_exception_when_at_least_one_item_is_not_null()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.Id);
            entityProfile.ToTable("test_entity", "custom");
            var elements = new List <TestEntity>
            {
                null,
                new TestEntity {
                    RecordId = "rec-01"
                },
                null
            };

            Assert.DoesNotThrow(() => _testService.Generate(elements, entityProfile, CancellationToken.None));
        }
Beispiel #11
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));
        }
Beispiel #13
0
        public void Should_include_scheme_and_table_name()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

            entityProfile.HasProperty <TestEntity, int>(x => x.Id);
            entityProfile.ToTable("test_entity", "custom");
            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 \"custom\".\"test_entity\""));
        }