@Test public void test() { if (isSqlServer()) return; List<OCachedBean> list = new ArrayList(); for (int i = 0; i < 3; i++) { OCachedBean bean = new OCachedBean(); bean.setName("name " + i); list.add(bean); } LoggedSqlCollector.start(); Transaction txn = Ebean.beginTransaction(); try { txn.setBatch(PersistBatch.ALL); Ebean.saveAll(list); txn.commit(); } finally { txn.end(); } List<String> loggedSql = LoggedSqlCollector.stop(); assertThat(loggedSql).hasSize(3); for (String sql : loggedSql) { assertThat(sql).contains("insert into o_cached_bean ("); assertThat(sql).contains("name) values (?"); } }
@Test public void testQueryBind() { if (!isH2()) { // only run this on H2 - PGCrypto not happy on CI server return; } LoggedSqlCollector.start(); Ebean.find(EBasicEncrypt.class).where().startsWith("description", "Rob").findList(); List<String> loggedSql = LoggedSqlCollector.stop(); assertThat(loggedSql).hasSize(1); assertThat(loggedSql.get(0)).contains("; --bind(****,Rob%)"); }
@Test public void test_noCount() throws ExecutionException, InterruptedException { ResetBasicData.reset(); PagedList<Order> pagedList = Ebean.find(Order.class).findPagedList(0, 4); LoggedSqlCollector.start(); List<Order> orders = pagedList.getList(); assertTrue(!orders.isEmpty()); List<String> loggedSql = LoggedSqlCollector.stop(); assertEquals("Only 1 SQL statement, no count query", 1, loggedSql.size()); }
@Test public void test_countUsingForegound() throws ExecutionException, InterruptedException { ResetBasicData.reset(); PagedList<Order> pagedList = Ebean.find(Order.class).findPagedList(0, 6); LoggedSqlCollector.start(); // kinda not normal but just wrap in a transaction to assert // the background fetch does not occur (which explicitly creates // its own transaction) ... so a bit naughty with the test here Ebean.beginTransaction(); try { List<Order> orders = pagedList.getList(); int totalRowCount = pagedList.getTotalRowCount(); // invoke it again but cached... int totalRowCountAgain = pagedList.getTotalRowCount(); List<String> loggedSql = LoggedSqlCollector.stop(); assertTrue(orders.size() < totalRowCount); assertEquals(2, loggedSql.size()); assertEquals(totalRowCount, totalRowCountAgain); String firstTxn = loggedSql.get(0).substring(0, 10); String secTxn = loggedSql.get(1).substring(0, 10); assertEquals(firstTxn, secTxn); } finally { Ebean.endTransaction(); } }