API cleanup, bug fixes

This commit is contained in:
Markus Unterwaditzer 2014-02-16 23:17:19 +01:00
parent 89aeb27afb
commit e1cb484d2d
6 changed files with 23 additions and 14 deletions

View file

@ -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):
'''

View file

@ -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))

View file

@ -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

View file

@ -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()

View file

@ -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)

View file

@ -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'