Skip to content

Commit

Permalink
Fix unit tests for procedure calls
Browse files Browse the repository at this point in the history
  • Loading branch information
hgschmie committed Dec 28, 2023
1 parent ef95cfd commit 9a1e91b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 90 deletions.
112 changes: 59 additions & 53 deletions core/src/test/java/org/jdbi/v3/core/statement/TestCallable.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,74 +69,80 @@ public void setUp() {

@Test
public void testStatement() {
OutParameters ret = h.createCall("CALL TO_DEGREES(?, ?)")
.registerOutParameter(0, Types.DOUBLE)
.bind(1, 100.0d)
.invoke();

Double expected = Math.toDegrees(100.0d);
assertThat(ret.getDouble(0)).isEqualTo(expected, Offset.offset(0.001));
assertThat(ret.getLong(0).longValue()).isEqualTo(expected.longValue());
assertThat(ret.getShort(0).shortValue()).isEqualTo(expected.shortValue());
assertThat(ret.getInt(0).intValue()).isEqualTo(expected.intValue());
assertThat(ret.getFloat(0).floatValue()).isEqualTo(expected.floatValue(), Offset.offset(0.001f));

assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> ret.getDate(0));
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> ret.getDate(1));
try (Call call = h.createCall("CALL TO_DEGREES(?, ?)")) {
OutParameters ret = call.registerOutParameter(0, Types.DOUBLE)
.bind(1, 100.0d)
.invoke();

Double expected = Math.toDegrees(100.0d);
assertThat(ret.getDouble(0)).isEqualTo(expected, Offset.offset(0.001));
assertThat(ret.getLong(0).longValue()).isEqualTo(expected.longValue());
assertThat(ret.getShort(0).shortValue()).isEqualTo(expected.shortValue());
assertThat(ret.getInt(0).intValue()).isEqualTo(expected.intValue());
assertThat(ret.getFloat(0).floatValue()).isEqualTo(expected.floatValue(), Offset.offset(0.001f));

assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> ret.getDate(0));
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> ret.getDate(1));
}
}

@Test
public void testStatementWithNamedParam() {
OutParameters ret = h.createCall("CALL TO_DEGREES(:x, :y)")
.registerOutParameter("x", Types.DOUBLE)
.bind("y", 100.0d)
.invoke();

Double expected = Math.toDegrees(100.0d);
assertThat(ret.getDouble("x")).isEqualTo(expected, Offset.offset(0.001));
assertThat(ret.getLong("x").longValue()).isEqualTo(expected.longValue());
assertThat(ret.getShort("x").shortValue()).isEqualTo(expected.shortValue());
assertThat(ret.getInt("x").intValue()).isEqualTo(expected.intValue());
assertThat(ret.getFloat("x")).isEqualTo(expected.floatValue());

assertThatExceptionOfType(Exception.class).isThrownBy(() -> ret.getDate("x"));
assertThatExceptionOfType(Exception.class).isThrownBy(() -> ret.getDate("y"));
try (Call call = h.createCall("CALL TO_DEGREES(:x, :y)")) {
OutParameters ret = call
.registerOutParameter("x", Types.DOUBLE)
.bind("y", 100.0d)
.invoke();

Double expected = Math.toDegrees(100.0d);
assertThat(ret.getDouble("x")).isEqualTo(expected, Offset.offset(0.001));
assertThat(ret.getLong("x").longValue()).isEqualTo(expected.longValue());
assertThat(ret.getShort("x").shortValue()).isEqualTo(expected.shortValue());
assertThat(ret.getInt("x").intValue()).isEqualTo(expected.intValue());
assertThat(ret.getFloat("x")).isEqualTo(expected.floatValue());

assertThatExceptionOfType(Exception.class).isThrownBy(() -> ret.getDate("x"));
assertThatExceptionOfType(Exception.class).isThrownBy(() -> ret.getDate("y"));
}
}

@Test
public void testWithNullReturn() {
OutParameters ret = h.createCall("CALL DO_LENGTH(?, ?)")
.bind(0, (String) null)
.registerOutParameter(1, Types.INTEGER)
.invoke();

Integer out = ret.getInt(1);
assertThat(out).isNull();
try (Call call = h.createCall("CALL DO_LENGTH(?, ?)")) {
OutParameters ret = call.bind(0, (String) null)
.registerOutParameter(1, Types.INTEGER)
.invoke();

Integer out = ret.getInt(1);
assertThat(out).isNull();
}
}

