From 37c2467f59007e56bba595320f4e272d93675455 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Tue, 25 Nov 2014 15:17:51 +0100 Subject: [PATCH] Stricten testsuite --- tests/test_sync.py | 38 ++++++++++++++++++------------------ tests/utils/test_main.py | 2 +- vdirsyncer/storage/memory.py | 14 ++++++++----- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/tests/test_sync.py b/tests/test_sync.py index 0a6b24d..73937ff 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -60,60 +60,60 @@ def test_missing_status_and_different_items(): def test_upload_and_update(): - a = MemoryStorage() - b = MemoryStorage() + a = MemoryStorage(fileext='.a') + b = MemoryStorage(fileext='.b') status = {} item = Item(u'UID:1') # new item 1 in a a.upload(item) sync(a, b, status) - assert_item_equals(b.get('1')[0], item) + assert_item_equals(b.get('1.b')[0], item) item = Item(u'UID:1\nASDF:YES') # update of item 1 in b - b.update('1', item, b.get('1')[1]) + b.update('1.b', item, b.get('1.b')[1]) sync(a, b, status) - assert_item_equals(a.get('1')[0], item) + assert_item_equals(a.get('1.a')[0], item) item2 = Item(u'UID:2') # new item 2 in b b.upload(item2) sync(a, b, status) - assert_item_equals(a.get('2')[0], item2) + assert_item_equals(a.get('2.a')[0], item2) item2 = Item(u'UID:2\nASDF:YES') # update of item 2 in a - a.update('2', item2, a.get('2')[1]) + a.update('2.a', item2, a.get('2.a')[1]) sync(a, b, status) - assert_item_equals(b.get('2')[0], item2) + assert_item_equals(b.get('2.b')[0], item2) def test_deletion(): - a = MemoryStorage() - b = MemoryStorage() + a = MemoryStorage(fileext='.a') + b = MemoryStorage(fileext='.b') status = {} item = Item(u'UID:1') a.upload(item) a.upload(Item(u'UID:2')) sync(a, b, status) - b.delete('1', b.get('1')[1]) + b.delete('1.b', b.get('1.b')[1]) sync(a, b, status) - assert not a.has('1') and not b.has('1') + assert not a.has('1.a') and not b.has('1.b') a.upload(item) sync(a, b, status) - assert a.has('1') and b.has('1') - a.delete('1', a.get('1')[1]) + assert a.has('1.a') and b.has('1.b') + a.delete('1.a', a.get('1.a')[1]) sync(a, b, status) - assert not a.has('1') and not b.has('1') + assert not a.has('1.a') and not b.has('1.b') def test_already_synced(): - a = MemoryStorage() - b = MemoryStorage() + a = MemoryStorage(fileext='.a') + b = MemoryStorage(fileext='.b') item = Item(u'UID:1') a.upload(item) b.upload(item) status = { - '1': ('1', a.get('1')[1], '1', b.get('1')[1]) + '1': ('1.a', a.get('1.a')[1], '1.b', b.get('1.b')[1]) } old_status = dict(status) a.update = b.update = a.upload = b.upload = \ @@ -122,7 +122,7 @@ def test_already_synced(): for i in (1, 2): sync(a, b, status) assert status == old_status - assert a.has('1') and b.has('1') + assert a.has('1.a') and b.has('1.b') @pytest.mark.parametrize('winning_storage', 'ab') diff --git a/tests/utils/test_main.py b/tests/utils/test_main.py index 0b0d7f7..f42aa61 100644 --- a/tests/utils/test_main.py +++ b/tests/utils/test_main.py @@ -232,7 +232,7 @@ def test_get_class_init_args_on_storage(): from vdirsyncer.storage.memory import MemoryStorage all, required = utils.get_class_init_args(MemoryStorage) - assert all == set(['collection', 'read_only', 'instance_name']) + assert all == set(['fileext', 'collection', 'read_only', 'instance_name']) assert not required diff --git a/vdirsyncer/storage/memory.py b/vdirsyncer/storage/memory.py index f8006b4..87ef77d 100644 --- a/vdirsyncer/storage/memory.py +++ b/vdirsyncer/storage/memory.py @@ -13,7 +13,7 @@ import vdirsyncer.exceptions as exceptions from vdirsyncer.storage.base import Storage -def _get_etag(): +def _random_string(): return '{:.9f}'.format(random.random()) @@ -23,12 +23,16 @@ class MemoryStorage(Storage): Saves data in RAM, only useful for testing. ''' - def __init__(self, **kwargs): + def __init__(self, fileext='', **kwargs): if kwargs.get('collection') is not None: raise ValueError('MemoryStorage does not support collections.') self.items = {} # href => (etag, item) + self.fileext = fileext super(MemoryStorage, self).__init__(**kwargs) + def _get_href(self, item): + return item.ident + self.fileext + def list(self): for href, (etag, item) in self.items.items(): yield href, etag @@ -41,10 +45,10 @@ class MemoryStorage(Storage): return href in self.items def upload(self, item): - href = item.ident + href = self._get_href(item) if href in self.items: raise exceptions.AlreadyExistingError(item) - etag = _get_etag() + etag = _random_string() self.items[href] = (etag, item) return href, etag @@ -55,7 +59,7 @@ class MemoryStorage(Storage): if etag != actual_etag: raise exceptions.WrongEtagError(etag, actual_etag) - new_etag = _get_etag() + new_etag = _random_string() self.items[href] = (new_etag, item) return new_etag