Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Pastebin

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
diff --git ming/tests/odm/test_declarative.py ming/tests/odm/test_declarative.py
index 3977a44..8c504c2 100644
--- ming/tests/odm/test_declarative.py
+++ ming/tests/odm/test_declarative.py
@@ -11,6 +11,7 @@ from ming.odm.declarative import MappedClass
 from ming.odm import state, mapper
 from ming.odm import MapperExtension, SessionExtension
 from ming.odm.odmsession import ODMCursor
+import bson

 class TestIndex(TestCase):

@@ -345,12 +346,19 @@ class ObjectIdRelationship(TestCase):
                 session = self.session
             _id = FieldProperty(S.ObjectId)
             children = ForeignIdProperty('Child', uselist=True)
+            field_with_default = ForeignIdProperty(
+                'Child',
+                uselist=True,
+                if_missing=lambda:[bson.ObjectId('deadbeefdeadbeefdeadbeef')])
         class Child(MappedClass):
             class __mongometa__:
                 name='child'
                 session = self.session
             _id = FieldProperty(S.ObjectId)
             parent_id = ForeignIdProperty(Parent)
+            field_with_default = ForeignIdProperty(
+                Parent,
+                if_missing=lambda:bson.ObjectId('deadbeefdeadbeefdeadbeef'))
         Mapper.compile_all()
         self.Parent = Parent
         self.Child = Child
@@ -369,6 +377,20 @@ class ObjectIdRelationship(TestCase):
         self.session.flush()
         self.assertEqual(parent.children, [])

+    def test_default_relationship(self):
+        child = self.Child()
+        self.session.flush()
+        self.assertEqual(
+            child.field_with_default,
+            bson.ObjectId('deadbeefdeadbeefdeadbeef'))
+
+    def test_default_list_relationship(self):
+        parent = self.Parent()
+        self.session.flush()
+        self.assertEqual(
+            parent.field_with_default,
+            [bson.ObjectId('deadbeefdeadbeefdeadbeef')])
+

 class TestBasicMapperExtension(TestCase):
     def setUp(self):