diff --git a/tests/frameworks/test_motor_asyncio.py b/tests/frameworks/test_motor_asyncio.py index 42f8443b..317a7d69 100644 --- a/tests/frameworks/test_motor_asyncio.py +++ b/tests/frameworks/test_motor_asyncio.py @@ -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 @@ -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): @@ -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() @@ -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)], @@ -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) @@ -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()) diff --git a/tests/frameworks/test_txmongo.py b/tests/frameworks/test_txmongo.py index 4074daff..e6e3fe13 100644 --- a/tests/frameworks/test_txmongo.py +++ b/tests/frameworks/test_txmongo.py @@ -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' @@ -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 @@ -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() @@ -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 @@ -929,5 +931,3 @@ class Person(PrePostHooksMixin, Document): callbacks.clear() yield p.delete() assert callbacks == ['pre_delete', 'post_delete'] - - diff --git a/tests/test_data_proxy.py b/tests/test_data_proxy.py index 4dbe1b9a..9b966a07 100644 --- a/tests/test_data_proxy.py +++ b/tests/test_data_proxy.py @@ -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() @@ -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: diff --git a/tests/test_document.py b/tests/test_document.py index 3dbd67e7..27640e2f 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -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): diff --git a/tests/test_embedded_document.py b/tests/test_embedded_document.py index 52c36f9a..a9237ca1 100644 --- a/tests/test_embedded_document.py +++ b/tests/test_embedded_document.py @@ -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=[{}]) @@ -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: @@ -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): diff --git a/tests/test_fields.py b/tests/test_fields.py index 8ad5e57e..d3735b90 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -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}, @@ -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}, @@ -88,7 +96,6 @@ class MyDoc(Document): ) doc.required_validate() - def test_required_nested_allow_none(self): @self.instance.register class MyEmbedded(EmbeddedDocument): @@ -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() diff --git a/tests/test_marshmallow.py b/tests/test_marshmallow.py index 497eb9c6..4e94709d 100644 --- a/tests/test_marshmallow.py +++ b/tests/test_marshmallow.py @@ -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()() diff --git a/tests/test_query_mapper.py b/tests/test_query_mapper.py index c0fe8eea..235a0bd7 100644 --- a/tests/test_query_mapper.py +++ b/tests/test_query_mapper.py @@ -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 @@ -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}}} diff --git a/tox.ini b/tox.ini index 118a0334..74d33c6b 100644 --- a/tox.ini +++ b/tox.ini @@ -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: