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

20 July 2008
11:45

By cmlenz as Diff

See also: http://code.google.com/p/couchdb-python/issues/detail?id=9

1 Index: couchdb/tests/schema.py
2 ===================================================================
3 --- couchdb/tests/schema.py (revision 77)
4 +++ couchdb/tests/schema.py (working copy)
5 @@ -12,6 +12,28 @@
6 from couchdb import schema
7
8
9 +class DocumentTestCase(unittest.TestCase):
10 +
11 + def setUp(self):
12 + uri = os.environ.get('COUCHDB_URI', 'http://localhost:5984/')
13 + self.server = client.Server(uri)
14 + if 'python-tests' in self.server:
15 + del self.server['python-tests']
16 + self.db = self.server.create('python-tests')
17 +
18 + def tearDown(self):
19 + if 'python-tests' in self.server:
20 + del self.server['python-tests']
21 +
22 + def test_explicit_id(self):
23 + class Post(schema.Document):
24 + title = schema.TextField()
25 + post = Post(id='foo_bar', title='Foo bar')
26 + self.assertEqual('foo_bar', post.id)
27 + post.store(self.db)
28 + self.assertEqual('Foo bar', self.db['foo_bar']['title'])
29 +
30 +
31 class ListFieldTestCase(unittest.TestCase):
32
33 def test_to_json(self):
34 Index: couchdb/schema.py
35 ===================================================================
36 --- couchdb/schema.py (revision 77)
37 +++ couchdb/schema.py (working copy)
38 @@ -173,19 +173,24 @@
39
40 class Document(Schema):
41
42 + def __init__(self, id=None, **values):
43 + Schema.__init__(self, **values)
44 + if id is not None:
45 + self._data['_id'] = id
46 +
47 def __repr__(self):
48 return '<%s %r@%r %r>' % (type(self).__name__, self.id, self.rev,
49 dict([(k, v) for k, v in self._data.items()
50 if k not in ('_id', '_rev')]))
51
52 def id(self):
53 - if hasattr(self._data, 'id'):
54 + if hasattr(self._data, 'id'): # When data is client.Document
55 return self._data.id
56 return self._data.get('_id')
57 id = property(id)
58
59 def rev(self):
60 - if hasattr(self._data, 'rev'):
61 + if hasattr(self._data, 'rev'): # When data is client.Document
62 return self._data.rev
63 return self._data.get('_rev')
64 rev = property(rev)

Download Raw Source

Comments

No comments so far.