Format the codebase using Black (#2109)
This commit: 1. Formats all of our existing code using `black`. 2. Adds a note about using `black` to `CONTRIBUTING.rst`. 3. Runs `black --check` as part of CI (failing builds that aren't properly formatted).
This commit is contained in:
		| @@ -7,12 +7,12 @@ from tests.utils import MongoDBTestCase | ||||
|  | ||||
|  | ||||
| class TestCachedReferenceField(MongoDBTestCase): | ||||
|  | ||||
|     def test_get_and_save(self): | ||||
|         """ | ||||
|         Tests #1047: CachedReferenceField creates DBRefs on to_python, | ||||
|         but can't save them on to_mongo. | ||||
|         """ | ||||
|  | ||||
|         class Animal(Document): | ||||
|             name = StringField() | ||||
|             tag = StringField() | ||||
| @@ -24,10 +24,11 @@ class TestCachedReferenceField(MongoDBTestCase): | ||||
|         Animal.drop_collection() | ||||
|         Ocorrence.drop_collection() | ||||
|  | ||||
|         Ocorrence(person="testte", | ||||
|                   animal=Animal(name="Leopard", tag="heavy").save()).save() | ||||
|         Ocorrence( | ||||
|             person="testte", animal=Animal(name="Leopard", tag="heavy").save() | ||||
|         ).save() | ||||
|         p = Ocorrence.objects.get() | ||||
|         p.person = 'new_testte' | ||||
|         p.person = "new_testte" | ||||
|         p.save() | ||||
|  | ||||
|     def test_general_things(self): | ||||
| @@ -37,8 +38,7 @@ class TestCachedReferenceField(MongoDBTestCase): | ||||
|  | ||||
|         class Ocorrence(Document): | ||||
|             person = StringField() | ||||
|             animal = CachedReferenceField( | ||||
|                 Animal, fields=['tag']) | ||||
|             animal = CachedReferenceField(Animal, fields=["tag"]) | ||||
|  | ||||
|         Animal.drop_collection() | ||||
|         Ocorrence.drop_collection() | ||||
| @@ -55,19 +55,18 @@ class TestCachedReferenceField(MongoDBTestCase): | ||||
|  | ||||
|         self.assertEqual(Ocorrence.objects(animal=None).count(), 1) | ||||
|  | ||||
|         self.assertEqual( | ||||
|             a.to_mongo(fields=['tag']), {'tag': 'heavy', "_id": a.pk}) | ||||
|         self.assertEqual(a.to_mongo(fields=["tag"]), {"tag": "heavy", "_id": a.pk}) | ||||
|  | ||||
|         self.assertEqual(o.to_mongo()['animal']['tag'], 'heavy') | ||||
|         self.assertEqual(o.to_mongo()["animal"]["tag"], "heavy") | ||||
|  | ||||
|         # counts | ||||
|         Ocorrence(person="teste 2").save() | ||||
|         Ocorrence(person="teste 3").save() | ||||
|  | ||||
|         count = Ocorrence.objects(animal__tag='heavy').count() | ||||
|         count = Ocorrence.objects(animal__tag="heavy").count() | ||||
|         self.assertEqual(count, 1) | ||||
|  | ||||
|         ocorrence = Ocorrence.objects(animal__tag='heavy').first() | ||||
|         ocorrence = Ocorrence.objects(animal__tag="heavy").first() | ||||
|         self.assertEqual(ocorrence.person, "teste") | ||||
|         self.assertIsInstance(ocorrence.animal, Animal) | ||||
|  | ||||
| @@ -78,28 +77,21 @@ class TestCachedReferenceField(MongoDBTestCase): | ||||
|  | ||||
|         class SocialTest(Document): | ||||
|             group = StringField() | ||||
|             person = CachedReferenceField( | ||||
|                 PersonAuto, | ||||
|                 fields=('salary',)) | ||||
|             person = CachedReferenceField(PersonAuto, fields=("salary",)) | ||||
|  | ||||
|         PersonAuto.drop_collection() | ||||
|         SocialTest.drop_collection() | ||||
|  | ||||
|         p = PersonAuto(name="Alberto", salary=Decimal('7000.00')) | ||||
|         p = PersonAuto(name="Alberto", salary=Decimal("7000.00")) | ||||
|         p.save() | ||||
|  | ||||
|         s = SocialTest(group="dev", person=p) | ||||
|         s.save() | ||||
|  | ||||
|         self.assertEqual( | ||||
|             SocialTest.objects._collection.find_one({'person.salary': 7000.00}), { | ||||
|                 '_id': s.pk, | ||||
|                 'group': s.group, | ||||
|                 'person': { | ||||
|                     '_id': p.pk, | ||||
|                     'salary': 7000.00 | ||||
|                 } | ||||
|             }) | ||||
|             SocialTest.objects._collection.find_one({"person.salary": 7000.00}), | ||||
|             {"_id": s.pk, "group": s.group, "person": {"_id": p.pk, "salary": 7000.00}}, | ||||
|         ) | ||||
|  | ||||
|     def test_cached_reference_field_reference(self): | ||||
|         class Group(Document): | ||||
| @@ -111,17 +103,14 @@ class TestCachedReferenceField(MongoDBTestCase): | ||||
|  | ||||
|         class SocialData(Document): | ||||
|             obs = StringField() | ||||
|             tags = ListField( | ||||
|                 StringField()) | ||||
|             person = CachedReferenceField( | ||||
|                 Person, | ||||
|                 fields=('group',)) | ||||
|             tags = ListField(StringField()) | ||||
|             person = CachedReferenceField(Person, fields=("group",)) | ||||
|  | ||||
|         Group.drop_collection() | ||||
|         Person.drop_collection() | ||||
|         SocialData.drop_collection() | ||||
|  | ||||
|         g1 = Group(name='dev') | ||||
|         g1 = Group(name="dev") | ||||
|         g1.save() | ||||
|  | ||||
|         g2 = Group(name="designers") | ||||
| @@ -136,22 +125,21 @@ class TestCachedReferenceField(MongoDBTestCase): | ||||
|         p3 = Person(name="Afro design", group=g2) | ||||
|         p3.save() | ||||
|  | ||||
|         s1 = SocialData(obs="testing 123", person=p1, tags=['tag1', 'tag2']) | ||||
|         s1 = SocialData(obs="testing 123", person=p1, tags=["tag1", "tag2"]) | ||||
|         s1.save() | ||||
|  | ||||
|         s2 = SocialData(obs="testing 321", person=p3, tags=['tag3', 'tag4']) | ||||
|         s2 = SocialData(obs="testing 321", person=p3, tags=["tag3", "tag4"]) | ||||
|         s2.save() | ||||
|  | ||||
|         self.assertEqual(SocialData.objects._collection.find_one( | ||||
|             {'tags': 'tag2'}), { | ||||
|                 '_id': s1.pk, | ||||
|                 'obs': 'testing 123', | ||||
|                 'tags': ['tag1', 'tag2'], | ||||
|                 'person': { | ||||
|                     '_id': p1.pk, | ||||
|                     'group': g1.pk | ||||
|                 } | ||||
|         }) | ||||
|         self.assertEqual( | ||||
|             SocialData.objects._collection.find_one({"tags": "tag2"}), | ||||
|             { | ||||
|                 "_id": s1.pk, | ||||
|                 "obs": "testing 123", | ||||
|                 "tags": ["tag1", "tag2"], | ||||
|                 "person": {"_id": p1.pk, "group": g1.pk}, | ||||
|             }, | ||||
|         ) | ||||
|  | ||||
|         self.assertEqual(SocialData.objects(person__group=g2).count(), 1) | ||||
|         self.assertEqual(SocialData.objects(person__group=g2).first(), s2) | ||||
| @@ -163,23 +151,18 @@ class TestCachedReferenceField(MongoDBTestCase): | ||||
|         Product.drop_collection() | ||||
|  | ||||
|         class Basket(Document): | ||||
|             products = ListField(CachedReferenceField(Product, fields=['name'])) | ||||
|             products = ListField(CachedReferenceField(Product, fields=["name"])) | ||||
|  | ||||
|         Basket.drop_collection() | ||||
|         product1 = Product(name='abc').save() | ||||
|         product2 = Product(name='def').save() | ||||
|         product1 = Product(name="abc").save() | ||||
|         product2 = Product(name="def").save() | ||||
|         basket = Basket(products=[product1]).save() | ||||
|         self.assertEqual( | ||||
|             Basket.objects._collection.find_one(), | ||||
|             { | ||||
|                 '_id': basket.pk, | ||||
|                 'products': [ | ||||
|                     { | ||||
|                         '_id': product1.pk, | ||||
|                         'name': product1.name | ||||
|                     } | ||||
|                 ] | ||||
|             } | ||||
|                 "_id": basket.pk, | ||||
|                 "products": [{"_id": product1.pk, "name": product1.name}], | ||||
|             }, | ||||
|         ) | ||||
|         # push to list | ||||
|         basket.update(push__products=product2) | ||||
| @@ -187,161 +170,135 @@ class TestCachedReferenceField(MongoDBTestCase): | ||||
|         self.assertEqual( | ||||
|             Basket.objects._collection.find_one(), | ||||
|             { | ||||
|                 '_id': basket.pk, | ||||
|                 'products': [ | ||||
|                     { | ||||
|                         '_id': product1.pk, | ||||
|                         'name': product1.name | ||||
|                     }, | ||||
|                     { | ||||
|                         '_id': product2.pk, | ||||
|                         'name': product2.name | ||||
|                     } | ||||
|                 ] | ||||
|             } | ||||
|                 "_id": basket.pk, | ||||
|                 "products": [ | ||||
|                     {"_id": product1.pk, "name": product1.name}, | ||||
|                     {"_id": product2.pk, "name": product2.name}, | ||||
|                 ], | ||||
|             }, | ||||
|         ) | ||||
|  | ||||
|     def test_cached_reference_field_update_all(self): | ||||
|         class Person(Document): | ||||
|             TYPES = ( | ||||
|                 ('pf', "PF"), | ||||
|                 ('pj', "PJ") | ||||
|             ) | ||||
|             TYPES = (("pf", "PF"), ("pj", "PJ")) | ||||
|             name = StringField() | ||||
|             tp = StringField(choices=TYPES) | ||||
|             father = CachedReferenceField('self', fields=('tp',)) | ||||
|             father = CachedReferenceField("self", fields=("tp",)) | ||||
|  | ||||
|         Person.drop_collection() | ||||
|  | ||||
|         a1 = Person(name="Wilson Father", tp="pj") | ||||
|         a1.save() | ||||
|  | ||||
|         a2 = Person(name='Wilson Junior', tp='pf', father=a1) | ||||
|         a2 = Person(name="Wilson Junior", tp="pf", father=a1) | ||||
|         a2.save() | ||||
|  | ||||
|         a2 = Person.objects.with_id(a2.id) | ||||
|         self.assertEqual(a2.father.tp, a1.tp) | ||||
|  | ||||
|         self.assertEqual(dict(a2.to_mongo()), { | ||||
|             "_id": a2.pk, | ||||
|             "name": u"Wilson Junior", | ||||
|             "tp": u"pf", | ||||
|             "father": { | ||||
|                 "_id": a1.pk, | ||||
|                 "tp": u"pj" | ||||
|             } | ||||
|         }) | ||||
|         self.assertEqual( | ||||
|             dict(a2.to_mongo()), | ||||
|             { | ||||
|                 "_id": a2.pk, | ||||
|                 "name": u"Wilson Junior", | ||||
|                 "tp": u"pf", | ||||
|                 "father": {"_id": a1.pk, "tp": u"pj"}, | ||||
|             }, | ||||
|         ) | ||||
|  | ||||
|         self.assertEqual(Person.objects(father=a1)._query, { | ||||
|             'father._id': a1.pk | ||||
|         }) | ||||
|         self.assertEqual(Person.objects(father=a1)._query, {"father._id": a1.pk}) | ||||
|         self.assertEqual(Person.objects(father=a1).count(), 1) | ||||
|  | ||||
|         Person.objects.update(set__tp="pf") | ||||
|         Person.father.sync_all() | ||||
|  | ||||
|         a2.reload() | ||||
|         self.assertEqual(dict(a2.to_mongo()), { | ||||
|             "_id": a2.pk, | ||||
|             "name": u"Wilson Junior", | ||||
|             "tp": u"pf", | ||||
|             "father": { | ||||
|                 "_id": a1.pk, | ||||
|                 "tp": u"pf" | ||||
|             } | ||||
|         }) | ||||
|         self.assertEqual( | ||||
|             dict(a2.to_mongo()), | ||||
|             { | ||||
|                 "_id": a2.pk, | ||||
|                 "name": u"Wilson Junior", | ||||
|                 "tp": u"pf", | ||||
|                 "father": {"_id": a1.pk, "tp": u"pf"}, | ||||
|             }, | ||||
|         ) | ||||
|  | ||||
|     def test_cached_reference_fields_on_embedded_documents(self): | ||||
|         with self.assertRaises(InvalidDocumentError): | ||||
|  | ||||
|             class Test(Document): | ||||
|                 name = StringField() | ||||
|  | ||||
|             type('WrongEmbeddedDocument', ( | ||||
|                 EmbeddedDocument,), { | ||||
|                     'test': CachedReferenceField(Test) | ||||
|             }) | ||||
|             type( | ||||
|                 "WrongEmbeddedDocument", | ||||
|                 (EmbeddedDocument,), | ||||
|                 {"test": CachedReferenceField(Test)}, | ||||
|             ) | ||||
|  | ||||
|     def test_cached_reference_auto_sync(self): | ||||
|         class Person(Document): | ||||
|             TYPES = ( | ||||
|                 ('pf', "PF"), | ||||
|                 ('pj', "PJ") | ||||
|             ) | ||||
|             TYPES = (("pf", "PF"), ("pj", "PJ")) | ||||
|             name = StringField() | ||||
|             tp = StringField( | ||||
|                 choices=TYPES | ||||
|             ) | ||||
|             tp = StringField(choices=TYPES) | ||||
|  | ||||
|             father = CachedReferenceField('self', fields=('tp',)) | ||||
|             father = CachedReferenceField("self", fields=("tp",)) | ||||
|  | ||||
|         Person.drop_collection() | ||||
|  | ||||
|         a1 = Person(name="Wilson Father", tp="pj") | ||||
|         a1.save() | ||||
|  | ||||
|         a2 = Person(name='Wilson Junior', tp='pf', father=a1) | ||||
|         a2 = Person(name="Wilson Junior", tp="pf", father=a1) | ||||
|         a2.save() | ||||
|  | ||||
|         a1.tp = 'pf' | ||||
|         a1.tp = "pf" | ||||
|         a1.save() | ||||
|  | ||||
|         a2.reload() | ||||
|         self.assertEqual(dict(a2.to_mongo()), { | ||||
|             '_id': a2.pk, | ||||
|             'name': 'Wilson Junior', | ||||
|             'tp': 'pf', | ||||
|             'father': { | ||||
|                 '_id': a1.pk, | ||||
|                 'tp': 'pf' | ||||
|             } | ||||
|         }) | ||||
|         self.assertEqual( | ||||
|             dict(a2.to_mongo()), | ||||
|             { | ||||
|                 "_id": a2.pk, | ||||
|                 "name": "Wilson Junior", | ||||
|                 "tp": "pf", | ||||
|                 "father": {"_id": a1.pk, "tp": "pf"}, | ||||
|             }, | ||||
|         ) | ||||
|  | ||||
|     def test_cached_reference_auto_sync_disabled(self): | ||||
|         class Persone(Document): | ||||
|             TYPES = ( | ||||
|                 ('pf', "PF"), | ||||
|                 ('pj', "PJ") | ||||
|             ) | ||||
|             TYPES = (("pf", "PF"), ("pj", "PJ")) | ||||
|             name = StringField() | ||||
|             tp = StringField( | ||||
|                 choices=TYPES | ||||
|             ) | ||||
|             tp = StringField(choices=TYPES) | ||||
|  | ||||
|             father = CachedReferenceField( | ||||
|                 'self', fields=('tp',), auto_sync=False) | ||||
|             father = CachedReferenceField("self", fields=("tp",), auto_sync=False) | ||||
|  | ||||
|         Persone.drop_collection() | ||||
|  | ||||
|         a1 = Persone(name="Wilson Father", tp="pj") | ||||
|         a1.save() | ||||
|  | ||||
|         a2 = Persone(name='Wilson Junior', tp='pf', father=a1) | ||||
|         a2 = Persone(name="Wilson Junior", tp="pf", father=a1) | ||||
|         a2.save() | ||||
|  | ||||
|         a1.tp = 'pf' | ||||
|         a1.tp = "pf" | ||||
|         a1.save() | ||||
|  | ||||
|         self.assertEqual(Persone.objects._collection.find_one({'_id': a2.pk}), { | ||||
|             '_id': a2.pk, | ||||
|             'name': 'Wilson Junior', | ||||
|             'tp': 'pf', | ||||
|             'father': { | ||||
|                 '_id': a1.pk, | ||||
|                 'tp': 'pj' | ||||
|             } | ||||
|         }) | ||||
|         self.assertEqual( | ||||
|             Persone.objects._collection.find_one({"_id": a2.pk}), | ||||
|             { | ||||
|                 "_id": a2.pk, | ||||
|                 "name": "Wilson Junior", | ||||
|                 "tp": "pf", | ||||
|                 "father": {"_id": a1.pk, "tp": "pj"}, | ||||
|             }, | ||||
|         ) | ||||
|  | ||||
|     def test_cached_reference_embedded_fields(self): | ||||
|         class Owner(EmbeddedDocument): | ||||
|             TPS = ( | ||||
|                 ('n', "Normal"), | ||||
|                 ('u', "Urgent") | ||||
|             ) | ||||
|             TPS = (("n", "Normal"), ("u", "Urgent")) | ||||
|             name = StringField() | ||||
|             tp = StringField( | ||||
|                 verbose_name="Type", | ||||
|                 db_field="t", | ||||
|                 choices=TPS) | ||||
|             tp = StringField(verbose_name="Type", db_field="t", choices=TPS) | ||||
|  | ||||
|         class Animal(Document): | ||||
|             name = StringField() | ||||
| @@ -351,43 +308,38 @@ class TestCachedReferenceField(MongoDBTestCase): | ||||
|  | ||||
|         class Ocorrence(Document): | ||||
|             person = StringField() | ||||
|             animal = CachedReferenceField( | ||||
|                 Animal, fields=['tag', 'owner.tp']) | ||||
|             animal = CachedReferenceField(Animal, fields=["tag", "owner.tp"]) | ||||
|  | ||||
|         Animal.drop_collection() | ||||
|         Ocorrence.drop_collection() | ||||
|  | ||||
|         a = Animal(name="Leopard", tag="heavy", | ||||
|                    owner=Owner(tp='u', name="Wilson Júnior") | ||||
|                    ) | ||||
|         a = Animal( | ||||
|             name="Leopard", tag="heavy", owner=Owner(tp="u", name="Wilson Júnior") | ||||
|         ) | ||||
|         a.save() | ||||
|  | ||||
|         o = Ocorrence(person="teste", animal=a) | ||||
|         o.save() | ||||
|         self.assertEqual(dict(a.to_mongo(fields=['tag', 'owner.tp'])), { | ||||
|             '_id': a.pk, | ||||
|             'tag': 'heavy', | ||||
|             'owner': { | ||||
|                 't': 'u' | ||||
|             } | ||||
|         }) | ||||
|         self.assertEqual(o.to_mongo()['animal']['tag'], 'heavy') | ||||
|         self.assertEqual(o.to_mongo()['animal']['owner']['t'], 'u') | ||||
|         self.assertEqual( | ||||
|             dict(a.to_mongo(fields=["tag", "owner.tp"])), | ||||
|             {"_id": a.pk, "tag": "heavy", "owner": {"t": "u"}}, | ||||
|         ) | ||||
|         self.assertEqual(o.to_mongo()["animal"]["tag"], "heavy") | ||||
|         self.assertEqual(o.to_mongo()["animal"]["owner"]["t"], "u") | ||||
|  | ||||
|         # Check to_mongo with fields | ||||
|         self.assertNotIn('animal', o.to_mongo(fields=['person'])) | ||||
|         self.assertNotIn("animal", o.to_mongo(fields=["person"])) | ||||
|  | ||||
|         # counts | ||||
|         Ocorrence(person="teste 2").save() | ||||
|         Ocorrence(person="teste 3").save() | ||||
|  | ||||
|         count = Ocorrence.objects( | ||||
|             animal__tag='heavy', animal__owner__tp='u').count() | ||||
|         count = Ocorrence.objects(animal__tag="heavy", animal__owner__tp="u").count() | ||||
|         self.assertEqual(count, 1) | ||||
|  | ||||
|         ocorrence = Ocorrence.objects( | ||||
|             animal__tag='heavy', | ||||
|             animal__owner__tp='u').first() | ||||
|             animal__tag="heavy", animal__owner__tp="u" | ||||
|         ).first() | ||||
|         self.assertEqual(ocorrence.person, "teste") | ||||
|         self.assertIsInstance(ocorrence.animal, Animal) | ||||
|  | ||||
| @@ -404,43 +356,39 @@ class TestCachedReferenceField(MongoDBTestCase): | ||||
|  | ||||
|         class Ocorrence(Document): | ||||
|             person = StringField() | ||||
|             animal = CachedReferenceField( | ||||
|                 Animal, fields=['tag', 'owner.tags']) | ||||
|             animal = CachedReferenceField(Animal, fields=["tag", "owner.tags"]) | ||||
|  | ||||
|         Animal.drop_collection() | ||||
|         Ocorrence.drop_collection() | ||||
|  | ||||
|         a = Animal(name="Leopard", tag="heavy", | ||||
|                    owner=Owner(tags=['cool', 'funny'], | ||||
|                                name="Wilson Júnior") | ||||
|                    ) | ||||
|         a = Animal( | ||||
|             name="Leopard", | ||||
|             tag="heavy", | ||||
|             owner=Owner(tags=["cool", "funny"], name="Wilson Júnior"), | ||||
|         ) | ||||
|         a.save() | ||||
|  | ||||
|         o = Ocorrence(person="teste 2", animal=a) | ||||
|         o.save() | ||||
|         self.assertEqual(dict(a.to_mongo(fields=['tag', 'owner.tags'])), { | ||||
|             '_id': a.pk, | ||||
|             'tag': 'heavy', | ||||
|             'owner': { | ||||
|                 'tags': ['cool', 'funny'] | ||||
|             } | ||||
|         }) | ||||
|         self.assertEqual( | ||||
|             dict(a.to_mongo(fields=["tag", "owner.tags"])), | ||||
|             {"_id": a.pk, "tag": "heavy", "owner": {"tags": ["cool", "funny"]}}, | ||||
|         ) | ||||
|  | ||||
|         self.assertEqual(o.to_mongo()['animal']['tag'], 'heavy') | ||||
|         self.assertEqual(o.to_mongo()['animal']['owner']['tags'], | ||||
|                          ['cool', 'funny']) | ||||
|         self.assertEqual(o.to_mongo()["animal"]["tag"], "heavy") | ||||
|         self.assertEqual(o.to_mongo()["animal"]["owner"]["tags"], ["cool", "funny"]) | ||||
|  | ||||
|         # counts | ||||
|         Ocorrence(person="teste 2").save() | ||||
|         Ocorrence(person="teste 3").save() | ||||
|  | ||||
|         query = Ocorrence.objects( | ||||
|             animal__tag='heavy', animal__owner__tags='cool')._query | ||||
|         self.assertEqual( | ||||
|             query, {'animal.owner.tags': 'cool', 'animal.tag': 'heavy'}) | ||||
|             animal__tag="heavy", animal__owner__tags="cool" | ||||
|         )._query | ||||
|         self.assertEqual(query, {"animal.owner.tags": "cool", "animal.tag": "heavy"}) | ||||
|  | ||||
|         ocorrence = Ocorrence.objects( | ||||
|             animal__tag='heavy', | ||||
|             animal__owner__tags='cool').first() | ||||
|             animal__tag="heavy", animal__owner__tags="cool" | ||||
|         ).first() | ||||
|         self.assertEqual(ocorrence.person, "teste 2") | ||||
|         self.assertIsInstance(ocorrence.animal, Animal) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user