Skip to content

Commit 5dda294

Browse files
committed
Update Mongo storage
1 parent 061a011 commit 5dda294

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

key-value/key-value-aio/src/key_value/aio/stores/mongodb/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def managed_entry_to_document(key: str, managed_entry: ManagedEntry, *, native_s
127127

128128

129129
class MongoDBStore(BaseEnumerateCollectionsStore, BaseDestroyCollectionStore, BaseContextManagerStore, BaseStore):
130-
"""MongoDB-based key-value store using Motor (async MongoDB driver)."""
130+
"""MongoDB-based key-value store using pymongo."""
131131

132132
_client: AsyncMongoClient[dict[str, Any]]
133133
_db: AsyncDatabase[dict[str, Any]]

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
from typing import Any
55

66
import pytest
7+
from bson import ObjectId
78
from dirty_equals import IsDatetime, IsFloat, IsInstance
89
from inline_snapshot import snapshot
910
from key_value.shared.stores.wait import async_wait_for_true
1011
from key_value.shared.utils.managed_entry import ManagedEntry
11-
from bson import ObjectId
1212
from pymongo import AsyncMongoClient
1313
from typing_extensions import override
1414

@@ -133,9 +133,8 @@ class TestMongoDBStoreNativeMode(BaseMongoDBStoreTests):
133133
@pytest.fixture
134134
async def store(self, setup_mongodb: None) -> MongoDBStore:
135135
store = MongoDBStore(url=f"mongodb://{MONGODB_HOST}:{MONGODB_HOST_PORT}", db_name=f"{MONGODB_TEST_DB}-native", native_storage=True)
136-
# Ensure a clean db by dropping our default test collection if it exists
137-
with contextlib.suppress(Exception):
138-
_ = await store._client.drop_database(name_or_database=f"{MONGODB_TEST_DB}-native") # pyright: ignore[reportPrivateUsage]
136+
137+
await clean_mongodb_database(store=store)
139138

140139
return store
141140

@@ -172,7 +171,6 @@ async def test_migration_from_legacy_mode(self, store: MongoDBStore):
172171
}
173172
)
174173

175-
# Should be able to read it in native mode
176174
result = await store.get(collection="test", key="legacy_key")
177175
assert result == {"legacy": "data"}
178176

@@ -210,7 +208,7 @@ async def test_value_stored_as_json(self, store: MongoDBStore):
210208
)
211209

212210
async def test_migration_from_native_mode(self, store: MongoDBStore):
213-
"""Verify legacy mode can read native-mode object data."""
211+
"""Verify non-native mode can read native mode data."""
214212
# Manually insert a legacy document with JSON string value in the new format
215213
await store._setup_collection(collection="test") # pyright: ignore[reportPrivateUsage]
216214
sanitized_collection = store._sanitize_collection_name(collection="test") # pyright: ignore[reportPrivateUsage]
@@ -223,6 +221,5 @@ async def test_migration_from_native_mode(self, store: MongoDBStore):
223221
}
224222
)
225223

226-
# Should be able to read it in native mode
227224
result = await store.get(collection="test", key="legacy_key")
228225
assert result == {"name": "Alice", "age": 30}

key-value/key-value-sync/src/key_value/sync/code_gen/stores/mongodb/store.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from pymongo.database import Database
2525
from pymongo.results import DeleteResult # noqa: TC002
2626
except ImportError as e:
27-
msg = "MongoDBStore requires py-key-value-sync[mongodb]"
27+
msg = "MongoDBStore requires py-key-value-aio[mongodb]"
2828
raise ImportError(msg) from e
2929

