@Test public void testBindOptionalPresent() { Something result = handle.createQuery(SELECT_BY_NAME) .bind("name", Optional.of("brian")) .mapToBean(Something.class) .findOnly(); assertThat(result).isEqualTo(new Something(2, "brian")); }
@Test public void testBindOptionalLong() { assertThat(handle.createQuery("SELECT :value") .bind("value", OptionalLong.empty()) .collectInto(OptionalLong.class)) .isEmpty(); assertThat(handle.createQuery("SELECT :value") .bind("value", OptionalLong.of(123)) .collectInto(OptionalLong.class)) .hasValue(123); }
@Test public void testBindOptionalEmpty() { List<Something> result = handle.createQuery(SELECT_BY_NAME) .bind("name", Optional.absent()) .mapToBean(Something.class) .list(); assertThat(result).containsExactly(new Something(1, "eric"), new Something(2, "brian")); }
@Test public void testSetPositionalString() { h.execute("insert into something (id, name) values (1, 'eric')"); h.execute("insert into something (id, name) values (2, 'brian')"); Something eric = h.createQuery("select * from something where name = ?") .bind(0, "eric") .mapToBean(Something.class) .list() .get(0); assertThat(eric.getId()).isEqualTo(1); }
@Test public void testSetPositionalInteger() { h.execute("insert into something (id, name) values (1, 'eric')"); h.execute("insert into something (id, name) values (2, 'brian')"); Something eric = h.createQuery("select * from something where id = ?") .bind(0, 1) .mapToBean(Something.class) .list().get(0); assertThat(eric.getId()).isEqualTo(1); }
@Test public void testGetTrySuccessShouldReturnCorrectRow() { Something result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME) .bind("name", Try.success("brian")) .mapToBean(Something.class) .findOnly(); assertThat(result).isEqualTo(BRIANSOMETHING); }
@Test public void testGetLazyShouldReturnCorrectRow() { Something result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME) .bind("name", Lazy.of(() -> "brian")) .mapToBean(Something.class) .findOnly(); assertThat(result).isEqualTo(BRIANSOMETHING); }
@Test public void testGetValidationInvalidShouldReturnAllRows() { List<Something> result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME) .bind("name", Validation.invalid("eric")) .mapToBean(Something.class) .list(); assertThat(result).hasSize(2); }
@Test public void testFiveMinuteFluentApi() { try (Handle h = dbRule.openHandle()) { h.execute("insert into something (id, name) values (?, ?)", 1, "Brian"); String name = h.createQuery("select name from something where id = :id") .bind("id", 1) .mapTo(String.class) .findOnly(); assertThat(name).isEqualTo("Brian"); } }
@Test public void testBindOptionalOfCustomType() { handle.registerArgument(new NameArgumentFactory()); List<Something> result = handle.createQuery(SELECT_BY_NAME) .bind("name", Optional.of(new Name("eric"))) .mapToBean(Something.class) .list(); assertThat(result).containsExactly(new Something(1, "eric")); }
@Test public void namesAreMappedCorrectly() { db.getJdbi().useHandle(h -> { Foobar name = h.createQuery("select :name") .bind("name", Foobar.FOO.name()) .mapTo(Foobar.class) .findOnly(); assertThat(name) .isEqualTo(Foobar.FOO); }); }
@Test public void columnMapper() { handle.registerColumnMapper(userNameFactory); handle.registerRowMapper(ConstructorMapper.factory(NamedUser.class)); NamedUser bob = handle.createQuery("SELECT id, name FROM user WHERE name = :name") .bind("name", "Bob") .mapTo(NamedUser.class) .findOnly(); assertThat(bob.name.name).isEqualTo("Bob"); } // end::columnMapper[]
@Test public void testBindOptionalOfUnregisteredCustomType() { exception.expect(UnsupportedOperationException.class); handle.createQuery(SELECT_BY_NAME) .bind("name", Optional.of(new Name("eric"))) .mapToBean(Something.class) .list(); }
@Test public void testHandlesNulls() { handle.execute("insert into intervals(id, foo) values(?, ?)", 5, null); final Period p = handle.createQuery("select foo from intervals where id=?") .bind(0, 5) .mapTo(Period.class) .findOnly(); assertThat(p).isNull(); }
@Test public void testHandlesEmptyMap() { handle.execute("insert into campaigns(id, caps) values (?,?)", 4, ImmutableMap.of()); Map<String, String> newCaps = handle.createQuery("select caps from campaigns where id=?") .bind(0, 4) .mapTo(QualifiedType.of(STRING_MAP).with(HStore.class)) .findOnly(); assertThat(newCaps).isEmpty(); }
@Test public void methodCallCanBeAnnotatedAsByName() { h.getConfig(Enums.class).setEnumStrategy(EnumStrategy.BY_ORDINAL); Object byName = h.createQuery("select :name") .bind("name", Foobar.FOO.name()) .mapTo(QualifiedType.of(Foobar.class).with(EnumByName.class)) .findOnly(); assertThat(byName) .isEqualTo(Foobar.FOO); }
@Test public void methodCallOverridesClassForName() { h.getConfig(Enums.class).setEnumStrategy(EnumStrategy.BY_ORDINAL); Object byName = h.createQuery("select :name") .bind("name", ByOrdinal.NUMERIC.name()) .mapTo(QualifiedType.of(ByOrdinal.class).with(EnumByName.class)) .findOnly(); assertThat(byName) .isEqualTo(ByOrdinal.NUMERIC); }
@Test public void testRegisterInferredOnJdbi() { db.registerRowMapper(new SomethingMapper()); Something sam = db.withHandle(handle1 -> { handle1.execute("insert into something (id, name) values (18, 'Sam')"); return handle1.createQuery("select id, name from something where id = :id") .bind("id", 18) .mapTo(Something.class) .findOnly(); }); assertThat(sam.getName()).isEqualTo("Sam"); }
@Test public void testDoesNotExist() { Handle h = dbRule.getSharedHandle(); h.execute("insert into something (id, name) values (1, 'Coda')"); Optional<String> rs = h.createQuery("select name from something where id = :id") .bind("id", 2) .mapTo(String.class) .collect(GuavaCollectors.toOptional()); assertThat(rs).isAbsent(); }