@Test public void testUnusedBinding() { assertThatThrownBy(() -> h.createQuery("select * from something") .bind("id", 1) .collectRows(Collectors.counting()) ).isInstanceOf(UnableToCreateStatementException.class); }
@Test // TODO it would be nice if this failed in the future public void testUsedAndUnusedNamed() { assertThatCode(() -> h.createQuery("select * from something where id = :id") .bind("id", 1) .bind("name", "jack") .collectRows(Collectors.counting()) ).doesNotThrowAnyException(); }
@Test // TODO it would be nice if this failed in the future public void testFarAwayPositional() { assertThatCode(() -> h.createQuery("select * from something where id = ?") .bind(0, 1) .bind(2, "jack") .collectRows(Collectors.counting()) ).doesNotThrowAnyException(); }
@Test public void testPermittedUnusedBinding() { assertThatCode(() -> h.configure(SqlStatements.class, s -> s.setUnusedBindingAllowed(true)) .createQuery("select * from something") .bind("id", 1) .collectRows(Collectors.counting())).doesNotThrowAnyException(); }
@Test public void testPermittedUsedAndUnusedBinding() { assertThatCode(() -> h.configure(SqlStatements.class, s -> s.setUnusedBindingAllowed(true)) .createQuery("select * from something where id = :id") .bind("id", 1) .bind("name", "jack") .collectRows(Collectors.counting())).doesNotThrowAnyException(); }
@Test public void testCollectRows() { Iterable<SomethingWithLocations> result = dbRule.getSharedHandle() .createQuery("SELECT something.id, name, location FROM something NATURAL JOIN something_location") .collectRows(Collector.<RowView, Map<Integer, SomethingWithLocations>, Iterable<SomethingWithLocations>>of( LinkedHashMap::new, (Map<Integer, SomethingWithLocations> map, RowView rv) -> map.computeIfAbsent(rv.getColumn("id", Integer.class), id -> new SomethingWithLocations(rv.getRow(Something.class))) .locations .add(rv.getColumn("location", String.class)), (a, b) -> { throw new UnsupportedOperationException("shouldn't use combiner"); }, Map::values)); assertThat(result).containsExactly( new SomethingWithLocations(new Something(1, "tree")).at("outside"), new SomethingWithLocations(new Something(2, "apple")).at("tree").at("pie")); }