@@ -48,13 +48,14 @@ class MongoDBFailedToStartError(Exception):
4848 pass
4949
5050
51- def test_managed_entry_document_conversion_native_mode ():
51+ def test_managed_entry_document_conversion ():
52+ """Test that documents are stored as BSON dicts."""
5253 created_at = datetime (year = 2025 , month = 1 , day = 1 , hour = 0 , minute = 0 , second = 0 , tzinfo = timezone .utc )
5354 expires_at = created_at + timedelta (seconds = 10 )
5455
5556 managed_entry = ManagedEntry (value = {"test" : "test" }, created_at = created_at , expires_at = expires_at )
5657
57- adapter = MongoDBSerializationAdapter (native_storage = True )
58+ adapter = MongoDBSerializationAdapter ()
5859 document = adapter .dump_dict (entry = managed_entry )
5960
6061 assert document == snapshot (
@@ -74,31 +75,6 @@ def test_managed_entry_document_conversion_native_mode():
7475 assert round_trip_managed_entry .expires_at == expires_at
7576
7677
77- def test_managed_entry_document_conversion_legacy_mode ():
78- created_at = datetime (year = 2025 , month = 1 , day = 1 , hour = 0 , minute = 0 , second = 0 , tzinfo = timezone .utc )
79- expires_at = created_at + timedelta (seconds = 10 )
80-
81- managed_entry = ManagedEntry (value = {"test" : "test" }, created_at = created_at , expires_at = expires_at )
82- adapter = MongoDBSerializationAdapter (native_storage = False )
83- document = adapter .dump_dict (entry = managed_entry )
84-
85- assert document == snapshot (
86- {
87- "version" : 1 ,
88- "value" : {"string" : '{"test": "test"}' },
89- "created_at" : datetime (2025 , 1 , 1 , 0 , 0 , tzinfo = timezone .utc ),
90- "expires_at" : datetime (2025 , 1 , 1 , 0 , 0 , 10 , tzinfo = timezone .utc ),
91- }
92- )
93-
94- round_trip_managed_entry = adapter .load_dict (data = document )
95-
96- assert round_trip_managed_entry .value == managed_entry .value
97- assert round_trip_managed_entry .created_at == created_at
98- assert round_trip_managed_entry .ttl == IsFloat (lt = 0 )
99- assert round_trip_managed_entry .expires_at == expires_at
100-
101-
10278async def clean_mongodb_database (store : MongoDBStore ) -> None :
10379 with contextlib .suppress (Exception ):
10480 _ = await store ._client .drop_database (name_or_database = store ._db .name ) # pyright: ignore[reportPrivateUsage]
@@ -151,13 +127,13 @@ async def test_mongodb_collection_name_sanitization(self, sanitizing_store: Mong
151127
152128
153129@pytest .mark .skipif (should_skip_docker_tests (), reason = "Docker is not available" )
154- class TestMongoDBStoreNativeMode (BaseMongoDBStoreTests ):
155- """Test MongoDBStore with native_storage=True (default) ."""
130+ class TestMongoDBStore (BaseMongoDBStoreTests ):
131+ """Test MongoDBStore with native BSON storage ."""
156132
157133 @override
158134 @pytest .fixture
159135 async def store (self , setup_mongodb : None ) -> MongoDBStore :
160- store = MongoDBStore (url = f"mongodb://{ MONGODB_HOST } :{ MONGODB_HOST_PORT } " , db_name = f" { MONGODB_TEST_DB } -native" , native_storage = True )
136+ store = MongoDBStore (url = f"mongodb://{ MONGODB_HOST } :{ MONGODB_HOST_PORT } " , db_name = MONGODB_TEST_DB )
161137
162138 await clean_mongodb_database (store = store )
163139
@@ -167,8 +143,7 @@ async def store(self, setup_mongodb: None) -> MongoDBStore:
167143 async def sanitizing_store (self , setup_mongodb : None ) -> MongoDBStore :
168144 store = MongoDBStore (
169145 url = f"mongodb://{ MONGODB_HOST } :{ MONGODB_HOST_PORT } " ,
170- db_name = f"{ MONGODB_TEST_DB } -native-sanitizing" ,
171- native_storage = True ,
146+ db_name = f"{ MONGODB_TEST_DB } -sanitizing" ,
172147 collection_sanitization_strategy = MongoDBV1CollectionSanitizationStrategy (),
173148 )
174149
@@ -196,83 +171,3 @@ async def test_value_stored_as_bson_dict(self, store: MongoDBStore):
196171 "version" : 1 ,
197172 }
198173 )
199-
200- async def test_migration_from_legacy_mode (self , store : MongoDBStore ):
201- """Verify native mode can read legacy JSON string data."""
202- await store ._setup_collection (collection = "test" ) # pyright: ignore[reportPrivateUsage]
203- sanitized_collection = store ._sanitize_collection (collection = "test" ) # pyright: ignore[reportPrivateUsage]
204- collection = store ._collections_by_name [sanitized_collection ] # pyright: ignore[reportPrivateUsage]
205-
206- await collection .insert_one (
207- {
208- "key" : "legacy_key" ,
209- "value" : {"string" : '{"legacy": "data"}' },
210- }
211- )
212-
213- result = await store .get (collection = "test" , key = "legacy_key" )
214- assert result == {"legacy" : "data" }
215-
216-
217- @pytest .mark .skipif (should_skip_docker_tests (), reason = "Docker is not available" )
218- class TestMongoDBStoreNonNativeMode (BaseMongoDBStoreTests ):
219- """Test MongoDBStore with native_storage=False (legacy mode) for backward compatibility."""
220-
221- @override
222- @pytest .fixture
223- async def store (self , setup_mongodb : None ) -> MongoDBStore :
224- store = MongoDBStore (url = f"mongodb://{ MONGODB_HOST } :{ MONGODB_HOST_PORT } " , db_name = MONGODB_TEST_DB , native_storage = False )
225-
226- await clean_mongodb_database (store = store )
227-
228- return store
229-
230- @pytest .fixture
231- async def sanitizing_store (self , setup_mongodb : None ) -> MongoDBStore :
232- store = MongoDBStore (
233- url = f"mongodb://{ MONGODB_HOST } :{ MONGODB_HOST_PORT } " ,
234- db_name = f"{ MONGODB_TEST_DB } -sanitizing" ,
235- native_storage = False ,
236- collection_sanitization_strategy = MongoDBV1CollectionSanitizationStrategy (),
237- )
238-
239- await clean_mongodb_database (store = store )
240-
241- return store
242-
243- async def test_value_stored_as_json (self , store : MongoDBStore ):
244- """Verify values are stored as JSON strings."""
245- await store .put (collection = "test" , key = "test_key" , value = {"name" : "Alice" , "age" : 30 })
246-
247- # Get the raw MongoDB document
248- await store ._setup_collection (collection = "test" ) # pyright: ignore[reportPrivateUsage]
249- sanitized_collection = store ._sanitize_collection (collection = "test" ) # pyright: ignore[reportPrivateUsage]
250- collection = store ._collections_by_name [sanitized_collection ] # pyright: ignore[reportPrivateUsage]
251- doc = await collection .find_one ({"key" : "test_key" })
252-
253- assert doc == snapshot (
254- {
255- "_id" : IsInstance (expected_type = ObjectId ),
256- "key" : "test_key" ,
257- "collection" : "test" ,
258- "created_at" : IsDatetime (),
259- "value" : {"string" : '{"age": 30, "name": "Alice"}' },
260- "version" : 1 ,
261- }
262- )
263-
264- async def test_migration_from_native_mode (self , store : MongoDBStore ):
265- """Verify non-native mode can read native mode data."""
266- await store ._setup_collection (collection = "test" ) # pyright: ignore[reportPrivateUsage]
267- sanitized_collection = store ._sanitize_collection (collection = "test" ) # pyright: ignore[reportPrivateUsage]
268- collection = store ._collections_by_name [sanitized_collection ] # pyright: ignore[reportPrivateUsage]
269-
270- await collection .insert_one (
271- {
272- "key" : "legacy_key" ,
273- "value" : {"object" : {"name" : "Alice" , "age" : 30 }},
274- }
275- )
276-
277- result = await store .get (collection = "test" , key = "legacy_key" )
278- assert result == {"name" : "Alice" , "age" : 30 }
0 commit comments