couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vatam...@apache.org
Subject [couchdb] 04/04: Add tests for queries against _users db
Date Thu, 13 Jul 2017 16:28:22 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 779f00e2bd8fa6a59be112418801def559623c8a
Author: Eric Avdey <eiri@eiri.ca>
AuthorDate: Fri Jul 7 16:54:59 2017 -0300

    Add tests for queries against _users db
---
 src/mango/test/13-users-db-find-test.py | 83 +++++++++++++++++++++++++++++++++
 src/mango/test/mango.py                 | 11 +++++
 src/mango/test/user_docs.py             | 36 ++++++++++++++
 3 files changed, 130 insertions(+)

diff --git a/src/mango/test/13-users-db-find-test.py b/src/mango/test/13-users-db-find-test.py
new file mode 100644
index 0000000..d8d32ad
--- /dev/null
+++ b/src/mango/test/13-users-db-find-test.py
@@ -0,0 +1,83 @@
+# -*- coding: latin-1 -*-
+# Licensed 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.
+
+
+import mango, requests
+
+
+class UsersDbFindTests(mango.UsersDbTests):
+
+    def test_simple_find(self):
+        docs = self.db.find({"name": {"$eq": "demo02"}})
+        assert len(docs) == 1
+        assert docs[0]["_id"] == "org.couchdb.user:demo02"
+
+    def test_multi_cond_and(self):
+        self.db.create_index(["type", "roles"])
+        docs = self.db.find({"type": "user", "roles": {"$eq": ["reader"]}})
+        assert len(docs) == 1
+        assert docs[0]["_id"] == "org.couchdb.user:demo02"
+
+    def test_multi_cond_or(self):
+        docs = self.db.find({
+                "$and":[
+                    {"type": "user"},
+                    {"$or": [
+                        {"order": 1},
+                        {"order": 3}
+                    ]}
+                ]
+            })
+        assert len(docs) == 2
+        assert docs[0]["_id"] == "org.couchdb.user:demo01"
+        assert docs[1]["_id"] == "org.couchdb.user:demo03"
+
+    def test_sort(self):
+        self.db.create_index(["order", "name"])
+        selector = {"name": {"$gt": "demo01"}}
+        docs1 = self.db.find(selector, sort=[{"order": "asc"}])
+        docs2 = list(sorted(docs1, key=lambda d: d["order"]))
+        assert docs1 is not docs2 and docs1 == docs2
+
+        docs1 = self.db.find(selector, sort=[{"order": "desc"}])
+        docs2 = list(reversed(sorted(docs1, key=lambda d: d["order"])))
+        assert docs1 is not docs2 and docs1 == docs2
+
+    def test_fields(self):
+        selector = {"name": {"$eq": "demo02"}}
+        docs = self.db.find(selector, fields=["name", "order"])
+        assert len(docs) == 1
+        assert sorted(docs[0].keys()) == ["name", "order"]
+
+    def test_empty(self):
+        docs = self.db.find({})
+        assert len(docs) == 3
+
+
+class UsersDbIndexFindTests(UsersDbFindTests):
+
+    def setUp(self):
+        self.db.create_index(["name"])
+
+    def test_multi_cond_and(self):
+        self.db.create_index(["type", "roles"])
+        super(UsersDbIndexFindTests, self).test_multi_cond_and()
+
+    def test_multi_cond_or(self):
+        self.db.create_index(["type", "order"])
+        super(UsersDbIndexFindTests, self).test_multi_cond_or()
+
+    def test_sort(self):
+        self.db.create_index(["order", "name"])
+        super(UsersDbIndexFindTests, self).test_sort()
+
diff --git a/src/mango/test/mango.py b/src/mango/test/mango.py
index da51180..bd34edc 100644
--- a/src/mango/test/mango.py
+++ b/src/mango/test/mango.py
@@ -192,6 +192,17 @@ class Database(object):
             return None
 
 
+class UsersDbTests(unittest.TestCase):
+
+    @classmethod
+    def setUpClass(klass):
+        klass.db = Database("127.0.0.1", "15984", "_users")
+        user_docs.setup_users(klass.db)
+
+    def setUp(self):
+        self.db = self.__class__.db
+
+
 class DbPerClass(unittest.TestCase):
 
     @classmethod
diff --git a/src/mango/test/user_docs.py b/src/mango/test/user_docs.py
index e2f1705..41d8602 100644
--- a/src/mango/test/user_docs.py
+++ b/src/mango/test/user_docs.py
@@ -54,6 +54,11 @@ With this pattern:
 import copy
 
 
+def setup_users(db, **kwargs):
+    db.recreate()
+    db.save_docs(copy.deepcopy(USERS_DOCS))
+
+
 def setup(db, index_type="view", **kwargs):
     db.recreate()
     db.save_docs(copy.deepcopy(DOCS))
@@ -488,3 +493,34 @@ DOCS = [
         ]
     }
 ]
+
+
+USERS_DOCS = [
+    {
+        "_id": "org.couchdb.user:demo01",
+        "name": "demo01",
+        "username": "demo01",
+        "password": "apple01",
+        "roles": ["design"],
+        "order": 1,
+        "type": "user"
+    },
+    {
+        "_id": "org.couchdb.user:demo02",
+        "name": "demo02",
+        "username": "demo02",
+        "password": "apple02",
+        "roles": ["reader"],
+        "order": 2,
+        "type": "user"
+    },
+    {
+        "_id": "org.couchdb.user:demo03",
+        "name": "demo03",
+        "username": "demo03",
+        "password": "apple03",
+        "roles": ["reader", "writer"],
+        "order": 3,
+        "type": "user"
+    }
+]

-- 
To stop receiving notification emails like this one, please contact
"commits@couchdb.apache.org" <commits@couchdb.apache.org>.

Mime
View raw message