Skip to content

Commit

Permalink
test mongodb cache index
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsha committed May 31, 2023
1 parent 2564a19 commit 8bf41a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/cachelib/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ def __init__(

if client is None or isinstance(client, str):
client = pymongo.MongoClient(host=client)
client.ensure_index([("id", pymongo.ASCENDING)])
self.client = client[db][collection]
index_info = self.client.index_information()
all_keys = {
subkey[0] for value in index_info.values() for subkey in value["key"]
}
if "id" not in all_keys:
self.client.create_index("id", unique=True)
self.key_prefix = key_prefix or ""
self.collection = collection

Expand Down
5 changes: 5 additions & 0 deletions tests/test_mongodb_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def _factory(self, *args, **kwargs):
kwargs["key_prefix"] = "prefix"

rc = request.param(*args, **kwargs)
index_info = rc.client.index_information()
all_keys = {
subkey[0] for value in index_info.values() for subkey in value["key"]
}
assert "id" in all_keys, "Failed to create index on 'id' field"
rc.clear()
return rc

Expand Down

0 comments on commit 8bf41a8

Please sign in to comment.