You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by se...@apache.org on 2019/06/29 19:40:29 UTC

[calcite] branch master updated: [CALCITE-3156] Mongo adapter. Replace fongo with Mongo Java Server for tests

This is an automated email from the ASF dual-hosted git repository.

sereda pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
     new 135daf3  [CALCITE-3156] Mongo adapter. Replace fongo with Mongo Java Server for tests
135daf3 is described below

commit 135daf3b302e7051768eb2bdce9839e75ba13e05
Author: Andrei Sereda <25...@users.noreply.github.com>
AuthorDate: Sat Jun 29 15:05:57 2019 -0400

    [CALCITE-3156] Mongo adapter. Replace fongo with Mongo Java Server for tests
    
    mongo java server is a better alternative to fongo. All tests (including IT) are passing.
---
 mongodb/pom.xml                                    | 11 +++++--
 .../calcite/adapter/mongodb/MongoAdapterTest.java  | 12 ++-----
 .../adapter/mongodb/MongoDatabasePolicy.java       | 38 ++++++++++++++--------
 .../org/apache/calcite/test/MongoAssertions.java   |  7 ++--
 pom.xml                                            | 18 +++++++---
 5 files changed, 54 insertions(+), 32 deletions(-)

diff --git a/mongodb/pom.xml b/mongodb/pom.xml
index 06c007a..8047ef4 100644
--- a/mongodb/pom.xml
+++ b/mongodb/pom.xml
@@ -70,9 +70,16 @@ limitations under the License.
       <artifactId>hamcrest-core</artifactId>
       <scope>test</scope>
     </dependency>
+    <!-- mongo-java-server is embedded, fake mongo engine used for tests -->
     <dependency>
-      <groupId>com.github.fakemongo</groupId>
-      <artifactId>fongo</artifactId>
+      <groupId>de.bwaldvogel</groupId>
+      <artifactId>mongo-java-server-core</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>de.bwaldvogel</groupId>
+      <artifactId>mongo-java-server-memory-backend</artifactId>
+      <version>${mongo-java-server.version}</version>
       <scope>test</scope>
     </dependency>
     <dependency>
