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.HasPropertyAsPartOfUniqueConstraint <TestEntity, string>(x => x.RecordId);
            entityProfile.HasUniqueConstraint("business_identity");
            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"));
        }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 4
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 Upsert_should_not_pass_entities_without_unique_properties()
        {
            var entityProfile = new EntityProfile(typeof(TestEntity));

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

            Assert.Throws <ArgumentException>(() => _testService.Generate(elements, entityProfile, CancellationToken.None));
        }