@Test public void testFirstWithNoResult() { Optional<Something> s = h.createQuery("select id, name from something").mapToBean(Something.class).findFirst(); assertThat(s.isPresent()).isFalse(); }
@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 testSimpleUpdate() { h.execute("insert into something (id, name) values (1, 'eric')"); h.execute("update something set name = 'cire' where id = 1"); Something eric = h.createQuery("select * from something where id = 1").mapToBean(Something.class).list().get(0); assertThat(eric.getName()).isEqualTo("cire"); }
@Test public void testMappedQueryObjectWithNullForPrimitiveIntField() { h.execute("insert into something (id, name, intValue) values (1, 'eric', null)"); ResultIterable<Something> query = h.createQuery("select * from something order by id").mapToBean(Something.class); List<Something> r = query.list(); Something eric = r.get(0); assertThat(eric).isEqualTo(new Something(1, "eric")); assertThat(eric.getIntValue()).isZero(); }
@Test public void testDynamicBindOptionalEmpty() { List<Something> result = handle.createQuery(SELECT_BY_NAME) .bindByType("name", Optional.empty(), new GenericType<Optional<String>>() {}) .mapToBean(Something.class) .list(); assertThat(result).containsExactly(new Something(1, "eric"), new Something(2, "brian")); }
@Test public void testFirstResult() { h.execute("insert into something (id, name) values (1, 'eric')"); h.execute("insert into something (id, name) values (2, 'brian')"); Something r = h.createQuery("select * from something order by id") .mapToBean(Something.class) .findFirst() .get(); assertThat(r.getName()).isEqualTo("eric"); }
@Test public void testUpdate() { h.execute("insert into something (id, name) values (1, 'eric')"); h.createUpdate("update something set name = 'ERIC' where id = 1").execute(); Something eric = h.createQuery("select * from something where id = 1").mapToBean(Something.class).list().get(0); assertThat(eric.getName()).isEqualTo("ERIC"); }
@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 testGetOptionShouldReturnCorrectRow() { Something result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME) .bind("name", Option.of("eric")) .mapToBean(Something.class) .findOnly(); assertThat(result).isEqualTo(ERICSOMETHING); }
@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 testGetOptionEmptyShouldReturnAllRows() { List<Something> result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME) .bind("name", Option.none()) .mapToBean(Something.class) .list(); assertThat(result).hasSize(2); }
@Test public void testGetEitherLeftShouldReturnAllRows() { List<Something> result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME) .bind("name", Either.left("eric")) .mapToBean(Something.class) .list(); assertThat(result).hasSize(2); }
@Test public void testStatementCustomizersPersistAfterMap() { h.execute("insert into something (id, name) values (?, ?)", 1, "hello"); h.execute("insert into something (id, name) values (?, ?)", 2, "world"); List<Something> rs = h.createQuery("select id, name from something") .setMaxRows(1) .mapToBean(Something.class) .list(); assertThat(rs).hasSize(1); }
@Test public void testMapInvalidEnumValue() { Handle h = dbRule.openHandle(); h.createUpdate("insert into something (id, name) values (1, 'joe')").execute(); assertThatThrownBy(() -> h.createQuery("select * from something order by id") .mapToBean(SomethingElse.class) .findFirst()).isInstanceOf(UnableToProduceResultException.class); }
@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 testBatchInsertWithKeyGenerationAndExplicitColumnNames() { PreparedBatch batch = h.prepareBatch("insert into something (name) values (?) "); batch.add("Brian"); batch.add("Thom"); List<Integer> ids = batch.executeAndReturnGeneratedKeys("id").mapTo(Integer.class).list(); assertThat(ids).containsExactly(1, 2); List<Something> somethings = h.createQuery("select id, name from something") .mapToBean(Something.class) .list(); assertThat(somethings).containsExactly(new Something(1, "Brian"), new Something(2, "Thom")); }
@Test public void testDynamicBindOptionalOfCustomType() { handle.registerArgument(new NameArgumentFactory()); handle.createQuery(SELECT_BY_NAME) .bindByType("name", Optional.of(new Name("eric")), new GenericType<Optional<Name>>() {}) .mapToBean(Something.class) .list(); }
@Test public void testBasics() { Handle h = dbRule.openHandle(); Batch b = h.createBatch(); b.add("insert into something (id, name) values (0, 'Keith')"); b.add("insert into something (id, name) values (1, 'Eric')"); b.add("insert into something (id, name) values (2, 'Brian')"); b.execute(); List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list(); assertThat(r).hasSize(3); }