You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2015/10/16 16:39:03 UTC

[4/4] incubator-calcite git commit: Fix tests that wrongly used executeQuery to execute INSERT statements

Fix tests that wrongly used executeQuery to execute INSERT statements


Project: http://git-wip-us.apache.org/repos/asf/incubator-calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-calcite/commit/9a410f5d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-calcite/tree/9a410f5d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-calcite/diff/9a410f5d

Branch: refs/heads/master
Commit: 9a410f5d61e5af24f130f2d5047d64e2c6ae341e
Parents: cee8e84
Author: Julian Hyde <jh...@apache.org>
Authored: Thu Oct 15 12:11:25 2015 -0700
Committer: Julian Hyde <jh...@apache.org>
Committed: Fri Oct 16 00:58:59 2015 -0700

----------------------------------------------------------------------
 .../org/apache/calcite/test/CalciteAssert.java  | 51 ++++++++++++++++----
 .../calcite/test/JdbcFrontLinqBackTest.java     | 12 ++---
 2 files changed, 47 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/9a410f5d/core/src/test/java/org/apache/calcite/test/CalciteAssert.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/CalciteAssert.java b/core/src/test/java/org/apache/calcite/test/CalciteAssert.java
index 7e96dba..aebf192 100644
--- a/core/src/test/java/org/apache/calcite/test/CalciteAssert.java
+++ b/core/src/test/java/org/apache/calcite/test/CalciteAssert.java
@@ -53,6 +53,8 @@ import com.google.common.collect.Lists;
 import net.hydromatic.foodmart.data.hsqldb.FoodmartHsqldb;
 import net.hydromatic.scott.data.hsqldb.ScottHsqldb;
 
+import org.hamcrest.CoreMatchers;
+
 import java.io.File;
 import java.io.PrintWriter;
 import java.io.StringWriter;
@@ -296,6 +298,15 @@ public class CalciteAssert {
     };
   }
 
