more work on coverage
This commit is contained in:
		@@ -110,9 +110,6 @@ class ValidationError(AssertionError):
 | 
			
		||||
 | 
			
		||||
        def build_dict(source):
 | 
			
		||||
            errors_dict = {}
 | 
			
		||||
            if not source:
 | 
			
		||||
                return errors_dict
 | 
			
		||||
 | 
			
		||||
            if isinstance(source, dict):
 | 
			
		||||
                for field_name, error in iteritems(source):
 | 
			
		||||
                    errors_dict[field_name] = build_dict(error)
 | 
			
		||||
 
 | 
			
		||||
@@ -839,7 +839,6 @@ class IndexesTest(unittest.TestCase):
 | 
			
		||||
        Customer.drop_collection()
 | 
			
		||||
        Customer.objects.first()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def test_unique_and_indexes(self):
 | 
			
		||||
        """Ensure that 'unique' constraints aren't overridden by
 | 
			
		||||
        meta.indexes.
 | 
			
		||||
 
 | 
			
		||||
@@ -75,6 +75,16 @@ class TestEmailField(MongoDBTestCase):
 | 
			
		||||
        user = User(email='me@localhost')
 | 
			
		||||
        user.validate()
 | 
			
		||||
 | 
			
		||||
    def test_email_domain_validation_fails_if_invalid_idn(self):
 | 
			
		||||
        class User(Document):
 | 
			
		||||
            email = EmailField()
 | 
			
		||||
 | 
			
		||||
        invalid_idn = '.google.com'
 | 
			
		||||
        user = User(email='me@%s' % invalid_idn)
 | 
			
		||||
        with self.assertRaises(ValidationError) as ctx_err:
 | 
			
		||||
            user.validate()
 | 
			
		||||
        self.assertIn("domain failed IDN encoding", str(ctx_err.exception))
 | 
			
		||||
 | 
			
		||||
    def test_email_field_ip_domain(self):
 | 
			
		||||
        class User(Document):
 | 
			
		||||
            email = EmailField()
 | 
			
		||||
 
 | 
			
		||||
@@ -158,6 +158,11 @@ class QuerySetTest(unittest.TestCase):
 | 
			
		||||
        self.assertEqual(person.name, 'User B')
 | 
			
		||||
        self.assertEqual(person.age, None)
 | 
			
		||||
 | 
			
		||||
    def test___getitem___invalid_index(self):
 | 
			
		||||
        """Ensure slicing a queryset works as expected."""
 | 
			
		||||
        with self.assertRaises(TypeError):
 | 
			
		||||
            self.Person.objects()['a']
 | 
			
		||||
 | 
			
		||||
    def test_slice(self):
 | 
			
		||||
        """Ensure slicing a queryset works as expected."""
 | 
			
		||||
        user_a = self.Person.objects.create(name='User A', age=20)
 | 
			
		||||
@@ -986,6 +991,29 @@ class QuerySetTest(unittest.TestCase):
 | 
			
		||||
        inserted_comment_id = Comment.objects.insert(comment, load_bulk=False)
 | 
			
		||||
        self.assertEqual(comment.id, inserted_comment_id)
 | 
			
		||||
 | 
			
		||||
    def test_bulk_insert_accepts_doc_with_ids(self):
 | 
			
		||||
        class Comment(Document):
 | 
			
		||||
            id = IntField(primary_key=True)
 | 
			
		||||
 | 
			
		||||
        Comment.drop_collection()
 | 
			
		||||
 | 
			
		||||
        com1 = Comment(id=0)
 | 
			
		||||
        com2 = Comment(id=1)
 | 
			
		||||
        Comment.objects.insert([com1, com2])
 | 
			
		||||
 | 
			
		||||
    def test_insert_raise_if_duplicate_in_constraint(self):
 | 
			
		||||
        class Comment(Document):
 | 
			
		||||
            id = IntField(primary_key=True)
 | 
			
		||||
 | 
			
		||||
        Comment.drop_collection()
 | 
			
		||||
 | 
			
		||||
        com1 = Comment(id=0)
 | 
			
		||||
 | 
			
		||||
        Comment.objects.insert(com1)
 | 
			
		||||
 | 
			
		||||
        with self.assertRaises(NotUniqueError):
 | 
			
		||||
            Comment.objects.insert(com1)
 | 
			
		||||
 | 
			
		||||
    def test_get_changed_fields_query_count(self):
 | 
			
		||||
        """Make sure we don't perform unnecessary db operations when
 | 
			
		||||
        none of document's fields were updated.
 | 
			
		||||
