Permalink
Browse files

Allow saving of partial documents as a subfield if the partial docume…

…nt is complete
  • Loading branch information...
1 parent 5f5f4b2 commit 7190701f9fc8e621e9e17914d2aab9da5df1043c @jeffjenkins committed Jan 12, 2013
Showing with 38 additions and 0 deletions.
  1. +3 −0 mongoalchemy/document.py
  2. +35 −0 test/test_query.py
View
3 mongoalchemy/document.py
@@ -395,6 +395,9 @@ def wrap(self):
except AttributeError, e:
if field.required:
raise MissingValueException(name)
+ except FieldNotRetrieved, fne:
+ if field.required:
+ raise
return res
@classmethod
View
35 test/test_query.py
@@ -202,6 +202,41 @@ def test_field_filter_non_retrieved_subdocument_field():
break
assert t2.t.j == 2
+@raises(FieldNotRetrieved)
+def test_save_partial_subdocument_fail():
+ class Foo(Document):
+ a = DocumentField('Bar')
+ class Bar(Document):
+ b = IntField()
+ c = IntField()
+ s = get_session()
+ s.clear_collection(Foo)
+ s.clear_collection(Bar)
+ bar = Bar(b=1432, c=1112)
+ s.insert(bar)
+ bar = s.query(Bar).filter_by(b=1432, c=1112).fields('c').one()
+ # print bar.c, bar.b
+ s.insert(Foo(a=bar))
+ s.query(Foo).filter(Foo.a.c==1112).one()
+
+
+def test_save_partial_subdocument():
+ class Foo(Document):
+ a = DocumentField('Bar')
+ class Bar(Document):
+ b = IntField(required=False)
+ c = IntField()
+ s = get_session()
+ s.clear_collection(Foo)
+ s.clear_collection(Bar)
+ bar = Bar(b=1432, c=1112)
+ s.insert(bar)
+ bar = s.query(Bar).filter_by(b=1432, c=1112).fields('c').one()
+ # print bar.c, bar.b
+ s.insert(Foo(a=bar))
+ s.query(Foo).filter(Foo.a.c==1112).one()
+
+
@raises(Exception)
def repeated_field_query_test():
s = get_session()

0 comments on commit 7190701

Please sign in to comment.