+  public static Function<Integer, Void> checkUpdateCount(final int expected) {
+    return new Function<Integer, Void>() {
+      public Void apply(Integer updateCount) {
+        assertThat(updateCount, CoreMatchers.is(expected));
+        return null;
+      }
+    };
+  }
+
   /** Checks that the result of the second and subsequent executions is the same
    * as the first.
    *
@@ -430,6 +441,7 @@ public class CalciteAssert {
       boolean materializationsEnabled,
       List<Pair<Hook, Function>> hooks,
       Function<ResultSet, Void> resultChecker,
+      Function<Integer, Void> updateChecker,
       Function<Throwable, Void> exceptionChecker) throws Exception {
     final String message =
         "With materializationsEnabled=" + materializationsEnabled
@@ -450,9 +462,14 @@ public class CalciteAssert {
       }
       Statement statement = connection.createStatement();
       statement.setMaxRows(limit <= 0 ? limit : Math.max(limit, 1));
-      ResultSet resultSet;
+      ResultSet resultSet = null;
+      Integer updateCount = null;
       try {
-        resultSet = statement.executeQuery(sql);
+        if (updateChecker == null) {
+          resultSet = statement.executeQuery(sql);
+        } else {
+          updateCount = statement.executeUpdate(sql);
+        }
         if (exceptionChecker != null) {
           exceptionChecker.apply(null);
           return;
@@ -467,7 +484,12 @@ public class CalciteAssert {
       if (resultChecker != null) {
         resultChecker.apply(resultSet);
       }
-      resultSet.close();
+      if (updateChecker != null) {
+        updateChecker.apply(updateCount);
+      }
+      if (resultSet != null) {
+        resultSet.close();
+      }
       statement.close();
       connection.close();
     } catch (Error | RuntimeException e) {
@@ -1163,11 +1185,22 @@ public class CalciteAssert {
       return returns(sql, checker);
     }
 
+    public final AssertQuery updates(int count) {
+      try {
+        assertQuery(createConnection(), sql, limit, materializationsEnabled,
+            hooks, null, checkUpdateCount(count), null);
+        return this;
+      } catch (Exception e) {
+        throw new RuntimeException(
+            "exception while executing [" + sql + "]", e);
+      }
+    }
+
     protected AssertQuery returns(String sql,
         Function<ResultSet, Void> checker) {
       try {
         assertQuery(createConnection(), sql, limit, materializationsEnabled,
-            hooks, checker, null);
+            hooks, checker, null, null);
         return this;
       } catch (Exception e) {
         throw new RuntimeException(
@@ -1182,7 +1215,7 @@ public class CalciteAssert {
     public AssertQuery throws_(String message) {
       try {
         assertQuery(createConnection(), sql, limit, materializationsEnabled,
-            hooks, null, checkException(message));
+            hooks, null, null, checkException(message));
         return this;
       } catch (Exception e) {
         throw new RuntimeException(
@@ -1193,7 +1226,7 @@ public class CalciteAssert {
     public AssertQuery runs() {
       try {
         assertQuery(createConnection(), sql, limit, materializationsEnabled,
-            hooks, null, null);
+            hooks, null, null, null);
         return this;
       } catch (Exception e) {
         throw new RuntimeException(
@@ -1204,7 +1237,7 @@ public class CalciteAssert {
     public AssertQuery typeIs(String expected) {
       try {
         assertQuery(createConnection(), sql, limit, false,
-            hooks, checkResultType(expected), null);
+            hooks, checkResultType(expected), null, null);
         return this;
       } catch (Exception e) {
         throw new RuntimeException(
@@ -1283,7 +1316,7 @@ public class CalciteAssert {
           });
       try {
         assertQuery(createConnection(), sql, limit, materializationsEnabled,
-            hooks, null, null);
+            hooks, null, null, null);
         assertNotNull(plan);
       } catch (Exception e) {
         throw new RuntimeException(
@@ -1306,7 +1339,7 @@ public class CalciteAssert {
           });
       try {
         assertQuery(createConnection(), sql, limit, materializationsEnabled,
-            hooks, null, null);
+            hooks, null, null, null);
         predicate1.apply(list);
         return this;
       } catch (Exception e) {

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/9a410f5d/core/src/test/java/org/apache/calcite/test/JdbcFrontLinqBackTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/JdbcFrontLinqBackTest.java b/core/src/test/java/org/apache/calcite/test/JdbcFrontLinqBackTest.java
index dc6c3bd..c09fb0b 100644
--- a/core/src/test/java/org/apache/calcite/test/JdbcFrontLinqBackTest.java
+++ b/core/src/test/java/org/apache/calcite/test/JdbcFrontLinqBackTest.java
@@ -216,7 +216,6 @@ public class JdbcFrontLinqBackTest {
             + "empid=110; deptno=10; name=Theodore; salary=11500.0; commission=250\n");
   }
 
-  @Ignore
   @Test public void testInsert() {
     final List<JdbcTest.Employee> employees = new ArrayList<>();
     CalciteAssert.AssertThat with = mutable(employees);
@@ -224,12 +223,12 @@ public class JdbcFrontLinqBackTest {
         .returns(
             "empid=0; deptno=0; name=first; salary=0.0; commission=null\n");
     with.query("insert into \"foo\".\"bar\" select * from \"hr\".\"emps\"")
-        .returns("ROWCOUNT=4\n");
+        .updates(4);
     with.query("select count(*) as c from \"foo\".\"bar\"")
         .returns("C=5\n");
     with.query("insert into \"foo\".\"bar\" "
         + "select * from \"hr\".\"emps\" where \"deptno\" = 10")
-        .returns("ROWCOUNT=3\n");
+        .updates(3);
     with.query("select \"name\", count(*) as c from \"foo\".\"bar\" "
         + "group by \"name\"")
         .returnsUnordered(
@@ -334,19 +333,18 @@ public class JdbcFrontLinqBackTest {
     };
   }
 
-  @Ignore
   @Test public void testInsert2() {
     final List<JdbcTest.Employee> employees = new ArrayList<>();
     CalciteAssert.AssertThat with = mutable(employees);
     with.query("insert into \"foo\".\"bar\" values (1, 1, 'second', 2, 2)")
-        .returns("ROWCOUNT=1\n");
+        .updates(1);
     with.query("insert into \"foo\".\"bar\"\n"
         + "values (1, 3, 'third', 0, 3), (1, 4, 'fourth', 0, 4), (1, 5, 'fifth ', 0, 3)")
-        .returns("ROWCOUNT=3\n");
+        .updates(3);
     with.query("select count(*) as c from \"foo\".\"bar\"")
         .returns("C=5\n");
     with.query("insert into \"foo\".\"bar\" values (1, 6, null, 0, null)")
-        .returns("ROWCOUNT=1\n");
+        .updates(1);
     with.query("select count(*) as c from \"foo\".\"bar\"")
         .returns("C=6\n");
   }