3030
DEFAULT_DB = "kv-store-adapter"
@@ -134,7 +134,7 @@ def managed_entry_to_document(key: str, managed_entry: ManagedEntry, *, native_s
134134

135135

136136
class MongoDBStore(BaseEnumerateCollectionsStore, BaseDestroyCollectionStore, BaseContextManagerStore, BaseStore):
137-
"""MongoDB-based key-value store using PyMongo (sync MongoDB driver)."""
137+
"""MongoDB-based key-value store using pymongo."""
138138

139139
_client: MongoClient[dict[str, Any]]
140140
_db: Database[dict[str, Any]]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# WARNING: this file is auto-generated by 'build_sync_library.py'
2+
# from the original file 'test.py'
3+
# DO NOT CHANGE! Change the original file instead.
4+
import os
5+
6+
from cryptography.fernet import Fernet
7+
from key_value.sync.code_gen.stores.dynamodb.store import DynamoDBStore
8+
9+
from key_value.sync.code_gen.wrappers.encryption.fernet import FernetEncryptionWrapper
10+
11+
PERSISTENCE_TABLE_NAME = os.getenv("PERSISTENCE_TABLE_NAME", "test-key-value")
12+
STORAGE_ENCRYPTION_KEY = os.getenv("STORAGE_ENCRYPTION_KEY")
13+
14+
wrapped_client_storage: DynamoDBStore
15+
16+
client_storage = DynamoDBStore(table_name=PERSISTENCE_TABLE_NAME)
17+
if STORAGE_ENCRYPTION_KEY:
18+
wrapped_client_storage = FernetEncryptionWrapper(key_value=client_storage, fernet=Fernet(STORAGE_ENCRYPTION_KEY))

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
from typing import Any
88

99
import pytest
10+
from bson import ObjectId
1011
from dirty_equals import IsDatetime, IsFloat, IsInstance
1112
from inline_snapshot import snapshot
1213
from key_value.shared.stores.wait import wait_for_true
1314
from key_value.shared.utils.managed_entry import ManagedEntry
14-
from bson import ObjectId
1515
from pymongo import MongoClient
1616
from typing_extensions import override
1717

@@ -134,9 +134,8 @@ class TestMongoDBStoreNativeMode(BaseMongoDBStoreTests):
134134
@pytest.fixture
135135
def store(self, setup_mongodb: None) -> MongoDBStore:
136136
store = MongoDBStore(url=f"mongodb://{MONGODB_HOST}:{MONGODB_HOST_PORT}", db_name=f"{MONGODB_TEST_DB}-native", native_storage=True)
137-
# Ensure a clean db by dropping our default test collection if it exists
138-
with contextlib.suppress(Exception):
139-
_ = store._client.drop_database(name_or_database=f"{MONGODB_TEST_DB}-native") # pyright: ignore[reportPrivateUsage]
137+
138+
clean_mongodb_database(store=store)
140139

141140
return store
142141

@@ -169,7 +168,6 @@ def test_migration_from_legacy_mode(self, store: MongoDBStore):
169168
# New format with JSON string
170169
collection.insert_one({"key": "legacy_key", "value": {"string": '{"legacy": "data"}'}})
171170

172-
# Should be able to read it in native mode
173171
result = store.get(collection="test", key="legacy_key")
174172
assert result == {"legacy": "data"}
175173

@@ -207,14 +205,13 @@ def test_value_stored_as_json(self, store: MongoDBStore):
207205
)
208206

209207
def test_migration_from_native_mode(self, store: MongoDBStore):
210-
"""Verify legacy mode can read native-mode object data."""
208+
"""Verify non-native mode can read native mode data."""
211209
# Manually insert a legacy document with JSON string value in the new format
212210
store._setup_collection(collection="test") # pyright: ignore[reportPrivateUsage]
213211
sanitized_collection = store._sanitize_collection_name(collection="test") # pyright: ignore[reportPrivateUsage]
214212
collection = store._collections_by_name[sanitized_collection] # pyright: ignore[reportPrivateUsage]
215213

216214
collection.insert_one({"key": "legacy_key", "value": {"object": {"name": "Alice", "age": 30}}})
217215

218-
# Should be able to read it in native mode
219216
result = store.get(collection="test", key="legacy_key")
220217
assert result == {"name": "Alice", "age": 30}

0 commit comments

Comments
 (0)