Skip to content

Commit

Permalink
[#137] Add test for JooqxSession
Browse files Browse the repository at this point in the history
  • Loading branch information
zero88 committed Aug 19, 2022
1 parent e41a2fd commit 25b23ee
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.github.zero88.integtest.jooqx.pg.jooq;

import java.util.Arrays;

import org.jooq.exception.DataAccessException;
import org.jooq.exception.SQLStateClass;
import org.jooq.impl.DSL;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -77,4 +80,28 @@ void test_transaction_failed_when_batch_insert(VertxTestContext ctx) {
});
}

@Test
void test_session_throw_ex_but_still_inserted_first_when_multiple_inserts_failed_in_second(VertxTestContext ctx) {
final Checkpoint flag = ctx.checkpoint();
final Authors table = schema().AUTHORS;
AuthorsRecord i1 = new AuthorsRecord().setName("n1").setCountry("AT");
AuthorsRecord i2 = new AuthorsRecord().setName("n2");
jooqx.session()
.perform(s -> s.execute(dsl -> dsl.insertInto(table).set(i1).returning(), DSLAdapter.fetchOne(table))
.flatMap(r1 -> s.execute(dsl -> dsl.insertInto(table).set(i2).returning(),
DSLAdapter.fetchOne(table)).map(r2 -> Arrays.asList(r1, r2))))
.onSuccess(result -> ctx.failNow("Should failed"))
.onFailure(t -> ctx.verify(() -> {
Assertions.assertInstanceOf(DataAccessException.class, t);
Assertions.assertTrue(
t.getMessage().contains("null value in column \"country\" violates not-null constraint"));
jooqx.fetchExists(dsl -> dsl.selectFrom(table).where(table.NAME.eq("n1").and(table.COUNTRY.eq("AT"))))
.onSuccess(b -> ctx.verify(() -> {
Assertions.assertTrue(b);
flag.flag();
}))
.onFailure(ctx::failNow);
}));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jooq.InsertSetMoreStep;
import org.jooq.Record;
import org.jooq.SelectConditionStep;
import org.jooq.exception.DataAccessException;
import org.jooq.exception.SQLStateClass;
import org.jooq.impl.DSL;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -161,4 +162,28 @@ void test_transaction_success_when_batch_insert(VertxTestContext context) {
}));
}

@Test
void test_session_throw_ex_but_still_inserted_first_when_multiple_inserts_failed_in_second(VertxTestContext ctx) {
final Checkpoint flag = ctx.checkpoint();
final Authors table = schema().AUTHORS;
AuthorsRecord i1 = new AuthorsRecord().setName("n1").setCountry("AT");
AuthorsRecord i2 = new AuthorsRecord().setName("n2");
jooqx.session()
.perform(s -> s.execute(dsl -> dsl.insertInto(table).set(i1).returning(), DSLAdapter.fetchOne(table))
.flatMap(r1 -> s.execute(dsl -> dsl.insertInto(table).set(i2).returning(),
DSLAdapter.fetchOne(table)).map(r2 -> Arrays.asList(r1, r2))))
.onSuccess(result -> ctx.failNow("Should failed"))
.onFailure(t -> ctx.verify(() -> {
Assertions.assertInstanceOf(DataAccessException.class, t);
Assertions.assertTrue(
t.getMessage().contains("null value in column \"country\" violates not-null " + "constraint"));
jooqx.fetchExists(dsl -> dsl.selectFrom(table).where(table.NAME.eq("n1").and(table.COUNTRY.eq("AT"))))
.onSuccess(b -> ctx.verify(() -> {
Assertions.assertTrue(b);
flag.flag();
}))
.onFailure(ctx::failNow);
}));
}

}

0 comments on commit 25b23ee

Please sign in to comment.