Skip to content

Commit

Permalink
flake8 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrech committed Oct 12, 2020
1 parent bf06ef8 commit 5b458d8
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 51 deletions.
33 changes: 20 additions & 13 deletions tests/frameworks/test_motor_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
from bson import ObjectId
import marshmallow as ma

from pymongo.results import InsertOneResult, UpdateResult, DeleteResult
from umongo import (
Document, EmbeddedDocument, MixinDocument, fields, exceptions, Reference
)

from ..common import BaseDBTest, TEST_DB


DEP_ERROR = 'Missing motor'

# Check if the required dependancies are met to run this driver's tests
Expand All @@ -17,15 +25,8 @@
else:
dep_error = False

from ..common import BaseDBTest, TEST_DB

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
from pymongo.results import InsertOneResult, UpdateResult, DeleteResult


def _stripped(indexes):
Expand Down Expand Up @@ -383,7 +384,8 @@ class EmbeddedDoc(EmbeddedDocument):
class IOStudent(Student):
io_field = fields.StrField(io_validate=io_validate)
list_io_field = fields.ListField(fields.IntField(io_validate=io_validate))
reference_io_field = fields.ReferenceField(classroom_model.Course, io_validate=io_validate)
reference_io_field = fields.ReferenceField(
classroom_model.Course, io_validate=io_validate)
embedded_io_field = fields.EmbeddedField(EmbeddedDoc, io_validate=io_validate)

