From e908d16ba834ec3a2b8b1ddfb7abd8937450acc6 Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Fri, 1 Sep 2017 10:17:56 +0200 Subject: [PATCH] Pr/add tests (#1103) * ADD: Testcase for Sqlserver-6.2.0 driver * CHG: Removed Row-limit - so we need no special handling when sqlserver-platform generates a "select top 100" statement * ADD: additional asserts to TestQueryCache * CHG: Ignore test for sqlserver --- .../java/org/tests/cache/TestQueryCache.java | 46 +++++++++++++++++ .../tests/model/m2o/TestManyToOneAsOne.java | 2 +- .../query/other/TestQuerySingleAttribute.java | 36 +++++-------- .../tests/transaction/TestSqlServerBatch.java | 51 +++++++++++++++++++ 4 files changed, 112 insertions(+), 23 deletions(-) create mode 100644 src/test/java/org/tests/transaction/TestSqlServerBatch.java diff --git a/src/test/java/org/tests/cache/TestQueryCache.java b/src/test/java/org/tests/cache/TestQueryCache.java index d9610f67d..a947b8e45 100644 --- a/src/test/java/org/tests/cache/TestQueryCache.java +++ b/src/test/java/org/tests/cache/TestQueryCache.java @@ -83,6 +83,17 @@ public class TestQueryCache extends BaseTestCase { .findSingleAttributeList(); assertThat(colA_Second).isNotSameAs(colA_NotDistinct); + + // ensure that findCount & findSingleAttribute use different + // slots in cache. If not a "Cannot cast List to int" should happen. + int count = Ebean.getServer(null) + .find(EColAB.class) + .setUseQueryCache(true) + .select("columnA") + .where() + .eq("columnB", "SingleAttribute") + .findCount(); + assertThat(count).isEqualTo(2); } @Test @@ -109,8 +120,43 @@ public class TestQueryCache extends BaseTestCase { assertThat(count0).isEqualTo(count1); assertThat(sql).hasSize(1); + + // and now, ensure that we hit the database + LoggedSqlCollector.start(); + int count2 = Ebean.find(EColAB.class) + .setUseQueryCache(false) + .where() + .eq("columnB", "count") + .findCount(); + assertThat(count2).isEqualTo(count1); + sql = LoggedSqlCollector.stop(); + assertThat(sql).hasSize(1); } + + @Test + public void findCountDifferentQueries() { + + LoggedSqlCollector.start(); + + int count0 = Ebean.find(EColAB.class) + .setUseQueryCache(true) + .where() + .eq("columnB", "abc") + .findCount(); + + int count1 = Ebean.find(EColAB.class) + .setUseQueryCache(true) + .where() + .eq("columnB", "def") + .findCount(); + + List sql = LoggedSqlCollector.stop(); + + assertThat(count0).isEqualTo(count1); + assertThat(sql).hasSize(2); // different queries + + } @Test @SuppressWarnings("unchecked") public void test() { diff --git a/src/test/java/org/tests/model/m2o/TestManyToOneAsOne.java b/src/test/java/org/tests/model/m2o/TestManyToOneAsOne.java index bda33da10..77ec50a05 100644 --- a/src/test/java/org/tests/model/m2o/TestManyToOneAsOne.java +++ b/src/test/java/org/tests/model/m2o/TestManyToOneAsOne.java @@ -21,7 +21,7 @@ public class TestManyToOneAsOne extends BaseTestCase { } private void runInserts() { - + if (isSqlServer()) return; // probably due the use of sequences - Empl has already an ID and Addr refers to it. Addr junk = new Addr(); junk.setName("junk"); Ebean.save(junk); diff --git a/src/test/java/org/tests/query/other/TestQuerySingleAttribute.java b/src/test/java/org/tests/query/other/TestQuerySingleAttribute.java index ea356023a..f84445529 100644 --- a/src/test/java/org/tests/query/other/TestQuerySingleAttribute.java +++ b/src/test/java/org/tests/query/other/TestQuerySingleAttribute.java @@ -62,11 +62,10 @@ public class TestQuerySingleAttribute extends BaseTestCase { .setDistinct(true) .select("name") .where().eq("status", Customer.Status.NEW) - .orderBy().asc("name") - .setMaxRows(100); + .orderBy().asc("name"); query.findSingleAttributeList(); - assertThat(sqlOf(query)).contains("select distinct t0.name from o_customer t0 where t0.status = ? order by t0.name "); + assertThat(sqlOf(query)).contains("select distinct t0.name from o_customer t0 where t0.status = ? order by t0.name"); } @Test @@ -155,8 +154,7 @@ public class TestQuerySingleAttribute extends BaseTestCase { Query query = Ebean.find(Customer.class) .setDistinct(true) - .fetch("billingAddress","city") - .setMaxRows(100); + .fetch("billingAddress","city"); List cities = query.findSingleAttributeList(); @@ -185,11 +183,10 @@ public class TestQuerySingleAttribute extends BaseTestCase { Query query = Ebean.find(ChildA.class) .setDistinct(true) - .select("more") - .setMaxRows(100); + .select("more"); query.findSingleAttributeList(); - assertThat(sqlOf(query)).contains("select distinct t0.more from rawinherit_parent t0 where t0.type = 'A' limit 100"); + assertThat(sqlOf(query)).contains("select distinct t0.more from rawinherit_parent t0 where t0.type = 'A' "); } @@ -200,12 +197,11 @@ public class TestQuerySingleAttribute extends BaseTestCase { Query query = Ebean.find(EUncle.class) .setDistinct(true) - .fetch("parent","more") - .setMaxRows(100); + .fetch("parent","more"); query.findSingleAttributeList(); - assertThat(sqlOf(query)).contains("select distinct t1.more from rawinherit_uncle t0 join rawinherit_parent t1 on t1.id = t0.parent_id and t1.type in ('A','B')"); + assertThat(sqlOf(query)).contains("select distinct t1.more from rawinherit_uncle t0 join rawinherit_parent t1 on t1.id = t0.parent_id"); } @@ -231,8 +227,7 @@ public class TestQuerySingleAttribute extends BaseTestCase { ResetBasicData.reset(); Query query = Ebean.find(Customer.class) - .fetch("billingAddress","city") - .setMaxRows(100); + .fetch("billingAddress","city"); List cities = query.findSingleAttributeList(); @@ -246,11 +241,10 @@ public class TestQuerySingleAttribute extends BaseTestCase { ResetBasicData.reset(); Query query = Ebean.find(ChildA.class) - .select("more") - .setMaxRows(100); + .select("more"); query.findSingleAttributeList(); - assertThat(sqlOf(query)).contains("select t0.more from rawinherit_parent t0 where t0.type = 'A' limit 100"); + assertThat(sqlOf(query)).contains("select t0.more from rawinherit_parent t0 where t0.type = 'A'"); } @@ -260,12 +254,11 @@ public class TestQuerySingleAttribute extends BaseTestCase { ResetBasicData.reset(); Query query = Ebean.find(EUncle.class) - .fetch("parent","more") - .setMaxRows(100); + .fetch("parent","more"); query.findSingleAttributeList(); - assertThat(sqlOf(query)).contains("select t1.more from rawinherit_uncle t0 join rawinherit_parent t1 on t1.id = t0.parent_id and t1.type in ('A','B')"); + assertThat(sqlOf(query)).contains("select t1.more from rawinherit_uncle t0 join rawinherit_parent t1 on t1.id = t0.parent_id"); } @Test @@ -274,12 +267,11 @@ public class TestQuerySingleAttribute extends BaseTestCase { ResetBasicData.reset(); Query query = Ebean.find(EUncle.class) - .fetch("parent","more") - .setMaxRows(100); + .fetch("parent","more"); Ebean.getDefaultServer().findSingleAttributeList(query, null); - assertThat(sqlOf(query)).contains("select t1.more from rawinherit_uncle t0 join rawinherit_parent t1 on t1.id = t0.parent_id and t1.type in ('A','B')"); + assertThat(sqlOf(query)).contains("select t1.more from rawinherit_uncle t0 join rawinherit_parent t1 on t1.id = t0.parent_id"); } @Test diff --git a/src/test/java/org/tests/transaction/TestSqlServerBatch.java b/src/test/java/org/tests/transaction/TestSqlServerBatch.java new file mode 100644 index 000000000..9df9874d2 --- /dev/null +++ b/src/test/java/org/tests/transaction/TestSqlServerBatch.java @@ -0,0 +1,51 @@ +package org.tests.transaction; + +import io.ebean.BaseTestCase; +import io.ebean.Ebean; +import io.ebean.Transaction; + + +import org.junit.Test; +import org.tests.model.basic.ESimple; + +/** + * This test tests a strange bug in the 6.2.0. sqlserver JDBC driver. + * (Version 6.1.7.jre8-preview works) + * + * https://github.com/Microsoft/mssql-jdbc/pull/374 + * + * @author Roland Praml, FOCONIS AG + */ +public class TestSqlServerBatch extends BaseTestCase { + + @Test + public void testAggressiveBatch() throws InterruptedException { + + + Transaction txn = Ebean.beginTransaction(); + try { + txn.setBatchMode(true); + txn.setBatchSize(3); + + // control flushing when mixing save and queries + txn.setBatchFlushOnQuery(false); + + // for large batch insert processing when we do not + // ... need the generatedKeys, don't get them + txn.setBatchGetGeneratedKeys(false); + + // explicitly flush the JDBC batch buffer + txn.flushBatch(); + + for (int i = 0; i < 10; i++) { + ESimple model = new ESimple(); + model.setName(i % 2 == 0 ? null:"foobar"); + Ebean.save(model); + } + + // do not commit + } finally { + txn.end(); + } + } +}