diff --git a/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoAdapterTest.java b/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoAdapterTest.java
index c62fe35..6f843df 100644
--- a/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoAdapterTest.java
+++ b/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoAdapterTest.java
@@ -393,9 +393,6 @@ public class MongoAdapterTest implements SchemaFactory {
   }
 
   @Test public void testCountGroupByEmptyMultiplyBy2() {
-    // This operation is not supported by fongo: https://github.com/fakemongo/fongo/issues/152
-    MongoAssertions.assumeRealMongoInstance();
-
     assertModel(MODEL)
         .query("select count(*)*2 from zips")
         .returns(String.format(Locale.ROOT, "EXPR$0=%d\n", ZIPS_SIZE * 2))
@@ -465,8 +462,6 @@ public class MongoAdapterTest implements SchemaFactory {
   }
 
   @Test public void testGroupByAvgSumCount() {
-    // This operation not supported by fongo: https://github.com/fakemongo/fongo/issues/152
-    MongoAssertions.assumeRealMongoInstance();
     assertModel(MODEL)
         .query(
             "select state, avg(pop) as a, sum(pop) as s, count(pop) as c from zips group by state order by state")
@@ -478,8 +473,8 @@ public class MongoAdapterTest implements SchemaFactory {
                 "{$project: {POP: '$pop', STATE: '$state'}}",
                 "{$group: {_id: '$STATE', _1: {$sum: '$POP'}, _2: {$sum: {$cond: [ {$eq: ['POP', null]}, 0, 1]}}}}",
                 "{$project: {STATE: '$_id', _1: '$_1', _2: '$_2'}}",
-                "{$sort: {STATE: 1}}",
-                "{$project: {STATE: 1, A: {$divide: [{$cond:[{$eq: ['$_2', {$literal: 0}]},null,'$_1']}, '$_2']}, S: {$cond:[{$eq: ['$_2', {$literal: 0}]},null,'$_1']}, C: '$_2'}}"));
+                "{$project: {STATE: 1, A: {$divide: [{$cond:[{$eq: ['$_2', {$literal: 0}]},null,'$_1']}, '$_2']}, S: {$cond:[{$eq: ['$_2', {$literal: 0}]},null,'$_1']}, C: '$_2'}}",
+                "{$sort: {STATE: 1}}"));
   }
 
   @Test public void testGroupByHaving() {
@@ -591,9 +586,6 @@ public class MongoAdapterTest implements SchemaFactory {
   }
 
   @Test public void testDistinctCountOrderBy() {
-    // java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to java.lang.Number
-    // https://github.com/fakemongo/fongo/issues/152
-    MongoAssertions.assumeRealMongoInstance();
     assertModel(MODEL)
         .query("select state, count(distinct city) as cdc\n"
             + "from zips\n"
diff --git a/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoDatabasePolicy.java b/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoDatabasePolicy.java
index f325360..f9df5c1 100644
--- a/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoDatabasePolicy.java
+++ b/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoDatabasePolicy.java
@@ -17,21 +17,26 @@
 package org.apache.calcite.adapter.mongodb;
 
 import org.apache.calcite.test.MongoAssertions;
+import org.apache.calcite.util.Closer;
 
-import com.github.fakemongo.Fongo;
 import com.mongodb.MongoClient;
 import com.mongodb.client.MongoDatabase;
 
 import org.junit.rules.ExternalResource;
 
+import java.io.Closeable;
+import java.net.InetSocketAddress;
 import java.util.Objects;
 
+import de.bwaldvogel.mongo.MongoServer;
+import de.bwaldvogel.mongo.backend.memory.MemoryBackend;
+
 /**
- * Instantiates a new connection to Fongo (or Mongo) database depending on the
- * current profile (unit or integration tests).
+ * Instantiates a new connection to a embedded (but fake) or real mongo database
+ * depending on current profile (unit or integration tests).
  *
- * <p>By default, this rule is executed as part of a unit test and in-memory database
- * <a href="https://github.com/fakemongo/fongo">Fongo</a> is used.
+ * <p>By default, this rule is executed as part of a unit test and
+ * <a href="https://github.com/bwaldvogel/mongo-java-server">in-memory database</a> is used.
  *
  * <p>However, if the maven profile is set to {@code IT} (eg. via command line
  * {@code $ mvn -Pit install}) this rule will connect to an existing (external)
@@ -43,10 +48,13 @@ class MongoDatabasePolicy extends ExternalResource {
 
   private final MongoDatabase database;
   private final MongoClient client;
+  private final Closer closer;
 
-  private MongoDatabasePolicy(MongoClient client) {
+  private MongoDatabasePolicy(MongoClient client, Closer closer) {
     this.client = Objects.requireNonNull(client, "client");
     this.database = client.getDatabase(DB_NAME);
+    this.closer = Objects.requireNonNull(closer, "closer");
+    closer.add(client::close);
   }
 
   /**
@@ -56,17 +64,21 @@ class MongoDatabasePolicy extends ExternalResource {
    */
   static MongoDatabasePolicy create() {
     final MongoClient client;
+    final Closer closer = new Closer();
     if (MongoAssertions.useMongo()) {
-      // use to real client (connects to mongo)
+      // use to real client (connects to default mongo instance)
       client = new MongoClient();
-    } else if (MongoAssertions.useFongo()) {
-      // in-memory DB (fake Mongo)
-      client = new Fongo(MongoDatabasePolicy.class.getSimpleName()).getMongo();
+    } else if (MongoAssertions.useFake()) {
+      final MongoServer server = new MongoServer(new MemoryBackend());
+      final InetSocketAddress address = server.bind();
+
+      closer.add((Closeable) server::shutdownNow);
+      client = new MongoClient("127.0.0.1", address.getPort());
     } else {
-      throw new UnsupportedOperationException("I can only connect to Mongo or Fongo instances");
+      throw new UnsupportedOperationException("I can only connect to Mongo or Fake instances");
     }
 
-    return new MongoDatabasePolicy(client);
+    return new MongoDatabasePolicy(client, closer);
   }
 
 
@@ -75,7 +87,7 @@ class MongoDatabasePolicy extends ExternalResource {
   }
 
   @Override protected void after() {
-    client.close();
+    closer.close();
   }
 
 }
diff --git a/mongodb/src/test/java/org/apache/calcite/test/MongoAssertions.java b/mongodb/src/test/java/org/apache/calcite/test/MongoAssertions.java
index 5761af5..14558b6 100644
--- a/mongodb/src/test/java/org/apache/calcite/test/MongoAssertions.java
+++ b/mongodb/src/test/java/org/apache/calcite/test/MongoAssertions.java
@@ -85,12 +85,13 @@ public class MongoAssertions {
   }
 
   /**
-   * Checks wherever tests should use Fongo instead of Mongo. Opposite of {@link #useMongo()}.
+   * Checks wherever tests should use Embedded Fake Mongo instead of connecting to real
+   * mongodb instance. Opposite of {@link #useMongo()}.
    *
    * @return Whether current tests should use embedded
-   * <a href="https://github.com/fakemongo/fongo">Fongo</a> instance
+   * <a href="https://github.com/bwaldvogel/mongo-java-server">Mongo Java Server</a> instance
    */
-  public static boolean useFongo() {
+  public static boolean useFake() {
     return !useMongo();
   }
 
diff --git a/pom.xml b/pom.xml
index 759023c..bd6b52e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -83,7 +83,7 @@ limitations under the License.
     <esri-geometry-api.version>2.2.0</esri-geometry-api.version>
     <findbugs.version>3.0.1</findbugs.version>
     <fmpp.version>0.9.16</fmpp.version>
-    <fongo.version>2.1.1</fongo.version>
+    <mongo-java-server.version>1.16.0</mongo-java-server.version>
     <foodmart-data-hsqldb.version>0.3</foodmart-data-hsqldb.version>
     <foodmart-data-json.version>0.4</foodmart-data-json.version>
     <foodmart-queries.version>0.4.1</foodmart-queries.version>
@@ -538,9 +538,19 @@ limitations under the License.
         <version>${mongo-java-driver.version}</version>
       </dependency>
       <dependency>
-        <groupId>com.github.fakemongo</groupId>
-        <artifactId>fongo</artifactId>
-        <version>${fongo.version}</version>
+        <groupId>de.bwaldvogel</groupId>
+        <artifactId>mongo-java-server</artifactId>
+        <version>${mongo-java-server.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>de.bwaldvogel</groupId>
+        <artifactId>mongo-java-server-core</artifactId>
+        <version>${mongo-java-server.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>de.bwaldvogel</groupId>
+        <artifactId>mongo-java-server-memory-backend</artifactId>
+        <version>${mongo-java-server.version}</version>
       </dependency>
       <dependency>
         <groupId>net.hydromatic</groupId>