diff options
author | Aleksey Lim <alsroot@sugarlabs.org> | 2014-04-29 06:35:16 (GMT) |
---|---|---|
committer | Aleksey Lim <alsroot@sugarlabs.org> | 2014-04-29 06:35:16 (GMT) |
commit | fe07c5f9f1da79059f8fcd5e33e0f043b7cc6814 (patch) | |
tree | 241c22d3d947ae05bf16c53505be9cdabc99d70e | |
parent | 8ed5799f7d891b8d09ffdc298368f93382ddea5b (diff) |
While patching, place all props into Resource object
-rw-r--r-- | sugar_network/db/directory.py | 9 | ||||
-rwxr-xr-x | tests/units/db/resource.py | 49 | ||||
-rwxr-xr-x | tests/units/db/volume.py | 22 |
3 files changed, 55 insertions, 25 deletions
diff --git a/sugar_network/db/directory.py b/sugar_network/db/directory.py index 79e7332..73aac90 100644 --- a/sugar_network/db/directory.py +++ b/sugar_network/db/directory.py @@ -231,20 +231,23 @@ class Directory(object): def patch(self, guid, patch, seqno=False): """Apply changes for documents.""" doc = self.resource(guid, self._storage.get(guid)) - merged = False + merge = [] for prop, meta in patch.items(): orig_meta = doc.meta(prop) if orig_meta and orig_meta['mtime'] >= meta['mtime']: continue + doc.posts[prop] = meta['value'] + merge.append((prop, meta)) + + for prop, meta in merge: if doc.post_seqno is None and seqno is not False: if not seqno: seqno = self._seqno.next() doc.post_seqno = seqno doc.post(prop, **meta) - merged = True - if merged and doc.exists: + if merge and doc.exists: # No need in after-merge event, further commit event # is enough to avoid increasing events flow self._index.store(guid, doc.posts, self._preindex) diff --git a/tests/units/db/resource.py b/tests/units/db/resource.py index 2e12a3c..30c25ab 100755 --- a/tests/units/db/resource.py +++ b/tests/units/db/resource.py @@ -610,6 +610,55 @@ class ResourceTest(tests.Test): sorted([guid1, guid2, guid3]), sorted([i.guid for i in directory])) + def test_patch_CallSetters(self): + + class Document(db.Resource): + + @db.stored_property(db.Numeric) + def prop(self, value): + return value + + @prop.setter + def prop(self, value): + return value + 1 + + directory = Directory('document', Document, IndexWriter, _SessionSeqno(), this.localcast) + + directory.patch('1', { + 'guid': {'mtime': 1, 'value': '1'}, + 'ctime': {'mtime': 1, 'value': 1}, + 'mtime': {'mtime': 1, 'value': 1}, + 'prop': {'mtime': 1, 'value': 1}, + }) + self.assertEqual(2, directory.get('1')['prop']) + + def test_patch_AllPropsInResourceObject(self): + + class Document(db.Resource): + + @db.stored_property(db.Numeric) + def prop1(self, value): + return value + + @prop1.setter + def prop1(self, value): + return self['prop2'] + 1 + + @db.stored_property(db.Numeric) + def prop2(self, value): + return value + + directory = Directory('document', Document, IndexWriter, _SessionSeqno(), this.localcast) + + directory.patch('1', { + 'guid': {'mtime': 1, 'value': '1'}, + 'ctime': {'mtime': 1, 'value': 1}, + 'mtime': {'mtime': 1, 'value': 1}, + 'prop1': {'mtime': 1, 'value': 1}, + 'prop2': {'mtime': 1, 'value': 2}, + }) + self.assertEqual(3, directory.get('1')['prop1']) + class _SessionSeqno(object): diff --git a/tests/units/db/volume.py b/tests/units/db/volume.py index 0ebd33f..9e96e3f 100755 --- a/tests/units/db/volume.py +++ b/tests/units/db/volume.py @@ -165,28 +165,6 @@ class VolumeTest(tests.Test): {'value': '2', 'mtime': 0}, directory['1'].meta('prop2')) - def test_patch_CallSetters(self): - - class Document(db.Resource): - - @db.stored_property(db.Numeric) - def prop(self, value): - return value - - @prop.setter - def prop(self, value): - return value + 1 - - directory = Directory('document', Document, IndexWriter, _SessionSeqno(), this.localcast) - - directory.patch('1', { - 'guid': {'mtime': 1, 'value': '1'}, - 'ctime': {'mtime': 1, 'value': 1}, - 'mtime': {'mtime': 1, 'value': 1}, - 'prop': {'mtime': 1, 'value': 1}, - }) - self.assertEqual(2, directory.get('1')['prop']) - class _SessionSeqno(object): |