From 8bf330e36b39372cb8aebb367a8d6d01bc66e86f Mon Sep 17 00:00:00 2001 From: babyfish-ct Date: Mon, 23 Dec 2024 20:55:59 +0800 Subject: [PATCH] #847, #848 --- .../sql/mutation/ConstraintViolationTest.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/project/jimmer-sql/src/test/java/org/babyfish/jimmer/sql/mutation/ConstraintViolationTest.java b/project/jimmer-sql/src/test/java/org/babyfish/jimmer/sql/mutation/ConstraintViolationTest.java index 7d91e6fc5..eae7ff52d 100644 --- a/project/jimmer-sql/src/test/java/org/babyfish/jimmer/sql/mutation/ConstraintViolationTest.java +++ b/project/jimmer-sql/src/test/java/org/babyfish/jimmer/sql/mutation/ConstraintViolationTest.java @@ -878,6 +878,81 @@ public void testConflictKeyWithIdByPostgres_Issue689() { ); } + @Test + public void testConflictKeyByPostgresForPR848() { + + NativeDatabases.assumeNativeDatabase(); + + Book book = BookDraft.$.produce(draft -> { + draft.setName("GraphQL in Action"); + draft.setEdition(3); + draft.setPrice(new BigDecimal("54.9")); + }); + setAutoIds(Book.class, UUID.randomUUID(), UUID.randomUUID()); + executeAndExpectResult( + NativeDatabases.POSTGRES_DATA_SOURCE, + getSqlClient(it -> { + it.setDialect(new PostgresDialect()); + }) + .getEntities() + .saveCommand(book) + .setMode(SaveMode.INSERT_ONLY), + ctx -> { + ctx.statement(it -> { + it.sql( + "insert into BOOK(ID, NAME, EDITION, PRICE) " + + "values(?, ?, ?, ?)" + ); + it.variables( + UNKNOWN_VARIABLE, + "GraphQL in Action", + 3, + new BigDecimal("54.9") + ); + }); + ctx.statement(it -> { + it.sql( + "select tb_1_.ID " + + "from BOOK tb_1_ " + + "where tb_1_.ID = ?" + ); + it.queryReason(QueryReason.INVESTIGATE_CONSTRAINT_VIOLATION_ERROR); + }); + ctx.statement(it -> { + it.sql( + "select tb_1_.ID, tb_1_.NAME, tb_1_.EDITION " + + "from BOOK tb_1_ " + + "where (tb_1_.NAME, tb_1_.EDITION) = (?, ?)" + ); + it.variables("GraphQL in Action", 3); + it.queryReason(QueryReason.INVESTIGATE_CONSTRAINT_VIOLATION_ERROR); + }); + ctx.throwable(it -> { + it.message( + "Save error caused by the path: \"\": Cannot save the entity, " + + "the value of the key properties \"[" + + "org.babyfish.jimmer.sql.model.Book.name, " + + "org.babyfish.jimmer.sql.model.Book.edition" + + "]\" are \"Tuple2(_1=GraphQL in Action, _2=3)\" which already exists" + ); + SaveException.NotUnique ex = it.type(SaveException.NotUnique.class); + Assertions.assertTrue( + ex.isMatched( + BookProps.NAME, + BookProps.EDITION + ) + ); + Assertions.assertTrue( + ex.isMatched( + BookProps.EDITION, + BookProps.NAME + ) + ); + }); + } + ); + } + @Test public void testIllegalForeignKeyByPostgres() {