@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 test() { LoggedSqlCollector.start(); Transaction txn = Ebean.beginTransaction(); try { txn.setBatch(PersistBatch.ALL); EdParent parent = new EdParent(); parent.setName("MyComputer"); EdChild child = new EdChild(); child.setName("Harddisk 123"); child.setParent(parent); ArrayList<EdChild> children = new ArrayList<EdChild>(); children.add(child); parent.setChildren(children); Ebean.save(parent); // nothing flushed yet List<String> loggedSql0 = LoggedSqlCollector.start(); assertEquals(0, loggedSql0.size()); parent.setName("MyDesk"); Ebean.save(parent); // nothing flushed yet assertEquals(0, LoggedSqlCollector.start().size()); Ebean.commitTransaction(); // insert statements for EdExtendedParent List<String> loggedSql2 = LoggedSqlCollector.start(); assertEquals(2, loggedSql2.size()); } finally { Ebean.endTransaction(); } }
@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(); } }