diff options
author | Gabor Pali <gabor.pali@ibm.com> | 2023-03-24 16:40:48 +0100 |
---|---|---|
committer | Nick Vatamaniuc <nickva@users.noreply.github.com> | 2023-04-18 23:51:32 -0400 |
commit | f5371aab79d6d241a6b28d04c0e67482804a9cf7 (patch) | |
tree | c5479997a33cd32197a887f6f325dfb31ba238b2 | |
parent | e6af4924c69f31976c39bfb39d03a3ee5772c4f0 (diff) | |
download | couchdb-f5371aab79d6d241a6b28d04c0e67482804a9cf7.tar.gz |
mango: add integration tests for keys-only covering indexes
-rw-r--r-- | src/mango/test/22-covering-index-test.py | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/mango/test/22-covering-index-test.py b/src/mango/test/22-covering-index-test.py new file mode 100644 index 000000000..b2f0202ed --- /dev/null +++ b/src/mango/test/22-covering-index-test.py @@ -0,0 +1,115 @@ +# 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 mango + + +class CoveringIndexTests(mango.UserDocsTests): + def is_covered(self, selector, fields, index, use_index=None): + resp = self.db.find(selector, fields=fields, use_index=use_index, explain=True) + self.assertEqual(resp["index"]["type"], "json") + self.assertEqual(resp["index"]["name"], index) + self.assertEqual(resp["mrargs"]["include_docs"], False) + self.assertEqual(resp["covered"], True) + + def is_not_covered(self, selector, fields): + resp = self.db.find(selector, fields=fields, explain=True) + self.assertEqual(resp["mrargs"]["include_docs"], True) + self.assertEqual(resp["covered"], False) + + def test_index_covers_query_1field_index_id(self): + self.is_covered({"age": {"$gte": 32}}, ["_id"], "age") + + def test_index_covers_query_2field_index_id(self): + self.is_covered( + {"company": "Lyria", "manager": True}, ["_id"], "company_and_manager" + ) + + def test_index_covers_query_2field_index_extract_field(self): + self.is_covered( + {"company": {"$exists": True}, "manager": True}, + ["company"], + "company_and_manager", + ) + + def test_index_covers_query_2field_index_extract_field_force_index(self): + self.is_covered( + {"company": {"$exists": True}, "manager": True}, + ["company"], + "company_and_manager", + use_index="company_and_manager", + ) + + def test_index_covers_query_elemMatch(self): + self.is_covered( + {"favorites": {"$elemMatch": {"$eq": "Erlang"}}}, ["favorites"], "favorites" + ) + + def test_index_covers_query_composite_field_id(self): + self.is_covered( + {"name": {"first": "Stephanie", "last": "Kirkland"}}, ["_id"], "name" + ) + + def test_index_does_not_cover_query_empty_selector(self): + self.is_not_covered({}, ["_id"]) + + def test_index_does_not_cover_query_field_not_in_index(self): + self.is_not_covered({"age": {"$gte": 32}}, ["name"]) + + def test_index_does_not_cover_query_all_fields(self): + self.is_not_covered({"age": {"$gte": 32}}, None) + + def test_index_does_not_cover_query_partial_selector_id(self): + self.is_not_covered({"location.state": "Nevada"}, ["_id"]) + + def test_index_does_not_cover_query_partial_selector(self): + self.is_not_covered({"name.last": "Hernandez"}, ["name.first"]) + + def test_covering_index_provides_correct_answer_id(self): + docs = self.db.find({"age": {"$gte": 32}}, fields=["_id"]) + expected = [ + {"_id": "659d0430-b1f4-413a-a6b7-9ea1ef071325"}, + {"_id": "48ca0455-8bd0-473f-9ae2-459e42e3edd1"}, + {"_id": "e900001d-bc48-48a6-9b1a-ac9a1f5d1a03"}, + {"_id": "b31dad3f-ae8b-4f86-8327-dfe8770beb27"}, + {"_id": "71562648-6acb-42bc-a182-df6b1f005b09"}, + {"_id": "c78c529f-0b07-4947-90a6-d6b7ca81da62"}, + {"_id": "8e1c90c0-ac18-4832-8081-40d14325bde0"}, + {"_id": "6c0afcf1-e57e-421d-a03d-0c0717ebf843"}, + {"_id": "5b61abc1-a3d3-4092-b9d7-ced90e675536"}, + {"_id": "a33d5457-741a-4dce-a217-3eab28b24e3e"}, + {"_id": "b06aadcf-cd0f-4ca6-9f7e-2c993e48d4c4"}, + {"_id": "b1e70402-8add-4068-af8f-b4f3d0feb049"}, + {"_id": "0461444c-e60a-457d-a4bb-b8d811853f21"}, + ] + self.assertEqual(docs, expected) + + def test_covering_index_provides_correct_answer_2field_index(self): + docs = self.db.find( + {"company": {"$exists": True}, "manager": True}, + sort=[{"company": "asc"}], + fields=["company"], + use_index="company_and_manager", + ) + expected = [ + {"company": "Affluex"}, + {"company": "Globoil"}, + {"company": "Lyria"}, + {"company": "Manglo"}, + {"company": "Myopium"}, + {"company": "Niquent"}, + {"company": "Oulu"}, + {"company": "Prosely"}, + {"company": "Tasmania"}, + {"company": "Zialactic"}, + ] + self.assertEqual(docs, expected) |