public Handle openHandle() { return getJdbi().open(); }
@Before public void getHandle() { db = dbRule.getJdbi(); }
@Before public void getHandle() { h = dbRule.getJdbi().open(); }
@Test public void testTransactions() { Dao dao = dbRule.getJdbi().onDemand(Dao.class); Something s = dao.insertAndFetch(1, "Brian"); assertThat(s).isEqualTo(new Something(1, "Brian")); }
@Test public void testRegisterRowMapperUpdate() { dbRule.getJdbi().useExtension(RegisterRowMapperDao.class, dao -> { dao.createTable(); IdCreateTime result = dao.insert("foo"); assertThat(result.id).isEqualTo(1); assertThat(result.createdOn).isNotNull(); }); }
@Test public void testUseRowMapperUpdate() { dbRule.getJdbi().useExtension(UseRowMapperDao.class, dao -> { dao.createTable(); IdCreateTime result = dao.insert("foo"); assertThat(result.id).isEqualTo(1); assertThat(result.createdOn).isNotNull(); }); }
@Test public void testBatch() { dbRule.getJdbi().useExtension(DAO.class, dao -> { dao.createSequence(); dao.createTable(); int[] ids = dao.insert(Arrays.asList("Burt", "Macklin")); assertThat(dao.findNameById(ids[0])).isEqualTo("Burt"); assertThat(dao.findNameById(ids[1])).isEqualTo("Macklin"); }); }
@Before public void createTable() { dbRule.getJdbi().useHandle(h -> h.execute("create table something (id int primary key, name varchar(50), integerValue integer, intValue integer)")); }
@Test public void testConnected() { int four = dbRule.getJdbi().withHandle(handle -> handle.createQuery("select 2 + 2").mapTo(Integer.class).findOnly()); assertThat(four).isEqualTo(4); }
@Test public void testFoo() { dbRule.getJdbi().useExtension(DAO.class, dao -> { dao.createSequence(); dao.createTable(); long brianId = dao.insert("Brian"); long keithId = dao.insert("Keith"); assertThat(dao.findNameById(brianId)).isEqualTo("Brian"); assertThat(dao.findNameById(keithId)).isEqualTo("Keith"); }); }
@Test public void testExplicitTransaction() { Dao dao = dbRule.getJdbi().onDemand(Dao.class); Something s = dao.inTransaction(transactional -> { transactional.insert(1, "Brian"); return transactional.findById(1); }); assertThat(s).isEqualTo(new Something(1, "Brian")); }
@Before public void setUp() { db = dbRule.getJdbi(); db.useHandle(h -> { h.execute("CREATE FUNCTION set100(OUT outparam INT) AS $$ BEGIN outparam := 100; END; $$ LANGUAGE plpgsql"); h.execute("CREATE FUNCTION swap(IN a INT, IN b INT, OUT c INT, OUT d INT) AS $$ BEGIN c := b; d := a; END; $$ LANGUAGE plpgsql"); }); }
@Before public void setUp() { dbRule.getJdbi().useHandle(handle -> { handle.execute("create table if not exists something (id int primary key, name varchar(100))"); handle.execute("delete from something"); }); }
@Test public void testUseRowMapperBatch() { dbRule.getJdbi().useExtension(UseRowMapperDao.class, dao -> { dao.createTable(); List<IdCreateTime> results = dao.insertBatch("foo", "bar"); assertThat(results).extracting(ic -> ic.id).containsExactly(1L, 2L); assertThat(results).extracting(ic -> ic.createdOn).hasSize(2).doesNotContainNull(); }); }
@Test public void testRegisterRowMapperBatch() { dbRule.getJdbi().useExtension(RegisterRowMapperDao.class, dao -> { dao.createTable(); List<IdCreateTime> results = dao.insertBatch("foo", "bar"); assertThat(results).extracting(ic -> ic.id).containsExactly(1L, 2L); assertThat(results).extracting(ic -> ic.createdOn).hasSize(2).doesNotContainNull(); }); }
@Before public void setUp() { dbRule.getJdbi() .registerArrayType(Integer.class, "integer") .registerArrayType(UUID.class, "uuid"); h = dbRule.openHandle(); h.useTransaction(th -> { th.execute("DROP TABLE IF EXISTS arrays"); th.execute("CREATE TABLE arrays (u UUID[], i INT[])"); }); }
@Test public void testLocateConfigDriven() throws Exception { Jdbi jdbi = dbRule.getJdbi(); jdbi.useHandle(h -> { h.execute("create table something (id int, name text)"); h.execute("insert into something (id, name) values (?, ?)", 2, "Alice"); h.execute("insert into something (id, name) values (?, ?)", 1, "Bob"); }); jdbi.getConfig(SqlObjects.class).setSqlLocator( (type, method, config) -> config.get(TestConfig.class).sql); jdbi.getConfig(TestConfig.class).sql = "select * from something order by id"; assertThat(jdbi.withExtension(TestDao.class, TestDao::list)) .containsExactly(new Something(1, "Bob"), new Something(2, "Alice")); jdbi.getConfig(TestConfig.class).sql = "select * from something order by name"; assertThat(jdbi.withExtension(TestDao.class, TestDao::list)) .containsExactly(new Something(2, "Alice"), new Something(1, "Bob")); }
@Test public void testTransactions() { Dao dao = dbRule.getJdbi().onDemand(Dao.class); Something s = dao.insertAndFetch(1, "Brian"); assertThat(s).isEqualTo(new Something(1, "Brian")); }
@Test public void testRegisterRowMapperUpdate() { dbRule.getJdbi().useExtension(RegisterRowMapperDao.class, dao -> { dao.createTable(); IdCreateTime result = dao.insert("foo"); assertThat(result.id).isEqualTo(1); assertThat(result.createdOn).isNotNull(); }); }
@Before public void setUp() { dbRule.getJdbi().useHandle(handle -> { handle.execute("create table if not exists something (id int primary key, name varchar(100))"); handle.execute("delete from something"); }); }