You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ga...@apache.org on 2017/09/05 13:18:18 UTC

[couchdb] branch master updated: Improve Mango operator tests (#792)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 7284b68  Improve Mango operator tests (#792)
7284b68 is described below

commit 7284b684f3e3a223e67946bce45424b4e3d73a62
Author: Will Holley <wi...@gmail.com>
AuthorDate: Tue Sep 5 14:18:16 2017 +0100

    Improve Mango operator tests (#792)
    
    Clarify behaviour for null / missing fields. Convert tests
    to unittest assertions for clearer errors.
---
 src/mango/test/03-operator-test.py | 93 ++++++++++++++++++++++++++++----------
 src/mango/test/user_docs.py        |  1 +
 2 files changed, 70 insertions(+), 24 deletions(-)

diff --git a/src/mango/test/03-operator-test.py b/src/mango/test/03-operator-test.py
index edfd95f..76e00cc 100644
--- a/src/mango/test/03-operator-test.py
+++ b/src/mango/test/03-operator-test.py
@@ -15,23 +15,27 @@ import mango
 
 class OperatorTests(mango.UserDocsTests):
 
+    def assertUserIds(self, user_ids, docs):
+        user_ids_returned = list(d["user_id"] for d in docs)
+        user_ids.sort()
+        user_ids_returned.sort()
+        self.assertEqual(user_ids_returned, user_ids)
+
     def test_all(self):
         docs = self.db.find({
                 "manager": True,
                 "favorites": {"$all": ["Lisp", "Python"]}
             })
-        assert len(docs) == 4
-        assert docs[0]["user_id"] == 2
-        assert docs[1]["user_id"] == 12
-        assert docs[2]["user_id"] == 9
-        assert docs[3]["user_id"] == 14
+        self.assertEqual(len(docs), 4)
+        user_ids = [2,12,9,14]
+        self.assertUserIds(user_ids, docs)
 
     def test_all_non_array(self):
         docs = self.db.find({
                 "manager": True,
                 "location": {"$all": ["Ohai"]}
             })
-        assert len(docs) == 0
+        self.assertEqual(len(docs), 0)
 
     def test_elem_match(self):
         emdocs = [
@@ -58,8 +62,8 @@ class OperatorTests(mango.UserDocsTests):
                 "bam": True
             }}
         })
-        assert len(docs) == 1
-        assert docs[0]["user_id"] == "b"
+        self.assertEqual(len(docs), 1)
+        self.assertEqual(docs[0]["user_id"], "b")
 
     def test_all_match(self):
         amdocs = [
@@ -97,8 +101,8 @@ class OperatorTests(mango.UserDocsTests):
                 "bar": {"$mod": [2,0]}
             }}
         })
-        assert len(docs) == 1
-        assert docs[0]["user_id"] == "a"
+        self.assertEqual(len(docs), 1)
+        self.assertEqual(docs[0]["user_id"], "a")
     
     def test_empty_all_match(self):
         amdocs = [
@@ -113,43 +117,84 @@ class OperatorTests(mango.UserDocsTests):
                 "foo": {"$eq": 2}
             }}
         })
-        assert len(docs) == 0
+        self.assertEqual(len(docs), 0)
 
     def test_in_operator_array(self):
         docs = self.db.find({
                 "manager": True,
                 "favorites": {"$in": ["Ruby", "Python"]}
             })
-        assert len(docs) == 7
-        assert docs[0]["user_id"] == 2
-        assert docs[1]["user_id"] == 12
+        self.assertUserIds([2,6,7,9,11,12,14], docs)
 
     def test_nin_operator_array(self):
         docs = self.db.find({
                 "manager": True,
                 "favorites": {"$nin": ["Erlang", "Python"]}
             })
-        assert len(docs) == 4
+        self.assertEqual(len(docs), 4)
         for doc in docs:
             if isinstance(doc["favorites"], list):
-                assert "Erlang" not in doc["favorites"]
-                assert "Python" not in doc["favorites"]
+                self.assertNotIn("Erlang", doc["favorites"])
+                self.assertNotIn("Python", doc["favorites"])
 
     def test_regex(self):
         docs = self.db.find({
                 "age": {"$gt": 40},
                 "location.state": {"$regex": "(?i)new.*"}
             })
-        assert len(docs) == 2
-        assert docs[0]["user_id"] == 2
-        assert docs[1]["user_id"] == 10
+        self.assertEqual(len(docs), 2)
+        self.assertUserIds([2,10], docs)
 
     def test_exists_false(self):
         docs = self.db.find({
                 "age": {"$gt": 0},
                 "twitter": {"$exists": False}
             })
-        user_ids = [2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 14]
-        assert len(docs) == len(user_ids)
-        for doc in docs:
-            assert doc["user_id"] in user_ids
+        user_ids = [2,3,5,6,7,8,10,11,12,14]
+        self.assertUserIds(user_ids, docs)
+        for d in docs:
+            self.assertNotIn("twitter", d)
+
+    def test_eq_null_does_not_include_missing(self):
+        docs = self.db.find({
+                "age": {"$gt": 0},
+                "twitter": None
+            })
+        user_ids = [9]
+        self.assertUserIds(user_ids, docs)
+        for d in docs:
+            self.assertEqual(d["twitter"], None)
+
+    def test_ne_includes_null_but_not_missing(self):
+        docs = self.db.find({
+                "twitter": {"$ne": "notamatch"}
+            })
+        user_ids = [0,1,4,9,13]
+        self.assertUserIds(user_ids, docs)
+        for d in docs:
+            self.assertIn("twitter", d)
+
+    def test_range_includes_null_but_not_missing(self):
+        docs = self.db.find({
+                "twitter": {"$lt": 1}
+            })
+        user_ids = [9]
+        self.assertUserIds(user_ids, docs)
+        for d in docs:
+            self.assertEqual(d["twitter"], None)
+
+    def test_range_gte_null_includes_null_but_not_missing(self):
+        docs = self.db.find({
+                "twitter": {"$gte": None}
+            })
+        self.assertGreater(len(docs), 0)
+        for d in docs:
+            self.assertIn("twitter", d)
+
+    def test_exists_false_returns_missing_but_not_null(self):
+        docs = self.db.find({
+                "twitter": {"$exists": False}
+            })
+        self.assertGreater(len(docs), 0)
+        for d in docs:
+            self.assertNotIn("twitter", d)
diff --git a/src/mango/test/user_docs.py b/src/mango/test/user_docs.py
index 41d8602..01105e2 100644
--- a/src/mango/test/user_docs.py
+++ b/src/mango/test/user_docs.py
@@ -354,6 +354,7 @@ DOCS = [
         "company": "Manglo",
         "email": "ramonafloyd@manglo.com",
         "manager": True,
+        "twitter": None,
         "favorites": [
             "Lisp",
             "Erlang",

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