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")); }
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_column_constraint_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.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_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_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_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)); }