mirror of
https://github.com/samsonjs/vdirsyncer.git
synced 2026-04-27 14:57:41 +00:00
Add tests, fix test breakage
This commit is contained in:
parent
d3c527618f
commit
32b0ac5a6b
7 changed files with 65 additions and 9 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
*.pyc
|
||||||
|
__pycache__
|
||||||
0
vdirsyncer/__init__.py
Normal file
0
vdirsyncer/__init__.py
Normal file
0
vdirsyncer/storage/__init__.py
Normal file
0
vdirsyncer/storage/__init__.py
Normal file
|
|
@ -6,13 +6,13 @@ class Item(object):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def uid(self):
|
def uid(self):
|
||||||
for line in raw.splitlines():
|
for line in self.raw.splitlines():
|
||||||
if line.startswith(b'UID'):
|
if line.startswith(b'UID'):
|
||||||
return line.lstrip(b'UID:').strip()
|
return line.lstrip(b'UID:').strip()
|
||||||
|
|
||||||
|
|
||||||
class Storage(object):
|
class Storage(object):
|
||||||
def __init__(self, fileext, item_class=Item):
|
def __init__(self, fileext='', item_class=Item):
|
||||||
self.fileext = fileext
|
self.fileext = fileext
|
||||||
self.item_class = item_class
|
self.item_class = item_class
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ class FilesystemStorage(Storage):
|
||||||
return os.path.getmtime(href)
|
return os.path.getmtime(href)
|
||||||
|
|
||||||
def _get_href(self, obj):
|
def _get_href(self, obj):
|
||||||
return os.path.join(self.path, obj.uid + b'b' + self.fileext)
|
return os.path.join(self.path, obj.uid + self.fileext)
|
||||||
|
|
||||||
def _get_hrefs(self):
|
def _get_hrefs(self):
|
||||||
for fname in os.listdir(self.path):
|
for fname in os.listdir(self.path):
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,9 @@ class MemoryStorage(Storage):
|
||||||
self.items = {} # href => (etag, object)
|
self.items = {} # href => (etag, object)
|
||||||
super(MemoryStorage, self).__init__(**kwargs)
|
super(MemoryStorage, self).__init__(**kwargs)
|
||||||
|
|
||||||
|
def _get_href(self, obj):
|
||||||
|
return obj.uid + self.fileext
|
||||||
|
|
||||||
def list_items(self):
|
def list_items(self):
|
||||||
for href, (etag, obj) in self.items.items():
|
for href, (etag, obj) in self.items.items():
|
||||||
yield href, etag
|
yield href, etag
|
||||||
|
|
@ -20,15 +23,17 @@ class MemoryStorage(Storage):
|
||||||
return href in self.items
|
return href in self.items
|
||||||
|
|
||||||
def upload(self, obj):
|
def upload(self, obj):
|
||||||
if obj.uid in self.items:
|
href = self._get_href(obj)
|
||||||
|
if href in self.items:
|
||||||
raise exceptions.AlreadyExistingError(obj)
|
raise exceptions.AlreadyExistingError(obj)
|
||||||
etag = datetime.datetime.now()
|
etag = datetime.datetime.now()
|
||||||
self.items[obj.uid] = (etag, obj)
|
self.items[href] = (etag, obj)
|
||||||
return obj.uid, etag
|
return href, etag
|
||||||
|
|
||||||
def update(self, obj, etag):
|
def update(self, obj, etag):
|
||||||
if obj.uid not in self.items:
|
href = self._get_href(obj)
|
||||||
|
if href not in self.items:
|
||||||
raise exceptions.NotFoundError(obj)
|
raise exceptions.NotFoundError(obj)
|
||||||
etag = datetime.datetime.now()
|
etag = datetime.datetime.now()
|
||||||
self.items[obj.uid] = (etag, obj)
|
self.items[href] = (etag, obj)
|
||||||
return obj.uid, etag
|
return href, etag
|
||||||
|
|
|
||||||
49
vdirsyncer/tests/test_storage.py
Normal file
49
vdirsyncer/tests/test_storage.py
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
from unittest import TestCase
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
import shutil
|
||||||
|
from vdirsyncer.storage.base import Item
|
||||||
|
from vdirsyncer.storage.filesystem import FilesystemStorage
|
||||||
|
from vdirsyncer.storage.memory import MemoryStorage
|
||||||
|
|
||||||
|
class StorageTests(object):
|
||||||
|
def _get_storage(self, **kwargs):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def test_generic_upload(self):
|
||||||
|
items = [
|
||||||
|
'UID:1',
|
||||||
|
'UID:2',
|
||||||
|
'UID:3',
|
||||||
|
'UID:4',
|
||||||
|
'UID:5',
|
||||||
|
'UID:6',
|
||||||
|
'UID:7',
|
||||||
|
'UID:8',
|
||||||
|
'UID:9'
|
||||||
|
]
|
||||||
|
fileext = ''
|
||||||
|
s = self._get_storage(fileext=fileext)
|
||||||
|
for item in items:
|
||||||
|
s.upload(Item(item))
|
||||||
|
hrefs = [href for href, etag in s.list_items()]
|
||||||
|
prefix = os.path.commonprefix(hrefs)
|
||||||
|
a = set(x[len(prefix):] for x in hrefs)
|
||||||
|
b = set(str(y) + fileext for y in range(1, 10))
|
||||||
|
assert a == b
|
||||||
|
|
||||||
|
|
||||||
|
class FilesystemStorageTests(TestCase, StorageTests):
|
||||||
|
tmpdir = None
|
||||||
|
def _get_storage(self, **kwargs):
|
||||||
|
path = self.tmpdir = tempfile.mkdtemp()
|
||||||
|
return FilesystemStorage(path=path, **kwargs)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
if self.tmpdir is not None:
|
||||||
|
shutil.rmtree(self.tmpdir)
|
||||||
|
self.tmpdir = None
|
||||||
|
|
||||||
|
class MemoryStorageTests(TestCase, StorageTests):
|
||||||
|
def _get_storage(self, **kwargs):
|
||||||
|
return MemoryStorage(**kwargs)
|
||||||
Loading…
Reference in a new issue