From a0cc22d805cf56274a6c155d0773630b0c53e0ca Mon Sep 17 00:00:00 2001 From: Tony Sun Date: Tue, 18 Aug 2015 13:55:42 -0700 Subject: [PATCH] Fix comparison operators The comparison operators for $lt and $gt were incorrectly using lower bound and upper bound values when comparing text. We modify the the lower bound limit to be an empty string, and the upper bound to be the highest unicode value. BugzId: 49262 --- src/mango_selector_text.erl | 21 +++++++++++++++++---- test/06-basic-text-test.py | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/mango_selector_text.erl b/src/mango_selector_text.erl index 27b8ee9..cb9f7b1 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) -> diff --git a/test/06-basic-text-test.py b/test/06-basic-text-test.py index a55f211..f292ba6 100644 --- a/test/06-basic-text-test.py +++ b/test/06-basic-text-test.py @@ -79,6 +79,10 @@ def test_lt(self): 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 @@ -92,6 +96,11 @@ def test_lte(self): 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 @@ -132,6 +141,9 @@ def test_gt(self): 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 @@ -150,6 +162,10 @@ def test_gte(self): 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