@@ -3570,6 +3598,11 @@ class QuerySetTest(unittest.TestCase):
 | 
			
		||||
                opts = {"deleted": False}
 | 
			
		||||
                return qryset(**opts)
 | 
			
		||||
 | 
			
		||||
            @queryset_manager
 | 
			
		||||
            def objects_1_arg(qryset):
 | 
			
		||||
                opts = {"deleted": False}
 | 
			
		||||
                return qryset(**opts)
 | 
			
		||||
 | 
			
		||||
            @queryset_manager
 | 
			
		||||
            def music_posts(doc_cls, queryset, deleted=False):
 | 
			
		||||
                return queryset(tags='music',
 | 
			
		||||
@@ -3584,6 +3617,8 @@ class QuerySetTest(unittest.TestCase):
 | 
			
		||||
 | 
			
		||||
        self.assertEqual([p.id for p in BlogPost.objects()],
 | 
			
		||||
                         [post1.id, post2.id, post3.id])
 | 
			
		||||
        self.assertEqual([p.id for p in BlogPost.objects_1_arg()],
 | 
			
		||||
                         [post1.id, post2.id, post3.id])
 | 
			
		||||
        self.assertEqual([p.id for p in BlogPost.music_posts()],
 | 
			
		||||
                         [post1.id, post2.id])
 | 
			
		||||
 | 
			
		||||
@@ -4968,6 +5003,38 @@ class QuerySetTest(unittest.TestCase):
 | 
			
		||||
            people.count()
 | 
			
		||||
            self.assertEqual(q, 3)
 | 
			
		||||
 | 
			
		||||
    def test_no_cached_queryset__repr__(self):
 | 
			
		||||
        class Person(Document):
 | 
			
		||||
            name = StringField()
 | 
			
		||||
 | 
			
		||||
        Person.drop_collection()
 | 
			
		||||
        qs = Person.objects.no_cache()
 | 
			
		||||
        self.assertEqual(repr(qs), '[]')
 | 
			
		||||
 | 
			
		||||
    def test_no_cached_on_a_cached_queryset_raise_error(self):
 | 
			
		||||
        class Person(Document):
 | 
			
		||||
            name = StringField()
 | 
			
		||||
 | 
			
		||||
        Person.drop_collection()
 | 
			
		||||
        Person(name='a').save()
 | 
			
		||||
        qs = Person.objects()
 | 
			
		||||
        _ = list(qs)
 | 
			
		||||
        with self.assertRaises(OperationError) as ctx_err:
 | 
			
		||||
            qs.no_cache()
 | 
			
		||||
        self.assertEqual("QuerySet already cached", str(ctx_err.exception))
 | 
			
		||||
 | 
			
		||||
    def test_no_cached_queryset_no_cache_back_to_cache(self):
 | 
			
		||||
        class Person(Document):
 | 
			
		||||
            name = StringField()
 | 
			
		||||
 | 
			
		||||
        Person.drop_collection()
 | 
			
		||||
        qs = Person.objects()
 | 
			
		||||
        self.assertIsInstance(qs, QuerySet)
 | 
			
		||||
        qs = qs.no_cache()
 | 
			
		||||
        self.assertIsInstance(qs, QuerySetNoCache)
 | 
			
		||||
        qs = qs.cache()
 | 
			
		||||
        self.assertIsInstance(qs, QuerySet)
 | 
			
		||||
 | 
			
		||||
    def test_cache_not_cloned(self):
 | 
			
		||||
 | 
			
		||||
        class User(Document):
 | 
			
		||||
 
 | 
			
		||||
@@ -71,6 +71,14 @@ class TransformTest(unittest.TestCase):
 | 
			
		||||
        update = transform.update(BlogPost, push_all__tags=['mongo', 'db'])
 | 
			
		||||
        self.assertEqual(update, {'$push': {'tags': {'$each': ['mongo', 'db']}}})
 | 
			
		||||
 | 
			
		||||
    def test_transform_update_no_operator_default_to_set(self):
 | 
			
		||||
        """Ensure the differences in behvaior between 'push' and 'push_all'"""
 | 
			
		||||
        class BlogPost(Document):
 | 
			
		||||
            tags = ListField(StringField())
 | 
			
		||||
 | 
			
		||||
        update = transform.update(BlogPost, tags=['mongo', 'db'])
 | 
			
		||||
        self.assertEqual(update, {'$set': {'tags': ['mongo', 'db']}})
 | 
			
		||||
 | 
			
		||||
    def test_query_field_name(self):
 | 
			
		||||
        """Ensure that the correct field name is used when querying.
 | 
			
		||||
        """
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,15 @@
 | 
			
		||||
import unittest
 | 
			
		||||
 | 
			
		||||
from mongoengine.common import _import_class
 | 
			
		||||
from mongoengine import Document
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestCommon(unittest.TestCase):
 | 
			
		||||
 | 
			
		||||
    def test__import_class(self):
 | 
			
		||||
        doc_cls = _import_class("Document")
 | 
			
		||||
        self.assertIs(doc_cls, Document)
 | 
			
		||||
 | 
			
		||||
    def test__import_class_raise_if_not_known(self):
 | 
			
		||||
        with self.assertRaises(ValueError):
 | 
			
		||||
            _import_class("UnknownClass")
 | 
			
		||||
 
 | 
			
		||||
@@ -270,6 +270,14 @@ class ContextManagersTest(unittest.TestCase):
 | 
			
		||||
                counter += 1
 | 
			
		||||
            self.assertEqual(q, counter)
 | 
			
		||||
 | 
			
		||||
            self.assertEqual(int(q), counter)       # test __int__
 | 
			
		||||
            self.assertEqual(repr(q), str(int(q)))  # test __repr__
 | 
			
		||||
            self.assertGreater(q, -1)               # test __gt__
 | 
			
		||||
            self.assertGreaterEqual(q, int(q))      # test __gte__
 | 
			
		||||
            self.assertNotEqual(q, -1)
 | 
			
		||||
            self.assertLess(q, 1000)
 | 
			
		||||
            self.assertLessEqual(q, int(q))
 | 
			
		||||
 | 
			
		||||
    def test_query_counter_counts_getmore_queries(self):
 | 
			
		||||
        connect('mongoenginetest')
 | 
			
		||||
        db = get_db()
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
import unittest
 | 
			
		||||
from six import iterkeys
 | 
			
		||||
 | 
			
		||||
from mongoengine import Document
 | 
			
		||||
from mongoengine.base.datastructures import StrictDict, BaseList, BaseDict
 | 
			
		||||
@@ -368,6 +369,20 @@ class TestStrictDict(unittest.TestCase):
 | 
			
		||||
        d = self.dtype(a=1, b=1, c=1)
 | 
			
		||||
        self.assertEqual((d.a, d.b, d.c), (1, 1, 1))
 | 
			
		||||
 | 
			
		||||
    def test_iterkeys(self):
 | 
			
		||||
        d = self.dtype(a=1)
 | 
			
		||||
        self.assertEqual(list(iterkeys(d)), ['a'])
 | 
			
		||||
 | 
			
		||||
    def test_len(self):
 | 
			
		||||
        d = self.dtype(a=1)
 | 
			
		||||
        self.assertEqual(len(d), 1)
 | 
			
		||||
 | 
			
		||||
    def test_pop(self):
 | 
			
		||||
        d = self.dtype(a=1)
 | 
			
		||||
        self.assertIn('a', d)
 | 
			
		||||
        d.pop('a')
 | 
			
		||||
        self.assertNotIn('a', d)
 | 
			
		||||
 | 
			
		||||
    def test_repr(self):
 | 
			
		||||
        d = self.dtype(a=1, b=2, c=3)
 | 
			
		||||
        self.assertEqual(repr(d), '{"a": 1, "b": 2, "c": 3}')
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user