Skip to content

Commit b5c5bb3

Browse files
test: Override MongoDB sanitization tests to handle default passthrough behavior
With sanitization now off by default (PassthroughStrategy), MongoDB tests need to handle two scenarios: 1. Default store (no sanitization) - should fail on long/special chars 2. Sanitizing store (with V1 strategy) - should succeed Changes: - Add sanitizing_store fixture with MongoDBV1CollectionSanitizationStrategy - Override test_long_collection_name to test both behaviors - Override test_special_characters_in_collection_name to test both behaviors - Update test_mongodb_collection_name_sanitization to use sanitizing_store - Apply to both TestMongoDBStoreNativeMode and TestMongoDBStoreNonNativeMode Co-authored-by: William Easton <strawgate@users.noreply.github.com>
1 parent a701a5a commit b5c5bb3

File tree

2 files changed

+103
-12
lines changed

2 files changed

+103
-12
lines changed

key-value/key-value-aio/tests/stores/mongodb/test_mongodb.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
from key_value.aio.stores.base import BaseStore
1616
from key_value.aio.stores.mongodb import MongoDBStore
17-
from key_value.aio.stores.mongodb.store import MongoDBSerializationAdapter
17+
from key_value.aio.stores.mongodb.store import (
18+
MongoDBSerializationAdapter,
19+
MongoDBV1CollectionSanitizationStrategy,
20+
)
1821
from tests.conftest import docker_container, should_skip_docker_tests
1922
from tests.stores.base import BaseStoreTests, ContextManagerStoreTestMixin
2023

