couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tonysu...@apache.org
Subject couchdb-mango git commit: Fix comparison operators for strings
Date Tue, 15 Sep 2015 01:18:29 GMT
Repository: couchdb-mango
Updated Branches:
  refs/heads/2808-fix-comparison [created] bb595d010


Fix comparison operators for strings

The comparison operators $lt, $lte, $gt, and $gte were incorrectly
using lower bound and upper bound values when comparing text.
For strings, we modify the the lower bound limit to be an empty string,
and the upper bound to be the highest unicode value.

COUCHDB-2808


Project: http://git-wip-us.apache.org/repos/asf/couchdb-mango/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mango/commit/bb595d01
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mango/tree/bb595d01
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mango/diff/bb595d01

Branch: refs/heads/2808-fix-comparison
Commit: bb595d01018f1f8e67623606f625a13ccf6cb239
Parents: f924032
Author: Tony Sun <tony.sun@cloudant.com>
Authored: Mon Sep 14 17:57:28 2015 -0700
Committer: Tony Sun <tony.sun@cloudant.com>
Committed: Mon Sep 14 18:17:53 2015 -0700

----------------------------------------------------------------------
 src/mango_selector_text.erl | 21 +++++++++++++++++----
 test/06-basic-text-test.py  | 16 ++++++++++++++++
 2 files changed, 33 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/bb595d01/src/mango_selector_text.erl
----------------------------------------------------------------------
diff --git a/src/mango_selector_text.erl b/src/mango_selector_text.erl
index 9450dfe..b6e1f09 100644
--- a/src/mango_selector_text.erl
+++ b/src/mango_selector_text.erl
@@ -267,13 +267,26 @@ make_field(Path, Arg) ->
 
 
 range(lt, Arg) ->
-    [<<"[-Infinity TO ">>, value_str(Arg), <<"}">>];
+    Min = get_range(min, Arg),
+    [<<"[", Min/binary, " TO ">>, value_str(Arg), <<"}">>];
 range(lte, Arg) ->
-    [<<"[-Infinity TO ">>, value_str(Arg), <<"]">>];
+    Min = get_range(min, Arg),
+    [<<"[", Min/binary, " TO ">>, value_str(Arg), <<"]">>];
 range(gte, Arg) ->
-    [<<"[">>, value_str(Arg), <<" TO Infinity]">>];
+    Max = get_range(max, Arg),
+    [<<"[">>, value_str(Arg), <<" TO ", Max/binary, "]">>];
 range(gt, Arg) ->
-    [<<"{">>, value_str(Arg), <<" TO Infinity]">>].
+    Max = get_range(max, Arg),
+    [<<"{">>, value_str(Arg), <<" TO ", Max/binary, "]">>].
+
+get_range(min, Arg) when is_number(Arg) ->
+    <<"-Infinity">>;
+get_range(min, _Arg) ->
+    <<"\"\"">>;
+get_range(max, Arg) when is_number(Arg) ->
+    <<"Infinity">>;
+get_range(max, _Arg) ->
+    <<"\u0x10FFFF">>.
 
 
 field_exists_query(Path) ->

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/bb595d01/test/06-basic-text-test.py
----------------------------------------------------------------------
diff --git a/test/06-basic-text-test.py b/test/06-basic-text-test.py
index c60779e..1e3d5df 100644
--- a/test/06-basic-text-test.py
+++ b/test/06-basic-text-test.py
@@ -96,6 +96,10 @@ class BasicTextTests(mango.UserDocsTextTests):
         for d in docs:
             assert d["user_id"] in (1, 7, 9)
 
+        docs = self.db.find({"company": {"$lt": "Dreamia"}})
+        assert len(docs) == 1
+        assert docs[0]["company"] == "Affluex"
+
     def test_lte(self):
         docs = self.db.find({"age": {"$lte": 21}})
         assert len(docs) == 0
@@ -109,6 +113,11 @@ class BasicTextTests(mango.UserDocsTextTests):
         for d in docs:
             assert d["user_id"] in (1, 7, 9)
 
+        docs = self.db.find({"company": {"$lte": "Dreamia"}})
+        assert len(docs) == 2
+        for d in docs:
+            assert d["user_id"] in (0, 11)
+
     def test_eq(self):
         docs = self.db.find({"age": 21})
         assert len(docs) == 0
@@ -149,6 +158,9 @@ class BasicTextTests(mango.UserDocsTextTests):
         docs = self.db.find({"age": {"$gt": 79}})
         assert len(docs) == 0
 
+        docs = self.db.find({"company": {"$gt": "Zialactic"}})
+        assert len(docs) == 0
+
     def test_gte(self):
         docs = self.db.find({"age": {"$gte": 77}})
         assert len(docs) == 2
@@ -167,6 +179,10 @@ class BasicTextTests(mango.UserDocsTextTests):
         docs = self.db.find({"age": {"$gte": 80}})
         assert len(docs) == 0
 
+        docs = self.db.find({"company": {"$gte": "Zialactic"}})
+        assert len(docs) == 1
+        assert docs[0]["company"] == "Zialactic"
+
     def test_and(self):
         docs = self.db.find({"age": 22, "manager": True})
         assert len(docs) == 1


Mime
View raw message