ran unittest2pytest

This commit is contained in:
Bastien Gérard
2019-08-30 16:13:30 +03:00
parent aa6ff8c84a
commit ac25f4b98b
46 changed files with 4247 additions and 4428 deletions

View File

@@ -29,43 +29,40 @@ class TestClassMethods(unittest.TestCase):
def test_definition(self):
"""Ensure that document may be defined using fields.
"""
self.assertEqual(
["_cls", "age", "id", "name"], sorted(self.Person._fields.keys())
)
self.assertEqual(
["IntField", "ObjectIdField", "StringField", "StringField"],
sorted([x.__class__.__name__ for x in self.Person._fields.values()]),
assert ["_cls", "age", "id", "name"] == sorted(self.Person._fields.keys())
assert ["IntField", "ObjectIdField", "StringField", "StringField"] == sorted(
[x.__class__.__name__ for x in self.Person._fields.values()]
)
def test_get_db(self):
"""Ensure that get_db returns the expected db.
"""
db = self.Person._get_db()
self.assertEqual(self.db, db)
assert self.db == db
def test_get_collection_name(self):
"""Ensure that get_collection_name returns the expected collection
name.
"""
collection_name = "person"
self.assertEqual(collection_name, self.Person._get_collection_name())
assert collection_name == self.Person._get_collection_name()
def test_get_collection(self):
"""Ensure that get_collection returns the expected collection.
"""
collection_name = "person"
collection = self.Person._get_collection()
self.assertEqual(self.db[collection_name], collection)
assert self.db[collection_name] == collection
def test_drop_collection(self):
"""Ensure that the collection may be dropped from the database.
"""
collection_name = "person"
self.Person(name="Test").save()
self.assertIn(collection_name, list_collection_names(self.db))
assert collection_name in list_collection_names(self.db)
self.Person.drop_collection()
self.assertNotIn(collection_name, list_collection_names(self.db))
assert collection_name not in list_collection_names(self.db)
def test_register_delete_rule(self):
"""Ensure that register delete rule adds a delete rule to the document
@@ -75,12 +72,10 @@ class TestClassMethods(unittest.TestCase):
class Job(Document):
employee = ReferenceField(self.Person)
self.assertEqual(self.Person._meta.get("delete_rules"), None)
assert self.Person._meta.get("delete_rules") == None
self.Person.register_delete_rule(Job, "employee", NULLIFY)
self.assertEqual(
self.Person._meta["delete_rules"], {(Job, "employee"): NULLIFY}
)
assert self.Person._meta["delete_rules"] == {(Job, "employee"): NULLIFY}
def test_compare_indexes(self):
""" Ensure that the indexes are properly created and that
@@ -98,22 +93,22 @@ class TestClassMethods(unittest.TestCase):
BlogPost.drop_collection()
BlogPost.ensure_indexes()
self.assertEqual(BlogPost.compare_indexes(), {"missing": [], "extra": []})
assert BlogPost.compare_indexes() == {"missing": [], "extra": []}
BlogPost.ensure_index(["author", "description"])
self.assertEqual(
BlogPost.compare_indexes(),
{"missing": [], "extra": [[("author", 1), ("description", 1)]]},
)
assert BlogPost.compare_indexes() == {
"missing": [],
"extra": [[("author", 1), ("description", 1)]],
}
BlogPost._get_collection().drop_index("author_1_description_1")
self.assertEqual(BlogPost.compare_indexes(), {"missing": [], "extra": []})
assert BlogPost.compare_indexes() == {"missing": [], "extra": []}
BlogPost._get_collection().drop_index("author_1_title_1")
self.assertEqual(
BlogPost.compare_indexes(),
{"missing": [[("author", 1), ("title", 1)]], "extra": []},
)
assert BlogPost.compare_indexes() == {
"missing": [[("author", 1), ("title", 1)]],
"extra": [],
}
def test_compare_indexes_inheritance(self):
""" Ensure that the indexes are properly created and that
@@ -138,22 +133,22 @@ class TestClassMethods(unittest.TestCase):
BlogPost.ensure_indexes()
BlogPostWithTags.ensure_indexes()
self.assertEqual(BlogPost.compare_indexes(), {"missing": [], "extra": []})
assert BlogPost.compare_indexes() == {"missing": [], "extra": []}
BlogPostWithTags.ensure_index(["author", "tag_list"])
self.assertEqual(
BlogPost.compare_indexes(),
{"missing": [], "extra": [[("_cls", 1), ("author", 1), ("tag_list", 1)]]},
)
assert BlogPost.compare_indexes() == {
"missing": [],
"extra": [[("_cls", 1), ("author", 1), ("tag_list", 1)]],
}
BlogPostWithTags._get_collection().drop_index("_cls_1_author_1_tag_list_1")
self.assertEqual(BlogPost.compare_indexes(), {"missing": [], "extra": []})
assert BlogPost.compare_indexes() == {"missing": [], "extra": []}
BlogPostWithTags._get_collection().drop_index("_cls_1_author_1_tags_1")
self.assertEqual(
BlogPost.compare_indexes(),
{"missing": [[("_cls", 1), ("author", 1), ("tags", 1)]], "extra": []},
)
assert BlogPost.compare_indexes() == {
"missing": [[("_cls", 1), ("author", 1), ("tags", 1)]],
"extra": [],
}
def test_compare_indexes_multiple_subclasses(self):
""" Ensure that compare_indexes behaves correctly if called from a
@@ -182,13 +177,9 @@ class TestClassMethods(unittest.TestCase):
BlogPostWithTags.ensure_indexes()
BlogPostWithCustomField.ensure_indexes()
self.assertEqual(BlogPost.compare_indexes(), {"missing": [], "extra": []})
self.assertEqual(
BlogPostWithTags.compare_indexes(), {"missing": [], "extra": []}
)
self.assertEqual(
BlogPostWithCustomField.compare_indexes(), {"missing": [], "extra": []}
)
assert BlogPost.compare_indexes() == {"missing": [], "extra": []}
assert BlogPostWithTags.compare_indexes() == {"missing": [], "extra": []}
assert BlogPostWithCustomField.compare_indexes() == {"missing": [], "extra": []}
def test_compare_indexes_for_text_indexes(self):
""" Ensure that compare_indexes behaves correctly for text indexes """
@@ -210,7 +201,7 @@ class TestClassMethods(unittest.TestCase):
Doc.ensure_indexes()
actual = Doc.compare_indexes()
expected = {"missing": [], "extra": []}
self.assertEqual(actual, expected)
assert actual == expected
def test_list_indexes_inheritance(self):
""" ensure that all of the indexes are listed regardless of the super-
@@ -240,19 +231,14 @@ class TestClassMethods(unittest.TestCase):
BlogPostWithTags.ensure_indexes()
BlogPostWithTagsAndExtraText.ensure_indexes()
self.assertEqual(BlogPost.list_indexes(), BlogPostWithTags.list_indexes())
self.assertEqual(
BlogPost.list_indexes(), BlogPostWithTagsAndExtraText.list_indexes()
)
self.assertEqual(
BlogPost.list_indexes(),
[
[("_cls", 1), ("author", 1), ("tags", 1)],
[("_cls", 1), ("author", 1), ("tags", 1), ("extra_text", 1)],
[(u"_id", 1)],
[("_cls", 1)],
],
)
assert BlogPost.list_indexes() == BlogPostWithTags.list_indexes()
assert BlogPost.list_indexes() == BlogPostWithTagsAndExtraText.list_indexes()
assert BlogPost.list_indexes() == [
[("_cls", 1), ("author", 1), ("tags", 1)],
[("_cls", 1), ("author", 1), ("tags", 1), ("extra_text", 1)],
[(u"_id", 1)],
[("_cls", 1)],
]
def test_register_delete_rule_inherited(self):
class Vaccine(Document):
@@ -271,8 +257,8 @@ class TestClassMethods(unittest.TestCase):
class Cat(Animal):
name = StringField(required=True)
self.assertEqual(Vaccine._meta["delete_rules"][(Animal, "vaccine_made")], PULL)
self.assertEqual(Vaccine._meta["delete_rules"][(Cat, "vaccine_made")], PULL)
assert Vaccine._meta["delete_rules"][(Animal, "vaccine_made")] == PULL
assert Vaccine._meta["delete_rules"][(Cat, "vaccine_made")] == PULL
def test_collection_naming(self):
"""Ensure that a collection with a specified name may be used.
@@ -281,19 +267,17 @@ class TestClassMethods(unittest.TestCase):
class DefaultNamingTest(Document):
pass
self.assertEqual(
"default_naming_test", DefaultNamingTest._get_collection_name()
)
assert "default_naming_test" == DefaultNamingTest._get_collection_name()
class CustomNamingTest(Document):
meta = {"collection": "pimp_my_collection"}
self.assertEqual("pimp_my_collection", CustomNamingTest._get_collection_name())
assert "pimp_my_collection" == CustomNamingTest._get_collection_name()
class DynamicNamingTest(Document):
meta = {"collection": lambda c: "DYNAMO"}
self.assertEqual("DYNAMO", DynamicNamingTest._get_collection_name())
assert "DYNAMO" == DynamicNamingTest._get_collection_name()
# Use Abstract class to handle backwards compatibility
class BaseDocument(Document):
@@ -302,14 +286,12 @@ class TestClassMethods(unittest.TestCase):
class OldNamingConvention(BaseDocument):
pass
self.assertEqual(
"oldnamingconvention", OldNamingConvention._get_collection_name()
)
assert "oldnamingconvention" == OldNamingConvention._get_collection_name()
class InheritedAbstractNamingTest(BaseDocument):
meta = {"collection": "wibble"}
self.assertEqual("wibble", InheritedAbstractNamingTest._get_collection_name())
assert "wibble" == InheritedAbstractNamingTest._get_collection_name()
# Mixin tests
class BaseMixin(object):
@@ -318,8 +300,9 @@ class TestClassMethods(unittest.TestCase):
class OldMixinNamingConvention(Document, BaseMixin):
pass
self.assertEqual(
"oldmixinnamingconvention", OldMixinNamingConvention._get_collection_name()
assert (
"oldmixinnamingconvention"
== OldMixinNamingConvention._get_collection_name()
)
class BaseMixin(object):
@@ -331,7 +314,7 @@ class TestClassMethods(unittest.TestCase):
class MyDocument(BaseDocument):
pass
self.assertEqual("basedocument", MyDocument._get_collection_name())
assert "basedocument" == MyDocument._get_collection_name()
def test_custom_collection_name_operations(self):
"""Ensure that a collection with a specified name is used as expected.
@@ -343,16 +326,16 @@ class TestClassMethods(unittest.TestCase):
meta = {"collection": collection_name}
Person(name="Test User").save()
self.assertIn(collection_name, list_collection_names(self.db))
assert collection_name in list_collection_names(self.db)
user_obj = self.db[collection_name].find_one()
self.assertEqual(user_obj["name"], "Test User")
assert user_obj["name"] == "Test User"
user_obj = Person.objects[0]
self.assertEqual(user_obj.name, "Test User")
assert user_obj.name == "Test User"
Person.drop_collection()
self.assertNotIn(collection_name, list_collection_names(self.db))
assert collection_name not in list_collection_names(self.db)
def test_collection_name_and_primary(self):
"""Ensure that a collection with a specified name may be used.
@@ -365,7 +348,7 @@ class TestClassMethods(unittest.TestCase):
Person(name="Test User").save()
user_obj = Person.objects.first()
self.assertEqual(user_obj.name, "Test User")
assert user_obj.name == "Test User"
Person.drop_collection()