Fixed ListField bug, added ReferenceField + tests
This commit is contained in:
@@ -9,7 +9,7 @@ from mongomap import *
|
||||
class CollectionManagerTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
connect(db='mongotest')
|
||||
connect(db='mongomaptest')
|
||||
|
||||
class Person(Document):
|
||||
name = StringField()
|
||||
@@ -54,6 +54,8 @@ class CollectionManagerTest(unittest.TestCase):
|
||||
self.assertEqual(people.count(), 2)
|
||||
results = list(people)
|
||||
self.assertTrue(isinstance(results[0], self.Person))
|
||||
self.assertTrue(isinstance(results[0]._id, (pymongo.objectid.ObjectId,
|
||||
str, unicode)))
|
||||
self.assertEqual(results[0].name, "User A")
|
||||
self.assertEqual(results[0].age, 20)
|
||||
self.assertEqual(results[1].name, "User B")
|
||||
|
||||
@@ -231,6 +231,36 @@ class DocumentTest(unittest.TestCase):
|
||||
# Ensure that the 'details' embedded object saved correctly
|
||||
self.assertEqual(employee_obj['details']['position'], 'Developer')
|
||||
|
||||
def test_save_reference(self):
|
||||
"""Ensure that a document reference field may be saved in the database.
|
||||
"""
|
||||
|
||||
class BlogPost(Document):
|
||||
meta = {'collection': 'blogpost_1'}
|
||||
content = StringField()
|
||||
author = ReferenceField(self.Person)
|
||||
|
||||
self.db.drop_collection(BlogPost._meta['collection'])
|
||||
|
||||
author = self.Person(name='Test User')
|
||||
author.save()
|
||||
|
||||
post = BlogPost(content='Watched some TV today... how exciting.')
|
||||
post.author = author
|
||||
post.save()
|
||||
|
||||
post_obj = BlogPost.objects.find_one()
|
||||
self.assertTrue(isinstance(post_obj.author, self.Person))
|
||||
self.assertEqual(post_obj.author.name, 'Test User')
|
||||
|
||||
post_obj.author.age = 25
|
||||
post_obj.author.save()
|
||||
|
||||
author = self.Person.objects.find_one(name='Test User')
|
||||
self.assertEqual(author.age, 25)
|
||||
|
||||
self.db.drop_collection(BlogPost._meta['collection'])
|
||||
|
||||
def tearDown(self):
|
||||
self.db.drop_collection(self.Person._meta['collection'])
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import unittest
|
||||
|
||||
from mongomap import *
|
||||
from mongomap.connection import _get_db
|
||||
|
||||
|
||||
class FieldTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
connect(db='mongomaptest')
|
||||
self.db = _get_db()
|
||||
|
||||
def test_default_values(self):
|
||||
"""Ensure that default field values are used when creating a document.
|
||||
@@ -146,6 +148,38 @@ class FieldTest(unittest.TestCase):
|
||||
post.author = User(name='Test User')
|
||||
post.author = PowerUser(name='Test User', power=47)
|
||||
|
||||
def test_reference_validation(self):
|
||||
"""Ensure that invalid embedded documents cannot be assigned to
|
||||
embedded document fields.
|
||||
"""
|
||||
class User(Document):
|
||||
name = StringField()
|
||||
|
||||
class BlogPost(Document):
|
||||
content = StringField()
|
||||
author = ReferenceField(User)
|
||||
|
||||
self.assertRaises(ValidationError, ReferenceField, EmbeddedDocument)
|
||||
|
||||
user = User(name='Test User')
|
||||
|
||||
post1 = BlogPost(content='Chips and gravy taste good.')
|
||||
post1.author = user
|
||||
self.assertRaises(ValidationError, post1.save)
|
||||
|
||||
post2 = BlogPost(content='Chips and chilli taste good.')
|
||||
self.assertRaises(ValidationError, post1.__setattr__, 'author', post2)
|
||||
|
||||
user.save()
|
||||
post1.author = user
|
||||
post1.save()
|
||||
|
||||
post2.save()
|
||||
self.assertRaises(ValidationError, post1.__setattr__, 'author', post2)
|
||||
|
||||
self.db.drop_collection(User._meta['collection'])
|
||||
self.db.drop_collection(BlogPost._meta['collection'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user