Patch for using a non-generated Document ID with couchdb.schema, Take 2

21 July 2008
08:50

By cmlenz as Diff

1 Index: couchdb/tests/schema.py
2 ===================================================================
3 --- couchdb/tests/schema.py (revision 93)
4 +++ couchdb/tests/schema.py (working copy)
5 @@ -7,11 +7,43 @@
6 # you should have received as part of this distribution.
7
8 import doctest
9 +import os
10 import unittest
11
12 -from couchdb import schema
13 +from couchdb import client, schema
14
15
16 +class DocumentTestCase(unittest.TestCase):
17 +
18 + def setUp(self):
19 + uri = os.environ.get('COUCHDB_URI', 'http://localhost:5984/')
20 + self.server = client.Server(uri)
21 + if 'python-tests' in self.server:
22 + del self.server['python-tests']
23 + self.db = self.server.create('python-tests')
24 +
25 + def tearDown(self):
26 + if 'python-tests' in self.server:
27 + del self.server['python-tests']
28 +
29 + def test_automatic_id(self):
30 + class Post(schema.Document):
31 + title = schema.TextField()
32 + post = Post(title='Foo bar')
33 + assert post.id is None
34 + post.store(self.db)
35 + assert post.id is not None
36 + self.assertEqual('Foo bar', self.db[post.id]['title'])
37 +
38 + def test_explicit_id(self):
39 + class Post(schema.Document):
40 + title = schema.TextField()
41 + post = Post(id='foo_bar', title='Foo bar')
42 + self.assertEqual('foo_bar', post.id)
43 + post.store(self.db)
44 + self.assertEqual('Foo bar', self.db['foo_bar']['title'])
45 +
46 +
47 class ListFieldTestCase(unittest.TestCase):
48
49 def test_to_json(self):
50 @@ -32,6 +64,7 @@
51 def suite():
52 suite = unittest.TestSuite()
53 suite.addTest(doctest.DocTestSuite(schema))
54 + suite.addTest(unittest.makeSuite(DocumentTestCase, 'test'))
55 suite.addTest(unittest.makeSuite(ListFieldTestCase, 'test'))
56 return suite
57
58 Index: couchdb/schema.py
59 ===================================================================
60 --- couchdb/schema.py (revision 93)
61 +++ couchdb/schema.py (working copy)
62 @@ -173,19 +173,24 @@
63
64 class Document(Schema):
65
66 + def __init__(self, id=None, **values):
67 + Schema.__init__(self, **values)
68 + if id is not None:
69 + self._data['_id'] = id
70 +
71 def __repr__(self):
72 return '<%s %r@%r %r>' % (type(self).__name__, self.id, self.rev,
73 dict([(k, v) for k, v in self._data.items()
74 if k not in ('_id', '_rev')]))
75
76 def id(self):
77 - if hasattr(self._data, 'id'):
78 + if hasattr(self._data, 'id'): # When data is client.Document
79 return self._data.id
80 return self._data.get('_id')
81 id = property(id)
82
83 def rev(self):
84 - if hasattr(self._data, 'rev'):
85 + if hasattr(self._data, 'rev'): # When data is client.Document
86 return self._data.rev
87 return self._data.get('_rev')
88 rev = property(rev)
89 @@ -197,11 +202,11 @@
90
91 def store(self, db):
92 """Store the document in the given database."""
93 - if getattr(self._data, 'id', None) is None:
94 + if self.id is None:
95 docid = db.create(self._data)
96 self._data = db.get(docid)
97 else:
98 - db[self._data.id] = self._data
99 + db[self.id] = self._data
100 return self
101
102 def query(cls, db, map_fun, reduce_fun, language='javascript',

Download Raw Source

Comments

No comments so far.