My favorites | Sign in
Project Home
New issue   Search
for
  Advanced search   Search tips   Subscriptions

Issue 185 attachment: dictfield_default.diff (2.1 KB)

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
diff -r 77f4db4628bf couchdb/mapping.py
--- a/couchdb/mapping.py Sat Jun 04 17:32:53 2011 +0100
+++ b/couchdb/mapping.py Mon Jun 06 15:30:06 2011 +0400
@@ -559,7 +559,12 @@
"""
def __init__(self, mapping=None, name=None, default=None):
default = default or {}
- Field.__init__(self, name=name, default=lambda: default.copy())
+ Field.__init__(self, name=name,
+ default=mapping
+ and (isinstance(default, mapping)
+ and (lambda: mapping.wrap(default._data.copy()))
+ or (lambda: mapping(**default)))
+ or (lambda: default.copy()))
self.mapping = mapping

def _to_python(self, value):
diff -r 77f4db4628bf couchdb/tests/mapping.py
--- a/couchdb/tests/mapping.py Sat Jun 04 17:32:53 2011 +0100
+++ b/couchdb/tests/mapping.py Mon Jun 06 15:30:06 2011 +0400
@@ -90,6 +90,23 @@
doc.get('foo', None)


+class DictFieldTestCase(testutil.TempDatabaseMixin, unittest.TestCase):
+ def test_mapping_default(self):
+ class Image(mapping.Mapping):
+ name = mapping.TextField()
+ url = mapping.TextField()
+
+ class Post(mapping.Mapping):
+ title = mapping.TextField()
+ logo = mapping.DictField(Image, default={'name': 'post logo'})
+ attached_image = mapping.DictField(Image, default=Image(name='attached image'))
+
+ post = Post(title='test post')
+
+ self.assertEqual('post logo', post.logo.name)
+ self.assertEqual('attached image', post.attached_image.name)
+
+
class ListFieldTestCase(testutil.TempDatabaseMixin, unittest.TestCase):

def test_to_json(self):
@@ -255,6 +272,7 @@
suite = unittest.TestSuite()
suite.addTest(doctest.DocTestSuite(mapping))
suite.addTest(unittest.makeSuite(DocumentTestCase, 'test'))
+ suite.addTest(unittest.makeSuite(DictFieldTestCase, 'test'))
suite.addTest(unittest.makeSuite(ListFieldTestCase, 'test'))
suite.addTest(unittest.makeSuite(WrappingTestCase, 'test'))
return suite
Powered by Google Project Hosting