@Test public void testTuple1CollectorWithMultiSelectShouldFail() { // first selection is not projectable to tuple param assertThatThrownBy(() -> dbRule.getSharedHandle() .createQuery("select t2, t3 from tuples") .collectInto(new GenericType<List<Tuple1<Integer>>>() {})).isInstanceOf(ResultSetException.class); }
@Test public void optionalMultiple() { assertThatThrownBy(() -> dbRule.getSharedHandle().createQuery("select intValue from something") .collectInto(new GenericType<Optional<Integer>>() {})).isInstanceOf(IllegalStateException.class); }
@Test public void testMapCollectorReversedShouldFail() { assertThatThrownBy(() -> dbRule.getSharedHandle() .createQuery("select intValue, name from something") .collectInto(new GenericType<HashMap<String, Integer>>() {})) .isInstanceOf(ResultSetException.class); }
@Test public void testBindOptionalInt() { assertThat(handle.createQuery("SELECT :value") .bind("value", OptionalInt.empty()) .collectInto(OptionalInt.class)) .isEmpty(); assertThat(handle.createQuery("SELECT :value") .bind("value", OptionalInt.of(123)) .collectInto(OptionalInt.class)) .hasValue(123); }
private <T extends Traversable<Tuple2<Integer, String>>> void testMapType(GenericType<T> containerType) { T values = dbRule.getSharedHandle().createQuery("select intValue, name from something") .collectInto(containerType); assertThat(values).containsOnlyElementsOf(expectedMap); }
private <T extends Iterable<Integer>> void testType(GenericType<T> containerType) { T values = dbRule.getSharedHandle().createQuery("select intValue from something") .collectInto(containerType); assertThat(values).containsOnlyElementsOf(expected); }
@Test public void testOptionMappedWithoutNestedMapperShouldFail() { assertThatThrownBy(() -> dbRule.getSharedHandle() .createQuery("select id, name from something") .collectInto(new GenericType<Set<Option<SomethingWithOption>>>() {})) .isInstanceOf(NoSuchMapperException.class) .hasMessageContaining("nested"); }
@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 immutableSortedSet() { ImmutableSortedSet<Integer> set = dbRule.getSharedHandle().createQuery("select intValue from something") .collectInto(new GenericType<ImmutableSortedSet<Integer>>(){}); assertThat(set).containsExactlyElementsOf(expected); }
@Test public void immutableList() { ImmutableList<Integer> list = dbRule.getSharedHandle().createQuery("select intValue from something") .collectInto(new GenericType<ImmutableList<Integer>>(){}); assertThat(list).containsOnlyElementsOf(expected); }
@Test public void immutableSet() { ImmutableSet<Integer> set = dbRule.getSharedHandle().createQuery("select intValue from something") .collectInto(new GenericType<ImmutableSet<Integer>>(){}); assertThat(set).containsOnlyElementsOf(expected); }
@Test public void testBindOptionalDouble() { assertThat(handle.createQuery("SELECT :value") .bind("value", OptionalDouble.empty()) .collectInto(OptionalDouble.class)) .isEmpty(); assertThat(handle.createQuery("SELECT :value") .bind("value", OptionalDouble.of(123.45)) .collectInto(OptionalDouble.class)) .hasValue(123.45); }
@Test public void testOptionMappedWithoutGenericParameterShouldFail() { assertThatThrownBy(() -> dbRule.getSharedHandle() .registerRowMapper(ConstructorMapper.factory(SomethingWithOption.class)) .createQuery("select name from something") .collectInto(new GenericType<Set<Option>>() {})) .isInstanceOf(NoSuchMapperException.class) .hasMessageContaining("raw"); }
@Test public void testTuple1CollectorWithMultiSelectShouldSucceed() { List<Tuple1<Integer>> firstColumnTuples = expected.map(Tuple1::new); List<Tuple1<Integer>> tupleProjection = dbRule.getSharedHandle() .createQuery("select * from tuples") .collectInto(new GenericType<List<Tuple1<Integer>>>() {}); assertThat(tupleProjection).containsOnlyElementsOf(firstColumnTuples); }
@Test public void testOptionMappedShouldSucceed() { final Set<Option<String>> result = dbRule.getSharedHandle() .createQuery("select name from something") .collectInto(new GenericType<Set<Option<String>>>() {}); assertThat(result).hasSize(2); assertThat(result).contains(Option.none(), Option.of("eric")); }
@Test public void testTuple3CollectorWithSelectedKeyValueShouldSucceed() { List<Tuple3<Integer, String, String>> expectedTuples = expected.map(i -> new Tuple3<>(i, "t2" + i, "t3" + (i + 1))); List<Tuple3<Integer, String, String>> tupleProjection = dbRule.getSharedHandle() .createQuery("select t1, t2, t3 from tuples") .collectInto(new GenericType<List<Tuple3<Integer, String, String>>>() {}); assertThat(tupleProjection).containsOnlyElementsOf(expectedTuples); }
@Test public void testMapCollectorWithCorrespondingTupleColsShouldSucceed() { HashMap<String, String> valueMap = dbRule.getSharedHandle() .configure(TupleMappers.class, c -> c.setColumn(1, "key_c").setColumn(2, "val_c")) .createQuery("select val_c, key_c from keyval") .collectInto(new GenericType<HashMap<String, String>>() {}); assertThat(valueMap).containsOnlyElementsOf(expectedMap); }
@Test public void optionalPresent() { Optional<Integer> shouldBePresent = dbRule.getSharedHandle().createQuery("select intValue from something where intValue = 1") .collectInto(new GenericType<Optional<Integer>>(){}); assertThat(shouldBePresent).contains(1); }
@Test public void testMapCollectorWithTupleConfigShouldSucceed() { HashMap<String, String> valueMap = dbRule.getSharedHandle() .configure(TupleMappers.class, c -> c.setKeyColumn("key_c").setValueColumn("val_c")) .createQuery("select val_c, key_c from keyval") .collectInto(new GenericType<HashMap<String, String>>() {}); assertThat(valueMap).containsOnlyElementsOf(expectedMap); }
@Test public void optionalAbsent() { Optional<Integer> shouldBeAbsent = dbRule.getSharedHandle().createQuery("select intValue from something where intValue = 100") .collectInto(new GenericType<Optional<Integer>>(){}); assertThat(shouldBeAbsent).isAbsent(); }