Skip to content

Commit

Permalink
Tests pre/post hooks in MixinDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrech committed Jul 20, 2020
1 parent a4eaf58 commit e8ed3c0
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 4 deletions.
54 changes: 53 additions & 1 deletion tests/frameworks/test_motor_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

from ..common import BaseDBTest, TEST_DB

from umongo import Document, EmbeddedDocument, fields, exceptions, Reference
from umongo import (
Document, EmbeddedDocument, MixinDocument, fields, exceptions, Reference
)

if not dep_error: # Make sure the module is valid by importing it
from umongo.frameworks import motor_asyncio as framework # noqa
Expand Down Expand Up @@ -954,3 +956,53 @@ def pre_delete(self):
await p.delete()

loop.run_until_complete(do_test())

def test_mixin_pre_post_hooks(self, loop, instance):

async def do_test():

callbacks = []

@instance.register
class PrePostHooksMixin(MixinDocument):

def pre_insert(self):
callbacks.append('pre_insert')

def pre_update(self):
callbacks.append('pre_update')

def pre_delete(self):
callbacks.append('pre_delete')

def post_insert(self, ret):
assert isinstance(ret, InsertOneResult)
callbacks.append('post_insert')

def post_update(self, ret):
assert isinstance(ret, UpdateResult)
callbacks.append('post_update')

def post_delete(self, ret):
assert isinstance(ret, DeleteResult)
callbacks.append('post_delete')

@instance.register
class Person(PrePostHooksMixin, Document):
name = fields.StrField()
age = fields.IntField()

p = Person(name='John', age=20)
await p.commit()
assert callbacks == ['pre_insert', 'post_insert']

callbacks.clear()
p.age = 22
await p.commit({'age': 22})
assert callbacks == ['pre_update', 'post_update']

callbacks.clear()
await p.delete()
assert callbacks == ['pre_delete', 'post_delete']

loop.run_until_complete(do_test())
52 changes: 50 additions & 2 deletions tests/frameworks/test_pymongo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timezone

import pytest

Expand All @@ -7,7 +7,9 @@
from pymongo.results import InsertOneResult, UpdateResult, DeleteResult
import marshmallow as ma

from umongo import Document, EmbeddedDocument, fields, exceptions, Reference
from umongo import (
Document, EmbeddedDocument, MixinDocument, fields, exceptions, Reference
)
from umongo.frameworks import pymongo as framework_pymongo # noqa

from ..common import BaseDBTest, TEST_DB
Expand Down Expand Up @@ -746,3 +748,49 @@ def pre_delete(self):
with pytest.raises(exceptions.DeleteError):
p_concurrent.delete()
p.delete()

def test_mixin_pre_post_hooks(self, instance):

callbacks = []

@instance.register
class PrePostHooksMixin(MixinDocument):

def pre_insert(self):
callbacks.append('pre_insert')

def pre_update(self):
callbacks.append('pre_update')

def pre_delete(self):
callbacks.append('pre_delete')

def post_insert(self, ret):
assert isinstance(ret, InsertOneResult)
callbacks.append('post_insert')

def post_update(self, ret):
assert isinstance(ret, UpdateResult)
callbacks.append('post_update')

def post_delete(self, ret):
assert isinstance(ret, DeleteResult)
callbacks.append('post_delete')

@instance.register
class Person(PrePostHooksMixin, Document):
name = fields.StrField()
age = fields.IntField()

p = Person(name='John', age=20)
p.commit()
assert callbacks == ['pre_insert', 'post_insert']

callbacks.clear()
p.age = 22
p.commit()
assert callbacks == ['pre_update', 'post_update']

callbacks.clear()
p.delete()
assert callbacks == ['pre_delete', 'post_delete']
53 changes: 52 additions & 1 deletion tests/frameworks/test_txmongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def wrapper(self):
else:
pytest_inlineCallbacks = pytest_twisted.inlineCallbacks

from umongo import Document, EmbeddedDocument, fields, exceptions, Reference
from umongo import (
Document, EmbeddedDocument, MixinDocument, fields, exceptions, Reference
)


if not dep_error: # Make sure the module is valid by importing it
Expand Down Expand Up @@ -879,3 +881,52 @@ def pre_delete(self):
with pytest.raises(exceptions.DeleteError):
yield p_concurrent.delete()
yield p.delete()

@pytest_inlineCallbacks
def test_mixin_pre_post_hooks(self, instance):

callbacks = []

@instance.register
class PrePostHooksMixin(MixinDocument):

def pre_insert(self):
callbacks.append('pre_insert')

def pre_update(self):
callbacks.append('pre_update')

def pre_delete(self):
callbacks.append('pre_delete')

def post_insert(self, ret):
assert isinstance(ret, InsertOneResult)
callbacks.append('post_insert')

def post_update(self, ret):
assert isinstance(ret, UpdateResult)
callbacks.append('post_update')

def post_delete(self, ret):
assert isinstance(ret, DeleteResult)
callbacks.append('post_delete')

@instance.register
class Person(PrePostHooksMixin, Document):
name = fields.StrField()
age = fields.IntField()

p = Person(name='John', age=20)
yield p.commit()
assert callbacks == ['pre_insert', 'post_insert']

callbacks.clear()
p.age = 22
yield p.commit({'age': 22})
assert callbacks == ['pre_update', 'post_update']

callbacks.clear()
yield p.delete()
assert callbacks == ['pre_delete', 'post_delete']


0 comments on commit e8ed3c0

Please sign in to comment.