Added update() and update_one() with tests/docs

This commit is contained in:
Harry Marr
2010-01-08 18:39:06 +00:00
parent 4d695a3544
commit 42a58dda57
5 changed files with 161 additions and 6 deletions

View File

@@ -262,7 +262,7 @@ class DocumentTest(unittest.TestCase):
# Two posts with the same slug is not allowed
post2 = BlogPost(title='test2', slug='test')
self.assertRaises(ValidationError, post2.save)
self.assertRaises(OperationError, post2.save)
class Date(EmbeddedDocument):
year = IntField(name='yr')
@@ -283,7 +283,7 @@ class DocumentTest(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(ValidationError, post3.save)
self.assertRaises(OperationError, post3.save)
BlogPost.drop_collection()

View File

@@ -201,6 +201,41 @@ class QuerySetTest(unittest.TestCase):
self.Person.objects.delete()
self.assertEqual(len(self.Person.objects), 0)
def test_update(self):
"""Ensure that atomic updates work properly.
"""
class BlogPost(Document):
title = StringField()
hits = IntField()
tags = ListField(StringField())
BlogPost.drop_collection()
post = BlogPost(name="Test Post", hits=5, tags=['test'])
post.save()
BlogPost.objects.update(set__hits=10)
post.reload()
self.assertEqual(post.hits, 10)
BlogPost.objects.update_one(inc__hits=1)
post.reload()
self.assertEqual(post.hits, 11)
BlogPost.objects.update_one(dec__hits=1)
post.reload()
self.assertEqual(post.hits, 10)
BlogPost.objects.update(push__tags='mongo')
post.reload()
self.assertTrue('mongo' in post.tags)
BlogPost.objects.update_one(push_all__tags=['db', 'nosql'])
post.reload()
self.assertTrue('db' in post.tags and 'nosql' in post.tags)
BlogPost.drop_collection()
def test_order_by(self):
"""Ensure that QuerySets may be ordered.
"""