mirror of
https://github.com/samsonjs/vdirsyncer.git
synced 2026-04-27 14:57:41 +00:00
Deduplication
This commit is contained in:
parent
bef7b3e25a
commit
03b6d11ac8
6 changed files with 45 additions and 36 deletions
|
|
@ -21,9 +21,12 @@ class StorageTests(object):
|
||||||
item_template = item_template or self.item_template
|
item_template = item_template or self.item_template
|
||||||
return Item(item_template.format(uid=uid, r=r))
|
return Item(item_template.format(uid=uid, r=r))
|
||||||
|
|
||||||
def _get_storage(self, **kwargs):
|
def get_storage_args(self, collection=None):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def _get_storage(self):
|
||||||
|
return self.storage_class(**self.get_storage_args())
|
||||||
|
|
||||||
def test_generic(self):
|
def test_generic(self):
|
||||||
items = map(self._create_bogus_item, range(1, 10))
|
items = map(self._create_bogus_item, range(1, 10))
|
||||||
for i, item in enumerate(items):
|
for i, item in enumerate(items):
|
||||||
|
|
@ -90,5 +93,15 @@ class StorageTests(object):
|
||||||
assert list(s.list())
|
assert list(s.list())
|
||||||
|
|
||||||
def test_discover(self):
|
def test_discover(self):
|
||||||
# Too storage specific to implement in an abstract way
|
items = []
|
||||||
raise NotImplementedError()
|
for i in range(4):
|
||||||
|
s = self.storage_class(**self.get_storage_args(collection=str(i)))
|
||||||
|
items.append(self._create_bogus_item(str(i)))
|
||||||
|
s.upload(items[-1])
|
||||||
|
|
||||||
|
d = self.storage_class.discover(
|
||||||
|
**self.get_storage_args(collection=None))
|
||||||
|
for s in d:
|
||||||
|
((href, etag),) = s.list()
|
||||||
|
item, etag = s.get(href)
|
||||||
|
assert item.raw in set(x.raw for x in items)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,6 @@
|
||||||
import pytest
|
import pytest
|
||||||
import tempfile
|
|
||||||
import shutil
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def class_tmpdir(request):
|
def class_tmpdir(request, tmpdir):
|
||||||
request.cls.tmpdir = x = tempfile.mkdtemp()
|
request.instance.tmpdir = str(tmpdir)
|
||||||
request.addfinalizer(lambda: shutil.rmtree(x))
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,8 @@
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import urlparse
|
import urlparse
|
||||||
import pytest
|
import tempfile
|
||||||
|
import shutil
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
from werkzeug.test import Client
|
from werkzeug.test import Client
|
||||||
|
|
@ -70,24 +71,22 @@ class Response(object):
|
||||||
raise HTTPError(str(self.status_code))
|
raise HTTPError(str(self.status_code))
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures('class_tmpdir')
|
|
||||||
class DavStorageTests(StorageTests):
|
class DavStorageTests(StorageTests):
|
||||||
'''hrefs are paths without scheme or netloc'''
|
'''hrefs are paths without scheme or netloc'''
|
||||||
storage_class = None
|
storage_class = None
|
||||||
patcher = None
|
patcher = None
|
||||||
|
tmpdir = None
|
||||||
|
|
||||||
def _get_storage(self, **kwargs):
|
def setup_method(self, method):
|
||||||
|
self.tmpdir = tempfile.mkdtemp()
|
||||||
do_the_radicale_dance(self.tmpdir)
|
do_the_radicale_dance(self.tmpdir)
|
||||||
from radicale import Application
|
from radicale import Application
|
||||||
app = Application()
|
app = Application()
|
||||||
|
|
||||||
c = Client(app, WerkzeugResponse)
|
c = Client(app, WerkzeugResponse)
|
||||||
server = 'http://127.0.0.1'
|
|
||||||
fileext = self.storage_class.fileext
|
|
||||||
full_url = server + '/bob/test{}/'.format(fileext)
|
|
||||||
|
|
||||||
def x(session, method, url, data=None, headers=None, **kw):
|
def x(session, method, url, data=None, headers=None, **kw):
|
||||||
path = urlparse.urlparse(url).path or self.radicale_path
|
path = urlparse.urlparse(url).path
|
||||||
assert isinstance(data, bytes) or data is None
|
assert isinstance(data, bytes) or data is None
|
||||||
r = c.open(path=path, method=method, data=data, headers=headers)
|
r = c.open(path=path, method=method, data=data, headers=headers)
|
||||||
r = Response(r)
|
r = Response(r)
|
||||||
|
|
@ -96,10 +95,17 @@ class DavStorageTests(StorageTests):
|
||||||
self.patcher = p = mock.patch('requests.Session.request', new=x)
|
self.patcher = p = mock.patch('requests.Session.request', new=x)
|
||||||
p.start()
|
p.start()
|
||||||
|
|
||||||
return self.storage_class(url=full_url, **kwargs)
|
def get_storage_args(self, collection=None):
|
||||||
|
url = 'http://127.0.0.1/bob/'
|
||||||
|
if collection is not None:
|
||||||
|
url += '{}{}'.format(collection, self.storage_class.fileext)
|
||||||
|
return {'url': url}
|
||||||
|
|
||||||
def teardown_method(self, method):
|
def teardown_method(self, method):
|
||||||
self.app = None
|
self.app = None
|
||||||
|
if self.tmpdir is not None:
|
||||||
|
shutil.rmtree(self.tmpdir)
|
||||||
|
self.tmpdir = None
|
||||||
if self.patcher is not None:
|
if self.patcher is not None:
|
||||||
self.patcher.stop()
|
self.patcher.stop()
|
||||||
self.patcher = None
|
self.patcher = None
|
||||||
|
|
|
||||||
|
|
@ -17,22 +17,11 @@ from . import StorageTests
|
||||||
|
|
||||||
@pytest.mark.usefixtures('class_tmpdir')
|
@pytest.mark.usefixtures('class_tmpdir')
|
||||||
class FilesystemStorageTests(TestCase, StorageTests):
|
class FilesystemStorageTests(TestCase, StorageTests):
|
||||||
|
storage_class = FilesystemStorage
|
||||||
|
|
||||||
def _get_storage(self, **kwargs):
|
def get_storage_args(self, collection=None):
|
||||||
return FilesystemStorage(path=self.tmpdir, fileext='.txt', **kwargs)
|
path = self.tmpdir
|
||||||
|
if collection is not None:
|
||||||
def test_discover(self):
|
path = os.path.join(path, collection)
|
||||||
paths = set()
|
os.makedirs(path)
|
||||||
for i, collection in enumerate('abcd'):
|
return {'path': path, 'fileext': '.txt'}
|
||||||
p = os.path.join(self.tmpdir, collection)
|
|
||||||
os.makedirs(os.path.join(self.tmpdir, collection))
|
|
||||||
fname = os.path.join(p, 'asdf.txt')
|
|
||||||
with open(fname, 'w+') as f:
|
|
||||||
f.write(self._create_bogus_item(i).raw)
|
|
||||||
paths.add(p)
|
|
||||||
|
|
||||||
storages = list(FilesystemStorage.discover(path=self.tmpdir,
|
|
||||||
fileext='.txt'))
|
|
||||||
assert len(storages) == 4
|
|
||||||
for s in storages:
|
|
||||||
assert s.path in paths
|
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,10 @@ from . import StorageTests
|
||||||
|
|
||||||
class MemoryStorageTests(TestCase, StorageTests):
|
class MemoryStorageTests(TestCase, StorageTests):
|
||||||
|
|
||||||
def _get_storage(self, **kwargs):
|
storage_class = MemoryStorage
|
||||||
return MemoryStorage(**kwargs)
|
|
||||||
|
def get_storage_args(collection=None):
|
||||||
|
return {}
|
||||||
|
|
||||||
def test_discover(self):
|
def test_discover(self):
|
||||||
'''This test doesn't make any sense here.'''
|
'''This test doesn't make any sense here.'''
|
||||||
|
|
|
||||||
|
|
@ -177,6 +177,8 @@ class DavStorage(Storage):
|
||||||
|
|
||||||
def update(self, href, obj, etag):
|
def update(self, href, obj, etag):
|
||||||
href = self._normalize_href(href)
|
href = self._normalize_href(href)
|
||||||
|
if etag is None:
|
||||||
|
raise ValueError('etag must be given and must not be None.')
|
||||||
return self._put(href, obj, etag)
|
return self._put(href, obj, etag)
|
||||||
|
|
||||||
def upload(self, obj):
|
def upload(self, obj):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue