@Test public void mapToMapFailsOnUnmappableGenericType() { jdbi.useHandle(h -> { Query query = h.createQuery(QUERY); GenericType<Alien> type = new GenericType<Alien>() {}; assertThatThrownBy(() -> query.mapToMap(type)) .hasMessage("no column mapper found for type " + type); }); }
@Test public void testConvenienceMethod() { h.execute("insert into something (id, name) values (1, 'eric')"); h.execute("insert into something (id, name) values (2, 'brian')"); List<Map<String, Object>> r = h.select("select * from something order by id").mapToMap().list(); assertThat(r).hasSize(2); assertThat(r.get(0).get("name")).isEqualTo("eric"); }
@Test public void testConvenienceMethodWithParam() { h.execute("insert into something (id, name) values (1, 'eric')"); h.execute("insert into something (id, name) values (2, 'brian')"); List<Map<String, Object>> r = h.select("select * from something where id = ?", 1).mapToMap().list(); assertThat(r).hasSize(1); assertThat(r.get(0).get("name")).isEqualTo("eric"); }
@Test public void testHelpfulErrorOnNothingSet() { assertThatThrownBy(() -> h.createQuery("select * from something where name = :name").mapToMap().list()) .isInstanceOf(UnableToCreateStatementException.class); }
@Test public void canFluentMapToMapWithGenericTypeForValue() { jdbi.useHandle(h -> { Map<String, BigDecimal> map = h.createQuery(QUERY) .mapToMap(new GenericType<BigDecimal>() {}) .findOnly(); assertThat(map) .containsOnlyKeys("one", "two", "three") .containsValues(new BigDecimal("1.0"), new BigDecimal("2.0"), new BigDecimal("3.0")); }); }
@Test public void testEmptyExplosion() { ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandleRollback() .mapToMap() .iterator(); assertThatThrownBy(it::next).isInstanceOf(NoSuchElementException.class); }
@Test public void testInsertWithPositionalParameters() { somethingDao.insertSomething(4, "Dave", 90); List<Map<String, Object>> rows = handle.select("select * from something where something_id=?", 4).mapToMap().list(); assertThat(rows).containsExactlyElementsOf(ImmutableList.of( ImmutableMap.of("something_id", 4, "name", "Dave", "code", 90))); }
@Test public void testFuzzyScript() { Handle h = dbRule.openHandle(); Script script = h.createScript(getResourceOnClasspath("script/fuzzy-script.sql")); script.executeAsSeparateStatements(); List<Map<String, Object>> rows = h.select("select id, name from something order by id").mapToMap().list(); assertThat(rows).isEqualTo(ImmutableList.of( ImmutableMap.of("id", 1L, "name", "eric"), ImmutableMap.of("id", 2L, "name", "sally;ann"), ImmutableMap.of("id", 3L, "name", "bob"), ImmutableMap.of("id", 12L, "name", "sally;ann;junior"))); }
@Test public void testNotClosing() { h.createUpdate("insert into something (id, name) values (1, 'eric')").execute(); h.createUpdate("insert into something (id, name) values (2, 'brian')").execute(); List<Map<String, Object>> results = h.createQuery("select * from something order by id").mapToMap().list(); assertThat(results).hasSize(2); assertThat(results.get(0)).containsEntry("name", "eric"); assertThat(h.isClosed()).isFalse(); }
@Test public void testMapMapperOrdering() { h.execute("insert into something (id, name) values (?, ?)", 1, "hello"); h.execute("insert into something (id, name) values (?, ?)", 2, "world"); List<Map<String, Object>> rs = h.createQuery("select id, name from something") .mapToMap() .list(); assertThat(rs).hasSize(2); assertThat(rs).hasOnlyElementsOfType(LinkedHashMap.class); } }
@Test public void testExecuteSomeStatements() { try (Handle h = dbRule.openHandle()) { h.execute("insert into something (id, name) values (?, ?)", 3, "Patrick"); List<Map<String, Object>> rs = h.select("select id, name from something").mapToMap().list(); assertThat(rs).containsExactlyElementsOf(ImmutableList.of(ImmutableMap.of("id", 3L, "name", "Patrick"))); } }
@Test public void testEmptyWorksToo() { ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandleRollback() .mapToMap() .iterator(); assertThat(it.hasNext()).isFalse(); }
@Test public void testClosing() { h.createUpdate("insert into something (id, name) values (1, 'eric')").execute(); h.createUpdate("insert into something (id, name) values (2, 'brian')").execute(); List<Map<String, Object>> results = h.createQuery("select * from something order by id") .cleanupHandleRollback() .mapToMap() .list(); assertThat(results).hasSize(2); assertThat(results.get(0)).containsEntry("name", "eric"); assertThat(h.isClosed()).isTrue(); }
@Test public void testQueriesWithNullResultSets() { expectedException.expect(NoResultsException.class); h.select("insert into something (id, name) values (?, ?)", 1, "hello").mapToMap().list(); }
@Test public void testIterateKeepHandle() { h.createUpdate("insert into something (id, name) values (1, 'eric')").execute(); h.createUpdate("insert into something (id, name) values (2, 'brian')").execute(); ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .mapToMap() .iterator(); assertThat(it).hasSize(2); assertThat(h.isClosed()).isFalse(); }
@Test public void testCaseUpper() { h.getConfig(MapMappers.class).setCaseChange(CaseStrategy.LOCALE_UPPER); Map<String, Object> noOne = h.createQuery("select * from Foo").mapToMap().findOnly(); assertThat(noOne).containsOnlyKeys("ID", "FIRSTNAME"); } }
@Test public void testStatementWithOptionalMapResults() { h.getConfig(ResultProducers.class).allowNoResults(true); assertThat(h.createQuery("commit").mapToMap().findFirst()).isEmpty(); }
@Test public void testCaseLower() { h.getConfig(MapMappers.class).setCaseChange(CaseStrategy.LOCALE_LOWER); Map<String, Object> noOne = h.createQuery("select * from Foo").mapToMap().findOnly(); assertThat(noOne).containsOnlyKeys("id", "firstname"); }
@Test public void testScriptWithStringSemicolon() { Handle h = dbRule.openHandle(); Script script = h.createScript(getResourceOnClasspath("script/insert-with-string-semicolons.sql")); script.execute(); assertThat(h.select("select * from something").mapToMap()).hasSize(3); }
@Test public void testHandleReentrant() { final TheBasics dao = db.onDemand(TheBasics.class); dao.withHandle(handle1 -> { dao.insert(new Something(7, "Martin")); handle1.createQuery("SELECT 1").mapToMap().list(); return null; }); }