Permalink
Browse files

Add test for #2 + catch RequireFieldError in addition to StructureError

  • Loading branch information...
1 parent e1f140a commit 09128d05b5b960c65e01929ee496cb517cb2b057 @merwok merwok committed with Pior Bastida Dec 18, 2012
Showing with 11 additions and 4 deletions.
  1. +6 −0 example/model/message.py
  2. +2 −2 pyramid_rest/mongo.py
  3. +3 −2 pyramid_rest/tests/functional/test_mongo.py
View
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
+import datetime
from bson.objectid import ObjectId
@@ -16,6 +17,11 @@ class Message(Document):
'content': unicode,
'application_id': ObjectId,
'user_id': ObjectId,
+ 'created': datetime.datetime,
+ }
+
+ default_values = {
+ 'created': datetime.datetime.utcnow,
}
required_fields = ['from', 'content', 'application_id', 'user_id']
View
@@ -137,9 +137,9 @@ def create(self, **identifiers):
document[k] = v
try:
document.save()
- except mongokit.StructureError:
+ except (mongokit.StructureError, mongokit.RequireFieldError):
log.exception(
- 'StructureError creating %s, POST: %s, ids: %s',
+ 'error while creating %s, POST: %s, ids: %s',
self.context.resource,
self.request.POST,
identifiers,
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
-from bson.objectid import ObjectId
+import datetime
+from bson.objectid import ObjectId
from pyramid.httpexceptions import HTTPBadRequest
from pyramid_rest.tests.functional import TestExampleController
@@ -73,6 +74,7 @@ def test_update(self):
'user_id': ObjectId(self.user_id),
'from': u'Peter Tosh',
'content': u'yo man',
+ 'created': datetime.datetime.utcnow(),
})
message.save()
@@ -84,7 +86,6 @@ def test_update(self):
)
result = self.mongo_db.Message.find_one({'_id': message._id})
-
self.assertEqual(u'yo man 2', result.content)
def test_delete(self):

0 comments on commit 09128d0

Please sign in to comment.