Skip to content

Commit

Permalink
Mango text index test fixes (#894)
Browse files Browse the repository at this point in the history
* Split out text index selection tests
* Skip operator tests that do not apply to text indexes
* Only run array length tests against text indexes
* Fix index crud tests when text indexes available
* Use environment variable to switch on text tests
* Fix incorrect text sort assertion in test
* Always use -test in fixture filename
* Fix index selection test compatibility with #816.
* Improve test README
  • Loading branch information
willholley authored Oct 17, 2017
1 parent 84edbb7 commit 464a6b1
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 148 deletions.
126 changes: 83 additions & 43 deletions src/mango/test/01-index-crud-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_bad_fields(self):
try:
self.db.create_index(fields)
except Exception as e:
assert e.response.status_code == 400
self.assertEqual(e.response.status_code, 400)
else:
raise AssertionError("bad create index")

Expand All @@ -55,7 +55,7 @@ def test_bad_types(self):
try:
self.db.create_index(["foo"], idx_type=bt)
except Exception as e:
assert e.response.status_code == 400, (bt, e.response.status_code)
self.assertEqual(e.response.status_code, 400, (bt, e.response.status_code))
else:
raise AssertionError("bad create index")

Expand All @@ -71,13 +71,13 @@ def test_bad_names(self):
try:
self.db.create_index(["foo"], name=bn)
except Exception as e:
assert e.response.status_code == 400
self.assertEqual(e.response.status_code, 400)
else:
raise AssertionError("bad create index")
try:
self.db.create_index(["foo"], ddoc=bn)
except Exception as e:
assert e.response.status_code == 400
self.assertEqual(e.response.status_code, 400)
else:
raise AssertionError("bad create index")

Expand All @@ -88,7 +88,7 @@ def test_create_idx_01(self):
for idx in self.db.list_indexes():
if idx["name"] != "idx_01":
continue
assert idx["def"]["fields"] == [{"foo": "asc"}, {"bar": "asc"}]
self.assertEqual(idx["def"]["fields"], [{"foo": "asc"}, {"bar": "asc"}])
return
raise AssertionError("index not created")

Expand All @@ -106,7 +106,7 @@ def test_create_idx_02(self):
for idx in self.db.list_indexes():
if idx["name"] != "idx_02":
continue
assert idx["def"]["fields"] == [{"baz": "asc"}, {"foo": "asc"}]
self.assertEqual(idx["def"]["fields"], [{"baz": "asc"}, {"foo": "asc"}])
return
raise AssertionError("index not created")

Expand All @@ -118,9 +118,9 @@ def test_read_idx_doc(self):
continue
ddocid = idx["ddoc"]
doc = self.db.open_doc(ddocid)
assert doc["_id"] == ddocid
self.assertEqual(doc["_id"], ddocid)
info = self.db.ddoc_info(ddocid)
assert info["name"] == ddocid.split('_design/')[-1]
self.assertEqual(info["name"], ddocid.split('_design/')[-1])

def test_delete_idx_escaped(self):
self.db.create_index(["foo", "bar"], name="idx_01")
Expand All @@ -130,10 +130,10 @@ def test_delete_idx_escaped(self):
for idx in self.db.list_indexes():
if idx["name"] != "idx_del_1":
continue
assert idx["def"]["fields"] == [{"bing": "asc"}]
self.assertEqual(idx["def"]["fields"], [{"bing": "asc"}])
self.db.delete_index(idx["ddoc"].replace("/", "%2F"), idx["name"])
post_indexes = self.db.list_indexes()
assert pre_indexes == post_indexes
self.assertEqual(pre_indexes, post_indexes)

def test_delete_idx_unescaped(self):
pre_indexes = self.db.list_indexes()
Expand All @@ -142,10 +142,10 @@ def test_delete_idx_unescaped(self):
for idx in self.db.list_indexes():
if idx["name"] != "idx_del_2":
continue
assert idx["def"]["fields"] == [{"bing": "asc"}]
self.assertEqual(idx["def"]["fields"], [{"bing": "asc"}])
self.db.delete_index(idx["ddoc"], idx["name"])
post_indexes = self.db.list_indexes()
assert pre_indexes == post_indexes
self.assertEqual(pre_indexes, post_indexes)

def test_delete_idx_no_design(self):
pre_indexes = self.db.list_indexes()
Expand All @@ -154,10 +154,10 @@ def test_delete_idx_no_design(self):
for idx in self.db.list_indexes():
if idx["name"] != "idx_del_3":
continue
assert idx["def"]["fields"] == [{"bing": "asc"}]
self.assertEqual(idx["def"]["fields"], [{"bing": "asc"}])
self.db.delete_index(idx["ddoc"].split("/")[-1], idx["name"])
post_indexes = self.db.list_indexes()
assert pre_indexes == post_indexes
self.assertEqual(pre_indexes, post_indexes)

def test_bulk_delete(self):
fields = ["field1"]
Expand All @@ -182,8 +182,8 @@ def test_bulk_delete(self):

ret = self.db.bulk_delete(docids)

assert ret["fail"][0]["id"] == "_design/this_is_not_an_index_name"
assert len(ret["success"]) == 3
self.assertEqual(ret["fail"][0]["id"], "_design/this_is_not_an_index_name")
self.assertEqual(len(ret["success"]), 3)

for idx in self.db.list_indexes():
assert idx["type"] != "json"
Expand All @@ -197,18 +197,18 @@ def test_recreate_index(self):
for idx in self.db.list_indexes():
if idx["name"] != "idx_recreate":
continue
assert idx["def"]["fields"] == [{"bing": "asc"}]
self.assertEqual(idx["def"]["fields"], [{"bing": "asc"}])
self.db.delete_index(idx["ddoc"], idx["name"])
break
post_indexes = self.db.list_indexes()
assert pre_indexes == post_indexes
self.assertEqual(pre_indexes, post_indexes)

def test_delete_misisng(self):
def test_delete_missing(self):
# Missing design doc
try:
self.db.delete_index("this_is_not_a_design_doc_id", "foo")
except Exception as e:
assert e.response.status_code == 404
self.assertEqual(e.response.status_code, 404)
else:
raise AssertionError("bad index delete")

Expand All @@ -221,19 +221,62 @@ def test_delete_misisng(self):
try:
self.db.delete_index(ddocid, "this_is_not_an_index_name")
except Exception as e:
assert e.response.status_code == 404
self.assertEqual(e.response.status_code, 404)
else:
raise AssertionError("bad index delete")

# Bad view type
try:
self.db.delete_index(ddocid, idx["name"], idx_type="not_a_real_type")
except Exception as e:
assert e.response.status_code == 404
self.assertEqual(e.response.status_code, 404)
else:
raise AssertionError("bad index delete")

@unittest.skipUnless(mango.has_text_service(), "requires text service")
def test_limit_skip_index(self):
fields = ["field1"]
ret = self.db.create_index(fields, name="idx_01")
assert ret is True

fields = ["field2"]
ret = self.db.create_index(fields, name="idx_02")
assert ret is True

fields = ["field3"]
ret = self.db.create_index(fields, name="idx_03")
assert ret is True

fields = ["field4"]
ret = self.db.create_index(fields, name="idx_04")
assert ret is True

fields = ["field5"]
ret = self.db.create_index(fields, name="idx_05")
assert ret is True

self.assertEqual(len(self.db.list_indexes(limit=2)), 2)
self.assertEqual(len(self.db.list_indexes(limit=5,skip=4)), 2)
self.assertEqual(len(self.db.list_indexes(skip=5)), 1)
self.assertEqual(len(self.db.list_indexes(skip=6)), 0)
self.assertEqual(len(self.db.list_indexes(skip=100)), 0)
self.assertEqual(len(self.db.list_indexes(limit=10000000)), 6)

try:
self.db.list_indexes(skip=-1)
except Exception as e:
self.assertEqual(e.response.status_code, 500)

try:
self.db.list_indexes(limit=0)
except Exception as e:
self.assertEqual(e.response.status_code, 500)


@unittest.skipUnless(mango.has_text_service(), "requires text service")
class IndexCrudTextTests(mango.DbPerClass):
def setUp(self):
self.db.recreate()

def test_create_text_idx(self):
fields = [
{"name":"stringidx", "type" : "string"},
Expand All @@ -244,14 +287,13 @@ def test_create_text_idx(self):
for idx in self.db.list_indexes():
if idx["name"] != "text_idx_01":
continue
assert idx["def"]["fields"] == [
self.assertEqual(idx["def"]["fields"], [
{"stringidx": "string"},
{"booleanidx": "boolean"}
]
])
return
raise AssertionError("index not created")

@unittest.skipUnless(mango.has_text_service(), "requires text service")
def test_create_bad_text_idx(self):
bad_fields = [
True,
Expand All @@ -270,10 +312,10 @@ def test_create_bad_text_idx(self):
try:
self.db.create_text_index(fields=fields)
except Exception as e:
assert e.response.status_code == 400
self.assertEqual(e.response.status_code, 400)
else:
raise AssertionError("bad create text index")

def test_limit_skip_index(self):
fields = ["field1"]
ret = self.db.create_index(fields, name="idx_01")
Expand All @@ -291,28 +333,26 @@ def test_limit_skip_index(self):
ret = self.db.create_index(fields, name="idx_04")
assert ret is True

fields = ["field5"]
ret = self.db.create_index(fields, name="idx_05")
fields = [
{"name":"stringidx", "type" : "string"},
{"name":"booleanidx", "type": "boolean"}
]
ret = self.db.create_text_index(fields=fields, name="idx_05")
assert ret is True

skip_add = 0

if mango.has_text_service():
skip_add = 1

assert len(self.db.list_indexes(limit=2)) == 2
assert len(self.db.list_indexes(limit=5,skip=4)) == 2 + skip_add
assert len(self.db.list_indexes(skip=5)) == 1 + skip_add
assert len(self.db.list_indexes(skip=6)) == 0 + skip_add
assert len(self.db.list_indexes(skip=100)) == 0
assert len(self.db.list_indexes(limit=10000000)) == 6 + skip_add
self.assertEqual(len(self.db.list_indexes(limit=2)), 2)
self.assertEqual(len(self.db.list_indexes(limit=5,skip=4)), 2)
self.assertEqual(len(self.db.list_indexes(skip=5)), 1)
self.assertEqual(len(self.db.list_indexes(skip=6)), 0)
self.assertEqual(len(self.db.list_indexes(skip=100)), 0)
self.assertEqual(len(self.db.list_indexes(limit=10000000)), 6)

try:
self.db.list_indexes(skip=-1)
except Exception as e:
assert e.response.status_code == 500
self.assertEqual(e.response.status_code, 500)

try:
self.db.list_indexes(limit=0)
except Exception as e:
assert e.response.status_code == 500
self.assertEqual(e.response.status_code, 500)
5 changes: 5 additions & 0 deletions src/mango/test/03-operator-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ def test_ne_includes_null_but_not_missing(self):
for d in docs:
self.assertIn("twitter", d)

# ideally this work be consistent across index types but, alas, it is not
@unittest.skipUnless(not mango.has_text_service(),
"text indexes do not support range queries across type boundaries")
def test_lt_includes_null_but_not_missing(self):
docs = self.db.find({
"twitter": {"$lt": 1}
Expand All @@ -183,6 +186,8 @@ def test_lt_includes_null_but_not_missing(self):
for d in docs:
self.assertEqual(d["twitter"], None)

@unittest.skipUnless(not mango.has_text_service(),
"text indexes do not support range queries across type boundaries")
def test_lte_includes_null_but_not_missing(self):
docs = self.db.find({
"twitter": {"$lt": 1}
Expand Down
Loading

0 comments on commit 464a6b1

Please sign in to comment.