Skip to content

Commit 061a011

Browse files
fix: incorporate CodeRabbit review feedback
- Fix ObjectId import to use bson module instead of pymongo.collection - Update sanitize_collection_name to use COLLECTION_ALLOWED_CHARACTERS - Fix test docstrings for migration tests - Update function docstrings (value.dict → value.object) - Fix import error message in sync module - Fix class docstring in sync module (Motor → PyMongo) Co-authored-by: William Easton <[email protected]>
1 parent 74e6282 commit 061a011

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def document_to_managed_entry(document: dict[str, Any]) -> ManagedEntry:
3939
"""Convert a MongoDB document back to a ManagedEntry.
4040
4141
This function deserializes a MongoDB document (created by `managed_entry_to_document`) back to a
42-
ManagedEntry object. It supports both native BSON storage (dict in value.dict field) and legacy
42+
ManagedEntry object. It supports both native BSON storage (dict in value.object field) and legacy
4343
JSON string storage (string in value.string field) for migration support.
4444
4545
Args:
@@ -238,7 +238,7 @@ def _sanitize_collection_name(self, collection: str) -> str:
238238
Returns:
239239
A sanitized collection name that meets MongoDB requirements.
240240
"""
241-
return sanitize_string(value=collection, max_length=MAX_COLLECTION_LENGTH, allowed_characters=ALPHANUMERIC_CHARACTERS)
241+
return sanitize_string(value=collection, max_length=MAX_COLLECTION_LENGTH, allowed_characters=COLLECTION_ALLOWED_CHARACTERS)
242242

243243
@override
244244
async def _setup_collection(self, *, collection: str) -> None:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
from inline_snapshot import snapshot
99
from key_value.shared.stores.wait import async_wait_for_true
1010
from key_value.shared.utils.managed_entry import ManagedEntry
11+
from bson import ObjectId
1112
from pymongo import AsyncMongoClient
12-
from pymongo.collection import ObjectId
1313
from typing_extensions import override
1414

1515
from key_value.aio.stores.base import BaseStore
@@ -210,7 +210,7 @@ async def test_value_stored_as_json(self, store: MongoDBStore):
210210
)
211211

212212
async def test_migration_from_native_mode(self, store: MongoDBStore):
213-
"""Verify native mode can read native mode JSON string data."""
213+
"""Verify legacy mode can read native-mode object data."""
214214
# Manually insert a legacy document with JSON string value in the new format
215215
await store._setup_collection(collection="test") # pyright: ignore[reportPrivateUsage]
216216
sanitized_collection = store._sanitize_collection_name(collection="test") # pyright: ignore[reportPrivateUsage]

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

Lines changed: 4 additions & 4 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-aio[mongodb]"
27+
msg = "MongoDBStore requires py-key-value-sync[mongodb]"
2828
raise ImportError(msg) from e
2929

3030
DEFAULT_DB = "kv-store-adapter"
@@ -46,7 +46,7 @@ def document_to_managed_entry(document: dict[str, Any]) -> ManagedEntry:
4646
"""Convert a MongoDB document back to a ManagedEntry.
4747
4848
This function deserializes a MongoDB document (created by `managed_entry_to_document`) back to a
49-
ManagedEntry object. It supports both native BSON storage (dict in value.dict field) and legacy
49+
ManagedEntry object. It supports both native BSON storage (dict in value.object field) and legacy
5050
JSON string storage (string in value.string field) for migration support.
5151
5252
Args:
@@ -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 Motor (sync MongoDB driver)."""
137+
"""MongoDB-based key-value store using PyMongo (sync MongoDB driver)."""
138138

139139
_client: MongoClient[dict[str, Any]]
140140
_db: Database[dict[str, Any]]
@@ -245,7 +245,7 @@ def _sanitize_collection_name(self, collection: str) -> str:
245245
Returns:
246246
A sanitized collection name that meets MongoDB requirements.
247247
"""
248-
return sanitize_string(value=collection, max_length=MAX_COLLECTION_LENGTH, allowed_characters=ALPHANUMERIC_CHARACTERS)
248+
return sanitize_string(value=collection, max_length=MAX_COLLECTION_LENGTH, allowed_characters=COLLECTION_ALLOWED_CHARACTERS)
249249

250250
@override
251251
def _setup_collection(self, *, collection: str) -> None:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from inline_snapshot import snapshot
1212
from key_value.shared.stores.wait import wait_for_true
1313
from key_value.shared.utils.managed_entry import ManagedEntry
14+
from bson import ObjectId
1415
from pymongo import MongoClient
15-
from pymongo.collection import ObjectId
1616
from typing_extensions import override
1717

1818
from key_value.sync.code_gen.stores.base import BaseStore
@@ -207,7 +207,7 @@ def test_value_stored_as_json(self, store: MongoDBStore):
207207
)
208208

209209
def test_migration_from_native_mode(self, store: MongoDBStore):
210-
"""Verify native mode can read native mode JSON string data."""
210+
"""Verify legacy mode can read native-mode object data."""
211211
# Manually insert a legacy document with JSON string value in the new format
212212
store._setup_collection(collection="test") # pyright: ignore[reportPrivateUsage]
213213
sanitized_collection = store._sanitize_collection_name(collection="test") # pyright: ignore[reportPrivateUsage]

0 commit comments

Comments
 (0)