Return-Path: X-Original-To: apmail-couchdb-commits-archive@www.apache.org Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 026E6179A0 for ; Thu, 10 Sep 2015 00:36:18 +0000 (UTC) Received: (qmail 39207 invoked by uid 500); 10 Sep 2015 00:36:17 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 39029 invoked by uid 500); 10 Sep 2015 00:36:17 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 39011 invoked by uid 99); 10 Sep 2015 00:36:17 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 10 Sep 2015 00:36:17 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 79B3CE0991; Thu, 10 Sep 2015 00:36:17 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: tonysun83@apache.org To: commits@couchdb.apache.org Date: Thu, 10 Sep 2015 00:36:18 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [2/2] couchdb-mango git commit: Use hypothesis instead of random values for num_string tests Use hypothesis instead of random values for num_string tests COUCHDB-2787 Project: http://git-wip-us.apache.org/repos/asf/couchdb-mango/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mango/commit/2793b9f5 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mango/tree/2793b9f5 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mango/diff/2793b9f5 Branch: refs/heads/2787-modify-testcases Commit: 2793b9f50561dd4d9a271ce66aad29d7bfbad942 Parents: ba961dc Author: Tony Sun Authored: Wed Sep 9 15:10:21 2015 -0700 Committer: Tony Sun Committed: Wed Sep 9 15:13:37 2015 -0700 ---------------------------------------------------------------------- test/06-basic-text-test.py | 60 ++++++++++++++++++++++++++++------------- test/README.md | 1 + test/literal_gen.py | 49 --------------------------------- test/num_string_docs.py | 49 --------------------------------- 4 files changed, 43 insertions(+), 116 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/2793b9f5/test/06-basic-text-test.py ---------------------------------------------------------------------- diff --git a/test/06-basic-text-test.py b/test/06-basic-text-test.py index 4024042..53e9159 100644 --- a/test/06-basic-text-test.py +++ b/test/06-basic-text-test.py @@ -14,7 +14,10 @@ import json import mango import unittest import user_docs +import copy import num_string_docs +from hypothesis import given, assume +import hypothesis.strategies as st @unittest.skipIf(mango.has_text_service(), "text service exists") class TextIndexCheckTests(mango.DbPerClass): @@ -556,30 +559,51 @@ class ElemMatchTests(mango.FriendDocsTextTests): # Test numeric strings for $text @unittest.skipUnless(mango.has_text_service(), "requires text service") -class NumStringTests(mango.NumStringDocsTextTests): +class NumStringTests(mango.DbPerClass): - def test_floating_point_val(self): - float_point_string = num_string_docs.DOCS[2]["number_string"] - q = {"$text": float_point_string} - docs = self.db.find(q) - assert len(docs) == 1 - assert docs[0]["number_string"] == float_point_string - - def test_hex_floating_point_val(self): - hex_float_point_string = num_string_docs.DOCS[3]["number_string"] - q = {"$text": hex_float_point_string} - docs = self.db.find(q) - assert len(docs) == 1 - assert docs[0]["number_string"] == hex_float_point_string + @classmethod + def setUpClass(klass): + super(NumStringTests, klass).setUpClass() + klass.db.recreate() + klass.db.create_text_index() def test_nan_val(self): + doc = {"number_NaN": "NaN"} + self.db.save_doc(doc) q = {"$text": "NaN"} docs = self.db.find(q) - assert len(docs) == 1 - assert docs[0]["number_string"] == "NaN" + print docs + assert docs[0]["number_NaN"] == "NaN" def test_infinity_val(self): + doc = {"number_Infinity": "Infinity"} + self.db.save_doc(doc) q = {"$text": "Infinity"} docs = self.db.find(q) - assert len(docs) == 1 - assert docs[0]["number_string"] == "Infinity" + assert docs[0]["number_Infinity"] == "Infinity" + + @given(float_point_string=st.floats().map(str)) + def test_floating_point_val(self,float_point_string): + assume(float_point_string!="nan") + doc = {"number_string": float_point_string} + self.db.save_doc(doc) + q = {"$text": float_point_string} + docs = self.db.find(q) + if len(docs) == 1: + assert docs[0]["number_string"] == float_point_string + if len(docs) == 2: + if docs[0]["number_string"] != float_point_string: + assert docs[1]["number_string"] == float_point_string + + @given(f=st.floats()) + def test_floating_point_val(self,f): + hex_float_point_string = f.hex() + doc = {"number_string": hex_float_point_string} + self.db.save_doc(doc) + q = {"$text": hex_float_point_string} + docs = self.db.find(q) + if len(docs) == 1: + assert docs[0]["number_string"] == hex_float_point_string + if len(docs) == 2: + if docs[0]["number_string"] != hex_float_point_string: + assert docs[1]["number_string"] == hex_float_point_string http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/2793b9f5/test/README.md ---------------------------------------------------------------------- diff --git a/test/README.md b/test/README.md index 17abf23..ed9e0fc 100644 --- a/test/README.md +++ b/test/README.md @@ -8,4 +8,5 @@ To run these, do this in the top level directory: $ virtualenv venv $ source venv/bin/activate $ pip install nose requests + $ pip install hypothesis $ nosetests http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/2793b9f5/test/literal_gen.py ---------------------------------------------------------------------- diff --git a/test/literal_gen.py b/test/literal_gen.py deleted file mode 100644 index cf46017..0000000 --- a/test/literal_gen.py +++ /dev/null @@ -1,49 +0,0 @@ -# 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 random -from string import digits, hexdigits - -def joiner(*items): - return ''.join(item() for item in items) - -def roll(item, n1, n2=None): - n2 = n2 or n1 - return lambda: ''.join(item() for _ in xrange(random.randint(n1, n2))) - -def rand(collection): - return lambda: random.choice(collection) - -def floating_point_literal(): - return joiner(roll(rand(' '), 0, 2), - roll(rand('+-'), 0, 1), - roll(rand(digits), 2, 20), - rand('.'), - roll(rand(digits), 2, 20), - roll(rand('eE'), 1, 1), - roll(rand('+-'), 0, 1), - roll(rand(digits), 2, 20), - roll(rand('fF'), 0, 1), - roll(rand(' '), 0, 2)) - -def hex_floating_point_literal(): - return joiner(roll(rand(' '), 0, 2), - rand('0'), - roll(rand('xX'), 1, 1), - roll(rand(hexdigits), 1, 6), - rand('.'), - roll(rand(hexdigits), 1, 7), - roll(rand('pP'), 1, 1), - roll(rand('+-'), 0, 1), - roll(rand(digits), 1, 4), - roll(rand('fFdD'), 0, 1), - roll(rand(' '), 0, 2)) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/2793b9f5/test/num_string_docs.py ---------------------------------------------------------------------- diff --git a/test/num_string_docs.py b/test/num_string_docs.py deleted file mode 100644 index f900120..0000000 --- a/test/num_string_docs.py +++ /dev/null @@ -1,49 +0,0 @@ -# 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 copy -import literal_gen - - -def setup(db, index_type="view"): - db.recreate() - DOCS[2]["number_string"] = literal_gen.floating_point_literal() - DOCS[3]["number_string"] = literal_gen.hex_floating_point_literal() - db.save_docs(copy.deepcopy(DOCS)) - if index_type == "view": - add_view_indexes(db) - elif index_type == "text": - add_text_indexes(db) - - -def add_text_indexes(db): - db.create_text_index() - - -DOCS = [ - { - "_id": "55118b87283f8f2901c59663", - "number_string": "NaN" - }, - { - "_id": "55118b873c98123d69bff407", - "number_string": "Infinity" - }, - { - "_id": "55118b87b4e99951e6fbe5c4", - "number_string": "filler" - }, - { - "_id": "55118b87bc21952536ef00da", - "number_string": "filler" - } -] \ No newline at end of file