bad_reference = ObjectId()
Expand Down Expand Up @@ -612,7 +614,8 @@ class Meta:
await UniqueIndexCompoundDoc.ensure_indexes()
indexes = await UniqueIndexCompoundDoc.collection.index_information()
# Must sort compound indexes to avoid random inconsistence
indexes['compound1_1_compound2_1']['key'] = sorted(indexes['compound1_1_compound2_1']['key'])
indexes['compound1_1_compound2_1']['key'] = sorted(
indexes['compound1_1_compound2_1']['key'])
expected_indexes = {
'_id_': {
'key': [('_id', 1)],
Expand All @@ -628,7 +631,8 @@ class Meta:
await UniqueIndexCompoundDoc.ensure_indexes()
indexes = await UniqueIndexCompoundDoc.collection.index_information()
# Must sort compound indexes to avoid random inconsistence
indexes['compound1_1_compound2_1']['key'] = sorted(indexes['compound1_1_compound2_1']['key'])
indexes['compound1_1_compound2_1']['key'] = sorted(
indexes['compound1_1_compound2_1']['key'])
assert _stripped(indexes) == expected_indexes

# Index is on the tuple (compound1, compound2)
Expand Down Expand Up @@ -819,11 +823,14 @@ class Book(Document):

res = await Book.count_documents({'title': 'The Hobbit'})
assert res == 1
res = await Book.count_documents({'author.name': {'$in': ['JK Rowling', 'JRR Tolkien']}})
res = await Book.count_documents(
{'author.name': {'$in': ['JK Rowling', 'JRR Tolkien']}})
assert res == 2
res = await Book.count_documents({'$and': [{'chapters.name': 'Roast Mutton'}, {'title': 'The Hobbit'}]})
res = await Book.count_documents(
{'$and': [{'chapters.name': 'Roast Mutton'}, {'title': 'The Hobbit'}]})
assert res == 1
res = await Book.count_documents({'chapters.name': {'$all': ['Roast Mutton', 'A Short Rest']}})
res = await Book.count_documents(
{'chapters.name': {'$all': ['Roast Mutton', 'A Short Rest']}})
assert res == 1

loop.run_until_complete(do_test())
Expand Down
16 changes: 8 additions & 8 deletions tests/frameworks/test_txmongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

from pymongo.results import InsertOneResult, UpdateResult, DeleteResult

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

from ..common import BaseDBTest, TEST_DB, con

DEP_ERROR = 'Missing txmongo or pytest_twisted'
Expand Down Expand Up @@ -35,10 +39,6 @@ def wrapper(self):
pytest_inlineCallbacks = pytest_twisted.inlineCallbacks
dep_error = False

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 txmongo as framework # noqa
Expand Down Expand Up @@ -321,7 +321,8 @@ class EmbeddedDoc(EmbeddedDocument):
class IOStudent(Student):
io_field = fields.StrField(io_validate=io_validate)
list_io_field = fields.ListField(fields.IntField(io_validate=io_validate))
reference_io_field = fields.ReferenceField(classroom_model.Course, io_validate=io_validate)
reference_io_field = fields.ReferenceField(
classroom_model.Course, io_validate=io_validate)
embedded_io_field = fields.EmbeddedField(EmbeddedDoc, io_validate=io_validate)

bad_reference = ObjectId()
Expand Down Expand Up @@ -755,7 +756,8 @@ class Book(Document):
assert len(res) == 1
res = yield Book.find({'author.name': {'$in': ['JK Rowling', 'JRR Tolkien']}})
assert len(res) == 2
res = yield Book.find({'$and': [{'chapters.name': 'Roast Mutton'}, {'title': 'The Hobbit'}]})
res = yield Book.find(
{'$and': [{'chapters.name': 'Roast Mutton'}, {'title': 'The Hobbit'}]})
assert len(res) == 1
res = yield Book.find({'chapters.name': {'$all': ['Roast Mutton', 'A Short Rest']}})
assert len(res) == 1
Expand Down Expand Up @@ -929,5 +931,3 @@ class Person(PrePostHooksMixin, Document):
callbacks.clear()
yield p.delete()
assert callbacks == ['pre_delete', 'post_delete']


5 changes: 3 additions & 2 deletions tests/test_data_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class MySchema(BaseSchema):

def test_default(self):
default_value = ObjectId('507f1f77bcf86cd799439011')
default_callable = lambda: ObjectId('507f1f77bcf86cd799439012')
default_callable = lambda: ObjectId('507f1f77bcf86cd799439012') # noqa: E731

class MySchema(BaseSchema):
no_default = fields.ObjectIdField()
Expand Down Expand Up @@ -375,7 +375,8 @@ class MySchema(BaseSchema):
d.load({'embedded': {}, 'required': 42})
with pytest.raises(ma.ValidationError) as exc:
d.required_validate()
assert exc.value.messages == {'embedded': {'required': ['Missing data for required field.']}}
assert exc.value.messages == {
'embedded': {'required': ['Missing data for required field.']}}

d.load({'embedded': {'required': 42}, 'required': 42, 'listed': [{}], 'dicted': {'a': {}}})
with pytest.raises(ma.ValidationError) as exc:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,8 @@ class DocChild2(AbsDoc):
class Meta:
collection_name = 'col2'

assert DocChild1.opts.collection_name is 'col1'
assert DocChild1Child.opts.collection_name is 'col1'
assert DocChild1.opts.collection_name == 'col1'
assert DocChild1Child.opts.collection_name == 'col1'
assert DocChild2.opts.collection_name == 'col2'

def test_inheritance_from_embedded_document(self):
Expand Down
28 changes: 14 additions & 14 deletions tests/test_embedded_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,28 @@ class MyEmbeddedDocument(EmbeddedDocument):
@self.instance.register
class MyDoc(Document):
e = fields.EmbeddedField(MyEmbeddedDocument)
l = fields.ListField(fields.EmbeddedField(MyEmbeddedDocument))
li = fields.ListField(fields.EmbeddedField(MyEmbeddedDocument))
b = fields.IntField(required=True)

with pytest.raises(ma.ValidationError) as exc:
MyDoc(l={})
assert exc.value.args[0] == {'l': ['Not a valid list.']}
MyDoc(li={})
assert exc.value.args[0] == {'li': ['Not a valid list.']}

with pytest.raises(ma.ValidationError) as exc:
MyDoc(l=True)
assert exc.value.args[0] == {'l': ['Not a valid list.']}
MyDoc(li=True)
assert exc.value.args[0] == {'li': ['Not a valid list.']}

with pytest.raises(ma.ValidationError) as exc:
MyDoc(l="string is not a list")
assert exc.value.args[0] == {'l': ['Not a valid list.']}
MyDoc(li="string is not a list")
assert exc.value.args[0] == {'li': ['Not a valid list.']}

with pytest.raises(ma.ValidationError) as exc:
MyDoc(l=[42])
assert exc.value.args[0] == {'l': {0: {'_schema': ['Invalid input type.']}}}
MyDoc(li=[42])
assert exc.value.args[0] == {'li': {0: {'_schema': ['Invalid input type.']}}}

with pytest.raises(ma.ValidationError) as exc:
MyDoc(l=[{}, 42])
assert exc.value.args[0] == {'l': {1: {'_schema': ['Invalid input type.']}}}
MyDoc(li=[{}, 42])
assert exc.value.args[0] == {'li': {1: {'_schema': ['Invalid input type.']}}}

with pytest.raises(ma.ValidationError) as exc:
MyDoc(b=[{}])
Expand Down Expand Up @@ -298,7 +298,8 @@ class OtherEmbedded(EmbeddedDocument):
assert cc.to_mongo() == {'in_mongo_a_parent': 1, 'b': 2, 'c': 3}
assert cgc.to_mongo() == {'in_mongo_a_child': 1, 'b': 2, 'c': 3, 'd': 4}
# But child of non abstract does
assert ccgc.to_mongo() == {'in_mongo_a_parent': 1, 'b': 2, 'c': 3, 'd': 4, '_cls': 'ConcreteConcreteGrandChild'}
assert ccgc.to_mongo() == {
'in_mongo_a_parent': 1, 'b': 2, 'c': 3, 'd': 4, '_cls': 'ConcreteConcreteGrandChild'}

# Cannot use abstract embedded document in EmbeddedField
with pytest.raises(exceptions.DocumentDefinitionError) as exc:
Expand All @@ -308,11 +309,10 @@ class MyDoc(Document):
assert exc.value.args[0] == "EmbeddedField doesn't accept abstract embedded document"
with pytest.raises(exceptions.DocumentDefinitionError) as exc:
@self.instance.register
class MyDoc(Document):
class MyOtherDoc(Document):
impossibru = fields.EmbeddedField(AbstractChild)
assert exc.value.args[0] == "EmbeddedField doesn't accept abstract embedded document"


def test_bad_inheritance(self):
@self.instance.register
class NotAbstractParent(EmbeddedDocument):
Expand Down
20 changes: 14 additions & 6 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ class MyDoc(Document):
assert exc.value.messages == {'embedded_required': ['Missing data for required field.']}
with pytest.raises(ma.ValidationError) as exc:
MyDoc(embedded_required={'optional_field': 1}).required_validate()
assert exc.value.messages == {'embedded_required': {'required_field': ['Missing data for required field.']}}
assert exc.value.messages == {
'embedded_required': {'required_field': ['Missing data for required field.']}}
with pytest.raises(ma.ValidationError) as exc:
MyDoc(
embedded={'optional_field': 1},
embedded_required={'required_field': 42}
).required_validate()
assert exc.value.messages == {'embedded': {'required_field': ['Missing data for required field.']}}
assert exc.value.messages == {
'embedded': {'required_field': ['Missing data for required field.']}}
with pytest.raises(ma.ValidationError) as exc:
MyDoc(
embedded={'required_field': 1},
Expand All @@ -74,11 +76,17 @@ class MyDoc(Document):
).required_validate()
assert exc.value.messages == {
'embedded_list': {0: {'required_field': ['Missing data for required field.']}},
'embedded_dict': {'a': {'value': {'required_field': ['Missing data for required field.']}}},
'embedded_dict': {
'a': {'value': {'required_field': ['Missing data for required field.']}}},
}

# Check valid constructions
doc = MyDoc(embedded={'required_field': 1}, embedded_list=[], embedded_dict={}, embedded_required={'required_field': 42})
doc = MyDoc(
embedded={'required_field': 1},
embedded_list=[],
embedded_dict={},
embedded_required={'required_field': 42}
)
doc.required_validate()
doc = MyDoc(
embedded={'required_field': 1},
Expand All @@ -88,7 +96,6 @@ class MyDoc(Document):
)
doc.required_validate()


def test_required_nested_allow_none(self):
@self.instance.register
class MyEmbedded(EmbeddedDocument):
Expand All @@ -97,7 +104,8 @@ class MyEmbedded(EmbeddedDocument):
@self.instance.register
class MyDoc(Document):
embedded_list = fields.ListField(fields.EmbeddedField(MyEmbedded), allow_none=True)
embedded_dict = fields.DictField(values=fields.EmbeddedField(MyEmbedded), allow_none=True)
embedded_dict = fields.DictField(
values=fields.EmbeddedField(MyEmbedded), allow_none=True)
embedded = fields.EmbeddedField(MyEmbedded, allow_none=True)

MyDoc(embedded_list=None, embedded_dict=None, embedded=None).required_validate()
Expand Down
11 changes: 8 additions & 3 deletions tests/test_marshmallow.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,20 @@ def test_missing_accessor(self):
@self.instance.register
class WithDefault(Document):
with_umongo_default = fields.DateTimeField(default=dt.datetime(1999, 1, 1))
with_marshmallow_missing = fields.DateTimeField(marshmallow_missing=dt.datetime(2000, 1, 1))
with_marshmallow_default = fields.DateTimeField(marshmallow_default=dt.datetime(2001, 1, 1))
with_marshmallow_missing = fields.DateTimeField(
marshmallow_missing=dt.datetime(2000, 1, 1))
with_marshmallow_default = fields.DateTimeField(
marshmallow_default=dt.datetime(2001, 1, 1))
with_marshmallow_and_umongo = fields.DateTimeField(
default=dt.datetime(1999, 1, 1),
marshmallow_missing=dt.datetime(2000, 1, 1),
marshmallow_default=dt.datetime(2001, 1, 1)
)
with_force_missing = fields.DateTimeField(
default=dt.datetime(2001, 1, 1), marshmallow_missing=missing, marshmallow_default=missing)
default=dt.datetime(2001, 1, 1),
marshmallow_missing=missing,
marshmallow_default=missing
)
with_nothing = fields.StrField()

ma_schema = WithDefault.schema.as_marshmallow_schema()()
Expand Down
6 changes: 4 additions & 2 deletions tests/test_query_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class Book(Document):

book_fields = Book.schema.fields
# No changes needed
assert map_query({'title': 'The Lord of The Ring'}, book_fields) == {'title': 'The Lord of The Ring'}
assert map_query({'title': 'The Lord of The Ring'}, book_fields) == {
'title': 'The Lord of The Ring'}
# Single substitution
assert map_query({'length': 350}, book_fields) == {'l': 350}
# Multiple substitutions
Expand Down Expand Up @@ -122,4 +123,5 @@ class Team(Document):
assert map_query({'sponsors.name': 1}, team_fields) == {'s.cn': 1}
assert map_query({'sponsors': {'name': 1}}, team_fields) == {'s': {'cn': 1}}
assert map_query({'sponsors.contact.name': 1}, team_fields) == {'s.cc.pn': 1}
assert map_query({'sponsors': {'contact': {'name': 1}}}, team_fields) == {'s': {'cc': {'pn': 1}}}
assert map_query(
{'sponsors': {'contact': {'name': 1}}}, team_fields) == {'s': {'cc': {'pn': 1}}}
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ deps =
flake8>=3.7.0
skip_install = true
commands =
flake8 umongo
flake8

; If you want to make tox run the tests with the same versions, create a
; requirements.txt with the pinned versions and uncomment the following lines:
Expand Down

0 comments on commit 5b458d8

Please sign in to comment.