From e1cb484d2d85f5f2022b2862a3f157b2e942addd Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Sun, 16 Feb 2014 23:17:19 +0100 Subject: [PATCH] API cleanup, bug fixes --- vdirsyncer/storage/base.py | 7 ++++--- vdirsyncer/storage/filesystem.py | 2 +- vdirsyncer/storage/memory.py | 2 +- vdirsyncer/sync.py | 4 ++-- vdirsyncer/tests/test_storage.py | 3 +-- vdirsyncer/tests/test_sync.py | 19 ++++++++++++++----- 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/vdirsyncer/storage/base.py b/vdirsyncer/storage/base.py index 1ebeaf0..414d725 100644 --- a/vdirsyncer/storage/base.py +++ b/vdirsyncer/storage/base.py @@ -38,17 +38,18 @@ class Storage(object): def get(self, uid): ''' :param uid: uid to fetch - :returns: (object, uid, etag) + :returns: (object, etag) ''' raise NotImplementedError() def get_multi(self, uids): ''' :param uids: list of uids to fetch - :returns: iterable of (object, uid, etag) + :returns: iterable of (uid, obj, etag) ''' for uid in uids: - yield self.get(uid) + obj, etag = self.get(uid) + yield uid, obj, etag def has(self, uid): ''' diff --git a/vdirsyncer/storage/filesystem.py b/vdirsyncer/storage/filesystem.py index 8a94f21..5b0a927 100644 --- a/vdirsyncer/storage/filesystem.py +++ b/vdirsyncer/storage/filesystem.py @@ -33,7 +33,7 @@ class FilesystemStorage(Storage): def get(self, uid): fpath = self._get_filepath(uid) with open(fpath, 'rb') as f: - return Item(f.read()), uid, os.path.getmtime(fpath) + return Item(f.read()), os.path.getmtime(fpath) def has(self, uid): return os.path.isfile(self._get_filepath(uid)) diff --git a/vdirsyncer/storage/memory.py b/vdirsyncer/storage/memory.py index 92c263a..91b72b7 100644 --- a/vdirsyncer/storage/memory.py +++ b/vdirsyncer/storage/memory.py @@ -25,7 +25,7 @@ class MemoryStorage(Storage): def get(self, uid): etag, obj = self.items[uid] - return obj, uid, etag + return obj, etag def has(self, uid): return uid in self.items diff --git a/vdirsyncer/sync.py b/vdirsyncer/sync.py index 05f9b8d..f4cc2c4 100644 --- a/vdirsyncer/sync.py +++ b/vdirsyncer/sync.py @@ -36,9 +36,9 @@ def sync(storage_a, storage_b, status): get_actions(list_a, list_b, status) def prefetch(): - for item, uid, etag in storage_a.get_multi(prefetch_from_a): + for uid, item, etag in storage_a.get_multi(prefetch_from_a): items_a[uid] = (item, etag) - for item, uid, etag in storage_b.get_multi(prefetch_from_b): + for uid, item, etag in storage_b.get_multi(prefetch_from_b): items_b[uid] = (item, etag) prefetch() diff --git a/vdirsyncer/tests/test_storage.py b/vdirsyncer/tests/test_storage.py index 3ca278a..3993139 100644 --- a/vdirsyncer/tests/test_storage.py +++ b/vdirsyncer/tests/test_storage.py @@ -41,8 +41,7 @@ class StorageTests(object): assert a == b for i in b: assert s.has(i) - item, uid, etag = s.get(i) - assert uid == i + item, etag = s.get(i) assert item.raw == 'UID:{}'.format(i) diff --git a/vdirsyncer/tests/test_sync.py b/vdirsyncer/tests/test_sync.py index b28fbf7..ff7c81f 100644 --- a/vdirsyncer/tests/test_sync.py +++ b/vdirsyncer/tests/test_sync.py @@ -35,10 +35,10 @@ class SyncTests(TestCase): item = Item('UID:1') a.upload(item) sync(a, b, status) - obj_a, uid_a, etag_a = a.get('1') - obj_b, uid_b, etag_b = b.get('1') + obj_a, etag_a = a.get('1') + obj_b, etag_b = b.get('1') + assert only(status) == '1' assert obj_a.raw == obj_b.raw == item.raw - assert uid_a == uid_b == only(status) == '1' # creation and deletion item2 = Item('UID:2') @@ -48,7 +48,7 @@ class SyncTests(TestCase): assert list(status) == ['2'] assert next(a.list())[0] == '2' assert next(b.list())[0] == '2' - obj2_a, uid2_a, etag2_a = a.get('2') + obj2_a, etag2_a = a.get('2') assert obj2_a.raw == item2.raw new_item2 = Item('UID:2\nHUEHUEHUE:PRECISELY') @@ -59,7 +59,7 @@ class SyncTests(TestCase): assert list(status) == list(old_status) assert next(a.list())[0] == '2' assert next(b.list())[0] == '2' - obj, uid, etag = b.get('2') + obj, etag = b.get('2') assert obj.raw == new_item2.raw def test_irrelevant_status(self): @@ -68,3 +68,12 @@ class SyncTests(TestCase): status = {'1': ('UID:1', 1234)} sync(a, b, status) assert not status + + def test_new_item(self): + a = MemoryStorage() + b = MemoryStorage() + status = {} + a.upload(Item('UID:1')) + sync(a, b, status) + obj, etag = b.get('1') + assert obj.raw == 'UID:1'