Skip to content

Commit

Permalink
fixup! changed session's save() logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dill0wn committed Jul 12, 2024
1 parent a751703 commit e65fec5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
12 changes: 6 additions & 6 deletions ming/metadata.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from bson import ObjectId
from datetime import datetime
from typing import Generic, TypeVar, Any, Optional, overload, List, Dict, Type, Union, Mapping, type_check_only
from pymongo.results import UpdateResult, DeleteResult

import bson
from ming.base import Cursor
Expand Down Expand Up @@ -178,7 +179,6 @@ class Field:
M = TypeVar('M')

MongoFilter = dict
ChangeResult = dict
SaveResult = Union[ObjectId, Any]
class _ClassManager(Generic[M]):
# proxies these from Session
Expand All @@ -190,9 +190,9 @@ class _ClassManager(Generic[M]):
def find_by(self, filter: MongoFilter, *args, validate: bool = True, **kwargs) -> Cursor[M]: ...
#@overload
#def find_by(self, filter: MongoFilter, *args, validate: Literal[False], **kwargs) -> Generator[M]: ...
def remove(self, spec_or_id: Union[MongoFilter, ObjectId] = None, **kwargs) -> ChangeResult: ...
def remove(self, spec_or_id: Union[MongoFilter, ObjectId] = None, **kwargs) -> DeleteResult: ...
def count(self) -> int: ...
def update_partial(self, filter: MongoFilter, fields: dict, **kwargs) -> ChangeResult: ...
def update_partial(self, filter: MongoFilter, fields: dict, **kwargs) -> UpdateResult: ...
def find_one_and_update(self, **kwargs) -> M: ...
def find_one_and_replace(self, **kwargs) -> M: ...
def find_one_and_delete(self, **kwargs) -> M: ...
Expand All @@ -209,9 +209,9 @@ class _InstanceManager:
# proxies these from Session
def save(self, *args: str, **kwargs) -> SaveResult: ...
def insert(self, **kwargs) -> SaveResult: ...
def upsert(self, spec_fields: List[str], **kwargs) -> ChangeResult: ...
def delete(self) -> ChangeResult: ...
def set(self, fields_values: Mapping[str, Any]) -> ChangeResult: ...
def upsert(self, spec_fields: List[str], **kwargs) -> UpdateResult: ...
def delete(self) -> DeleteResult: ...
def set(self, fields_values: Mapping[str, Any]) -> UpdateResult: ...
def increase_field(self, **kwargs) -> None: ...


Expand Down
28 changes: 15 additions & 13 deletions ming/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,31 +166,33 @@ def save(self, doc, *args, **kwargs) -> bson.ObjectId:
|---------------------------|
"""
data = self._prep_save(doc, kwargs.pop('validate', True))
_id = getattr(doc, '_id', None) or None

# if _id is None:
# doc.pop('_id', None)

new_id = None
if args:
if not _id:
raise ValueError('Cannot save a subset without an _id')
else:
if '_id' in doc:
arg_data = {arg: data[arg] for arg in args}
result = self._impl(doc).update_one(
dict(_id=_id), {'$set': arg_data},
dict(_id=doc._id), {'$set': arg_data},
**fix_write_concern(kwargs)
)
else:
if not _id:
result = self._impl(doc).insert_one(
data, **fix_write_concern(kwargs)
)
new_id = result.inserted_id
else:
raise ValueError('Cannot save a subset without an _id')

Check warning on line 182 in ming/session.py

View check run for this annotation

Codecov / codecov/patch

ming/session.py#L182

Added line #L182 was not covered by tests
else:
if '_id' in doc:
result = self._impl(doc).replace_one(
dict(_id=_id), data,
dict(_id=doc._id), data,
upsert=True, **fix_write_concern(kwargs)
)
new_id = result.upserted_id
if result and '_id' not in doc and new_id:
else:
result = self._impl(doc).insert_one(

Check warning on line 191 in ming/session.py

View check run for this annotation

Codecov / codecov/patch

ming/session.py#L191

Added line #L191 was not covered by tests
data, **fix_write_concern(kwargs)
)
new_id = result.inserted_id

Check warning on line 194 in ming/session.py

View check run for this annotation

Codecov / codecov/patch

ming/session.py#L194

Added line #L194 was not covered by tests
if result and ('_id' not in doc) and (new_id is not None):
doc._id = new_id

Check warning on line 196 in ming/session.py

View check run for this annotation

Codecov / codecov/patch

ming/session.py#L196

Added line #L196 was not covered by tests

return result
Expand Down
2 changes: 1 addition & 1 deletion ming/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_base_session(self):

doc = TestDoc({})
sess.save(doc)
impl.replace_one.assert_called_with(dict(_id=None), doc, upsert=True)
impl.replace_one.assert_called_with(dict(_id=doc._id), doc, upsert=True)
self.assertEqual(doc.a, None)
self.assertEqual(doc.b, dict(a=None))
del doc._id
Expand Down

0 comments on commit e65fec5

Please sign in to comment.