@Test
public void testWithNullReturnWithNamedParam() {
OutParameters ret = h.createCall("CALL DO_LENGTH(:in, :out)")
.bindNull("in", Types.VARCHAR)
.registerOutParameter("out", Types.INTEGER)
.invoke();

Integer out = ret.getInt(1);
assertThat(out).isNull();
try (Call call = h.createCall("CALL DO_LENGTH(:in, :out)")) {
OutParameters ret = call.bindNull("in", Types.VARCHAR)
.registerOutParameter("out", Types.INTEGER)
.invoke();

Integer out = ret.getInt(1);
assertThat(out).isNull();
}
}

@Test
public void testProcedureWithoutOutParameter() {
h.createCall("CALL WITH_SIDE_EFFECT(:id, :name)")
.bind("id", 10)
.bind("name", "John")
.invoke();

String name = h.createQuery("SELECT name FROM something WHERE id = :id")
.bind("id", 10)
.mapTo(String.class)
.one();

assertThat(name).isEqualTo("John Doe");
try (Call call = h.createCall("CALL WITH_SIDE_EFFECT(:id, :name)")) {
call.bind("id", 10)
.bind("name", "John")
.invoke();

String name = h.createQuery("SELECT name FROM something WHERE id = :id")
.bind("id", 10)
.mapTo(String.class)
.one();

assertThat(name).isEqualTo("John Doe");
}
}
}
27 changes: 14 additions & 13 deletions core/src/test/java/org/jdbi/v3/core/statement/TestStatements.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,26 +164,27 @@ public void testUnusedBindingWithOutParameter() {

h.execute("CREATE ALIAS TO_DEGREES FOR \"java.lang.Math.toDegrees\"");

Call call = h.createCall("? = CALL TO_DEGREES(?)")
.registerOutParameter(0, Types.DOUBLE)
.bind(1, 100.0d)
.bind(2, "foo");
try (Call call = h.createCall("? = CALL TO_DEGREES(?)")) {
call.registerOutParameter(0, Types.DOUBLE)
.bind(1, 100.0d)
.bind(2, "foo");

assertThatThrownBy(call::invoke).isInstanceOf(UnableToCreateStatementException.class);
assertThatThrownBy(call::invoke).isInstanceOf(UnableToCreateStatementException.class);
}
}

