fixed merge conflict in queryset test
This commit is contained in:
@@ -156,6 +156,20 @@ class DocumentTest(unittest.TestCase):
|
||||
class Employee(self.Person):
|
||||
meta = {'allow_inheritance': False}
|
||||
self.assertRaises(ValueError, create_employee_class)
|
||||
|
||||
# Test the same for embedded documents
|
||||
class Comment(EmbeddedDocument):
|
||||
content = StringField()
|
||||
meta = {'allow_inheritance': False}
|
||||
|
||||
def create_special_comment():
|
||||
class SpecialComment(Comment):
|
||||
pass
|
||||
self.assertRaises(ValueError, create_special_comment)
|
||||
|
||||
comment = Comment(content='test')
|
||||
self.assertFalse('_cls' in comment.to_mongo())
|
||||
self.assertFalse('_types' in comment.to_mongo())
|
||||
|
||||
def test_collection_name(self):
|
||||
"""Ensure that a collection with a specified name may be used.
|
||||
@@ -391,7 +405,7 @@ class DocumentTest(unittest.TestCase):
|
||||
|
||||
self.assertTrue('content' in Comment._fields)
|
||||
self.assertFalse('id' in Comment._fields)
|
||||
self.assertFalse(hasattr(Comment, '_meta'))
|
||||
self.assertFalse('collection' in Comment._meta)
|
||||
|
||||
def test_embedded_document_validation(self):
|
||||
"""Ensure that embedded documents may be validated.
|
||||
|
||||
100
tests/fields.py
100
tests/fields.py
@@ -80,23 +80,6 @@ class FieldTest(unittest.TestCase):
|
||||
person.name = 'Shorter name'
|
||||
person.validate()
|
||||
|
||||
def test_url_validation(self):
|
||||
"""Ensure that invalid URLs cannot be assigned to URL fields.
|
||||
"""
|
||||
class Person(Document):
|
||||
name = StringField()
|
||||
personal_blog = URLField()
|
||||
|
||||
person = Person()
|
||||
person.name = "Guido van Rossum"
|
||||
person.personal_blog = "pep8 or bust!"
|
||||
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
|
||||
# swap in a real URL
|
||||
person.personal_blog = "http://neopythonic.blogspot.com/"
|
||||
person.validate()
|
||||
|
||||
def test_int_validation(self):
|
||||
"""Ensure that invalid values cannot be assigned to int fields.
|
||||
"""
|
||||
@@ -124,31 +107,13 @@ class FieldTest(unittest.TestCase):
|
||||
person.height = 1.89
|
||||
person.validate()
|
||||
|
||||
person.height = 2
|
||||
person.height = '2.0'
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
person.height = 0.01
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
person.height = 4.0
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
|
||||
def test_decimal_validation(self):
|
||||
"""Ensure that invalid values cannot be assigned to decimal fields.
|
||||
"""
|
||||
class AlbumReview(Document):
|
||||
score = DecimalField()
|
||||
|
||||
review = AlbumReview()
|
||||
review.score = "8.7"
|
||||
review.validate()
|
||||
review.score = Decimal("10.0")
|
||||
review.validate()
|
||||
# implicit conversion from float to string
|
||||
review.score = 3.14
|
||||
review.validate()
|
||||
|
||||
review.score = "it stinks!"
|
||||
self.assertRaises(ValidationError, review.validate)
|
||||
|
||||
def test_boolean_validation(self):
|
||||
"""Ensure that invalid values cannot be assigned to boolean fields.
|
||||
"""
|
||||
@@ -212,6 +177,28 @@ class FieldTest(unittest.TestCase):
|
||||
post.comments = 'yay'
|
||||
self.assertRaises(ValidationError, post.validate)
|
||||
|
||||
def test_dict_validation(self):
|
||||
"""Ensure that dict types work as expected.
|
||||
"""
|
||||
class BlogPost(Document):
|
||||
info = DictField()
|
||||
|
||||
post = BlogPost()
|
||||
post.info = 'my post'
|
||||
self.assertRaises(ValidationError, post.validate)
|
||||
|
||||
post.info = ['test', 'test']
|
||||
self.assertRaises(ValidationError, post.validate)
|
||||
|
||||
post.info = {'$title': 'test'}
|
||||
self.assertRaises(ValidationError, post.validate)
|
||||
|
||||
post.info = {'the.title': 'test'}
|
||||
self.assertRaises(ValidationError, post.validate)
|
||||
|
||||
post.info = {'title': 'test'}
|
||||
post.validate()
|
||||
|
||||
def test_embedded_document_validation(self):
|
||||
"""Ensure that invalid embedded documents cannot be assigned to
|
||||
embedded document fields.
|
||||
@@ -220,7 +207,7 @@ class FieldTest(unittest.TestCase):
|
||||
content = StringField()
|
||||
|
||||
class PersonPreferences(EmbeddedDocument):
|
||||
food = StringField()
|
||||
food = StringField(required=True)
|
||||
number = IntField()
|
||||
|
||||
class Person(Document):
|
||||
@@ -231,9 +218,14 @@ class FieldTest(unittest.TestCase):
|
||||
person.preferences = 'My Preferences'
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
|
||||
# Check that only the right embedded doc works
|
||||
person.preferences = Comment(content='Nice blog post...')
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
|
||||
# Check that the embedded doc is valid
|
||||
person.preferences = PersonPreferences()
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
|
||||
person.preferences = PersonPreferences(food='Cheese', number=47)
|
||||
self.assertEqual(person.preferences.food, 'Cheese')
|
||||
person.validate()
|
||||
@@ -295,6 +287,40 @@ class FieldTest(unittest.TestCase):
|
||||
User.drop_collection()
|
||||
BlogPost.drop_collection()
|
||||
|
||||
def test_reference_query_conversion(self):
|
||||
"""Ensure that ReferenceFields can be queried using objects and values
|
||||
of the type of the primary key of the referenced object.
|
||||
"""
|
||||
class Member(Document):
|
||||
user_num = IntField(primary_key=True)
|
||||
|
||||
class BlogPost(Document):
|
||||
title = StringField()
|
||||
author = ReferenceField(Member)
|
||||
|
||||
Member.drop_collection()
|
||||
BlogPost.drop_collection()
|
||||
|
||||
m1 = Member(user_num=1)
|
||||
m1.save()
|
||||
m2 = Member(user_num=2)
|
||||
m2.save()
|
||||
|
||||
post1 = BlogPost(title='post 1', author=m1)
|
||||
post1.save()
|
||||
|
||||
post2 = BlogPost(title='post 2', author=m2)
|
||||
post2.save()
|
||||
|
||||
post = BlogPost.objects(author=m1.id).first()
|
||||
self.assertEqual(post.id, post1.id)
|
||||
|
||||
post = BlogPost.objects(author=m2.id).first()
|
||||
self.assertEqual(post.id, post2.id)
|
||||
|
||||
Member.drop_collection()
|
||||
BlogPost.drop_collection()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
@@ -2,7 +2,8 @@ import unittest
|
||||
import pymongo
|
||||
from datetime import datetime
|
||||
|
||||
from mongoengine.queryset import QuerySet
|
||||
from mongoengine.queryset import (QuerySet, MultipleObjectsReturned,
|
||||
DoesNotExist)
|
||||
from mongoengine import *
|
||||
|
||||
|
||||
@@ -15,12 +16,12 @@ class QuerySetTest(unittest.TestCase):
|
||||
name = StringField()
|
||||
age = IntField()
|
||||
self.Person = Person
|
||||
|
||||
|
||||
def test_initialisation(self):
|
||||
"""Ensure that a QuerySet is correctly initialised by QuerySetManager.
|
||||
"""
|
||||
self.assertTrue(isinstance(self.Person.objects, QuerySet))
|
||||
self.assertEqual(self.Person.objects._collection.name(),
|
||||
self.assertEqual(self.Person.objects._collection.name,
|
||||
self.Person._meta['collection'])
|
||||
self.assertTrue(isinstance(self.Person.objects._collection,
|
||||
pymongo.collection.Collection))
|
||||
@@ -135,6 +136,54 @@ class QuerySetTest(unittest.TestCase):
|
||||
person = self.Person.objects.with_id(person1.id)
|
||||
self.assertEqual(person.name, "User A")
|
||||
|
||||
def test_find_only_one(self):
|
||||
"""Ensure that a query using ``get`` returns at most one result.
|
||||
"""
|
||||
# Try retrieving when no objects exists
|
||||
self.assertRaises(DoesNotExist, self.Person.objects.get)
|
||||
|
||||
person1 = self.Person(name="User A", age=20)
|
||||
person1.save()
|
||||
person2 = self.Person(name="User B", age=30)
|
||||
person2.save()
|
||||
|
||||
# Retrieve the first person from the database
|
||||
self.assertRaises(MultipleObjectsReturned, self.Person.objects.get)
|
||||
|
||||
# Use a query to filter the people found to just person2
|
||||
person = self.Person.objects.get(age=30)
|
||||
self.assertEqual(person.name, "User B")
|
||||
|
||||
person = self.Person.objects.get(age__lt=30)
|
||||
self.assertEqual(person.name, "User A")
|
||||
|
||||
def test_get_or_create(self):
|
||||
"""Ensure that ``get_or_create`` returns one result or creates a new
|
||||
document.
|
||||
"""
|
||||
person1 = self.Person(name="User A", age=20)
|
||||
person1.save()
|
||||
person2 = self.Person(name="User B", age=30)
|
||||
person2.save()
|
||||
|
||||
# Retrieve the first person from the database
|
||||
self.assertRaises(MultipleObjectsReturned,
|
||||
self.Person.objects.get_or_create)
|
||||
|
||||
# Use a query to filter the people found to just person2
|
||||
person = self.Person.objects.get_or_create(age=30)
|
||||
self.assertEqual(person.name, "User B")
|
||||
|
||||
person = self.Person.objects.get_or_create(age__lt=30)
|
||||
self.assertEqual(person.name, "User A")
|
||||
|
||||
# Try retrieving when no objects exists - new doc should be created
|
||||
self.Person.objects.get_or_create(age=50, defaults={'name': 'User C'})
|
||||
|
||||
person = self.Person.objects.get(age=50)
|
||||
self.assertEqual(person.name, "User C")
|
||||
|
||||
|
||||
def test_filter_chaining(self):
|
||||
"""Ensure filters can be chained together.
|
||||
"""
|
||||
@@ -146,7 +195,7 @@ class QuerySetTest(unittest.TestCase):
|
||||
published_date = DateTimeField()
|
||||
|
||||
@queryset_manager
|
||||
def published(queryset):
|
||||
def published(doc_cls, queryset):
|
||||
return queryset(is_published=True)
|
||||
|
||||
blog_post_1 = BlogPost(title="Blog Post #1",
|
||||
@@ -171,30 +220,6 @@ class QuerySetTest(unittest.TestCase):
|
||||
|
||||
BlogPost.drop_collection()
|
||||
|
||||
# def test_field_subsets(self):
|
||||
# """Ensure that a call to ``only`` loads only selected fields.
|
||||
# """
|
||||
#
|
||||
# class DinerReview(Document):
|
||||
# title = StringField()
|
||||
# abstract = StringField()
|
||||
# content = StringField()
|
||||
#
|
||||
# review = DinerReview(title="Lorraine's Diner")
|
||||
# review.abstract = "Dirty dishes, great food."
|
||||
# review.content = """
|
||||
# Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||
# Mauris eu felis risus, eget congue ante. Mauris consectetur
|
||||
# dignissim velit, quis dictum risus tincidunt ac.
|
||||
# Phasellus condimentum imperdiet laoreet.
|
||||
# """
|
||||
# review.save()
|
||||
#
|
||||
# review = DinerReview.objects.only("title").first()
|
||||
# self.assertEqual(review.content, None)
|
||||
#
|
||||
# DinerReview.drop_collection()
|
||||
|
||||
def test_ordering(self):
|
||||
"""Ensure default ordering is applied and can be overridden.
|
||||
"""
|
||||
@@ -252,7 +277,25 @@ class QuerySetTest(unittest.TestCase):
|
||||
|
||||
BlogPost.drop_collection()
|
||||
|
||||
def test_find_dict_item(self):
|
||||
"""Ensure that DictField items may be found.
|
||||
"""
|
||||
class BlogPost(Document):
|
||||
info = DictField()
|
||||
|
||||
BlogPost.drop_collection()
|
||||
|
||||
post = BlogPost(info={'title': 'test'})
|
||||
post.save()
|
||||
|
||||
post_obj = BlogPost.objects(info__title='test').first()
|
||||
self.assertEqual(post_obj.id, post.id)
|
||||
|
||||
BlogPost.drop_collection()
|
||||
|
||||
def test_q(self):
|
||||
"""Ensure that Q objects may be used to query for documents.
|
||||
"""
|
||||
class BlogPost(Document):
|
||||
publish_date = DateTimeField()
|
||||
published = BooleanField()
|
||||
@@ -288,6 +331,15 @@ class QuerySetTest(unittest.TestCase):
|
||||
|
||||
BlogPost.drop_collection()
|
||||
|
||||
# Check the 'in' operator
|
||||
self.Person(name='user1', age=20).save()
|
||||
self.Person(name='user2', age=20).save()
|
||||
self.Person(name='user3', age=30).save()
|
||||
self.Person(name='user4', age=40).save()
|
||||
|
||||
self.assertEqual(len(self.Person.objects(Q(age__in=[20]))), 2)
|
||||
self.assertEqual(len(self.Person.objects(Q(age__in=[20, 30]))), 3)
|
||||
|
||||
def test_exec_js_query(self):
|
||||
"""Ensure that queries are properly formed for use in exec_js.
|
||||
"""
|
||||
@@ -514,7 +566,7 @@ class QuerySetTest(unittest.TestCase):
|
||||
tags = ListField(StringField())
|
||||
|
||||
@queryset_manager
|
||||
def music_posts(queryset):
|
||||
def music_posts(doc_cls, queryset):
|
||||
return queryset(tags='music')
|
||||
|
||||
BlogPost.drop_collection()
|
||||
@@ -623,6 +675,8 @@ class QuerySetTest(unittest.TestCase):
|
||||
class QTest(unittest.TestCase):
|
||||
|
||||
def test_or_and(self):
|
||||
"""Ensure that Q objects may be combined correctly.
|
||||
"""
|
||||
q1 = Q(name='test')
|
||||
q2 = Q(age__gte=18)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user