Skip to content

Commit

Permalink
test improvements - from #45
Browse files Browse the repository at this point in the history
  • Loading branch information
brondsem committed Jun 2, 2022
1 parent 4ae649e commit 531e404
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
33 changes: 33 additions & 0 deletions ming/tests/odm/test_declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,3 +837,36 @@ def test_hook_base(self):
Mapper.replace_session(new_session)
assert id(self.Basic.query.session) == id(new_session)
assert id(self.session) != id(new_session)


class TestBeforeSave(TestCase):

def setUp(self):
Mapper._mapper_by_classname.clear()
self.datastore = create_datastore('mim:///test_db')
self.session = ODMSession(bind=self.datastore)
class Basic(MappedClass):
class __mongometa__:
name = 'hook'
session = self.session
def before_save(instance):
instance.a = 9

_id = FieldProperty(S.ObjectId)
a = FieldProperty(int)
Mapper.compile_all()
self.Basic = Basic
self.session.remove(self.Basic)

def test_hook_base(self):
doc = self.Basic()
doc.a = 5
self.session.flush() # first insert
self.session.close()
doc = self.Basic.query.get(doc._id)
assert doc.a == 9, doc.a
doc.a = 6
self.session.flush() # then save
self.session.close()
doc = self.Basic.query.get(doc._id)
assert doc.a == 9, doc.a
17 changes: 17 additions & 0 deletions ming/tests/odm/test_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,23 @@ def test_group(self, pymongo_group):
self.Basic.query.group()
assert pymongo_group.called

def test_multiple_update_flushes(self):
initial_doc = self.Basic()
initial_doc.a = 1
self.session.flush()
self.session.close()

doc_updating = self.Basic.query.get(_id=initial_doc._id)
doc_updating.a = 2
self.session.flush()
doc_updating.a = 1 # back to "initial" value
doc_updating.e = 'foo' # change something else too
self.session.flush()
self.session.close()

doc_after_updates = self.Basic.query.get(_id=doc_updating._id)
assert doc_after_updates.a == 1


class TestRelation(TestCase):
def setUp(self):
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 @@ -81,7 +81,7 @@ def test_base_session(self):
doc = self.TestDocNoSchema({'_id':5, 'a':5})
sess.save(doc)
impl.save.assert_called_with(dict(_id=5, a=5))
doc = self.TestDocNoSchema({'_id':5, 'a':5})
doc = self.TestDocNoSchema({'_id':5, 'a':5, 'b': 6})
sess.save(doc, 'a')
impl.update.assert_called_with(dict(_id=5), {'$set':dict(a=5)})
doc = self.TestDocNoSchema({'_id':5, 'a':5})
Expand Down

0 comments on commit 531e404

Please sign in to comment.