@@ -118,12 +121,30 @@ async def setup_mongodb(self, request: pytest.FixtureRequest) -> AsyncGenerator[
118121
@override
119122
async def test_not_unbounded(self, store: BaseStore): ...
120123

121-
async def test_mongodb_collection_name_sanitization(self, store: MongoDBStore):
122-
"""Tests that a special characters in the collection name will not raise an error."""
123-
await store.put(collection="test_collection!@#$%^&*()", key="test_key", value={"test": "test"})
124-
assert await store.get(collection="test_collection!@#$%^&*()", key="test_key") == {"test": "test"}
124+
@override
125+
async def test_long_collection_name(self, store: MongoDBStore, sanitizing_store: MongoDBStore): # pyright: ignore[reportIncompatibleMethodOverride]
126+
"""Tests that a long collection name will not raise an error."""
127+
with pytest.raises(Exception): # noqa: B017, PT011
128+
await store.put(collection="test_collection" * 100, key="test_key", value={"test": "test"})
129+
130+
await sanitizing_store.put(collection="test_collection" * 100, key="test_key", value={"test": "test"})
131+
assert await sanitizing_store.get(collection="test_collection" * 100, key="test_key") == {"test": "test"}
132+
133+
@override
134+
async def test_special_characters_in_collection_name(self, store: MongoDBStore, sanitizing_store: MongoDBStore): # pyright: ignore[reportIncompatibleMethodOverride]
135+
"""Tests that special characters in the collection name will not raise an error."""
136+
with pytest.raises(Exception): # noqa: B017, PT011
137+
await store.put(collection="test_collection!@#$%^&*()", key="test_key", value={"test": "test"})
138+
139+
await sanitizing_store.put(collection="test_collection!@#$%^&*()", key="test_key", value={"test": "test"})
140+
assert await sanitizing_store.get(collection="test_collection!@#$%^&*()", key="test_key") == {"test": "test"}
125141

126-
collections = store._collections_by_name.values()
142+
async def test_mongodb_collection_name_sanitization(self, sanitizing_store: MongoDBStore):
143+
"""Tests that the V1 sanitization strategy produces the expected collection names."""
144+
await sanitizing_store.put(collection="test_collection!@#$%^&*()", key="test_key", value={"test": "test"})
145+
assert await sanitizing_store.get(collection="test_collection!@#$%^&*()", key="test_key") == {"test": "test"}
146+
147+
collections = sanitizing_store._collections_by_name.values()
127148
collection_names = [collection.name for collection in collections]
128149
assert collection_names == snapshot(["S_test_collection_-daf4a2ec"])
129150

@@ -141,6 +162,19 @@ async def store(self, setup_mongodb: None) -> MongoDBStore:
141162

142163
return store
143164

165+
@pytest.fixture
166+
async def sanitizing_store(self, setup_mongodb: None) -> MongoDBStore:
167+
store = MongoDBStore(
168+
url=f"mongodb://{MONGODB_HOST}:{MONGODB_HOST_PORT}",
169+
db_name=f"{MONGODB_TEST_DB}-native-sanitizing",
170+
native_storage=True,
171+
collection_sanitization_strategy=MongoDBV1CollectionSanitizationStrategy(),
172+
)
173+
174+
await clean_mongodb_database(store=store)
175+
176+
return store
177+
144178
async def test_value_stored_as_bson_dict(self, store: MongoDBStore):
145179
"""Verify values are stored as BSON dicts, not JSON strings."""
146180
await store.put(collection="test", key="test_key", value={"name": "Alice", "age": 30})
@@ -190,6 +224,19 @@ async def store(self, setup_mongodb: None) -> MongoDBStore:
190224

191225
return store
192226

227+
@pytest.fixture
228+
async def sanitizing_store(self, setup_mongodb: None) -> MongoDBStore:
229+
store = MongoDBStore(
230+
url=f"mongodb://{MONGODB_HOST}:{MONGODB_HOST_PORT}",
231+
db_name=f"{MONGODB_TEST_DB}-sanitizing",
232+
native_storage=False,
233+
collection_sanitization_strategy=MongoDBV1CollectionSanitizationStrategy(),
234+
)
235+
236+
await clean_mongodb_database(store=store)
237+
238+
return store
239+
193240
async def test_value_stored_as_json(self, store: MongoDBStore):
194241
"""Verify values are stored as JSON strings."""
195242
await store.put(collection="test", key="test_key", value={"name": "Alice", "age": 30})

key-value/key-value-sync/tests/code_gen/stores/mongodb/test_mongodb.py

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from key_value.sync.code_gen.stores.base import BaseStore
1919
from key_value.sync.code_gen.stores.mongodb import MongoDBStore
20-
from key_value.sync.code_gen.stores.mongodb.store import MongoDBSerializationAdapter
20+
from key_value.sync.code_gen.stores.mongodb.store import MongoDBSerializationAdapter, MongoDBV1CollectionSanitizationStrategy
2121
from tests.code_gen.conftest import docker_container, should_skip_docker_tests
2222
from tests.code_gen.stores.base import BaseStoreTests, ContextManagerStoreTestMixin
2323

@@ -119,12 +119,30 @@ def setup_mongodb(self, request: pytest.FixtureRequest) -> Generator[None, None,
119119
@override
120120
def test_not_unbounded(self, store: BaseStore): ...
121121

122-
def test_mongodb_collection_name_sanitization(self, store: MongoDBStore):
123-
"""Tests that a special characters in the collection name will not raise an error."""
124-
store.put(collection="test_collection!@#$%^&*()", key="test_key", value={"test": "test"})
125-
assert store.get(collection="test_collection!@#$%^&*()", key="test_key") == {"test": "test"}
122+
@override
123+
def test_long_collection_name(self, store: MongoDBStore, sanitizing_store: MongoDBStore): # pyright: ignore[reportIncompatibleMethodOverride]
124+
"Tests that a long collection name will not raise an error."
125+
with pytest.raises(Exception): # noqa: B017, PT011
126+
store.put(collection="test_collection" * 100, key="test_key", value={"test": "test"})
127+
128+
sanitizing_store.put(collection="test_collection" * 100, key="test_key", value={"test": "test"})
129+
assert sanitizing_store.get(collection="test_collection" * 100, key="test_key") == {"test": "test"}
130+
131+
@override
132+
def test_special_characters_in_collection_name(self, store: MongoDBStore, sanitizing_store: MongoDBStore): # pyright: ignore[reportIncompatibleMethodOverride]
133+
"Tests that special characters in the collection name will not raise an error."
134+
with pytest.raises(Exception): # noqa: B017, PT011
135+
store.put(collection="test_collection!@#$%^&*()", key="test_key", value={"test": "test"})
136+
137+
sanitizing_store.put(collection="test_collection!@#$%^&*()", key="test_key", value={"test": "test"})
138+
assert sanitizing_store.get(collection="test_collection!@#$%^&*()", key="test_key") == {"test": "test"}
126139

127-
collections = store._collections_by_name.values()
140+
def test_mongodb_collection_name_sanitization(self, sanitizing_store: MongoDBStore):
141+
"""Tests that the V1 sanitization strategy produces the expected collection names."""
142+
sanitizing_store.put(collection="test_collection!@#$%^&*()", key="test_key", value={"test": "test"})
143+
assert sanitizing_store.get(collection="test_collection!@#$%^&*()", key="test_key") == {"test": "test"}
144+
145+
collections = sanitizing_store._collections_by_name.values()
128146
collection_names = [collection.name for collection in collections]
129147
assert collection_names == snapshot(["S_test_collection_-daf4a2ec"])
130148

@@ -142,6 +160,19 @@ def store(self, setup_mongodb: None) -> MongoDBStore:
142160

143161
return store
144162

163+
@pytest.fixture
164+
def sanitizing_store(self, setup_mongodb: None) -> MongoDBStore:
165+
store = MongoDBStore(
166+
url=f"mongodb://{MONGODB_HOST}:{MONGODB_HOST_PORT}",
167+
db_name=f"{MONGODB_TEST_DB}-native-sanitizing",
168+
native_storage=True,
169+
collection_sanitization_strategy=MongoDBV1CollectionSanitizationStrategy(),
170+
)
171+
172+
clean_mongodb_database(store=store)
173+
174+
return store
175+
145176
def test_value_stored_as_bson_dict(self, store: MongoDBStore):
146177
"""Verify values are stored as BSON dicts, not JSON strings."""
147178
store.put(collection="test", key="test_key", value={"name": "Alice", "age": 30})
@@ -186,6 +217,19 @@ def store(self, setup_mongodb: None) -> MongoDBStore:
186217

187218
return store
188219

220+
@pytest.fixture
221+
def sanitizing_store(self, setup_mongodb: None) -> MongoDBStore:
222+
store = MongoDBStore(
223+
url=f"mongodb://{MONGODB_HOST}:{MONGODB_HOST_PORT}",
224+
db_name=f"{MONGODB_TEST_DB}-sanitizing",
225+
native_storage=False,
226+
collection_sanitization_strategy=MongoDBV1CollectionSanitizationStrategy(),
227+
)
228+
229+
clean_mongodb_database(store=store)
230+
231+
return store
232+
189233
def test_value_stored_as_json(self, store: MongoDBStore):
190234
"""Verify values are stored as JSON strings."""
191235
store.put(collection="test", key="test_key", value={"name": "Alice", "age": 30})

0 commit comments

Comments
 (0)