Repository: metamodel
Updated Branches:
  refs/heads/master c7cc4acf0 -> 484cbb1d0


METAMODEL-1173: Added CouchDB integration test in add. to MongoDB closes 
apache/metamodel#174


Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo
Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/484cbb1d
Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/484cbb1d
Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/484cbb1d

Branch: refs/heads/master
Commit: 484cbb1d02e2f97d4d699f3097b9a138e88248dc
Parents: c7cc4ac
Author: Kasper Sørensen <[email protected]>
Authored: Tue Dec 12 18:15:37 2017 +0100
Committer: Dennis Du Krøger <[email protected]>
Committed: Tue Dec 12 18:15:37 2017 +0100

----------------------------------------------------------------------
 CHANGES.md                                      |   1 +
 couchdb/pom.xml                                 |   2 +-
 .../couchdb/CouchDbDataContextTest.java         | 132 ++++++++++++++++---
 .../metamodel/couchdb/CouchDbTestCase.java      |  96 --------------
 .../metamodel/couchdb/CouchDbTestSupport.java   |  91 +++++++++++++
 5 files changed, 205 insertions(+), 117 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/metamodel/blob/484cbb1d/CHANGES.md
----------------------------------------------------------------------
diff --git a/CHANGES.md b/CHANGES.md
index 860c0b7..19bed05 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -1,4 +1,5 @@
  * [METAMODEL-1169] - Fixed issue with SQL Server milliseconds precision in 
WHERE.
+ * [METAMODEL-1173] - Added CouchDB integration test in add. to MongoDB
  * [METAMODEL-1173] - Fixed parsing and handling of scalar functions in WHERE 
clause.
  * [METAMODEL-1171] - Fixed parsing of query operators with DATE, TIME, 
TIMESTAMP prefix to operand date/time values.
  * [METAMODEL-1174] - Upgraded Excel module's dependency on Apache POI to 
latest stable version (3.17).

http://git-wip-us.apache.org/repos/asf/metamodel/blob/484cbb1d/couchdb/pom.xml
----------------------------------------------------------------------
diff --git a/couchdb/pom.xml b/couchdb/pom.xml
index c443673..ea13240 100644
--- a/couchdb/pom.xml
+++ b/couchdb/pom.xml
@@ -35,7 +35,7 @@ under the License.
                <dependency>
                        <groupId>org.ektorp</groupId>
                        <artifactId>org.ektorp</artifactId>
-                       <version>1.4.2</version>
+                       <version>1.5.0</version>
                </dependency>
                <dependency>
                        <groupId>com.fasterxml.jackson.core</groupId>

http://git-wip-us.apache.org/repos/asf/metamodel/blob/484cbb1d/couchdb/src/test/java/org/apache/metamodel/couchdb/CouchDbDataContextTest.java
----------------------------------------------------------------------
diff --git 
a/couchdb/src/test/java/org/apache/metamodel/couchdb/CouchDbDataContextTest.java
 
b/couchdb/src/test/java/org/apache/metamodel/couchdb/CouchDbDataContextTest.java
index 508f3f1..689d334 100644
--- 
a/couchdb/src/test/java/org/apache/metamodel/couchdb/CouchDbDataContextTest.java
+++ 
b/couchdb/src/test/java/org/apache/metamodel/couchdb/CouchDbDataContextTest.java
@@ -18,6 +18,12 @@
  */
 package org.apache.metamodel.couchdb;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -40,20 +46,24 @@ import org.ektorp.CouchDbConnector;
 import org.ektorp.http.HttpClient;
 import org.ektorp.http.StdHttpClient;
 import org.ektorp.impl.StdCouchDbInstance;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
 
