1414
1515from key_value .aio .stores .base import BaseStore
1616from 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+ )
1821from tests .conftest import docker_container , should_skip_docker_tests
1922from 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 })
0 commit comments