Merge branch 'master' of github.com:MongoEngine/mongoengine into negative_indexes_in_list
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from .class_methods import *
|
||||
from .delta import *
|
||||
from .dynamic import *
|
||||
from .indexes import *
|
||||
from .inheritance import *
|
||||
from .instance import *
|
||||
from .json_serialisation import *
|
||||
from .validation import *
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -2,15 +2,12 @@
|
||||
import unittest
|
||||
|
||||
from mongoengine import *
|
||||
from mongoengine.pymongo_support import list_collection_names
|
||||
|
||||
from mongoengine.queryset import NULLIFY, PULL
|
||||
from mongoengine.connection import get_db
|
||||
|
||||
__all__ = ("ClassMethodsTest",)
|
||||
from mongoengine.pymongo_support import list_collection_names
|
||||
from mongoengine.queryset import NULLIFY, PULL
|
||||
|
||||
|
||||
class ClassMethodsTest(unittest.TestCase):
|
||||
class TestClassMethods(unittest.TestCase):
|
||||
def setUp(self):
|
||||
connect(db="mongoenginetest")
|
||||
self.db = get_db()
|
||||
@@ -32,43 +29,40 @@ class ClassMethodsTest(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
|
||||
@@ -78,12 +72,10 @@ class ClassMethodsTest(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") is 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
|
||||
@@ -101,22 +93,22 @@ class ClassMethodsTest(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
|
||||
@@ -141,22 +133,22 @@ class ClassMethodsTest(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
|
||||
@@ -185,13 +177,9 @@ class ClassMethodsTest(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 """
|
||||
@@ -213,7 +201,7 @@ class ClassMethodsTest(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-
|
||||
@@ -243,19 +231,14 @@ class ClassMethodsTest(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):
|
||||
@@ -274,8 +257,8 @@ class ClassMethodsTest(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.
|
||||
@@ -284,19 +267,17 @@ class ClassMethodsTest(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):
|
||||
@@ -305,14 +286,12 @@ class ClassMethodsTest(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):
|
||||
@@ -321,8 +300,9 @@ class ClassMethodsTest(unittest.TestCase):
|
||||
class OldMixinNamingConvention(Document, BaseMixin):
|
||||
pass
|
||||
|
||||
self.assertEqual(
|
||||
"oldmixinnamingconvention", OldMixinNamingConvention._get_collection_name()
|
||||
assert (
|
||||
"oldmixinnamingconvention"
|
||||
== OldMixinNamingConvention._get_collection_name()
|
||||
)
|
||||
|
||||
class BaseMixin(object):
|
||||
@@ -334,7 +314,7 @@ class ClassMethodsTest(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.
|
||||
@@ -346,16 +326,16 @@ class ClassMethodsTest(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.
|
||||
@@ -368,7 +348,7 @@ class ClassMethodsTest(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()
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,7 @@
|
||||
import unittest
|
||||
|
||||
import pytest
|
||||
|
||||
from mongoengine import *
|
||||
from tests.utils import MongoDBTestCase
|
||||
|
||||
@@ -25,15 +27,15 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
p.name = "James"
|
||||
p.age = 34
|
||||
|
||||
self.assertEqual(p.to_mongo(), {"_cls": "Person", "name": "James", "age": 34})
|
||||
self.assertEqual(p.to_mongo().keys(), ["_cls", "name", "age"])
|
||||
assert p.to_mongo() == {"_cls": "Person", "name": "James", "age": 34}
|
||||
assert p.to_mongo().keys() == ["_cls", "name", "age"]
|
||||
p.save()
|
||||
self.assertEqual(p.to_mongo().keys(), ["_id", "_cls", "name", "age"])
|
||||
assert p.to_mongo().keys() == ["_id", "_cls", "name", "age"]
|
||||
|
||||
self.assertEqual(self.Person.objects.first().age, 34)
|
||||
assert self.Person.objects.first().age == 34
|
||||
|
||||
# Confirm no changes to self.Person
|
||||
self.assertFalse(hasattr(self.Person, "age"))
|
||||
assert not hasattr(self.Person, "age")
|
||||
|
||||
def test_change_scope_of_variable(self):
|
||||
"""Test changing the scope of a dynamic field has no adverse effects"""
|
||||
@@ -47,7 +49,7 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
p.save()
|
||||
|
||||
p = self.Person.objects.get()
|
||||
self.assertEqual(p.misc, {"hello": "world"})
|
||||
assert p.misc == {"hello": "world"}
|
||||
|
||||
def test_delete_dynamic_field(self):
|
||||
"""Test deleting a dynamic field works"""
|
||||
@@ -62,19 +64,19 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
p.save()
|
||||
|
||||
p = self.Person.objects.get()
|
||||
self.assertEqual(p.misc, {"hello": "world"})
|
||||
assert p.misc == {"hello": "world"}
|
||||
collection = self.db[self.Person._get_collection_name()]
|
||||
obj = collection.find_one()
|
||||
self.assertEqual(sorted(obj.keys()), ["_cls", "_id", "misc", "name"])
|
||||
assert sorted(obj.keys()) == ["_cls", "_id", "misc", "name"]
|
||||
|
||||
del p.misc
|
||||
p.save()
|
||||
|
||||
p = self.Person.objects.get()
|
||||
self.assertFalse(hasattr(p, "misc"))
|
||||
assert not hasattr(p, "misc")
|
||||
|
||||
obj = collection.find_one()
|
||||
self.assertEqual(sorted(obj.keys()), ["_cls", "_id", "name"])
|
||||
assert sorted(obj.keys()) == ["_cls", "_id", "name"]
|
||||
|
||||
def test_reload_after_unsetting(self):
|
||||
p = self.Person()
|
||||
@@ -88,12 +90,12 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
p = self.Person.objects.create()
|
||||
p.update(age=1)
|
||||
|
||||
self.assertEqual(len(p._data), 3)
|
||||
self.assertEqual(sorted(p._data.keys()), ["_cls", "id", "name"])
|
||||
assert len(p._data) == 3
|
||||
assert sorted(p._data.keys()) == ["_cls", "id", "name"]
|
||||
|
||||
p.reload()
|
||||
self.assertEqual(len(p._data), 4)
|
||||
self.assertEqual(sorted(p._data.keys()), ["_cls", "age", "id", "name"])
|
||||
assert len(p._data) == 4
|
||||
assert sorted(p._data.keys()) == ["_cls", "age", "id", "name"]
|
||||
|
||||
def test_fields_without_underscore(self):
|
||||
"""Ensure we can query dynamic fields"""
|
||||
@@ -103,16 +105,18 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
p.save()
|
||||
|
||||
raw_p = Person.objects.as_pymongo().get(id=p.id)
|
||||
self.assertEqual(raw_p, {"_cls": u"Person", "_id": p.id, "name": u"Dean"})
|
||||
assert raw_p == {"_cls": u"Person", "_id": p.id, "name": u"Dean"}
|
||||
|
||||
p.name = "OldDean"
|
||||
p.newattr = "garbage"
|
||||
p.save()
|
||||
raw_p = Person.objects.as_pymongo().get(id=p.id)
|
||||
self.assertEqual(
|
||||
raw_p,
|
||||
{"_cls": u"Person", "_id": p.id, "name": "OldDean", "newattr": u"garbage"},
|
||||
)
|
||||
assert raw_p == {
|
||||
"_cls": u"Person",
|
||||
"_id": p.id,
|
||||
"name": "OldDean",
|
||||
"newattr": u"garbage",
|
||||
}
|
||||
|
||||
def test_fields_containing_underscore(self):
|
||||
"""Ensure we can query dynamic fields"""
|
||||
@@ -127,14 +131,14 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
p.save()
|
||||
|
||||
raw_p = WeirdPerson.objects.as_pymongo().get(id=p.id)
|
||||
self.assertEqual(raw_p, {"_id": p.id, "_name": u"Dean", "name": u"Dean"})
|
||||
assert raw_p == {"_id": p.id, "_name": u"Dean", "name": u"Dean"}
|
||||
|
||||
p.name = "OldDean"
|
||||
p._name = "NewDean"
|
||||
p._newattr1 = "garbage" # Unknown fields won't be added
|
||||
p.save()
|
||||
raw_p = WeirdPerson.objects.as_pymongo().get(id=p.id)
|
||||
self.assertEqual(raw_p, {"_id": p.id, "_name": u"NewDean", "name": u"OldDean"})
|
||||
assert raw_p == {"_id": p.id, "_name": u"NewDean", "name": u"OldDean"}
|
||||
|
||||
def test_dynamic_document_queries(self):
|
||||
"""Ensure we can query dynamic fields"""
|
||||
@@ -143,10 +147,10 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
p.age = 22
|
||||
p.save()
|
||||
|
||||
self.assertEqual(1, self.Person.objects(age=22).count())
|
||||
assert 1 == self.Person.objects(age=22).count()
|
||||
p = self.Person.objects(age=22)
|
||||
p = p.get()
|
||||
self.assertEqual(22, p.age)
|
||||
assert 22 == p.age
|
||||
|
||||
def test_complex_dynamic_document_queries(self):
|
||||
class Person(DynamicDocument):
|
||||
@@ -166,8 +170,8 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
p2.age = 10
|
||||
p2.save()
|
||||
|
||||
self.assertEqual(Person.objects(age__icontains="ten").count(), 2)
|
||||
self.assertEqual(Person.objects(age__gte=10).count(), 1)
|
||||
assert Person.objects(age__icontains="ten").count() == 2
|
||||
assert Person.objects(age__gte=10).count() == 1
|
||||
|
||||
def test_complex_data_lookups(self):
|
||||
"""Ensure you can query dynamic document dynamic fields"""
|
||||
@@ -175,12 +179,12 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
p.misc = {"hello": "world"}
|
||||
p.save()
|
||||
|
||||
self.assertEqual(1, self.Person.objects(misc__hello="world").count())
|
||||
assert 1 == self.Person.objects(misc__hello="world").count()
|
||||
|
||||
def test_three_level_complex_data_lookups(self):
|
||||
"""Ensure you can query three level document dynamic fields"""
|
||||
p = self.Person.objects.create(misc={"hello": {"hello2": "world"}})
|
||||
self.assertEqual(1, self.Person.objects(misc__hello__hello2="world").count())
|
||||
self.Person.objects.create(misc={"hello": {"hello2": "world"}})
|
||||
assert 1 == self.Person.objects(misc__hello__hello2="world").count()
|
||||
|
||||
def test_complex_embedded_document_validation(self):
|
||||
"""Ensure embedded dynamic documents may be validated"""
|
||||
@@ -198,11 +202,13 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
embedded_doc_1.validate()
|
||||
|
||||
embedded_doc_2 = Embedded(content="this is not a url")
|
||||
self.assertRaises(ValidationError, embedded_doc_2.validate)
|
||||
with pytest.raises(ValidationError):
|
||||
embedded_doc_2.validate()
|
||||
|
||||
doc.embedded_field_1 = embedded_doc_1
|
||||
doc.embedded_field_2 = embedded_doc_2
|
||||
self.assertRaises(ValidationError, doc.validate)
|
||||
with pytest.raises(ValidationError):
|
||||
doc.validate()
|
||||
|
||||
def test_inheritance(self):
|
||||
"""Ensure that dynamic document plays nice with inheritance"""
|
||||
@@ -212,11 +218,9 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
|
||||
Employee.drop_collection()
|
||||
|
||||
self.assertIn("name", Employee._fields)
|
||||
self.assertIn("salary", Employee._fields)
|
||||
self.assertEqual(
|
||||
Employee._get_collection_name(), self.Person._get_collection_name()
|
||||
)
|
||||
assert "name" in Employee._fields
|
||||
assert "salary" in Employee._fields
|
||||
assert Employee._get_collection_name() == self.Person._get_collection_name()
|
||||
|
||||
joe_bloggs = Employee()
|
||||
joe_bloggs.name = "Joe Bloggs"
|
||||
@@ -224,11 +228,11 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
joe_bloggs.age = 20
|
||||
joe_bloggs.save()
|
||||
|
||||
self.assertEqual(1, self.Person.objects(age=20).count())
|
||||
self.assertEqual(1, Employee.objects(age=20).count())
|
||||
assert 1 == self.Person.objects(age=20).count()
|
||||
assert 1 == Employee.objects(age=20).count()
|
||||
|
||||
joe_bloggs = self.Person.objects.first()
|
||||
self.assertIsInstance(joe_bloggs, Employee)
|
||||
assert isinstance(joe_bloggs, Employee)
|
||||
|
||||
def test_embedded_dynamic_document(self):
|
||||
"""Test dynamic embedded documents"""
|
||||
@@ -249,26 +253,23 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
embedded_1.list_field = ["1", 2, {"hello": "world"}]
|
||||
doc.embedded_field = embedded_1
|
||||
|
||||
self.assertEqual(
|
||||
doc.to_mongo(),
|
||||
{
|
||||
"embedded_field": {
|
||||
"_cls": "Embedded",
|
||||
"string_field": "hello",
|
||||
"int_field": 1,
|
||||
"dict_field": {"hello": "world"},
|
||||
"list_field": ["1", 2, {"hello": "world"}],
|
||||
}
|
||||
},
|
||||
)
|
||||
assert doc.to_mongo() == {
|
||||
"embedded_field": {
|
||||
"_cls": "Embedded",
|
||||
"string_field": "hello",
|
||||
"int_field": 1,
|
||||
"dict_field": {"hello": "world"},
|
||||
"list_field": ["1", 2, {"hello": "world"}],
|
||||
}
|
||||
}
|
||||
doc.save()
|
||||
|
||||
doc = Doc.objects.first()
|
||||
self.assertEqual(doc.embedded_field.__class__, Embedded)
|
||||
self.assertEqual(doc.embedded_field.string_field, "hello")
|
||||
self.assertEqual(doc.embedded_field.int_field, 1)
|
||||
self.assertEqual(doc.embedded_field.dict_field, {"hello": "world"})
|
||||
self.assertEqual(doc.embedded_field.list_field, ["1", 2, {"hello": "world"}])
|
||||
assert doc.embedded_field.__class__ == Embedded
|
||||
assert doc.embedded_field.string_field == "hello"
|
||||
assert doc.embedded_field.int_field == 1
|
||||
assert doc.embedded_field.dict_field == {"hello": "world"}
|
||||
assert doc.embedded_field.list_field == ["1", 2, {"hello": "world"}]
|
||||
|
||||
def test_complex_embedded_documents(self):
|
||||
"""Test complex dynamic embedded documents setups"""
|
||||
@@ -296,44 +297,41 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
embedded_1.list_field = ["1", 2, embedded_2]
|
||||
doc.embedded_field = embedded_1
|
||||
|
||||
self.assertEqual(
|
||||
doc.to_mongo(),
|
||||
{
|
||||
"embedded_field": {
|
||||
"_cls": "Embedded",
|
||||
"string_field": "hello",
|
||||
"int_field": 1,
|
||||
"dict_field": {"hello": "world"},
|
||||
"list_field": [
|
||||
"1",
|
||||
2,
|
||||
{
|
||||
"_cls": "Embedded",
|
||||
"string_field": "hello",
|
||||
"int_field": 1,
|
||||
"dict_field": {"hello": "world"},
|
||||
"list_field": ["1", 2, {"hello": "world"}],
|
||||
},
|
||||
],
|
||||
}
|
||||
},
|
||||
)
|
||||
assert doc.to_mongo() == {
|
||||
"embedded_field": {
|
||||
"_cls": "Embedded",
|
||||
"string_field": "hello",
|
||||
"int_field": 1,
|
||||
"dict_field": {"hello": "world"},
|
||||
"list_field": [
|
||||
"1",
|
||||
2,
|
||||
{
|
||||
"_cls": "Embedded",
|
||||
"string_field": "hello",
|
||||
"int_field": 1,
|
||||
"dict_field": {"hello": "world"},
|
||||
"list_field": ["1", 2, {"hello": "world"}],
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
doc.save()
|
||||
doc = Doc.objects.first()
|
||||
self.assertEqual(doc.embedded_field.__class__, Embedded)
|
||||
self.assertEqual(doc.embedded_field.string_field, "hello")
|
||||
self.assertEqual(doc.embedded_field.int_field, 1)
|
||||
self.assertEqual(doc.embedded_field.dict_field, {"hello": "world"})
|
||||
self.assertEqual(doc.embedded_field.list_field[0], "1")
|
||||
self.assertEqual(doc.embedded_field.list_field[1], 2)
|
||||
assert doc.embedded_field.__class__ == Embedded
|
||||
assert doc.embedded_field.string_field == "hello"
|
||||
assert doc.embedded_field.int_field == 1
|
||||
assert doc.embedded_field.dict_field == {"hello": "world"}
|
||||
assert doc.embedded_field.list_field[0] == "1"
|
||||
assert doc.embedded_field.list_field[1] == 2
|
||||
|
||||
embedded_field = doc.embedded_field.list_field[2]
|
||||
|
||||
self.assertEqual(embedded_field.__class__, Embedded)
|
||||
self.assertEqual(embedded_field.string_field, "hello")
|
||||
self.assertEqual(embedded_field.int_field, 1)
|
||||
self.assertEqual(embedded_field.dict_field, {"hello": "world"})
|
||||
self.assertEqual(embedded_field.list_field, ["1", 2, {"hello": "world"}])
|
||||
assert embedded_field.__class__ == Embedded
|
||||
assert embedded_field.string_field == "hello"
|
||||
assert embedded_field.int_field == 1
|
||||
assert embedded_field.dict_field == {"hello": "world"}
|
||||
assert embedded_field.list_field == ["1", 2, {"hello": "world"}]
|
||||
|
||||
def test_dynamic_and_embedded(self):
|
||||
"""Ensure embedded documents play nicely"""
|
||||
@@ -352,18 +350,18 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
person.address.city = "Lundenne"
|
||||
person.save()
|
||||
|
||||
self.assertEqual(Person.objects.first().address.city, "Lundenne")
|
||||
assert Person.objects.first().address.city == "Lundenne"
|
||||
|
||||
person = Person.objects.first()
|
||||
person.address = Address(city="Londinium")
|
||||
person.save()
|
||||
|
||||
self.assertEqual(Person.objects.first().address.city, "Londinium")
|
||||
assert Person.objects.first().address.city == "Londinium"
|
||||
|
||||
person = Person.objects.first()
|
||||
person.age = 35
|
||||
person.save()
|
||||
self.assertEqual(Person.objects.first().age, 35)
|
||||
assert Person.objects.first().age == 35
|
||||
|
||||
def test_dynamic_embedded_works_with_only(self):
|
||||
"""Ensure custom fieldnames on a dynamic embedded document are found by qs.only()"""
|
||||
@@ -380,10 +378,10 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
name="Eric", address=Address(city="San Francisco", street_number="1337")
|
||||
).save()
|
||||
|
||||
self.assertEqual(Person.objects.first().address.street_number, "1337")
|
||||
self.assertEqual(
|
||||
Person.objects.only("address__street_number").first().address.street_number,
|
||||
"1337",
|
||||
assert Person.objects.first().address.street_number == "1337"
|
||||
assert (
|
||||
Person.objects.only("address__street_number").first().address.street_number
|
||||
== "1337"
|
||||
)
|
||||
|
||||
def test_dynamic_and_embedded_dict_access(self):
|
||||
@@ -408,20 +406,20 @@ class TestDynamicDocument(MongoDBTestCase):
|
||||
person["address"]["city"] = "Lundenne"
|
||||
person.save()
|
||||
|
||||
self.assertEqual(Person.objects.first().address.city, "Lundenne")
|
||||
assert Person.objects.first().address.city == "Lundenne"
|
||||
|
||||
self.assertEqual(Person.objects.first().phone, "555-1212")
|
||||
assert Person.objects.first().phone == "555-1212"
|
||||
|
||||
person = Person.objects.first()
|
||||
person.address = Address(city="Londinium")
|
||||
person.save()
|
||||
|
||||
self.assertEqual(Person.objects.first().address.city, "Londinium")
|
||||
assert Person.objects.first().address.city == "Londinium"
|
||||
|
||||
person = Person.objects.first()
|
||||
person["age"] = 35
|
||||
person.save()
|
||||
self.assertEqual(Person.objects.first().age, 35)
|
||||
assert Person.objects.first().age == 35
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -2,18 +2,16 @@
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
|
||||
from nose.plugins.skip import SkipTest
|
||||
from pymongo.collation import Collation
|
||||
from pymongo.errors import OperationFailure
|
||||
import pymongo
|
||||
import pytest
|
||||
from six import iteritems
|
||||
|
||||
from mongoengine import *
|
||||
from mongoengine.connection import get_db
|
||||
|
||||
__all__ = ("IndexesTest",)
|
||||
|
||||
|
||||
class IndexesTest(unittest.TestCase):
|
||||
class TestIndexes(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.connection = connect(db="mongoenginetest")
|
||||
self.db = get_db()
|
||||
@@ -55,15 +53,15 @@ class IndexesTest(unittest.TestCase):
|
||||
{"fields": [("tags", 1)]},
|
||||
{"fields": [("category", 1), ("addDate", -1)]},
|
||||
]
|
||||
self.assertEqual(expected_specs, BlogPost._meta["index_specs"])
|
||||
assert expected_specs == BlogPost._meta["index_specs"]
|
||||
|
||||
BlogPost.ensure_indexes()
|
||||
info = BlogPost.objects._collection.index_information()
|
||||
# _id, '-date', 'tags', ('cat', 'date')
|
||||
self.assertEqual(len(info), 4)
|
||||
assert len(info) == 4
|
||||
info = [value["key"] for key, value in iteritems(info)]
|
||||
for expected in expected_specs:
|
||||
self.assertIn(expected["fields"], info)
|
||||
assert expected["fields"] in info
|
||||
|
||||
def _index_test_inheritance(self, InheritFrom):
|
||||
class BlogPost(InheritFrom):
|
||||
@@ -80,7 +78,7 @@ class IndexesTest(unittest.TestCase):
|
||||
{"fields": [("_cls", 1), ("tags", 1)]},
|
||||
{"fields": [("_cls", 1), ("category", 1), ("addDate", -1)]},
|
||||
]
|
||||
self.assertEqual(expected_specs, BlogPost._meta["index_specs"])
|
||||
assert expected_specs == BlogPost._meta["index_specs"]
|
||||
|
||||
BlogPost.ensure_indexes()
|
||||
info = BlogPost.objects._collection.index_information()
|
||||
@@ -88,17 +86,17 @@ class IndexesTest(unittest.TestCase):
|
||||
# NB: there is no index on _cls by itself, since
|
||||
# the indices on -date and tags will both contain
|
||||
# _cls as first element in the key
|
||||
self.assertEqual(len(info), 4)
|
||||
assert len(info) == 4
|
||||
info = [value["key"] for key, value in iteritems(info)]
|
||||
for expected in expected_specs:
|
||||
self.assertIn(expected["fields"], info)
|
||||
assert expected["fields"] in info
|
||||
|
||||
class ExtendedBlogPost(BlogPost):
|
||||
title = StringField()
|
||||
meta = {"indexes": ["title"]}
|
||||
|
||||
expected_specs.append({"fields": [("_cls", 1), ("title", 1)]})
|
||||
self.assertEqual(expected_specs, ExtendedBlogPost._meta["index_specs"])
|
||||
assert expected_specs == ExtendedBlogPost._meta["index_specs"]
|
||||
|
||||
BlogPost.drop_collection()
|
||||
|
||||
@@ -106,7 +104,7 @@ class IndexesTest(unittest.TestCase):
|
||||
info = ExtendedBlogPost.objects._collection.index_information()
|
||||
info = [value["key"] for key, value in iteritems(info)]
|
||||
for expected in expected_specs:
|
||||
self.assertIn(expected["fields"], info)
|
||||
assert expected["fields"] in info
|
||||
|
||||
def test_indexes_document_inheritance(self):
|
||||
"""Ensure that indexes are used when meta[indexes] is specified for
|
||||
@@ -130,10 +128,8 @@ class IndexesTest(unittest.TestCase):
|
||||
class B(A):
|
||||
description = StringField()
|
||||
|
||||
self.assertEqual(A._meta["index_specs"], B._meta["index_specs"])
|
||||
self.assertEqual(
|
||||
[{"fields": [("_cls", 1), ("title", 1)]}], A._meta["index_specs"]
|
||||
)
|
||||
assert A._meta["index_specs"] == B._meta["index_specs"]
|
||||
assert [{"fields": [("_cls", 1), ("title", 1)]}] == A._meta["index_specs"]
|
||||
|
||||
def test_index_no_cls(self):
|
||||
"""Ensure index specs are inhertited correctly"""
|
||||
@@ -146,11 +142,11 @@ class IndexesTest(unittest.TestCase):
|
||||
"index_cls": False,
|
||||
}
|
||||
|
||||
self.assertEqual([("title", 1)], A._meta["index_specs"][0]["fields"])
|
||||
assert [("title", 1)] == A._meta["index_specs"][0]["fields"]
|
||||
A._get_collection().drop_indexes()
|
||||
A.ensure_indexes()
|
||||
info = A._get_collection().index_information()
|
||||
self.assertEqual(len(info.keys()), 2)
|
||||
assert len(info.keys()) == 2
|
||||
|
||||
class B(A):
|
||||
c = StringField()
|
||||
@@ -160,8 +156,8 @@ class IndexesTest(unittest.TestCase):
|
||||
"allow_inheritance": True,
|
||||
}
|
||||
|
||||
self.assertEqual([("c", 1)], B._meta["index_specs"][1]["fields"])
|
||||
self.assertEqual([("_cls", 1), ("d", 1)], B._meta["index_specs"][2]["fields"])
|
||||
assert [("c", 1)] == B._meta["index_specs"][1]["fields"]
|
||||
assert [("_cls", 1), ("d", 1)] == B._meta["index_specs"][2]["fields"]
|
||||
|
||||
def test_build_index_spec_is_not_destructive(self):
|
||||
class MyDoc(Document):
|
||||
@@ -169,12 +165,12 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
meta = {"indexes": ["keywords"], "allow_inheritance": False}
|
||||
|
||||
self.assertEqual(MyDoc._meta["index_specs"], [{"fields": [("keywords", 1)]}])
|
||||
assert MyDoc._meta["index_specs"] == [{"fields": [("keywords", 1)]}]
|
||||
|
||||
# Force index creation
|
||||
MyDoc.ensure_indexes()
|
||||
|
||||
self.assertEqual(MyDoc._meta["index_specs"], [{"fields": [("keywords", 1)]}])
|
||||
assert MyDoc._meta["index_specs"] == [{"fields": [("keywords", 1)]}]
|
||||
|
||||
def test_embedded_document_index_meta(self):
|
||||
"""Ensure that embedded document indexes are created explicitly
|
||||
@@ -189,7 +185,7 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
meta = {"indexes": ["rank.title"], "allow_inheritance": False}
|
||||
|
||||
self.assertEqual([{"fields": [("rank.title", 1)]}], Person._meta["index_specs"])
|
||||
assert [{"fields": [("rank.title", 1)]}] == Person._meta["index_specs"]
|
||||
|
||||
Person.drop_collection()
|
||||
|
||||
@@ -197,7 +193,7 @@ class IndexesTest(unittest.TestCase):
|
||||
list(Person.objects)
|
||||
info = Person.objects._collection.index_information()
|
||||
info = [value["key"] for key, value in iteritems(info)]
|
||||
self.assertIn([("rank.title", 1)], info)
|
||||
assert [("rank.title", 1)] in info
|
||||
|
||||
def test_explicit_geo2d_index(self):
|
||||
"""Ensure that geo2d indexes work when created via meta[indexes]
|
||||
@@ -207,14 +203,12 @@ class IndexesTest(unittest.TestCase):
|
||||
location = DictField()
|
||||
meta = {"allow_inheritance": True, "indexes": ["*location.point"]}
|
||||
|
||||
self.assertEqual(
|
||||
[{"fields": [("location.point", "2d")]}], Place._meta["index_specs"]
|
||||
)
|
||||
assert [{"fields": [("location.point", "2d")]}] == Place._meta["index_specs"]
|
||||
|
||||
Place.ensure_indexes()
|
||||
info = Place._get_collection().index_information()
|
||||
info = [value["key"] for key, value in iteritems(info)]
|
||||
self.assertIn([("location.point", "2d")], info)
|
||||
assert [("location.point", "2d")] in info
|
||||
|
||||
def test_explicit_geo2d_index_embedded(self):
|
||||
"""Ensure that geo2d indexes work when created via meta[indexes]
|
||||
@@ -227,14 +221,14 @@ class IndexesTest(unittest.TestCase):
|
||||
current = DictField(field=EmbeddedDocumentField("EmbeddedLocation"))
|
||||
meta = {"allow_inheritance": True, "indexes": ["*current.location.point"]}
|
||||
|
||||
self.assertEqual(
|
||||
[{"fields": [("current.location.point", "2d")]}], Place._meta["index_specs"]
|
||||
)
|
||||
assert [{"fields": [("current.location.point", "2d")]}] == Place._meta[
|
||||
"index_specs"
|
||||
]
|
||||
|
||||
Place.ensure_indexes()
|
||||
info = Place._get_collection().index_information()
|
||||
info = [value["key"] for key, value in iteritems(info)]
|
||||
self.assertIn([("current.location.point", "2d")], info)
|
||||
assert [("current.location.point", "2d")] in info
|
||||
|
||||
def test_explicit_geosphere_index(self):
|
||||
"""Ensure that geosphere indexes work when created via meta[indexes]
|
||||
@@ -244,19 +238,19 @@ class IndexesTest(unittest.TestCase):
|
||||
location = DictField()
|
||||
meta = {"allow_inheritance": True, "indexes": ["(location.point"]}
|
||||
|
||||
self.assertEqual(
|
||||
[{"fields": [("location.point", "2dsphere")]}], Place._meta["index_specs"]
|
||||
)
|
||||
assert [{"fields": [("location.point", "2dsphere")]}] == Place._meta[
|
||||
"index_specs"
|
||||
]
|
||||
|
||||
Place.ensure_indexes()
|
||||
info = Place._get_collection().index_information()
|
||||
info = [value["key"] for key, value in iteritems(info)]
|
||||
self.assertIn([("location.point", "2dsphere")], info)
|
||||
assert [("location.point", "2dsphere")] in info
|
||||
|
||||
def test_explicit_geohaystack_index(self):
|
||||
"""Ensure that geohaystack indexes work when created via meta[indexes]
|
||||
"""
|
||||
raise SkipTest(
|
||||
pytest.skip(
|
||||
"GeoHaystack index creation is not supported for now"
|
||||
"from meta, as it requires a bucketSize parameter."
|
||||
)
|
||||
@@ -266,15 +260,14 @@ class IndexesTest(unittest.TestCase):
|
||||
name = StringField()
|
||||
meta = {"indexes": [(")location.point", "name")]}
|
||||
|
||||
self.assertEqual(
|
||||
[{"fields": [("location.point", "geoHaystack"), ("name", 1)]}],
|
||||
Place._meta["index_specs"],
|
||||
)
|
||||
assert [
|
||||
{"fields": [("location.point", "geoHaystack"), ("name", 1)]}
|
||||
] == Place._meta["index_specs"]
|
||||
|
||||
Place.ensure_indexes()
|
||||
info = Place._get_collection().index_information()
|
||||
info = [value["key"] for key, value in iteritems(info)]
|
||||
self.assertIn([("location.point", "geoHaystack")], info)
|
||||
assert [("location.point", "geoHaystack")] in info
|
||||
|
||||
def test_create_geohaystack_index(self):
|
||||
"""Ensure that geohaystack indexes can be created
|
||||
@@ -287,7 +280,7 @@ class IndexesTest(unittest.TestCase):
|
||||
Place.create_index({"fields": (")location.point", "name")}, bucketSize=10)
|
||||
info = Place._get_collection().index_information()
|
||||
info = [value["key"] for key, value in iteritems(info)]
|
||||
self.assertIn([("location.point", "geoHaystack"), ("name", 1)], info)
|
||||
assert [("location.point", "geoHaystack"), ("name", 1)] in info
|
||||
|
||||
def test_dictionary_indexes(self):
|
||||
"""Ensure that indexes are used when meta[indexes] contains
|
||||
@@ -300,16 +293,15 @@ class IndexesTest(unittest.TestCase):
|
||||
tags = ListField(StringField())
|
||||
meta = {"indexes": [{"fields": ["-date"], "unique": True, "sparse": True}]}
|
||||
|
||||
self.assertEqual(
|
||||
[{"fields": [("addDate", -1)], "unique": True, "sparse": True}],
|
||||
BlogPost._meta["index_specs"],
|
||||
)
|
||||
assert [
|
||||
{"fields": [("addDate", -1)], "unique": True, "sparse": True}
|
||||
] == BlogPost._meta["index_specs"]
|
||||
|
||||
BlogPost.drop_collection()
|
||||
|
||||
info = BlogPost.objects._collection.index_information()
|
||||
# _id, '-date'
|
||||
self.assertEqual(len(info), 2)
|
||||
assert len(info) == 2
|
||||
|
||||
# Indexes are lazy so use list() to perform query
|
||||
list(BlogPost.objects)
|
||||
@@ -318,7 +310,7 @@ class IndexesTest(unittest.TestCase):
|
||||
(value["key"], value.get("unique", False), value.get("sparse", False))
|
||||
for key, value in iteritems(info)
|
||||
]
|
||||
self.assertIn(([("addDate", -1)], True, True), info)
|
||||
assert ([("addDate", -1)], True, True) in info
|
||||
|
||||
BlogPost.drop_collection()
|
||||
|
||||
@@ -340,11 +332,9 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
Person(name="test", user_guid="123").save()
|
||||
|
||||
self.assertEqual(1, Person.objects.count())
|
||||
assert 1 == Person.objects.count()
|
||||
info = Person.objects._collection.index_information()
|
||||
self.assertEqual(
|
||||
sorted(info.keys()), ["_cls_1_name_1", "_cls_1_user_guid_1", "_id_"]
|
||||
)
|
||||
assert sorted(info.keys()) == ["_cls_1_name_1", "_cls_1_user_guid_1", "_id_"]
|
||||
|
||||
def test_disable_index_creation(self):
|
||||
"""Tests setting auto_create_index to False on the connection will
|
||||
@@ -367,13 +357,13 @@ class IndexesTest(unittest.TestCase):
|
||||
User(user_guid="123").save()
|
||||
MongoUser(user_guid="123").save()
|
||||
|
||||
self.assertEqual(2, User.objects.count())
|
||||
assert 2 == User.objects.count()
|
||||
info = User.objects._collection.index_information()
|
||||
self.assertEqual(list(info.keys()), ["_id_"])
|
||||
assert list(info.keys()) == ["_id_"]
|
||||
|
||||
User.ensure_indexes()
|
||||
info = User.objects._collection.index_information()
|
||||
self.assertEqual(sorted(info.keys()), ["_cls_1_user_guid_1", "_id_"])
|
||||
assert sorted(info.keys()) == ["_cls_1_user_guid_1", "_id_"]
|
||||
|
||||
def test_embedded_document_index(self):
|
||||
"""Tests settings an index on an embedded document
|
||||
@@ -391,7 +381,7 @@ class IndexesTest(unittest.TestCase):
|
||||
BlogPost.drop_collection()
|
||||
|
||||
info = BlogPost.objects._collection.index_information()
|
||||
self.assertEqual(sorted(info.keys()), ["_id_", "date.yr_-1"])
|
||||
assert sorted(info.keys()) == ["_id_", "date.yr_-1"]
|
||||
|
||||
def test_list_embedded_document_index(self):
|
||||
"""Ensure list embedded documents can be indexed
|
||||
@@ -410,7 +400,7 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
info = BlogPost.objects._collection.index_information()
|
||||
# we don't use _cls in with list fields by default
|
||||
self.assertEqual(sorted(info.keys()), ["_id_", "tags.tag_1"])
|
||||
assert sorted(info.keys()) == ["_id_", "tags.tag_1"]
|
||||
|
||||
post1 = BlogPost(
|
||||
title="Embedded Indexes tests in place",
|
||||
@@ -428,7 +418,7 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
RecursiveDocument.ensure_indexes()
|
||||
info = RecursiveDocument._get_collection().index_information()
|
||||
self.assertEqual(sorted(info.keys()), ["_cls_1", "_id_"])
|
||||
assert sorted(info.keys()) == ["_cls_1", "_id_"]
|
||||
|
||||
def test_covered_index(self):
|
||||
"""Ensure that covered indexes can be used
|
||||
@@ -448,46 +438,45 @@ class IndexesTest(unittest.TestCase):
|
||||
# Need to be explicit about covered indexes as mongoDB doesn't know if
|
||||
# the documents returned might have more keys in that here.
|
||||
query_plan = Test.objects(id=obj.id).exclude("a").explain()
|
||||
self.assertEqual(
|
||||
assert (
|
||||
query_plan.get("queryPlanner")
|
||||
.get("winningPlan")
|
||||
.get("inputStage")
|
||||
.get("stage"),
|
||||
"IDHACK",
|
||||
.get("stage")
|
||||
== "IDHACK"
|
||||
)
|
||||
|
||||
query_plan = Test.objects(id=obj.id).only("id").explain()
|
||||
self.assertEqual(
|
||||
assert (
|
||||
query_plan.get("queryPlanner")
|
||||
.get("winningPlan")
|
||||
.get("inputStage")
|
||||
.get("stage"),
|
||||
"IDHACK",
|
||||
.get("stage")
|
||||
== "IDHACK"
|
||||
)
|
||||
|
||||
query_plan = Test.objects(a=1).only("a").exclude("id").explain()
|
||||
self.assertEqual(
|
||||
assert (
|
||||
query_plan.get("queryPlanner")
|
||||
.get("winningPlan")
|
||||
.get("inputStage")
|
||||
.get("stage"),
|
||||
"IXSCAN",
|
||||
.get("stage")
|
||||
== "IXSCAN"
|
||||
)
|
||||
self.assertEqual(
|
||||
query_plan.get("queryPlanner").get("winningPlan").get("stage"), "PROJECTION"
|
||||
assert (
|
||||
query_plan.get("queryPlanner").get("winningPlan").get("stage")
|
||||
== "PROJECTION"
|
||||
)
|
||||
|
||||
query_plan = Test.objects(a=1).explain()
|
||||
self.assertEqual(
|
||||
assert (
|
||||
query_plan.get("queryPlanner")
|
||||
.get("winningPlan")
|
||||
.get("inputStage")
|
||||
.get("stage"),
|
||||
"IXSCAN",
|
||||
)
|
||||
self.assertEqual(
|
||||
query_plan.get("queryPlanner").get("winningPlan").get("stage"), "FETCH"
|
||||
.get("stage")
|
||||
== "IXSCAN"
|
||||
)
|
||||
assert query_plan.get("queryPlanner").get("winningPlan").get("stage") == "FETCH"
|
||||
|
||||
def test_index_on_id(self):
|
||||
class BlogPost(Document):
|
||||
@@ -500,9 +489,7 @@ class IndexesTest(unittest.TestCase):
|
||||
BlogPost.drop_collection()
|
||||
|
||||
indexes = BlogPost.objects._collection.index_information()
|
||||
self.assertEqual(
|
||||
indexes["categories_1__id_1"]["key"], [("categories", 1), ("_id", 1)]
|
||||
)
|
||||
assert indexes["categories_1__id_1"]["key"] == [("categories", 1), ("_id", 1)]
|
||||
|
||||
def test_hint(self):
|
||||
TAGS_INDEX_NAME = "tags_1"
|
||||
@@ -518,27 +505,59 @@ class IndexesTest(unittest.TestCase):
|
||||
BlogPost(tags=tags).save()
|
||||
|
||||
# Hinting by shape should work.
|
||||
self.assertEqual(BlogPost.objects.hint([("tags", 1)]).count(), 10)
|
||||
assert BlogPost.objects.hint([("tags", 1)]).count() == 10
|
||||
|
||||
# Hinting by index name should work.
|
||||
self.assertEqual(BlogPost.objects.hint(TAGS_INDEX_NAME).count(), 10)
|
||||
assert BlogPost.objects.hint(TAGS_INDEX_NAME).count() == 10
|
||||
|
||||
# Clearing the hint should work fine.
|
||||
self.assertEqual(BlogPost.objects.hint().count(), 10)
|
||||
self.assertEqual(BlogPost.objects.hint([("ZZ", 1)]).hint().count(), 10)
|
||||
assert BlogPost.objects.hint().count() == 10
|
||||
assert BlogPost.objects.hint([("ZZ", 1)]).hint().count() == 10
|
||||
|
||||
# Hinting on a non-existent index shape should fail.
|
||||
with self.assertRaises(OperationFailure):
|
||||
with pytest.raises(OperationFailure):
|
||||
BlogPost.objects.hint([("ZZ", 1)]).count()
|
||||
|
||||
# Hinting on a non-existent index name should fail.
|
||||
with self.assertRaises(OperationFailure):
|
||||
with pytest.raises(OperationFailure):
|
||||
BlogPost.objects.hint("Bad Name").count()
|
||||
|
||||
# Invalid shape argument (missing list brackets) should fail.
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
BlogPost.objects.hint(("tags", 1)).count()
|
||||
|
||||
def test_collation(self):
|
||||
base = {"locale": "en", "strength": 2}
|
||||
|
||||
class BlogPost(Document):
|
||||
name = StringField()
|
||||
meta = {
|
||||
"indexes": [
|
||||
{"fields": ["name"], "name": "name_index", "collation": base}
|
||||
]
|
||||
}
|
||||
|
||||
BlogPost.drop_collection()
|
||||
|
||||
names = ["tag1", "Tag2", "tag3", "Tag4", "tag5"]
|
||||
for name in names:
|
||||
BlogPost(name=name).save()
|
||||
|
||||
query_result = BlogPost.objects.collation(base).order_by("name")
|
||||
assert [x.name for x in query_result] == sorted(names, key=lambda x: x.lower())
|
||||
assert 5 == query_result.count()
|
||||
|
||||
query_result = BlogPost.objects.collation(Collation(**base)).order_by("name")
|
||||
assert [x.name for x in query_result] == sorted(names, key=lambda x: x.lower())
|
||||
assert 5 == query_result.count()
|
||||
|
||||
incorrect_collation = {"arndom": "wrdo"}
|
||||
with pytest.raises(OperationFailure):
|
||||
BlogPost.objects.collation(incorrect_collation).count()
|
||||
|
||||
query_result = BlogPost.objects.collation({}).order_by("name")
|
||||
assert [x.name for x in query_result] == sorted(names)
|
||||
|
||||
def test_unique(self):
|
||||
"""Ensure that uniqueness constraints are applied to fields.
|
||||
"""
|
||||
@@ -554,11 +573,14 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
# Two posts with the same slug is not allowed
|
||||
post2 = BlogPost(title="test2", slug="test")
|
||||
self.assertRaises(NotUniqueError, post2.save)
|
||||
self.assertRaises(NotUniqueError, BlogPost.objects.insert, post2)
|
||||
with pytest.raises(NotUniqueError):
|
||||
post2.save()
|
||||
with pytest.raises(NotUniqueError):
|
||||
BlogPost.objects.insert(post2)
|
||||
|
||||
# Ensure backwards compatibility for errors
|
||||
self.assertRaises(OperationError, post2.save)
|
||||
with pytest.raises(OperationError):
|
||||
post2.save()
|
||||
|
||||
def test_primary_key_unique_not_working(self):
|
||||
"""Relates to #1445"""
|
||||
@@ -568,23 +590,21 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
Blog.drop_collection()
|
||||
|
||||
with self.assertRaises(OperationFailure) as ctx_err:
|
||||
with pytest.raises(OperationFailure) as exc_info:
|
||||
Blog(id="garbage").save()
|
||||
|
||||
# One of the errors below should happen. Which one depends on the
|
||||
# PyMongo version and dict order.
|
||||
err_msg = str(ctx_err.exception)
|
||||
self.assertTrue(
|
||||
any(
|
||||
[
|
||||
"The field 'unique' is not valid for an _id index specification"
|
||||
in err_msg,
|
||||
"The field 'background' is not valid for an _id index specification"
|
||||
in err_msg,
|
||||
"The field 'sparse' is not valid for an _id index specification"
|
||||
in err_msg,
|
||||
]
|
||||
)
|
||||
err_msg = str(exc_info.value)
|
||||
assert any(
|
||||
[
|
||||
"The field 'unique' is not valid for an _id index specification"
|
||||
in err_msg,
|
||||
"The field 'background' is not valid for an _id index specification"
|
||||
in err_msg,
|
||||
"The field 'sparse' is not valid for an _id index specification"
|
||||
in err_msg,
|
||||
]
|
||||
)
|
||||
|
||||
def test_unique_with(self):
|
||||
@@ -610,7 +630,8 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
# Now there will be two docs with the same slug and the same day: fail
|
||||
post3 = BlogPost(title="test3", date=Date(year=2010), slug="test")
|
||||
self.assertRaises(OperationError, post3.save)
|
||||
with pytest.raises(OperationError):
|
||||
post3.save()
|
||||
|
||||
def test_unique_embedded_document(self):
|
||||
"""Ensure that uniqueness constraints are applied to fields on embedded documents.
|
||||
@@ -635,7 +656,8 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
# Now there will be two docs with the same sub.slug
|
||||
post3 = BlogPost(title="test3", sub=SubDocument(year=2010, slug="test"))
|
||||
self.assertRaises(NotUniqueError, post3.save)
|
||||
with pytest.raises(NotUniqueError):
|
||||
post3.save()
|
||||
|
||||
def test_unique_embedded_document_in_list(self):
|
||||
"""
|
||||
@@ -665,7 +687,8 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
post2 = BlogPost(title="test2", subs=[SubDocument(year=2014, slug="conflict")])
|
||||
|
||||
self.assertRaises(NotUniqueError, post2.save)
|
||||
with pytest.raises(NotUniqueError):
|
||||
post2.save()
|
||||
|
||||
def test_unique_embedded_document_in_sorted_list(self):
|
||||
"""
|
||||
@@ -695,12 +718,13 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
# confirm that the unique index is created
|
||||
indexes = BlogPost._get_collection().index_information()
|
||||
self.assertIn("subs.slug_1", indexes)
|
||||
self.assertTrue(indexes["subs.slug_1"]["unique"])
|
||||
assert "subs.slug_1" in indexes
|
||||
assert indexes["subs.slug_1"]["unique"]
|
||||
|
||||
post2 = BlogPost(title="test2", subs=[SubDocument(year=2014, slug="conflict")])
|
||||
|
||||
self.assertRaises(NotUniqueError, post2.save)
|
||||
with pytest.raises(NotUniqueError):
|
||||
post2.save()
|
||||
|
||||
def test_unique_embedded_document_in_embedded_document_list(self):
|
||||
"""
|
||||
@@ -730,12 +754,13 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
# confirm that the unique index is created
|
||||
indexes = BlogPost._get_collection().index_information()
|
||||
self.assertIn("subs.slug_1", indexes)
|
||||
self.assertTrue(indexes["subs.slug_1"]["unique"])
|
||||
assert "subs.slug_1" in indexes
|
||||
assert indexes["subs.slug_1"]["unique"]
|
||||
|
||||
post2 = BlogPost(title="test2", subs=[SubDocument(year=2014, slug="conflict")])
|
||||
|
||||
self.assertRaises(NotUniqueError, post2.save)
|
||||
with pytest.raises(NotUniqueError):
|
||||
post2.save()
|
||||
|
||||
def test_unique_with_embedded_document_and_embedded_unique(self):
|
||||
"""Ensure that uniqueness constraints are applied to fields on
|
||||
@@ -761,11 +786,13 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
# Now there will be two docs with the same sub.slug
|
||||
post3 = BlogPost(title="test3", sub=SubDocument(year=2010, slug="test"))
|
||||
self.assertRaises(NotUniqueError, post3.save)
|
||||
with pytest.raises(NotUniqueError):
|
||||
post3.save()
|
||||
|
||||
# Now there will be two docs with the same title and year
|
||||
post3 = BlogPost(title="test1", sub=SubDocument(year=2009, slug="test-1"))
|
||||
self.assertRaises(NotUniqueError, post3.save)
|
||||
with pytest.raises(NotUniqueError):
|
||||
post3.save()
|
||||
|
||||
def test_ttl_indexes(self):
|
||||
class Log(Document):
|
||||
@@ -777,7 +804,7 @@ class IndexesTest(unittest.TestCase):
|
||||
# Indexes are lazy so use list() to perform query
|
||||
list(Log.objects)
|
||||
info = Log.objects._collection.index_information()
|
||||
self.assertEqual(3600, info["created_1"]["expireAfterSeconds"])
|
||||
assert 3600 == info["created_1"]["expireAfterSeconds"]
|
||||
|
||||
def test_index_drop_dups_silently_ignored(self):
|
||||
class Customer(Document):
|
||||
@@ -805,14 +832,14 @@ class IndexesTest(unittest.TestCase):
|
||||
cust.save()
|
||||
|
||||
cust_dupe = Customer(cust_id=1)
|
||||
with self.assertRaises(NotUniqueError):
|
||||
with pytest.raises(NotUniqueError):
|
||||
cust_dupe.save()
|
||||
|
||||
cust = Customer(cust_id=2)
|
||||
cust.save()
|
||||
|
||||
# duplicate key on update
|
||||
with self.assertRaises(NotUniqueError):
|
||||
with pytest.raises(NotUniqueError):
|
||||
cust.cust_id = 1
|
||||
cust.save()
|
||||
|
||||
@@ -833,8 +860,8 @@ class IndexesTest(unittest.TestCase):
|
||||
user = User(name="huangz", password="secret2")
|
||||
user.save()
|
||||
|
||||
self.assertEqual(User.objects.count(), 1)
|
||||
self.assertEqual(User.objects.get().password, "secret2")
|
||||
assert User.objects.count() == 1
|
||||
assert User.objects.get().password == "secret2"
|
||||
|
||||
def test_unique_and_primary_create(self):
|
||||
"""Create a new record with a duplicate primary key
|
||||
@@ -848,11 +875,11 @@ class IndexesTest(unittest.TestCase):
|
||||
User.drop_collection()
|
||||
|
||||
User.objects.create(name="huangz", password="secret")
|
||||
with self.assertRaises(NotUniqueError):
|
||||
with pytest.raises(NotUniqueError):
|
||||
User.objects.create(name="huangz", password="secret2")
|
||||
|
||||
self.assertEqual(User.objects.count(), 1)
|
||||
self.assertEqual(User.objects.get().password, "secret")
|
||||
assert User.objects.count() == 1
|
||||
assert User.objects.get().password == "secret"
|
||||
|
||||
def test_index_with_pk(self):
|
||||
"""Ensure you can use `pk` as part of a query"""
|
||||
@@ -876,7 +903,7 @@ class IndexesTest(unittest.TestCase):
|
||||
info = BlogPost.objects._collection.index_information()
|
||||
info = [value["key"] for key, value in iteritems(info)]
|
||||
index_item = [("_id", 1), ("comments.comment_id", 1)]
|
||||
self.assertIn(index_item, info)
|
||||
assert index_item in info
|
||||
|
||||
def test_compound_key_embedded(self):
|
||||
class CompoundKey(EmbeddedDocument):
|
||||
@@ -890,10 +917,8 @@ class IndexesTest(unittest.TestCase):
|
||||
my_key = CompoundKey(name="n", term="ok")
|
||||
report = ReportEmbedded(text="OK", key=my_key).save()
|
||||
|
||||
self.assertEqual(
|
||||
{"text": "OK", "_id": {"term": "ok", "name": "n"}}, report.to_mongo()
|
||||
)
|
||||
self.assertEqual(report, ReportEmbedded.objects.get(pk=my_key))
|
||||
assert {"text": "OK", "_id": {"term": "ok", "name": "n"}} == report.to_mongo()
|
||||
assert report == ReportEmbedded.objects.get(pk=my_key)
|
||||
|
||||
def test_compound_key_dictfield(self):
|
||||
class ReportDictField(Document):
|
||||
@@ -903,15 +928,13 @@ class IndexesTest(unittest.TestCase):
|
||||
my_key = {"name": "n", "term": "ok"}
|
||||
report = ReportDictField(text="OK", key=my_key).save()
|
||||
|
||||
self.assertEqual(
|
||||
{"text": "OK", "_id": {"term": "ok", "name": "n"}}, report.to_mongo()
|
||||
)
|
||||
assert {"text": "OK", "_id": {"term": "ok", "name": "n"}} == report.to_mongo()
|
||||
|
||||
# We can't directly call ReportDictField.objects.get(pk=my_key),
|
||||
# because dicts are unordered, and if the order in MongoDB is
|
||||
# different than the one in `my_key`, this test will fail.
|
||||
self.assertEqual(report, ReportDictField.objects.get(pk__name=my_key["name"]))
|
||||
self.assertEqual(report, ReportDictField.objects.get(pk__term=my_key["term"]))
|
||||
assert report == ReportDictField.objects.get(pk__name=my_key["name"])
|
||||
assert report == ReportDictField.objects.get(pk__term=my_key["term"])
|
||||
|
||||
def test_string_indexes(self):
|
||||
class MyDoc(Document):
|
||||
@@ -920,8 +943,8 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
info = MyDoc.objects._collection.index_information()
|
||||
info = [value["key"] for key, value in iteritems(info)]
|
||||
self.assertIn([("provider_ids.foo", 1)], info)
|
||||
self.assertIn([("provider_ids.bar", 1)], info)
|
||||
assert [("provider_ids.foo", 1)] in info
|
||||
assert [("provider_ids.bar", 1)] in info
|
||||
|
||||
def test_sparse_compound_indexes(self):
|
||||
class MyDoc(Document):
|
||||
@@ -933,11 +956,10 @@ class IndexesTest(unittest.TestCase):
|
||||
}
|
||||
|
||||
info = MyDoc.objects._collection.index_information()
|
||||
self.assertEqual(
|
||||
[("provider_ids.foo", 1), ("provider_ids.bar", 1)],
|
||||
info["provider_ids.foo_1_provider_ids.bar_1"]["key"],
|
||||
)
|
||||
self.assertTrue(info["provider_ids.foo_1_provider_ids.bar_1"]["sparse"])
|
||||
assert [("provider_ids.foo", 1), ("provider_ids.bar", 1)] == info[
|
||||
"provider_ids.foo_1_provider_ids.bar_1"
|
||||
]["key"]
|
||||
assert info["provider_ids.foo_1_provider_ids.bar_1"]["sparse"]
|
||||
|
||||
def test_text_indexes(self):
|
||||
class Book(Document):
|
||||
@@ -945,9 +967,9 @@ class IndexesTest(unittest.TestCase):
|
||||
meta = {"indexes": ["$title"]}
|
||||
|
||||
indexes = Book.objects._collection.index_information()
|
||||
self.assertIn("title_text", indexes)
|
||||
assert "title_text" in indexes
|
||||
key = indexes["title_text"]["key"]
|
||||
self.assertIn(("_fts", "text"), key)
|
||||
assert ("_fts", "text") in key
|
||||
|
||||
def test_hashed_indexes(self):
|
||||
class Book(Document):
|
||||
@@ -955,8 +977,8 @@ class IndexesTest(unittest.TestCase):
|
||||
meta = {"indexes": ["#ref_id"]}
|
||||
|
||||
indexes = Book.objects._collection.index_information()
|
||||
self.assertIn("ref_id_hashed", indexes)
|
||||
self.assertIn(("ref_id", "hashed"), indexes["ref_id_hashed"]["key"])
|
||||
assert "ref_id_hashed" in indexes
|
||||
assert ("ref_id", "hashed") in indexes["ref_id_hashed"]["key"]
|
||||
|
||||
def test_indexes_after_database_drop(self):
|
||||
"""
|
||||
@@ -993,7 +1015,8 @@ class IndexesTest(unittest.TestCase):
|
||||
|
||||
# Create Post #2
|
||||
post2 = BlogPost(title="test2", slug="test")
|
||||
self.assertRaises(NotUniqueError, post2.save)
|
||||
with pytest.raises(NotUniqueError):
|
||||
post2.save()
|
||||
finally:
|
||||
# Drop the temporary database at the end
|
||||
connection.drop_database("tempdatabase")
|
||||
@@ -1040,15 +1063,12 @@ class IndexesTest(unittest.TestCase):
|
||||
"dropDups"
|
||||
] # drop the index dropDups - it is deprecated in MongoDB 3+
|
||||
|
||||
self.assertEqual(
|
||||
index_info,
|
||||
{
|
||||
"txt_1": {"key": [("txt", 1)], "background": False},
|
||||
"_id_": {"key": [("_id", 1)]},
|
||||
"txt2_1": {"key": [("txt2", 1)], "background": False},
|
||||
"_cls_1": {"key": [("_cls", 1)], "background": False},
|
||||
},
|
||||
)
|
||||
assert index_info == {
|
||||
"txt_1": {"key": [("txt", 1)], "background": False},
|
||||
"_id_": {"key": [("_id", 1)]},
|
||||
"txt2_1": {"key": [("txt2", 1)], "background": False},
|
||||
"_cls_1": {"key": [("_cls", 1)], "background": False},
|
||||
}
|
||||
|
||||
def test_compound_index_underscore_cls_not_overwritten(self):
|
||||
"""
|
||||
@@ -1071,7 +1091,7 @@ class IndexesTest(unittest.TestCase):
|
||||
TestDoc.ensure_indexes()
|
||||
|
||||
index_info = TestDoc._get_collection().index_information()
|
||||
self.assertIn("shard_1_1__cls_1_txt_1_1", index_info)
|
||||
assert "shard_1_1__cls_1_txt_1_1" in index_info
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -2,6 +2,7 @@
|
||||
import unittest
|
||||
import warnings
|
||||
|
||||
import pytest
|
||||
from six import iteritems
|
||||
|
||||
from mongoengine import (
|
||||
@@ -15,13 +16,11 @@ from mongoengine import (
|
||||
StringField,
|
||||
)
|
||||
from mongoengine.pymongo_support import list_collection_names
|
||||
from tests.utils import MongoDBTestCase
|
||||
from tests.fixtures import Base
|
||||
|
||||
__all__ = ("InheritanceTest",)
|
||||
from tests.utils import MongoDBTestCase
|
||||
|
||||
|
||||
class InheritanceTest(MongoDBTestCase):
|
||||
class TestInheritance(MongoDBTestCase):
|
||||
def tearDown(self):
|
||||
for collection in list_collection_names(self.db):
|
||||
self.db.drop_collection(collection)
|
||||
@@ -39,12 +38,12 @@ class InheritanceTest(MongoDBTestCase):
|
||||
meta = {"allow_inheritance": True}
|
||||
|
||||
test_doc = DataDoc(name="test", embed=EmbedData(data="data"))
|
||||
self.assertEqual(test_doc._cls, "DataDoc")
|
||||
self.assertEqual(test_doc.embed._cls, "EmbedData")
|
||||
assert test_doc._cls == "DataDoc"
|
||||
assert test_doc.embed._cls == "EmbedData"
|
||||
test_doc.save()
|
||||
saved_doc = DataDoc.objects.with_id(test_doc.id)
|
||||
self.assertEqual(test_doc._cls, saved_doc._cls)
|
||||
self.assertEqual(test_doc.embed._cls, saved_doc.embed._cls)
|
||||
assert test_doc._cls == saved_doc._cls
|
||||
assert test_doc.embed._cls == saved_doc.embed._cls
|
||||
test_doc.delete()
|
||||
|
||||
def test_superclasses(self):
|
||||
@@ -69,12 +68,12 @@ class InheritanceTest(MongoDBTestCase):
|
||||
class Human(Mammal):
|
||||
pass
|
||||
|
||||
self.assertEqual(Animal._superclasses, ())
|
||||
self.assertEqual(Fish._superclasses, ("Animal",))
|
||||
self.assertEqual(Guppy._superclasses, ("Animal", "Animal.Fish"))
|
||||
self.assertEqual(Mammal._superclasses, ("Animal",))
|
||||
self.assertEqual(Dog._superclasses, ("Animal", "Animal.Mammal"))
|
||||
self.assertEqual(Human._superclasses, ("Animal", "Animal.Mammal"))
|
||||
assert Animal._superclasses == ()
|
||||
assert Fish._superclasses == ("Animal",)
|
||||
assert Guppy._superclasses == ("Animal", "Animal.Fish")
|
||||
assert Mammal._superclasses == ("Animal",)
|
||||
assert Dog._superclasses == ("Animal", "Animal.Mammal")
|
||||
assert Human._superclasses == ("Animal", "Animal.Mammal")
|
||||
|
||||
def test_external_superclasses(self):
|
||||
"""Ensure that the correct list of super classes is assembled when
|
||||
@@ -99,18 +98,12 @@ class InheritanceTest(MongoDBTestCase):
|
||||
class Human(Mammal):
|
||||
pass
|
||||
|
||||
self.assertEqual(Animal._superclasses, ("Base",))
|
||||
self.assertEqual(Fish._superclasses, ("Base", "Base.Animal"))
|
||||
self.assertEqual(
|
||||
Guppy._superclasses, ("Base", "Base.Animal", "Base.Animal.Fish")
|
||||
)
|
||||
self.assertEqual(Mammal._superclasses, ("Base", "Base.Animal"))
|
||||
self.assertEqual(
|
||||
Dog._superclasses, ("Base", "Base.Animal", "Base.Animal.Mammal")
|
||||
)
|
||||
self.assertEqual(
|
||||
Human._superclasses, ("Base", "Base.Animal", "Base.Animal.Mammal")
|
||||
)
|
||||
assert Animal._superclasses == ("Base",)
|
||||
assert Fish._superclasses == ("Base", "Base.Animal")
|
||||
assert Guppy._superclasses == ("Base", "Base.Animal", "Base.Animal.Fish")
|
||||
assert Mammal._superclasses == ("Base", "Base.Animal")
|
||||
assert Dog._superclasses == ("Base", "Base.Animal", "Base.Animal.Mammal")
|
||||
assert Human._superclasses == ("Base", "Base.Animal", "Base.Animal.Mammal")
|
||||
|
||||
def test_subclasses(self):
|
||||
"""Ensure that the correct list of _subclasses (subclasses) is
|
||||
@@ -135,24 +128,22 @@ class InheritanceTest(MongoDBTestCase):
|
||||
class Human(Mammal):
|
||||
pass
|
||||
|
||||
self.assertEqual(
|
||||
Animal._subclasses,
|
||||
(
|
||||
"Animal",
|
||||
"Animal.Fish",
|
||||
"Animal.Fish.Guppy",
|
||||
"Animal.Mammal",
|
||||
"Animal.Mammal.Dog",
|
||||
"Animal.Mammal.Human",
|
||||
),
|
||||
assert Animal._subclasses == (
|
||||
"Animal",
|
||||
"Animal.Fish",
|
||||
"Animal.Fish.Guppy",
|
||||
"Animal.Mammal",
|
||||
"Animal.Mammal.Dog",
|
||||
"Animal.Mammal.Human",
|
||||
)
|
||||
self.assertEqual(Fish._subclasses, ("Animal.Fish", "Animal.Fish.Guppy"))
|
||||
self.assertEqual(Guppy._subclasses, ("Animal.Fish.Guppy",))
|
||||
self.assertEqual(
|
||||
Mammal._subclasses,
|
||||
("Animal.Mammal", "Animal.Mammal.Dog", "Animal.Mammal.Human"),
|
||||
assert Fish._subclasses == ("Animal.Fish", "Animal.Fish.Guppy")
|
||||
assert Guppy._subclasses == ("Animal.Fish.Guppy",)
|
||||
assert Mammal._subclasses == (
|
||||
"Animal.Mammal",
|
||||
"Animal.Mammal.Dog",
|
||||
"Animal.Mammal.Human",
|
||||
)
|
||||
self.assertEqual(Human._subclasses, ("Animal.Mammal.Human",))
|
||||
assert Human._subclasses == ("Animal.Mammal.Human",)
|
||||
|
||||
def test_external_subclasses(self):
|
||||
"""Ensure that the correct list of _subclasses (subclasses) is
|
||||
@@ -177,30 +168,22 @@ class InheritanceTest(MongoDBTestCase):
|
||||
class Human(Mammal):
|
||||
pass
|
||||
|
||||
self.assertEqual(
|
||||
Animal._subclasses,
|
||||
(
|
||||
"Base.Animal",
|
||||
"Base.Animal.Fish",
|
||||
"Base.Animal.Fish.Guppy",
|
||||
"Base.Animal.Mammal",
|
||||
"Base.Animal.Mammal.Dog",
|
||||
"Base.Animal.Mammal.Human",
|
||||
),
|
||||
assert Animal._subclasses == (
|
||||
"Base.Animal",
|
||||
"Base.Animal.Fish",
|
||||
"Base.Animal.Fish.Guppy",
|
||||
"Base.Animal.Mammal",
|
||||
"Base.Animal.Mammal.Dog",
|
||||
"Base.Animal.Mammal.Human",
|
||||
)
|
||||
self.assertEqual(
|
||||
Fish._subclasses, ("Base.Animal.Fish", "Base.Animal.Fish.Guppy")
|
||||
assert Fish._subclasses == ("Base.Animal.Fish", "Base.Animal.Fish.Guppy")
|
||||
assert Guppy._subclasses == ("Base.Animal.Fish.Guppy",)
|
||||
assert Mammal._subclasses == (
|
||||
"Base.Animal.Mammal",
|
||||
"Base.Animal.Mammal.Dog",
|
||||
"Base.Animal.Mammal.Human",
|
||||
)
|
||||
self.assertEqual(Guppy._subclasses, ("Base.Animal.Fish.Guppy",))
|
||||
self.assertEqual(
|
||||
Mammal._subclasses,
|
||||
(
|
||||
"Base.Animal.Mammal",
|
||||
"Base.Animal.Mammal.Dog",
|
||||
"Base.Animal.Mammal.Human",
|
||||
),
|
||||
)
|
||||
self.assertEqual(Human._subclasses, ("Base.Animal.Mammal.Human",))
|
||||
assert Human._subclasses == ("Base.Animal.Mammal.Human",)
|
||||
|
||||
def test_dynamic_declarations(self):
|
||||
"""Test that declaring an extra class updates meta data"""
|
||||
@@ -208,33 +191,31 @@ class InheritanceTest(MongoDBTestCase):
|
||||
class Animal(Document):
|
||||
meta = {"allow_inheritance": True}
|
||||
|
||||
self.assertEqual(Animal._superclasses, ())
|
||||
self.assertEqual(Animal._subclasses, ("Animal",))
|
||||
assert Animal._superclasses == ()
|
||||
assert Animal._subclasses == ("Animal",)
|
||||
|
||||
# Test dynamically adding a class changes the meta data
|
||||
class Fish(Animal):
|
||||
pass
|
||||
|
||||
self.assertEqual(Animal._superclasses, ())
|
||||
self.assertEqual(Animal._subclasses, ("Animal", "Animal.Fish"))
|
||||
assert Animal._superclasses == ()
|
||||
assert Animal._subclasses == ("Animal", "Animal.Fish")
|
||||
|
||||
self.assertEqual(Fish._superclasses, ("Animal",))
|
||||
self.assertEqual(Fish._subclasses, ("Animal.Fish",))
|
||||
assert Fish._superclasses == ("Animal",)
|
||||
assert Fish._subclasses == ("Animal.Fish",)
|
||||
|
||||
# Test dynamically adding an inherited class changes the meta data
|
||||
class Pike(Fish):
|
||||
pass
|
||||
|
||||
self.assertEqual(Animal._superclasses, ())
|
||||
self.assertEqual(
|
||||
Animal._subclasses, ("Animal", "Animal.Fish", "Animal.Fish.Pike")
|
||||
)
|
||||
assert Animal._superclasses == ()
|
||||
assert Animal._subclasses == ("Animal", "Animal.Fish", "Animal.Fish.Pike")
|
||||
|
||||
self.assertEqual(Fish._superclasses, ("Animal",))
|
||||
self.assertEqual(Fish._subclasses, ("Animal.Fish", "Animal.Fish.Pike"))
|
||||
assert Fish._superclasses == ("Animal",)
|
||||
assert Fish._subclasses == ("Animal.Fish", "Animal.Fish.Pike")
|
||||
|
||||
self.assertEqual(Pike._superclasses, ("Animal", "Animal.Fish"))
|
||||
self.assertEqual(Pike._subclasses, ("Animal.Fish.Pike",))
|
||||
assert Pike._superclasses == ("Animal", "Animal.Fish")
|
||||
assert Pike._subclasses == ("Animal.Fish.Pike",)
|
||||
|
||||
def test_inheritance_meta_data(self):
|
||||
"""Ensure that document may inherit fields from a superclass document.
|
||||
@@ -249,10 +230,10 @@ class InheritanceTest(MongoDBTestCase):
|
||||
class Employee(Person):
|
||||
salary = IntField()
|
||||
|
||||
self.assertEqual(
|
||||
["_cls", "age", "id", "name", "salary"], sorted(Employee._fields.keys())
|
||||
assert ["_cls", "age", "id", "name", "salary"] == sorted(
|
||||
Employee._fields.keys()
|
||||
)
|
||||
self.assertEqual(Employee._get_collection_name(), Person._get_collection_name())
|
||||
assert Employee._get_collection_name() == Person._get_collection_name()
|
||||
|
||||
def test_inheritance_to_mongo_keys(self):
|
||||
"""Ensure that document may inherit fields from a superclass document.
|
||||
@@ -267,17 +248,17 @@ class InheritanceTest(MongoDBTestCase):
|
||||
class Employee(Person):
|
||||
salary = IntField()
|
||||
|
||||
self.assertEqual(
|
||||
["_cls", "age", "id", "name", "salary"], sorted(Employee._fields.keys())
|
||||
assert ["_cls", "age", "id", "name", "salary"] == sorted(
|
||||
Employee._fields.keys()
|
||||
)
|
||||
self.assertEqual(
|
||||
Person(name="Bob", age=35).to_mongo().keys(), ["_cls", "name", "age"]
|
||||
)
|
||||
self.assertEqual(
|
||||
Employee(name="Bob", age=35, salary=0).to_mongo().keys(),
|
||||
["_cls", "name", "age", "salary"],
|
||||
)
|
||||
self.assertEqual(Employee._get_collection_name(), Person._get_collection_name())
|
||||
assert Person(name="Bob", age=35).to_mongo().keys() == ["_cls", "name", "age"]
|
||||
assert Employee(name="Bob", age=35, salary=0).to_mongo().keys() == [
|
||||
"_cls",
|
||||
"name",
|
||||
"age",
|
||||
"salary",
|
||||
]
|
||||
assert Employee._get_collection_name() == Person._get_collection_name()
|
||||
|
||||
def test_indexes_and_multiple_inheritance(self):
|
||||
""" Ensure that all of the indexes are created for a document with
|
||||
@@ -303,13 +284,10 @@ class InheritanceTest(MongoDBTestCase):
|
||||
|
||||
C.ensure_indexes()
|
||||
|
||||
self.assertEqual(
|
||||
sorted(
|
||||
[idx["key"] for idx in C._get_collection().index_information().values()]
|
||||
),
|
||||
sorted(
|
||||
[[(u"_cls", 1), (u"b", 1)], [(u"_id", 1)], [(u"_cls", 1), (u"a", 1)]]
|
||||
),
|
||||
assert sorted(
|
||||
[idx["key"] for idx in C._get_collection().index_information().values()]
|
||||
) == sorted(
|
||||
[[(u"_cls", 1), (u"b", 1)], [(u"_id", 1)], [(u"_cls", 1), (u"a", 1)]]
|
||||
)
|
||||
|
||||
def test_polymorphic_queries(self):
|
||||
@@ -340,13 +318,13 @@ class InheritanceTest(MongoDBTestCase):
|
||||
Human().save()
|
||||
|
||||
classes = [obj.__class__ for obj in Animal.objects]
|
||||
self.assertEqual(classes, [Animal, Fish, Mammal, Dog, Human])
|
||||
assert classes == [Animal, Fish, Mammal, Dog, Human]
|
||||
|
||||
classes = [obj.__class__ for obj in Mammal.objects]
|
||||
self.assertEqual(classes, [Mammal, Dog, Human])
|
||||
assert classes == [Mammal, Dog, Human]
|
||||
|
||||
classes = [obj.__class__ for obj in Human.objects]
|
||||
self.assertEqual(classes, [Human])
|
||||
assert classes == [Human]
|
||||
|
||||
def test_allow_inheritance(self):
|
||||
"""Ensure that inheritance is disabled by default on simple
|
||||
@@ -357,20 +335,18 @@ class InheritanceTest(MongoDBTestCase):
|
||||
name = StringField()
|
||||
|
||||
# can't inherit because Animal didn't explicitly allow inheritance
|
||||
with self.assertRaises(ValueError) as cm:
|
||||
with pytest.raises(ValueError, match="Document Animal may not be subclassed"):
|
||||
|
||||
class Dog(Animal):
|
||||
pass
|
||||
|
||||
self.assertIn("Document Animal may not be subclassed", str(cm.exception))
|
||||
|
||||
# Check that _cls etc aren't present on simple documents
|
||||
dog = Animal(name="dog").save()
|
||||
self.assertEqual(dog.to_mongo().keys(), ["_id", "name"])
|
||||
assert dog.to_mongo().keys() == ["_id", "name"]
|
||||
|
||||
collection = self.db[Animal._get_collection_name()]
|
||||
obj = collection.find_one()
|
||||
self.assertNotIn("_cls", obj)
|
||||
assert "_cls" not in obj
|
||||
|
||||
def test_cant_turn_off_inheritance_on_subclass(self):
|
||||
"""Ensure if inheritance is on in a subclass you cant turn it off.
|
||||
@@ -380,14 +356,14 @@ class InheritanceTest(MongoDBTestCase):
|
||||
name = StringField()
|
||||
meta = {"allow_inheritance": True}
|
||||
|
||||
with self.assertRaises(ValueError) as cm:
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
|
||||
class Mammal(Animal):
|
||||
meta = {"allow_inheritance": False}
|
||||
|
||||
self.assertEqual(
|
||||
str(cm.exception),
|
||||
'Only direct subclasses of Document may set "allow_inheritance" to False',
|
||||
assert (
|
||||
str(exc_info.value)
|
||||
== 'Only direct subclasses of Document may set "allow_inheritance" to False'
|
||||
)
|
||||
|
||||
def test_allow_inheritance_abstract_document(self):
|
||||
@@ -401,14 +377,14 @@ class InheritanceTest(MongoDBTestCase):
|
||||
class Animal(FinalDocument):
|
||||
name = StringField()
|
||||
|
||||
with self.assertRaises(ValueError) as cm:
|
||||
with pytest.raises(ValueError):
|
||||
|
||||
class Mammal(Animal):
|
||||
pass
|
||||
|
||||
# Check that _cls isn't present in simple documents
|
||||
doc = Animal(name="dog")
|
||||
self.assertNotIn("_cls", doc.to_mongo())
|
||||
assert "_cls" not in doc.to_mongo()
|
||||
|
||||
def test_using_abstract_class_in_reference_field(self):
|
||||
# Ensures no regression of #1920
|
||||
@@ -454,10 +430,10 @@ class InheritanceTest(MongoDBTestCase):
|
||||
name = StringField()
|
||||
|
||||
berlin = EuropeanCity(name="Berlin", continent="Europe")
|
||||
self.assertEqual(len(berlin._db_field_map), len(berlin._fields_ordered))
|
||||
self.assertEqual(len(berlin._reverse_db_field_map), len(berlin._fields_ordered))
|
||||
self.assertEqual(len(berlin._fields_ordered), 3)
|
||||
self.assertEqual(berlin._fields_ordered[0], "id")
|
||||
assert len(berlin._db_field_map) == len(berlin._fields_ordered)
|
||||
assert len(berlin._reverse_db_field_map) == len(berlin._fields_ordered)
|
||||
assert len(berlin._fields_ordered) == 3
|
||||
assert berlin._fields_ordered[0] == "id"
|
||||
|
||||
def test_auto_id_not_set_if_specific_in_parent_class(self):
|
||||
class City(Document):
|
||||
@@ -469,10 +445,10 @@ class InheritanceTest(MongoDBTestCase):
|
||||
name = StringField()
|
||||
|
||||
berlin = EuropeanCity(name="Berlin", continent="Europe")
|
||||
self.assertEqual(len(berlin._db_field_map), len(berlin._fields_ordered))
|
||||
self.assertEqual(len(berlin._reverse_db_field_map), len(berlin._fields_ordered))
|
||||
self.assertEqual(len(berlin._fields_ordered), 3)
|
||||
self.assertEqual(berlin._fields_ordered[0], "city_id")
|
||||
assert len(berlin._db_field_map) == len(berlin._fields_ordered)
|
||||
assert len(berlin._reverse_db_field_map) == len(berlin._fields_ordered)
|
||||
assert len(berlin._fields_ordered) == 3
|
||||
assert berlin._fields_ordered[0] == "city_id"
|
||||
|
||||
def test_auto_id_vs_non_pk_id_field(self):
|
||||
class City(Document):
|
||||
@@ -484,12 +460,12 @@ class InheritanceTest(MongoDBTestCase):
|
||||
name = StringField()
|
||||
|
||||
berlin = EuropeanCity(name="Berlin", continent="Europe")
|
||||
self.assertEqual(len(berlin._db_field_map), len(berlin._fields_ordered))
|
||||
self.assertEqual(len(berlin._reverse_db_field_map), len(berlin._fields_ordered))
|
||||
self.assertEqual(len(berlin._fields_ordered), 4)
|
||||
self.assertEqual(berlin._fields_ordered[0], "auto_id_0")
|
||||
assert len(berlin._db_field_map) == len(berlin._fields_ordered)
|
||||
assert len(berlin._reverse_db_field_map) == len(berlin._fields_ordered)
|
||||
assert len(berlin._fields_ordered) == 4
|
||||
assert berlin._fields_ordered[0] == "auto_id_0"
|
||||
berlin.save()
|
||||
self.assertEqual(berlin.pk, berlin.auto_id_0)
|
||||
assert berlin.pk == berlin.auto_id_0
|
||||
|
||||
def test_abstract_document_creation_does_not_fail(self):
|
||||
class City(Document):
|
||||
@@ -497,9 +473,9 @@ class InheritanceTest(MongoDBTestCase):
|
||||
meta = {"abstract": True, "allow_inheritance": False}
|
||||
|
||||
city = City(continent="asia")
|
||||
self.assertEqual(None, city.pk)
|
||||
assert city.pk is None
|
||||
# TODO: expected error? Shouldn't we create a new error type?
|
||||
with self.assertRaises(KeyError):
|
||||
with pytest.raises(KeyError):
|
||||
setattr(city, "pk", 1)
|
||||
|
||||
def test_allow_inheritance_embedded_document(self):
|
||||
@@ -508,20 +484,20 @@ class InheritanceTest(MongoDBTestCase):
|
||||
class Comment(EmbeddedDocument):
|
||||
content = StringField()
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
|
||||
class SpecialComment(Comment):
|
||||
pass
|
||||
|
||||
doc = Comment(content="test")
|
||||
self.assertNotIn("_cls", doc.to_mongo())
|
||||
assert "_cls" not in doc.to_mongo()
|
||||
|
||||
class Comment(EmbeddedDocument):
|
||||
content = StringField()
|
||||
meta = {"allow_inheritance": True}
|
||||
|
||||
doc = Comment(content="test")
|
||||
self.assertIn("_cls", doc.to_mongo())
|
||||
assert "_cls" in doc.to_mongo()
|
||||
|
||||
def test_document_inheritance(self):
|
||||
"""Ensure mutliple inheritance of abstract documents
|
||||
@@ -539,7 +515,7 @@ class InheritanceTest(MongoDBTestCase):
|
||||
pass
|
||||
|
||||
except Exception:
|
||||
self.assertTrue(False, "Couldn't create MyDocument class")
|
||||
assert False, "Couldn't create MyDocument class"
|
||||
|
||||
def test_abstract_documents(self):
|
||||
"""Ensure that a document superclass can be marked as abstract
|
||||
@@ -576,20 +552,20 @@ class InheritanceTest(MongoDBTestCase):
|
||||
|
||||
for k, v in iteritems(defaults):
|
||||
for cls in [Animal, Fish, Guppy]:
|
||||
self.assertEqual(cls._meta[k], v)
|
||||
assert cls._meta[k] == v
|
||||
|
||||
self.assertNotIn("collection", Animal._meta)
|
||||
self.assertNotIn("collection", Mammal._meta)
|
||||
assert "collection" not in Animal._meta
|
||||
assert "collection" not in Mammal._meta
|
||||
|
||||
self.assertEqual(Animal._get_collection_name(), None)
|
||||
self.assertEqual(Mammal._get_collection_name(), None)
|
||||
assert Animal._get_collection_name() is None
|
||||
assert Mammal._get_collection_name() is None
|
||||
|
||||
self.assertEqual(Fish._get_collection_name(), "fish")
|
||||
self.assertEqual(Guppy._get_collection_name(), "fish")
|
||||
self.assertEqual(Human._get_collection_name(), "human")
|
||||
assert Fish._get_collection_name() == "fish"
|
||||
assert Guppy._get_collection_name() == "fish"
|
||||
assert Human._get_collection_name() == "human"
|
||||
|
||||
# ensure that a subclass of a non-abstract class can't be abstract
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
|
||||
class EvilHuman(Human):
|
||||
evil = BooleanField(default=True)
|
||||
@@ -603,7 +579,7 @@ class InheritanceTest(MongoDBTestCase):
|
||||
class B(A):
|
||||
pass
|
||||
|
||||
self.assertFalse(B._meta["abstract"])
|
||||
assert not B._meta["abstract"]
|
||||
|
||||
def test_inherited_collections(self):
|
||||
"""Ensure that subclassed documents don't override parents'
|
||||
@@ -649,8 +625,8 @@ class InheritanceTest(MongoDBTestCase):
|
||||
real_person = Drinker(drink=beer)
|
||||
real_person.save()
|
||||
|
||||
self.assertEqual(Drinker.objects[0].drink.name, red_bull.name)
|
||||
self.assertEqual(Drinker.objects[1].drink.name, beer.name)
|
||||
assert Drinker.objects[0].drink.name == red_bull.name
|
||||
assert Drinker.objects[1].drink.name == beer.name
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,21 +1,14 @@
|
||||
import unittest
|
||||
import uuid
|
||||
|
||||
from nose.plugins.skip import SkipTest
|
||||
from datetime import datetime
|
||||
from bson import ObjectId
|
||||
|
||||
import pymongo
|
||||
|
||||
from mongoengine import *
|
||||
|
||||
__all__ = ("TestJson",)
|
||||
from tests.utils import MongoDBTestCase
|
||||
|
||||
|
||||
class TestJson(unittest.TestCase):
|
||||
def setUp(self):
|
||||
connect(db="mongoenginetest")
|
||||
|
||||
class TestJson(MongoDBTestCase):
|
||||
def test_json_names(self):
|
||||
"""
|
||||
Going to test reported issue:
|
||||
@@ -39,7 +32,7 @@ class TestJson(unittest.TestCase):
|
||||
|
||||
expected_json = """{"embedded":{"string":"Inner Hello"},"string":"Hello"}"""
|
||||
|
||||
self.assertEqual(doc_json, expected_json)
|
||||
assert doc_json == expected_json
|
||||
|
||||
def test_json_simple(self):
|
||||
class Embedded(EmbeddedDocument):
|
||||
@@ -59,9 +52,9 @@ class TestJson(unittest.TestCase):
|
||||
|
||||
doc_json = doc.to_json(sort_keys=True, separators=(",", ":"))
|
||||
expected_json = """{"embedded_field":{"string":"Hi"},"string":"Hi"}"""
|
||||
self.assertEqual(doc_json, expected_json)
|
||||
assert doc_json == expected_json
|
||||
|
||||
self.assertEqual(doc, Doc.from_json(doc.to_json()))
|
||||
assert doc == Doc.from_json(doc.to_json())
|
||||
|
||||
def test_json_complex(self):
|
||||
class EmbeddedDoc(EmbeddedDocument):
|
||||
@@ -106,7 +99,7 @@ class TestJson(unittest.TestCase):
|
||||
return json.loads(self.to_json()) == json.loads(other.to_json())
|
||||
|
||||
doc = Doc()
|
||||
self.assertEqual(doc, Doc.from_json(doc.to_json()))
|
||||
assert doc == Doc.from_json(doc.to_json())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -2,25 +2,23 @@
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from mongoengine import *
|
||||
|
||||
__all__ = ("ValidatorErrorTest",)
|
||||
from tests.utils import MongoDBTestCase
|
||||
|
||||
|
||||
class ValidatorErrorTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
connect(db="mongoenginetest")
|
||||
|
||||
class TestValidatorError(MongoDBTestCase):
|
||||
def test_to_dict(self):
|
||||
"""Ensure a ValidationError handles error to_dict correctly.
|
||||
"""
|
||||
error = ValidationError("root")
|
||||
self.assertEqual(error.to_dict(), {})
|
||||
assert error.to_dict() == {}
|
||||
|
||||
# 1st level error schema
|
||||
error.errors = {"1st": ValidationError("bad 1st")}
|
||||
self.assertIn("1st", error.to_dict())
|
||||
self.assertEqual(error.to_dict()["1st"], "bad 1st")
|
||||
assert "1st" in error.to_dict()
|
||||
assert error.to_dict()["1st"] == "bad 1st"
|
||||
|
||||
# 2nd level error schema
|
||||
error.errors = {
|
||||
@@ -28,10 +26,10 @@ class ValidatorErrorTest(unittest.TestCase):
|
||||
"bad 1st", errors={"2nd": ValidationError("bad 2nd")}
|
||||
)
|
||||
}
|
||||
self.assertIn("1st", error.to_dict())
|
||||
self.assertIsInstance(error.to_dict()["1st"], dict)
|
||||
self.assertIn("2nd", error.to_dict()["1st"])
|
||||
self.assertEqual(error.to_dict()["1st"]["2nd"], "bad 2nd")
|
||||
assert "1st" in error.to_dict()
|
||||
assert isinstance(error.to_dict()["1st"], dict)
|
||||
assert "2nd" in error.to_dict()["1st"]
|
||||
assert error.to_dict()["1st"]["2nd"] == "bad 2nd"
|
||||
|
||||
# moar levels
|
||||
error.errors = {
|
||||
@@ -49,13 +47,13 @@ class ValidatorErrorTest(unittest.TestCase):
|
||||
},
|
||||
)
|
||||
}
|
||||
self.assertIn("1st", error.to_dict())
|
||||
self.assertIn("2nd", error.to_dict()["1st"])
|
||||
self.assertIn("3rd", error.to_dict()["1st"]["2nd"])
|
||||
self.assertIn("4th", error.to_dict()["1st"]["2nd"]["3rd"])
|
||||
self.assertEqual(error.to_dict()["1st"]["2nd"]["3rd"]["4th"], "Inception")
|
||||
assert "1st" in error.to_dict()
|
||||
assert "2nd" in error.to_dict()["1st"]
|
||||
assert "3rd" in error.to_dict()["1st"]["2nd"]
|
||||
assert "4th" in error.to_dict()["1st"]["2nd"]["3rd"]
|
||||
assert error.to_dict()["1st"]["2nd"]["3rd"]["4th"] == "Inception"
|
||||
|
||||
self.assertEqual(error.message, "root(2nd.3rd.4th.Inception: ['1st'])")
|
||||
assert error.message == "root(2nd.3rd.4th.Inception: ['1st'])"
|
||||
|
||||
def test_model_validation(self):
|
||||
class User(Document):
|
||||
@@ -65,19 +63,19 @@ class ValidatorErrorTest(unittest.TestCase):
|
||||
try:
|
||||
User().validate()
|
||||
except ValidationError as e:
|
||||
self.assertIn("User:None", e.message)
|
||||
self.assertEqual(
|
||||
e.to_dict(),
|
||||
{"username": "Field is required", "name": "Field is required"},
|
||||
)
|
||||
assert "User:None" in e.message
|
||||
assert e.to_dict() == {
|
||||
"username": "Field is required",
|
||||
"name": "Field is required",
|
||||
}
|
||||
|
||||
user = User(username="RossC0", name="Ross").save()
|
||||
user.name = None
|
||||
try:
|
||||
user.save()
|
||||
except ValidationError as e:
|
||||
self.assertIn("User:RossC0", e.message)
|
||||
self.assertEqual(e.to_dict(), {"name": "Field is required"})
|
||||
assert "User:RossC0" in e.message
|
||||
assert e.to_dict() == {"name": "Field is required"}
|
||||
|
||||
def test_fields_rewrite(self):
|
||||
class BasePerson(Document):
|
||||
@@ -89,7 +87,8 @@ class ValidatorErrorTest(unittest.TestCase):
|
||||
name = StringField(required=True)
|
||||
|
||||
p = Person(age=15)
|
||||
self.assertRaises(ValidationError, p.validate)
|
||||
with pytest.raises(ValidationError):
|
||||
p.validate()
|
||||
|
||||
def test_embedded_document_validation(self):
|
||||
"""Ensure that embedded documents may be validated.
|
||||
@@ -100,17 +99,19 @@ class ValidatorErrorTest(unittest.TestCase):
|
||||
content = StringField(required=True)
|
||||
|
||||
comment = Comment()
|
||||
self.assertRaises(ValidationError, comment.validate)
|
||||
with pytest.raises(ValidationError):
|
||||
comment.validate()
|
||||
|
||||
comment.content = "test"
|
||||
comment.validate()
|
||||
|
||||
comment.date = 4
|
||||
self.assertRaises(ValidationError, comment.validate)
|
||||
with pytest.raises(ValidationError):
|
||||
comment.validate()
|
||||
|
||||
comment.date = datetime.now()
|
||||
comment.validate()
|
||||
self.assertEqual(comment._instance, None)
|
||||
assert comment._instance is None
|
||||
|
||||
def test_embedded_db_field_validate(self):
|
||||
class SubDoc(EmbeddedDocument):
|
||||
@@ -123,10 +124,8 @@ class ValidatorErrorTest(unittest.TestCase):
|
||||
try:
|
||||
Doc(id="bad").validate()
|
||||
except ValidationError as e:
|
||||
self.assertIn("SubDoc:None", e.message)
|
||||
self.assertEqual(
|
||||
e.to_dict(), {"e": {"val": "OK could not be converted to int"}}
|
||||
)
|
||||
assert "SubDoc:None" in e.message
|
||||
assert e.to_dict() == {"e": {"val": "OK could not be converted to int"}}
|
||||
|
||||
Doc.drop_collection()
|
||||
|
||||
@@ -134,18 +133,16 @@ class ValidatorErrorTest(unittest.TestCase):
|
||||
|
||||
doc = Doc.objects.first()
|
||||
keys = doc._data.keys()
|
||||
self.assertEqual(2, len(keys))
|
||||
self.assertIn("e", keys)
|
||||
self.assertIn("id", keys)
|
||||
assert 2 == len(keys)
|
||||
assert "e" in keys
|
||||
assert "id" in keys
|
||||
|
||||
doc.e.val = "OK"
|
||||
try:
|
||||
doc.save()
|
||||
except ValidationError as e:
|
||||
self.assertIn("Doc:test", e.message)
|
||||
self.assertEqual(
|
||||
e.to_dict(), {"e": {"val": "OK could not be converted to int"}}
|
||||
)
|
||||
assert "Doc:test" in e.message
|
||||
assert e.to_dict() == {"e": {"val": "OK could not be converted to int"}}
|
||||
|
||||
def test_embedded_weakref(self):
|
||||
class SubDoc(EmbeddedDocument):
|
||||
@@ -161,14 +158,16 @@ class ValidatorErrorTest(unittest.TestCase):
|
||||
|
||||
s = SubDoc()
|
||||
|
||||
self.assertRaises(ValidationError, s.validate)
|
||||
with pytest.raises(ValidationError):
|
||||
s.validate()
|
||||
|
||||
d1.e = s
|
||||
d2.e = s
|
||||
|
||||
del d1
|
||||
|
||||
self.assertRaises(ValidationError, d2.validate)
|
||||
with pytest.raises(ValidationError):
|
||||
d2.validate()
|
||||
|
||||
def test_parent_reference_in_child_document(self):
|
||||
"""
|
||||
Reference in New Issue
Block a user