mirror of
https://github.com/samsonjs/vdirsyncer.git
synced 2026-04-27 14:57:41 +00:00
Add basic discover tests
This commit is contained in:
parent
476ca75eb1
commit
ef786c3586
7 changed files with 54 additions and 14 deletions
|
|
@ -88,3 +88,7 @@ class StorageTests(object):
|
||||||
assert not list(s.list())
|
assert not list(s.list())
|
||||||
s.upload(self._create_bogus_item('1'))
|
s.upload(self._create_bogus_item('1'))
|
||||||
assert list(s.list())
|
assert list(s.list())
|
||||||
|
|
||||||
|
def test_discover(self):
|
||||||
|
# Too storage specific to implement in an abstract way
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
|
||||||
8
tests/storage/conftest.py
Normal file
8
tests/storage/conftest.py
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
import pytest
|
||||||
|
import tempfile
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def class_tmpdir(request):
|
||||||
|
request.cls.tmpdir = x = tempfile.mkdtemp()
|
||||||
|
request.addfinalizer(lambda: shutil.rmtree(x))
|
||||||
|
|
@ -18,6 +18,7 @@ import shutil
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import urlparse
|
import urlparse
|
||||||
|
import pytest
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
from werkzeug.test import Client
|
from werkzeug.test import Client
|
||||||
|
|
@ -71,16 +72,14 @@ 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'''
|
||||||
tmpdir = None
|
|
||||||
storage_class = None
|
storage_class = None
|
||||||
radicale_path = None
|
radicale_path = None
|
||||||
patcher = None
|
patcher = None
|
||||||
|
|
||||||
def _get_storage(self, **kwargs):
|
def _get_storage(self, **kwargs):
|
||||||
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()
|
||||||
|
|
@ -103,9 +102,6 @@ class DavStorageTests(StorageTests):
|
||||||
|
|
||||||
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
|
||||||
|
|
|
||||||
|
|
@ -10,19 +10,30 @@
|
||||||
|
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import pytest
|
||||||
import shutil
|
import shutil
|
||||||
|
import os
|
||||||
from vdirsyncer.storage.filesystem import FilesystemStorage
|
from vdirsyncer.storage.filesystem import FilesystemStorage
|
||||||
from . import StorageTests
|
from . import StorageTests
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures('class_tmpdir')
|
||||||
class FilesystemStorageTests(TestCase, StorageTests):
|
class FilesystemStorageTests(TestCase, StorageTests):
|
||||||
tmpdir = None
|
|
||||||
|
|
||||||
def _get_storage(self, **kwargs):
|
def _get_storage(self, **kwargs):
|
||||||
path = self.tmpdir = tempfile.mkdtemp()
|
return FilesystemStorage(path=self.tmpdir, fileext='.txt', **kwargs)
|
||||||
return FilesystemStorage(path=path, fileext='.txt', **kwargs)
|
|
||||||
|
|
||||||
def teardown_method(self, method):
|
def test_discover(self):
|
||||||
if self.tmpdir is not None:
|
paths = set()
|
||||||
shutil.rmtree(self.tmpdir)
|
for i, collection in enumerate('abcd'):
|
||||||
self.tmpdir = None
|
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
|
||||||
|
|
|
||||||
|
|
@ -17,3 +17,6 @@ class MemoryStorageTests(TestCase, StorageTests):
|
||||||
|
|
||||||
def _get_storage(self, **kwargs):
|
def _get_storage(self, **kwargs):
|
||||||
return MemoryStorage(**kwargs)
|
return MemoryStorage(**kwargs)
|
||||||
|
|
||||||
|
def test_discover(self):
|
||||||
|
'''This test doesn't make any sense here.'''
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,11 @@ class Storage(object):
|
||||||
- HREF: Per-storage identifier of item, might be UID.
|
- HREF: Per-storage identifier of item, might be UID.
|
||||||
- ETAG: Checksum of item, or something similar that changes when the
|
- ETAG: Checksum of item, or something similar that changes when the
|
||||||
object does.
|
object does.
|
||||||
|
|
||||||
|
:param collection: If None, the given URL or path is already directly
|
||||||
|
referring to a collection. Otherwise it will be treated as a basepath
|
||||||
|
to many collections (e.g. a vdir) and the given collection name will be
|
||||||
|
looked for.
|
||||||
'''
|
'''
|
||||||
fileext = '.txt'
|
fileext = '.txt'
|
||||||
_repr_attributes = ()
|
_repr_attributes = ()
|
||||||
|
|
@ -40,6 +45,12 @@ class Storage(object):
|
||||||
def __init__(self, item_class=Item):
|
def __init__(self, item_class=Item):
|
||||||
self.item_class = item_class
|
self.item_class = item_class
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def discover(cls, **kwargs):
|
||||||
|
'''Discover collections given a basepath to many collections.
|
||||||
|
:returns: Iterable of storages.'''
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
def _get_href(self, uid):
|
def _get_href(self, uid):
|
||||||
return uid + self.fileext
|
return uid + self.fileext
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,12 +25,19 @@ class FilesystemStorage(Storage):
|
||||||
'''
|
'''
|
||||||
:param path: Absolute path to a *collection* inside a vdir.
|
:param path: Absolute path to a *collection* inside a vdir.
|
||||||
'''
|
'''
|
||||||
|
super(FilesystemStorage, self).__init__(**kwargs)
|
||||||
self.path = expand_path(path)
|
self.path = expand_path(path)
|
||||||
if collection is not None:
|
if collection is not None:
|
||||||
self.path = os.path.join(self.path, collection)
|
self.path = os.path.join(self.path, collection)
|
||||||
self.encoding = encoding
|
self.encoding = encoding
|
||||||
self.fileext = fileext
|
self.fileext = fileext
|
||||||
super(FilesystemStorage, self).__init__(**kwargs)
|
|
||||||
|
@classmethod
|
||||||
|
def discover(cls, path, **kwargs):
|
||||||
|
for collection in os.listdir(path):
|
||||||
|
s = cls(path=path, collection=collection, **kwargs)
|
||||||
|
if next(s.list(), None) is not None:
|
||||||
|
yield s
|
||||||
|
|
||||||
def _get_filepath(self, href):
|
def _get_filepath(self, href):
|
||||||
return os.path.join(self.path, href)
|
return os.path.join(self.path, href)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue