You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metamodel.apache.org by ka...@apache.org on 2019/05/01 19:20:37 UTC

[metamodel] 01/02: Added unit test which validates the working of a query which joins two tables and then uses a compound FilterItem in the where statement to filter out some more results.

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

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

commit 30095d826eecd572851eb89d1a77176f3f525e4c
Author: Arjan Seijkens <a....@quadient.com>
AuthorDate: Wed Apr 24 14:28:16 2019 +0200

    Added unit test which validates the working of a query which joins two tables and then uses a compound FilterItem in the where statement to filter out some more results.
---
 .../apache/metamodel/pojo/PojoDataContextTest.java | 44 +++++++++++++++++
 .../test/java/org/apache/metamodel/pojo/Role.java  | 45 +++++++++++++++++
 .../test/java/org/apache/metamodel/pojo/User.java  | 57 ++++++++++++++++++++++
 3 files changed, 146 insertions(+)

diff --git a/pojo/src/test/java/org/apache/metamodel/pojo/PojoDataContextTest.java b/pojo/src/test/java/org/apache/metamodel/pojo/PojoDataContextTest.java
index d520556..6d1638f 100644
--- a/pojo/src/test/java/org/apache/metamodel/pojo/PojoDataContextTest.java
+++ b/pojo/src/test/java/org/apache/metamodel/pojo/PojoDataContextTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.metamodel.pojo;
 
+import java.sql.Date;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -31,7 +32,13 @@ import org.apache.metamodel.DataContext;
 import org.apache.metamodel.UpdateCallback;
 import org.apache.metamodel.UpdateScript;
 import org.apache.metamodel.data.DataSet;
+import org.apache.metamodel.data.Row;
+import org.apache.metamodel.query.FilterItem;
+import org.apache.metamodel.query.LogicalOperator;
+import org.apache.metamodel.query.OperatorType;
 import org.apache.metamodel.query.Query;
+import org.apache.metamodel.query.SelectItem;
+import org.apache.metamodel.schema.Column;
 import org.apache.metamodel.schema.ColumnType;
 import org.apache.metamodel.schema.Schema;
 import org.apache.metamodel.schema.Table;
@@ -215,4 +222,41 @@ public class PojoDataContextTest extends TestCase {
         map.put("col3", c);
         return map;
     }
+
+    public void testJoinWithCompoundFilterItems() {
+        final List<Role> roles = new ArrayList<>();
+        roles.add(new Role(1, "admin"));
+        roles.add(new Role(2, "guest"));
+
+        final List<User> users = new ArrayList<>();
+        users.add(new User("pete", null, 1));
+        users.add(new User("jake", Date.valueOf("2018-1-1"), 1));
+        users.add(new User("susan", Date.valueOf("2020-1-1"), 2));
+
+        final DataContext dataContext = new PojoDataContext("userdb", new ObjectTableDataProvider<User>("users",
+                User.class, users), new ObjectTableDataProvider<Role>("roles", Role.class, roles));
+
+        final SelectItem expirationDateField = new SelectItem(dataContext
+                .getColumnByQualifiedLabel("userdb.users.expirationDate"));
+        final Column userRoleIdColumn = dataContext.getColumnByQualifiedLabel("userdb.users.roleId");
+        final Column roleIdColumn = dataContext.getColumnByQualifiedLabel("userdb.roles.id");
+
+        DataSet dataSet = dataContext
+                .query()
+                .from("users")
+                .and("roles")
+                .select("users.name")
+                .where(userRoleIdColumn)
+                .eq(roleIdColumn)
+                .where(new FilterItem(LogicalOperator.OR, new FilterItem(expirationDateField, OperatorType.EQUALS_TO,
+                        null), new FilterItem(expirationDateField, OperatorType.GREATER_THAN_OR_EQUAL, Date
+                                .valueOf("2019-1-1"))))
+                .orderBy("name")
+                .execute();
+
+        List<Row> rows = dataSet.toRows();
+        assertEquals(2, rows.size());
+        assertEquals("pete", rows.get(0).getValue(0));
+        assertEquals("susan", rows.get(1).getValue(0));
+    }
 }
\ No newline at end of file
diff --git a/pojo/src/test/java/org/apache/metamodel/pojo/Role.java b/pojo/src/test/java/org/apache/metamodel/pojo/Role.java
new file mode 100644
index 0000000..06a5f80
--- /dev/null
+++ b/pojo/src/test/java/org/apache/metamodel/pojo/Role.java
@@ -0,0 +1,45 @@
+/**
+ * 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.pojo;
+
+class Role {
+    private int id; 
+    private String name;
+
+    public Role(final int id, final String name) {
+        this.name = name;
+        this.id = id;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(final int id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(final String name) {
+        this.name = name;
+    }
+}
diff --git a/pojo/src/test/java/org/apache/metamodel/pojo/User.java b/pojo/src/test/java/org/apache/metamodel/pojo/User.java
new file mode 100644
index 0000000..37987c2
--- /dev/null
+++ b/pojo/src/test/java/org/apache/metamodel/pojo/User.java
@@ -0,0 +1,57 @@
+/**
+ * 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.pojo;
+
+import java.util.Date;
+
+class User {
+    private String name;
+    private Date expirationDate;
+    private int roleId;
+
+    public User(final String name, final Date expirationDate, final int roleId) {
+        this.name = name;
+        this.expirationDate = expirationDate;
+        this.roleId = roleId;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(final String name) {
+        this.name = name;
+    }
+
+    public Date getExpirationDate() {
+        return expirationDate;
+    }
+
+    public void setExpirationDate(final Date expirationDate) {
+        this.expirationDate = expirationDate;
+    }
+
+    public int getRoleId() {
+        return roleId;
+    }
+
+    public void setRole(final int roleId) {
+        this.roleId = roleId;
+    }
+}