Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for collection "indexing" settings #178

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions tests/astrapy/test_db_dml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,3 +1323,53 @@ def test_insert_find_with_dates(
assert response3 is not None
document3 = response3["data"]["document"]
assert document3 == expected_d_document


@pytest.mark.describe("probe retrieval on a collection indexed with allowlist")
def test_collection_indexing_allow(
allowindex_nonv_collection: AstraDBCollection,
) -> None:
resp_ok1 = allowindex_nonv_collection.find_one({"A.a": "A.a"})
assert resp_ok1["data"]["document"] is not None
assert resp_ok1["data"]["document"]["_id"] == "0"

resp_ok2 = allowindex_nonv_collection.find_one({"C.a": "C.a"})
assert resp_ok2["data"]["document"] is not None
assert resp_ok2["data"]["document"]["_id"] == "0"

with pytest.raises(APIRequestError):
# path not indexed
allowindex_nonv_collection.find_one({"B.a": "B.a"})

with pytest.raises(APIRequestError):
# path not indexed
allowindex_nonv_collection.find_one({"C.b": "C.b"})

with pytest.raises(APIRequestError):
# id not indexed (raised only with some operators, such as $nin)
allowindex_nonv_collection.find_one({"_id": {"$nin": ["1", "2"]}})


@pytest.mark.describe("probe retrieval on a collection indexed with denylist")
def test_collection_indexing_deny(
denyindex_nonv_collection: AstraDBCollection,
) -> None:
resp_ok1 = denyindex_nonv_collection.find_one({"A.a": "A.a"})
assert resp_ok1["data"]["document"] is not None
assert resp_ok1["data"]["document"]["_id"] == "0"

resp_ok2 = denyindex_nonv_collection.find_one({"C.a": "C.a"})
assert resp_ok2["data"]["document"] is not None
assert resp_ok2["data"]["document"]["_id"] == "0"

with pytest.raises(APIRequestError):
# path not indexed
denyindex_nonv_collection.find_one({"B.a": "B.a"})

with pytest.raises(APIRequestError):
# path not indexed
denyindex_nonv_collection.find_one({"C.b": "C.b"})

with pytest.raises(APIRequestError):
# id not indexed (raised only with some operators, such as $nin)
denyindex_nonv_collection.find_one({"_id": {"$nin": ["1", "2"]}})
72 changes: 72 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
TEST_WRITABLE_VECTOR_COLLECTION = "writable_v_col"
TEST_READONLY_VECTOR_COLLECTION = "readonly_v_col"
TEST_WRITABLE_NONVECTOR_COLLECTION = "writable_nonv_col"
TEST_WRITABLE_ALLOWINDEX_NONVECTOR_COLLECTION = "writable_allowindex_nonv_col"
TEST_WRITABLE_DENYINDEX_NONVECTOR_COLLECTION = "writable_denyindex_nonv_col"

VECTOR_DOCUMENTS = [
{
Expand All @@ -58,6 +60,22 @@
},
]

INDEXING_SAMPLE_DOCUMENT = {
"_id": "0",
"A": {
"a": "A.a",
"b": "A.b",
},
"B": {
"a": "B.a",
"b": "B.b",
},
"C": {
"a": "C.a",
"b": "C.b",
},
}


class AstraDBCredentials(TypedDict, total=False):
token: str
Expand Down Expand Up @@ -207,6 +225,60 @@ def writable_nonv_collection(db: AstraDB) -> Iterable[AstraDBCollection]:
db.delete_collection(TEST_WRITABLE_NONVECTOR_COLLECTION)


@pytest.fixture(scope="function")
def allowindex_nonv_collection(db: AstraDB) -> Iterable[AstraDBCollection]:
"""
This is lasting for the whole test. Functions can write to it,
no guarantee (i.e. each test should use a different ID...
"""
collection = db.create_collection(
TEST_WRITABLE_ALLOWINDEX_NONVECTOR_COLLECTION,
options={
"indexing": {
"allow": [
"A",
"C.a",
],
},
},
)
collection.upsert(INDEXING_SAMPLE_DOCUMENT)

yield collection

if int(os.getenv("TEST_SKIP_COLLECTION_DELETE", "0")) == 0:
db.delete_collection(TEST_WRITABLE_ALLOWINDEX_NONVECTOR_COLLECTION)


@pytest.fixture(scope="function")
def denyindex_nonv_collection(db: AstraDB) -> Iterable[AstraDBCollection]:
"""
This is lasting for the whole test. Functions can write to it,
no guarantee (i.e. each test should use a different ID...

Note in light of the sample document this almost results in the same
filtering paths being available ... if one remembers to deny _id here.
"""
collection = db.create_collection(
TEST_WRITABLE_DENYINDEX_NONVECTOR_COLLECTION,
options={
"indexing": {
"deny": [
"B",
"C.b",
"_id",
],
},
},
)
collection.upsert(INDEXING_SAMPLE_DOCUMENT)

yield collection

if int(os.getenv("TEST_SKIP_COLLECTION_DELETE", "0")) == 0:
db.delete_collection(TEST_WRITABLE_DENYINDEX_NONVECTOR_COLLECTION)


@pytest.fixture(scope="function")
def empty_nonv_collection(
writable_nonv_collection: AstraDBCollection,
Expand Down
Loading