@Test
public void testPermittedUnusedBindingWithOutParameter() {
Handle h = h2Extension.getSharedHandle();

h.execute("CREATE ALIAS TO_DEGREES FOR \"java.lang.Math.toDegrees\"");

Call call = h.configure(SqlStatements.class, stmts -> stmts.setUnusedBindingAllowed(true))
.createCall("? = CALL TO_DEGREES(?)")
.registerOutParameter(0, Types.DOUBLE)
.bind(1, 100.0d)
.bind(2, "foo");

assertThatCode(call::invoke).doesNotThrowAnyException();
h.configure(SqlStatements.class, stmts -> stmts.setUnusedBindingAllowed(true));
try (Call call = h.createCall("? = CALL TO_DEGREES(?)")) {
call.registerOutParameter(0, Types.DOUBLE)
.bind(1, 100.0d)
.bind(2, "foo");

assertThatCode(call::invoke).doesNotThrowAnyException();
}
}
}
17 changes: 9 additions & 8 deletions docs/src/test/java/jdbi/doc/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import de.softwareforge.testing.postgres.junit5.EmbeddedPgExtension;
import de.softwareforge.testing.postgres.junit5.MultiDatabaseBuilder;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.statement.Call;
import org.jdbi.v3.core.statement.OutParameters;
import org.jdbi.v3.testing.junit5.JdbiExtension;
import org.junit.jupiter.api.Test;
Expand All @@ -41,18 +42,18 @@ public void testCall() {
handle.execute(findSqlOnClasspath("create_stored_proc_add"));

// tag::invokeProcedure[]
OutParameters result = handle
.createCall("{:sum = call add(:a, :b)}") // <1>
.bind("a", 13) // <2>
try (Call call = handle.createCall("{:sum = call add(:a, :b)}")) { // <1>
OutParameters result = call.bind("a", 13) // <2>
.bind("b", 9) // <2>
.registerOutParameter("sum", Types.INTEGER) // <3> <4>
.invoke(); // <5>
// end::invokeProcedure[]
// end::invokeProcedure[]

// tag::getOutParameters[]
int sum = result.getInt("sum");
// end::getOutParameters[]
// tag::getOutParameters[]
int sum = result.getInt("sum");
// end::getOutParameters[]

assertThat(sum).isEqualTo(22);
assertThat(sum).isEqualTo(22);
}
}
}
37 changes: 21 additions & 16 deletions postgres/src/test/java/org/jdbi/v3/postgres/TestPostgresTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.generic.GenericType;
import org.jdbi.v3.core.statement.Call;
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.statement.SqlCall;
Expand Down Expand Up @@ -155,39 +156,42 @@ public void testReadListViaFluentAPI() {
public void testWriteViaFluentAPI() {
FooBarPGType fooBar3 = new FooBarPGType(3, "foo3", "bar3");

handle.createCall("SELECT insert_foo_bar(:fooBar)")
.bind("fooBar", fooBar3)
try (Call call = handle.createCall("SELECT insert_foo_bar(:fooBar)")) {

call.bind("fooBar", fooBar3)
.invoke();

FooBarPGType result = handle.createQuery("SELECT get_foo_bar(:id)")
FooBarPGType result = handle.createQuery("SELECT get_foo_bar(:id)")
.bind("id", fooBar3.getId())
.mapTo(FooBarPGType.class)
.one();

assertThat(fooBar3).isEqualTo(result);
assertThat(fooBar3).isEqualTo(result);
}
}

@Test
public void testWriteArrayViaFluentAPI() {
FooBarPGType fooBar5 = new FooBarPGType(5, "foo5", "bar5");
FooBarPGType fooBar6 = new FooBarPGType(6, "foo6", "bar6");

handle.createCall("SELECT insert_foo_bars(:fooBar)")
.bind("fooBar", new FooBarPGType[]{fooBar5, fooBar6})
try (Call call = handle.createCall("SELECT insert_foo_bars(:fooBar)")) {
call.bind("fooBar", new FooBarPGType[] {fooBar5, fooBar6})
.invoke();

FooBarPGType result5 = handle.createQuery("SELECT get_foo_bar(:id)")
FooBarPGType result5 = handle.createQuery("SELECT get_foo_bar(:id)")
.bind("id", fooBar5.getId())
.mapTo(FooBarPGType.class)
.one();

FooBarPGType result6 = handle.createQuery("SELECT get_foo_bar(:id)")
FooBarPGType result6 = handle.createQuery("SELECT get_foo_bar(:id)")
.bind("id", fooBar6.getId())
.mapTo(FooBarPGType.class)
.one();

assertThat(fooBar5).isEqualTo(result5);
assertThat(fooBar6).isEqualTo(result6);
assertThat(fooBar5).isEqualTo(result5);
assertThat(fooBar6).isEqualTo(result6);
}
}

@Test
Expand Down Expand Up @@ -243,17 +247,18 @@ public void testBindListAsArrayViaFluentAPI() {
foos.add(new FooBarPGType(9, "foo9", "bar9"));
foos.add(new FooBarPGType(10, "foo10", "bar10"));

handle.createCall("SELECT insert_foo_bars(:fooBar)")
.bindByType("fooBar", foos, new GenericType<List<FooBarPGType>>() {})
try (Call call = handle.createCall("SELECT insert_foo_bars(:fooBar)")) {
call.bindByType("fooBar", foos, new GenericType<List<FooBarPGType>>() {})
.invoke();

assertThat(handle.createQuery("SELECT get_foo_bars()")
assertThat(handle.createQuery("SELECT get_foo_bars()")
.mapTo(FooBarPGType.class)
.list())
.containsExactlyInAnyOrder(new FooBarPGType(1, "foo1", "bar1"),
new FooBarPGType(2, "foo2", "bar2"),
new FooBarPGType(9, "foo9", "bar9"),
new FooBarPGType(10, "foo10", "bar10"));
new FooBarPGType(2, "foo2", "bar2"),
new FooBarPGType(9, "foo9", "bar9"),
new FooBarPGType(10, "foo10", "bar10"));
}
}

@Test
Expand Down

0 comments on commit 9a1e91b

Please sign in to comment.