-public class CouchDbDataContextTest extends CouchDbTestCase {
+public class CouchDbDataContextTest extends CouchDbTestSupport {
 
     private HttpClient httpClient;
     private StdCouchDbInstance couchDbInstance;
     private CouchDbConnector connector;
     private SimpleTableDef predefinedTableDef;
 
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
+    @Before
+    public void before() throws Exception {
+        loadConfiguration();
 
         if (isConfigured()) {
-            final int timeout = 8 * 1000; // 8 seconds should be more than 
enough
+            final int timeout = 8 * 1000; // 8 seconds should be more than
+                                          // enough
             httpClient = new 
StdHttpClient.Builder().socketTimeout(timeout).host(getHostname()).build();
 
             // set up a simple database
@@ -66,16 +76,15 @@ public class CouchDbDataContextTest extends CouchDbTestCase 
{
             connector = couchDbInstance.createConnector(databaseName, true);
 
             final String[] columnNames = new String[] { "name", "gender", 
"age" };
-            final ColumnType[] columnTypes = new ColumnType[] { 
ColumnType.STRING, ColumnType.CHAR, ColumnType.INTEGER };
+            final ColumnType[] columnTypes = new ColumnType[] { 
ColumnType.STRING, ColumnType.CHAR,
+                    ColumnType.INTEGER };
             predefinedTableDef = new SimpleTableDef(databaseName, columnNames, 
columnTypes);
         }
 
     }
 
-    @Override
-    protected void tearDown() throws Exception {
-        super.tearDown();
-
+    @After
+    public void after() {
         connector = null;
 
         if (isConfigured()) {
@@ -85,6 +94,7 @@ public class CouchDbDataContextTest extends CouchDbTestCase {
         }
     }
 
+    @Test
     public void testWorkingWithMapsAndLists() throws Exception {
         if (!isConfigured()) {
             System.err.println(getInvalidConfigurationMessage());
@@ -118,14 +128,14 @@ public class CouchDbDataContextTest extends 
CouchDbTestCase {
             assertFalse(ds.next());
             ds.close();
 
-            assertEquals("Row[values=[1, {hello=[world, welt, verden], 
foo=bar}, [{}, {meta=model, couch=db}]]]",
-                    row.toString());
+            assertEquals("Row[values=[1, {hello=[world, welt, verden], 
foo=bar}, [{}, {meta=model, couch=db}]]]", row
+                    .toString());
             assertTrue(row.getValue(0) instanceof Integer);
             assertTrue(row.getValue(1) instanceof Map);
             assertTrue(row.getValue(2) instanceof List);
 
-            CouchDbDataContext dc2 = new CouchDbDataContext(couchDbInstance, 
new SimpleTableDef("test_table_map_and_list",
-                    new String[] { "foo.hello[0]", "bar[1].couch" }));
+            CouchDbDataContext dc2 = new CouchDbDataContext(couchDbInstance, 
new SimpleTableDef(
+                    "test_table_map_and_list", new String[] { "foo.hello[0]", 
"bar[1].couch" }));
             ds = 
dc2.query().from("test_table_map_and_list").select("foo.hello[0]", 
"bar[1].couch").execute();
             assertTrue(ds.next());
             assertEquals("Row[values=[world, db]]", ds.getRow().toString());
@@ -138,6 +148,7 @@ public class CouchDbDataContextTest extends CouchDbTestCase 
{
 
     }
 
+    @Test
     public void testCreateUpdateDeleteScenario() throws Exception {
         if (!isConfigured()) {
             System.err.println(getInvalidConfigurationMessage());
@@ -165,8 +176,8 @@ public class CouchDbDataContextTest extends CouchDbTestCase 
{
                 + 
"Column[name=_rev,columnNumber=1,type=STRING,nullable=false,nativeType=null,columnSize=null],
 "
                 + 
"Column[name=bar,columnNumber=2,type=STRING,nullable=null,nativeType=null,columnSize=null],
 "
                 + 
"Column[name=baz,columnNumber=3,type=INTEGER,nullable=null,nativeType=null,columnSize=null],
 "
-                + 
"Column[name=foo,columnNumber=4,type=STRING,nullable=null,nativeType=null,columnSize=null]]",
-                Arrays.toString(table.getColumns().toArray()));
+                + 
"Column[name=foo,columnNumber=4,type=STRING,nullable=null,nativeType=null,columnSize=null]]",
 Arrays
+                        .toString(table.getColumns().toArray()));
 
         // first delete the manually created database!
         dc.executeUpdate(new UpdateScript() {
@@ -182,8 +193,8 @@ public class CouchDbDataContextTest extends CouchDbTestCase 
{
         dc.executeUpdate(new UpdateScript() {
             @Override
             public void run(UpdateCallback callback) {
-                Table table = callback.createTable(dc.getDefaultSchema(), 
databaseName).withColumn("foo")
-                        
.ofType(ColumnType.STRING).withColumn("greeting").ofType(ColumnType.STRING).execute();
+                Table table = callback.createTable(dc.getDefaultSchema(), 
databaseName).withColumn("foo").ofType(
+                        
ColumnType.STRING).withColumn("greeting").ofType(ColumnType.STRING).execute();
                 assertEquals("[_id, _rev, foo, greeting]", 
Arrays.toString(table.getColumnNames().toArray()));
             }
         });
@@ -230,6 +241,7 @@ public class CouchDbDataContextTest extends CouchDbTestCase 
{
         ds.close();
     }
 
+    @Test
     public void testBasicQuery() throws Exception {
         if (!isConfigured()) {
             System.err.println(getInvalidConfigurationMessage());
@@ -258,8 +270,8 @@ public class CouchDbDataContextTest extends CouchDbTestCase 
{
         Schema schema = dc.getMainSchema();
         assertEquals("[" + getDatabaseName() + "]", 
Arrays.toString(schema.getTableNames().toArray()));
 
-        assertEquals("[_id, _rev, age, gender, name]",
-                
Arrays.toString(schema.getTableByName(getDatabaseName()).getColumnNames().toArray()));
+        assertEquals("[_id, _rev, age, gender, name]", 
Arrays.toString(schema.getTableByName(getDatabaseName())
+                .getColumnNames().toArray()));
         Column idColumn = 
schema.getTableByName(getDatabaseName()).getColumnByName("_id");
         
assertEquals("Column[name=_id,columnNumber=0,type=STRING,nullable=false,nativeType=null,columnSize=null]",
                 idColumn.toString());
@@ -346,6 +358,7 @@ public class CouchDbDataContextTest extends CouchDbTestCase 
{
         ds.close();
     }
 
+    @Test
     public void testFirstRowAndLastRow() throws Exception {
         if (!isConfigured()) {
             System.err.println(getInvalidConfigurationMessage());
@@ -393,6 +406,7 @@ public class CouchDbDataContextTest extends CouchDbTestCase 
{
         ds2.close();
     }
 
+    @Test
     public void testInsert() throws Exception {
         if (!isConfigured()) {
             System.err.println(getInvalidConfigurationMessage());
@@ -438,4 +452,82 @@ public class CouchDbDataContextTest extends 
CouchDbTestCase {
         assertFalse(ds.next());
         ds.close();
     }
+
+    @Test
+    public void testSelectNestedObject() {
+        if (!isConfigured()) {
+            System.err.println(getInvalidConfigurationMessage());
+            return;
+        }
+
+        try (DataSet ds = executeNestedObjectQuery("SELECT name.given FROM " + 
getDatabaseName() + " WHERE id = 42")) {
+            assertTrue(ds.next());
+            assertEquals("John", (String) ds.getRow().getValue(0));
+            assertFalse(ds.next());
+        }
+    }
+
+    @Test
+    public void testWhereNestedObject() {
+        if (!isConfigured()) {
+            System.err.println(getInvalidConfigurationMessage());
+            return;
+        }
+
+        try (DataSet ds = executeNestedObjectQuery("SELECT id FROM " + 
getDatabaseName()
+                + " WHERE name.given = 'Jane'")) {
+            assertTrue(ds.next());
+            assertEquals(43, ((Number) ds.getRow().getValue(0)).intValue());
+            assertFalse(ds.next());
+        }
+    }
+
+    @Test
+    public void testSelectAndWhereNestedObject() {
+        if (!isConfigured()) {
+            System.err.println(getInvalidConfigurationMessage());
+            return;
+        }
+
+        try (DataSet ds = executeNestedObjectQuery("SELECT name.family FROM " 
+ getDatabaseName()
+                + " WHERE name.given = 'Jane'")) {
+            assertTrue(ds.next());
+            assertEquals("Johnson", (String) ds.getRow().getValue(0));
+            assertFalse(ds.next());
+        }
+    }
+
+    // reusable method for a couple of test cases above
+    private DataSet executeNestedObjectQuery(String sql) {
+     // insert a few records
+        {
+            HashMap<String, String> name = new HashMap<>();
+            name.put("given", "John");
+            name.put("family", "Doe");
+            
+            HashMap<String, Object> map;
+            
+            map = new HashMap<>();
+            map.put("id", 42);
+            map.put("name", name);
+            connector.create(map);
+            
+            name.put("given", "Jane");
+            name.put("family", "Johnson");
+
+            map = new HashMap<>();
+            map.put("id", 43);
+            map.put("name", name);
+            connector.create(map);
+        }
+        
+        final CouchDbDataContext dataContext = new 
CouchDbDataContext(httpClient);
+        
+        
assertTrue(dataContext.getDefaultSchema().getTableNames().contains(getDatabaseName()));
+
+        final Table table = 
dataContext.getDefaultSchema().getTableByName(getDatabaseName());
+        assertEquals("[_id, _rev, id, name]", 
table.getColumnNames().toString());
+
+        return dataContext.executeQuery(sql);
+    }
 }

http://git-wip-us.apache.org/repos/asf/metamodel/blob/484cbb1d/couchdb/src/test/java/org/apache/metamodel/couchdb/CouchDbTestCase.java
----------------------------------------------------------------------
diff --git 
a/couchdb/src/test/java/org/apache/metamodel/couchdb/CouchDbTestCase.java 
b/couchdb/src/test/java/org/apache/metamodel/couchdb/CouchDbTestCase.java
deleted file mode 100644
index 9e1ede5..0000000
--- a/couchdb/src/test/java/org/apache/metamodel/couchdb/CouchDbTestCase.java
+++ /dev/null
@@ -1,96 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.metamodel.couchdb;
-
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.Properties;
-
-import junit.framework.TestCase;
-
-public abstract class CouchDbTestCase extends TestCase {
-
-    private static final String DEFAULT_TEST_DATABASE_NAME = 
"eobjects_metamodel_test";
-
-    private String _hostname;
-    private boolean _configured;
-    private String _databaseName;
-
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-
-        File file = new File(getPropertyFilePath());
-        if (file.exists()) {
-            loadPropertyFile(file);
-        } else {
-            // Continuous integration case
-            if (System.getenv("CONTINUOUS_INTEGRATION") != null) {
-                File travisFile = new 
File("../travis-metamodel-integrationtest-configuration.properties");
-                if (travisFile.exists()) {
-                    loadPropertyFile(travisFile);
-                } else {
-                    _configured = false;
-                }
-            } else {
-                _configured = false;
-            }
-        }
-    }
-
-    private void loadPropertyFile(File file) throws IOException, 
FileNotFoundException {
-        Properties properties = new Properties();
-        properties.load(new FileReader(file));
-        _hostname = properties.getProperty("couchdb.hostname");
-        _databaseName = properties.getProperty("couchdb.databaseName");
-        if (_databaseName == null || _databaseName.isEmpty()) {
-            _databaseName = DEFAULT_TEST_DATABASE_NAME;
-        }
-
-        _configured = (_hostname != null && !_hostname.isEmpty());
-
-        if (_configured) {
-            System.out.println("Loaded CouchDB configuration. Hostname=" + 
_hostname + ", Database=" + _databaseName);
-        }
-    }
-
-    private String getPropertyFilePath() {
-        String userHome = System.getProperty("user.home");
-        return userHome + 
"/metamodel-integrationtest-configuration.properties";
-    }
-
-    protected String getInvalidConfigurationMessage() {
-        return "!!! WARN !!! CouchDB module ignored\r\n" + "Please configure 
couchdb connection locally ("
-                + getPropertyFilePath() + "), to run integration tests";
-    }
-
-    public boolean isConfigured() {
-        return _configured;
-    }
-
-    public String getHostname() {
-        return _hostname;
-    }
-
-    public String getDatabaseName() {
-        return _databaseName;
-    }
-}

http://git-wip-us.apache.org/repos/asf/metamodel/blob/484cbb1d/couchdb/src/test/java/org/apache/metamodel/couchdb/CouchDbTestSupport.java
----------------------------------------------------------------------
diff --git 
a/couchdb/src/test/java/org/apache/metamodel/couchdb/CouchDbTestSupport.java 
b/couchdb/src/test/java/org/apache/metamodel/couchdb/CouchDbTestSupport.java
new file mode 100644
index 0000000..b98cead
--- /dev/null
+++ b/couchdb/src/test/java/org/apache/metamodel/couchdb/CouchDbTestSupport.java
@@ -0,0 +1,91 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.couchdb;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Properties;
+
+public abstract class CouchDbTestSupport {
+
+    private static final String DEFAULT_TEST_DATABASE_NAME = 
"eobjects_metamodel_test";
+
+    private String _hostname;
+    private boolean _configured;
+    private String _databaseName;
+
+    public void loadConfiguration() throws Exception {
+        final File file = new File(getPropertyFilePath());
+        if (file.exists()) {
+            loadPropertyFile(file);
+        } else {
+            // Continuous integration case
+            if (System.getenv("CONTINUOUS_INTEGRATION") != null) {
+                File travisFile = new 
File("../travis-metamodel-integrationtest-configuration.properties");
+                if (travisFile.exists()) {
+                    loadPropertyFile(travisFile);
+                } else {
+                    _configured = false;
+                }
+            } else {
+                _configured = false;
+            }
+        }
+    }
+
+    private void loadPropertyFile(File file) throws IOException, 
FileNotFoundException {
+        Properties properties = new Properties();
+        properties.load(new FileReader(file));
+        _hostname = properties.getProperty("couchdb.hostname");
+        _databaseName = properties.getProperty("couchdb.databaseName");
+        if (_databaseName == null || _databaseName.isEmpty()) {
+            _databaseName = DEFAULT_TEST_DATABASE_NAME;
+        }
+
+        _configured = (_hostname != null && !_hostname.isEmpty());
+
+        if (_configured) {
+            System.out.println("Loaded CouchDB configuration. Hostname=" + 
_hostname + ", Database=" + _databaseName);
+        }
+    }
+
+    private String getPropertyFilePath() {
+        String userHome = System.getProperty("user.home");
+        return userHome + 
"/metamodel-integrationtest-configuration.properties";
+    }
+
+    protected String getInvalidConfigurationMessage() {
+        return "!!! WARN !!! CouchDB module ignored\r\n" + "Please configure 
couchdb connection locally ("
+                + getPropertyFilePath() + "), to run integration tests";
+    }
+
+    public boolean isConfigured() {
+        return _configured;
+    }
+
+    public String getHostname() {
+        return _hostname;
+    }
+
+    public String getDatabaseName() {
+        return _databaseName;
+    }
+}

Reply via email to