Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Validate on write to a field

  • Loading branch information...
commit 0683d5590f17f712a05aba84bf8401d1d3686980 1 parent 71dc85c
Jeff Jenkins authored
View
28 mongoalchemy/document.py
@@ -382,20 +382,7 @@ def wrap(self):
if field.required:
raise MissingValueException(name)
return res
-
- @classmethod
- def validate_unwrap(cls, obj, fields=None, session=None):
- ''' Attempts to unwrap the document, and raises a BadValueException if
- the operation fails. A TODO is to make this function do the checks
- without actually doing the (potentially expensive)
- deserialization'''
- try:
- cls.unwrap(obj, fields=fields, session=session)
- except Exception, e:
- e_type = type(e).__name__
- cls_name = cls.__name__
- raise BadValueException(cls_name, obj, '%s Exception validating document' % e_type, cause=e)
-
+
@classmethod
def unwrap(cls, obj, fields=None, session=None):
''' Returns an instance of this document class based on the mongo object
@@ -547,18 +534,14 @@ def sub_type(self):
return self.type
def is_valid_unwrap(self, value, fields=None):
- ''' Called before wrapping. Calls :func:`~DocumentField.is_valid_unwrap` and
- raises a :class:`BadValueException` if validation fails
+ ''' Always True. Document-level validation errors will
+ be handled during unwrappingself.
:param value: The value to validate
:param fields: The fields being returned if this is a partial \
document. They will be ignored when validating the fields \
of ``value``
'''
- try:
- self.validate_unwrap(value, fields=fields)
- except BadValueException, bve:
- return False
return True
def wrap(self, value):
@@ -585,10 +568,7 @@ def validate_unwrap(self, value, fields=None, session=None):
''' Validates every field in the underlying document type. If ``fields``
is not ``None``, only the fields in ``fields`` will be checked.
'''
- try:
- self.type.validate_unwrap(value, fields=fields, session=session)
- except BadValueException, bve:
- self._fail_validation(value, 'Bad value for DocumentField field', cause=bve)
+ return
class BadIndexException(Exception):
pass
View
10 mongoalchemy/fields.py
@@ -195,7 +195,7 @@ def __get__(self, instance, owner):
return QueryField(self)
if self._name in instance._field_values:
return instance._field_values[self._name]
- if self.default != UNSET:
+ if self.default is not UNSET:
self.set_value(instance, self.default)
return instance._field_values[self._name]
if instance.partial and self.db_field not in instance.retrieved_fields:
@@ -214,6 +214,7 @@ def __delete__(self, instance):
instance._dirty[self._name] = '$unset'
def set_value(self, instance, value, from_db=False):
+ self.validate_wrap(value)
instance._field_values[self._name] = value
if self.on_update != 'ignore':
instance._dirty[self._name] = self.on_update
@@ -1277,11 +1278,8 @@ def validate_unwrap(self, value, session=None):
if isinstance(value, DBRef):
self.validate_reference(value)
return True
- try:
- self.type.validate_unwrap(value)
- except BadValueException, bve:
- print 'ERROR'
- self._fail_validation(value, 'RefField invalid', cause=bve)
+ if not isinstance(value, self.type.type):
+ self._fail_validation(value, 'RefField invalid')
View
2  scripts/test.bash
@@ -1 +1 @@
-nosetests --cover-erase --with-coverage --cover-package mongoalchemy
+nosetests --cover-tests --cover-erase --with-coverage --cover-package mongoalchemy
View
14 test/test_documents.py
@@ -1,6 +1,6 @@
from nose.tools import *
from mongoalchemy.session import Session
-from mongoalchemy.document import Document, Index, DocumentField, MissingValueException, DocumentException, DictDoc, document_type_registry
+from mongoalchemy.document import *
from mongoalchemy.fields import *
from test.util import known_failure
@@ -236,6 +236,8 @@ def test_doc_field():
wrapped = doca.wrap()
unwrapped = DocA.unwrap(wrapped)
assert unwrapped == doca
+ assert DocA.test_doc.is_valid_unwrap(wrapped)
+
@raises(BadValueException)
def wrong_wrap_type_test():
@@ -250,13 +252,13 @@ def wrong_wrap_type_test2():
doca = DocA(test_doc2=doc2)
doca.wrap()
-def is_valid_unwrap_test_true():
- assert DocA.test_doc.is_valid_unwrap({ 'int1' : 1 }) == True
+# def is_valid_unwrap_test_true():
+# assert DocA.test_doc.is_valid_unwrap({ 'int1' : 1 }) == True
-def is_valid_unwrap_test_false():
- assert DocA.test_doc2.is_valid_unwrap({ 'int1' : 1 }) == False
+# def is_valid_unwrap_test_false():
+# assert DocA.test_doc2.is_valid_unwrap({ 'int1' : 1 }) == False
-@raises(BadValueException)
+@raises(ExtraValueException)
def wrong_unwrap_type_test():
DocA.unwrap({ 'test_doc2' : { 'int1' : 1 } })
View
15 test/test_ref_field.py
@@ -39,8 +39,8 @@ def test_proxy():
class B(Document):
b = IntField(default=3)
class A(Document):
- x_ids = ListField(RefField(B), iproxy='xs', default_empty=True)
- x_id = RefField(B, proxy='x')
+ x_ids = ListField(RefField(B, allow_none=True), iproxy='xs', default_empty=True, allow_none=True)
+ x_id = RefField(B, proxy='x', allow_none=True)
s = get_session()
a = A()
@@ -56,6 +56,7 @@ class A(Document):
assert [z.b for z in aa.xs] == range(0, 3)
a_none = A(x_id=None, x_ids=[None])
+ a_none._set_session(s)
assert a_none.x == None
assert list(a_none.xs) == [None]
@@ -249,13 +250,7 @@ class C(Document):
def test_unwrap_bad_type_extra():
class A(Document):
x = IntField()
- class AA(Document):
- x = IntField()
- class B(Document):
- y = RefField(DocumentField(A))
- class C(Document):
- y = RefField(DocumentField(A), autoload=True)
-
+
s = get_session()
a = A(x=5)
@@ -264,7 +259,7 @@ class C(Document):
aref = {'$id':a.mongo_id, '$ref':'A'}
dbaref = DBRef(db='unit-testing', collection='A', id=a.mongo_id)
- ret = RefField(DocumentField(A)).validate_unwrap(5)
+ ret = RefField(A).validate_unwrap(5)
def test_simple():
class A(Document):
Please sign in to comment.
Something went wrong with that request. Please try again.