@Test public void testReadsViaFluentAPI() { List<Map<String, String>> caps = handle.createQuery("select caps from campaigns order by id") .mapTo(STRING_MAP) .list(); assertThat(caps).isEqualTo(ImmutableList.of( ImmutableMap.of("yearly", "10000", "monthly", "5000", "daily", "200"), ImmutableMap.of("yearly", "1000", "monthly", "200", "daily", "20") )); }
@Test public void immutableSortedSetWithComparator() { Comparator<Integer> comparator = Comparator.<Integer>naturalOrder().reversed(); ImmutableSortedSet<Integer> set = dbRule.getSharedHandle().createQuery("select intValue from something") .mapTo(int.class) .collect(ImmutableSortedSet.toImmutableSortedSet(comparator)); assertThat(set).containsExactlyElementsOf(expected.stream() .sorted(comparator) .collect(Collectors.toList())); }
@Test public void testInvalidDuration() { assertThatThrownBy(() -> handle.createQuery("select foo from intervals where id=?") .bind(0, 3) // The bad one. .mapTo(Duration.class) .findOnly()).isInstanceOf(IllegalArgumentException.class); }
@Benchmark public List<Tribble> mapByExactName() { return jdbi.withHandle(h -> h.select("select name from exact_name") .mapTo(QualifiedType.of(Tribble.class).with(EnumByName.class)) .list()); }
@Benchmark public List<Tribble> mapByRandomCaseName() { return jdbi.withHandle(h -> h.select("select name from random_case") .mapTo(QualifiedType.of(Tribble.class).with(EnumByName.class)) .list()); }
@Test public void testReduceBiFunction() { assertThat( dbRule.getSharedHandle().createQuery("SELECT * FROM reduce") .mapTo(Integer.class) .reduce(0, TestResultBearing::add)) .isEqualTo(10); }
@Test public void badNameThrows() { db.getJdbi().useHandle(h -> { assertThatThrownBy(h.createQuery("select 'xxx'").mapTo(Foobar.class)::findOnly) .isInstanceOf(UnableToProduceResultException.class) .hasMessageContaining("no Foobar value could be matched to the name xxx"); }); }
@Test public void testTimeout() { h.getConfig(SqlStatements.class).setQueryTimeout(2); assertThatCode(h.createQuery("select pg_sleep(1)").mapTo(String.class)::findOnly) .doesNotThrowAnyException(); assertThatThrownBy(h.createQuery("select pg_sleep(3)").mapTo(String.class)::findOnly) .isInstanceOf(UnableToExecuteStatementException.class) .hasMessageContaining("canceling statement due to user request"); } }
@Test public void methodCallCanBeAnnotatedAsByOrdinal() { Object byOrdinal = h.createQuery("select :ordinal") .bind("ordinal", Foobar.FOO.ordinal()) .mapTo(QualifiedType.of(Foobar.class).with(EnumByOrdinal.class)) .findOnly(); assertThat(byOrdinal) .isEqualTo(Foobar.FOO); }
@Test public void testReadNegativeDuration() { handle.execute("insert into intervals(id, foo) values(?, interval '-2 days -3 hours')", 7); final Duration d = handle.createQuery("select foo from intervals where id=?") .bind(0, 7) .mapTo(Duration.class) .findOnly(); assertThat(d).isEqualTo(Duration.ofDays(-2).plusHours(-3)); }
@Test public void testHandlesNulls() { handle.execute("insert into campaigns(id, caps) values (?,?)", 4, null); Map<String, String> newCaps = handle.createQuery("select caps from campaigns where id=?") .bind(0, 4) .mapTo(STRING_MAP) .findOnly(); assertThat(newCaps).isNull(); }
@Test public void testTrivialDuration() { handle.execute("insert into intervals(id, foo) values(?, ?)", 4, Duration.ZERO); Duration d = handle.createQuery("select foo from intervals where id=?") .bind(0, 4) .mapTo(Duration.class) .findOnly(); assertThat(d.isZero()); }
@Test public void testDefineStrings() { assertThat( db.getSharedHandle().createQuery("select <a> from values(:a) union all select <b> from values(:b)") .defineNamedBindings() .bindBean(new DefinedBean()) .mapTo(boolean.class) .list()) .isEqualTo(Arrays.asList(true, false)); }
@Test public void testBigishBatch() { PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); int count = 100; for (int i = 0; i < count; ++i) { b.bind("id", i).bind("name", "A Name").add(); } b.execute(); int rowCount = h.createQuery("select count(id) from something").mapTo(int.class).findOnly(); assertThat(rowCount).isEqualTo(count); }
@Test public void testSillyNumberOfCallbacks() throws Exception { try (Handle h = dbRule.openHandle()) { h.execute("insert into something (id, name) values (1, 'Keith')"); } // strangely enough, the compiler can't infer this and thinks the throws is redundant String value = dbRule.getJdbi().<String, Exception>withHandle(handle -> handle.inTransaction(handle1 -> handle1.createQuery("select name from something where id = 1").mapTo(String.class).findOnly())); assertThat(value).isEqualTo("Keith"); }
@Test public void ordinalsAreMappedCorrectly() { db.getJdbi().useHandle(h -> { h.getConfig(Enums.class).setEnumStrategy(EnumStrategy.BY_ORDINAL); Foobar name = h.createQuery("select :ordinal") .bind("ordinal", Foobar.FOO.ordinal()) .mapTo(Foobar.class) .findOnly(); assertThat(name) .isEqualTo(Foobar.FOO); }); }
@Test public void testWriteReadNegativeDuration() { handle.execute("insert into intervals(id, foo) values(?, ?)", 8, Duration.ofDays(-3).plusMinutes(2)); final Duration d = handle.createQuery("select foo from intervals where id=?") .bind(0, 8) .mapTo(Duration.class) .findOnly(); assertThat(d).isEqualTo(Duration.ofDays(-3).plusMinutes(2)); }
@Test public void testNestedHalfPresent() { Handle handle = dbRule.getSharedHandle(); assertThat(handle .registerRowMapper(FieldMapper.factory(NullableNestedThing.class)) .select("SELECT 42 as testValue, '3' as s") .mapTo(NullableNestedThing.class) .findOnly()) .extracting("testValue", "nested.i", "nested.s") .containsExactly(42, null, "3"); }
@Test public void nestedParameters() { assertThat(dbRule.getSharedHandle() .registerRowMapper(ConstructorMapper.factory(NestedBean.class)) .select("select s, i from bean") .mapTo(NestedBean.class) .findOnly()) .extracting("nested.s", "nested.i") .containsExactly("3", 2); }
@Test public void testUri() { Handle h = dbRule.openHandle(); h.createUpdate("insert into something (id, name) values (1, :uri)") .bind("uri", TEST_URI).execute(); assertThat(h.createQuery("SELECT name FROM something") .mapTo(URI.class) .findOnly()).isEqualTo(TEST_URI); } }