Test against more item types

This commit is contained in:
Markus Unterwaditzer 2014-11-04 20:33:19 +01:00
parent 0e68966963
commit ca30542801
4 changed files with 42 additions and 66 deletions

View file

@ -14,12 +14,17 @@ import vdirsyncer.exceptions as exceptions
from vdirsyncer.storage.base import Item from vdirsyncer.storage.base import Item
from vdirsyncer.utils.compat import iteritems, text_type from vdirsyncer.utils.compat import iteritems, text_type
from .. import SIMPLE_TEMPLATE, assert_item_equals from .. import EVENT_TEMPLATE, TASK_TEMPLATE, VCARD_TEMPLATE, \
assert_item_equals
def format_item(item_template):
# assert that special chars are handled correctly.
r = '{}@vdirsyncer'.format(random.random())
return Item(item_template.format(r=r))
class BaseStorageTests(object): class BaseStorageTests(object):
item_template = SIMPLE_TEMPLATE
@pytest.fixture @pytest.fixture
def storage_args(self): def storage_args(self):
return self.get_storage_args return self.get_storage_args
@ -38,15 +43,13 @@ class BaseStorageTests(object):
def s(self, get_storage): def s(self, get_storage):
return get_storage() return get_storage()
@pytest.fixture @pytest.fixture(params=[EVENT_TEMPLATE, TASK_TEMPLATE, VCARD_TEMPLATE])
def get_item(self): def item_template(self, request):
def inner(item_template=None): return request.param
# assert that special chars are handled correctly.
r = '{}@vdirsyncer'.format(random.random())
item_template = item_template or self.item_template
return Item(item_template.format(r=r))
return inner @pytest.fixture
def get_item(self, item_template):
return lambda: format_item(item_template)
def test_generic(self, s, get_item): def test_generic(self, s, get_item):
items = [get_item() for i in range(1, 10)] items = [get_item() for i in range(1, 10)]

View file

@ -23,7 +23,7 @@ from vdirsyncer.storage.base import Item
from vdirsyncer.storage.dav import CaldavStorage, CarddavStorage, \ from vdirsyncer.storage.dav import CaldavStorage, CarddavStorage, \
_normalize_href _normalize_href
from .. import StorageTests from .. import StorageTests, format_item
dav_server = os.environ.get('DAV_SERVER', '').strip() or 'radicale' dav_server = os.environ.get('DAV_SERVER', '').strip() or 'radicale'
@ -37,13 +37,6 @@ def _get_server_mixin(server_name):
ServerMixin = _get_server_mixin(dav_server) ServerMixin = _get_server_mixin(dav_server)
templates = {
'VCARD': VCARD_TEMPLATE,
'VEVENT': EVENT_TEMPLATE,
'VTODO': TASK_TEMPLATE
}
class DavStorageTests(ServerMixin, StorageTests): class DavStorageTests(ServerMixin, StorageTests):
def test_dav_broken_item(self, s): def test_dav_broken_item(self, s):
item = Item(u'HAHA:YES') item = Item(u'HAHA:YES')
@ -65,47 +58,30 @@ class DavStorageTests(ServerMixin, StorageTests):
class TestCaldavStorage(DavStorageTests): class TestCaldavStorage(DavStorageTests):
storage_class = CaldavStorage storage_class = CaldavStorage
item_template = TASK_TEMPLATE @pytest.fixture(params=[EVENT_TEMPLATE, TASK_TEMPLATE])
def item_template(self, request):
def test_both_vtodo_and_vevent(self, s, get_item): return request.param
task = get_item(item_template=TASK_TEMPLATE)
event = get_item(item_template=EVENT_TEMPLATE)
href_etag_task = s.upload(task)
href_etag_event = s.upload(event)
assert set(s.list()) == set([
href_etag_task,
href_etag_event
])
@pytest.mark.parametrize('item_type', ['VTODO', 'VEVENT']) @pytest.mark.parametrize('item_type', ['VTODO', 'VEVENT'])
def test_item_types_correctness(self, item_type, storage_args, get_item): def test_doesnt_accept_vcard(self, item_type, storage_args):
other_item_type = 'VTODO' if item_type == 'VEVENT' else 'VEVENT'
s = self.storage_class(item_types=(item_type,), **storage_args()) s = self.storage_class(item_types=(item_type,), **storage_args())
try: try:
s.upload(get_item(item_template=templates[other_item_type])) s.upload(format_item(VCARD_TEMPLATE))
s.upload(get_item(item_template=templates[other_item_type]))
except (exceptions.Error, requests.exceptions.HTTPError): except (exceptions.Error, requests.exceptions.HTTPError):
pass pass
href, etag = \ assert not list(s.list())
s.upload(get_item(
item_template=templates[item_type]))
((href2, etag2),) = s.list()
assert href2 == href
assert etag2 == etag
@pytest.mark.parametrize('item_types', [ @pytest.mark.parametrize('item_types,calls_num', [
('VTODO',), (('VTODO',), 1),
('VEVENT',), (('VEVENT',), 1),
('VTODO', 'VEVENT'), (('VTODO', 'VEVENT'), 2),
('VTODO', 'VEVENT', 'VJOURNAL'), (('VTODO', 'VEVENT', 'VJOURNAL'), 3),
() ((), 1)
]) ])
def test_item_types_performance(self, storage_args, item_types, def test_item_types_performance(self, storage_args, item_types, calls_num,
monkeypatch, get_item): monkeypatch, get_item):
s = self.storage_class(item_types=item_types, **storage_args()) s = self.storage_class(item_types=item_types, **storage_args())
item = get_item()
href, etag = s.upload(item)
old_dav_query = s._dav_query old_dav_query = s._dav_query
calls = [] calls = []
@ -114,22 +90,18 @@ class TestCaldavStorage(DavStorageTests):
return old_dav_query(*a, **kw) return old_dav_query(*a, **kw)
monkeypatch.setattr(s, '_dav_query', _dav_query) monkeypatch.setattr(s, '_dav_query', _dav_query)
list(s.list())
rv = list(s.list()) assert len(calls) == calls_num
if (dav_server != 'radicale' and not s.item_types) \
or item.parsed.name in s.item_types:
assert rv == [(href, etag)]
assert len(calls) == (len(item_types) or 1)
@pytest.mark.xfail(dav_server == 'radicale', @pytest.mark.xfail(dav_server == 'radicale',
reason='Radicale doesn\'t support timeranges.') reason='Radicale doesn\'t support timeranges.')
def test_timerange_correctness(self, storage_args, get_item): def test_timerange_correctness(self, storage_args):
start_date = datetime.datetime(2013, 9, 10) start_date = datetime.datetime(2013, 9, 10)
end_date = datetime.datetime(2013, 9, 13) end_date = datetime.datetime(2013, 9, 13)
s = self.storage_class(start_date=start_date, end_date=end_date, s = self.storage_class(start_date=start_date, end_date=end_date,
**storage_args()) **storage_args())
too_old_item = get_item(item_template=dedent(u''' too_old_item = format_item(dedent(u'''
BEGIN:VCALENDAR BEGIN:VCALENDAR
VERSION:2.0 VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN PRODID:-//hacksw/handcal//NONSGML v1.0//EN
@ -143,7 +115,7 @@ class TestCaldavStorage(DavStorageTests):
END:VCALENDAR END:VCALENDAR
''').strip()) ''').strip())
too_new_item = get_item(item_template=dedent(u''' too_new_item = format_item(dedent(u'''
BEGIN:VCALENDAR BEGIN:VCALENDAR
VERSION:2.0 VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN PRODID:-//hacksw/handcal//NONSGML v1.0//EN
@ -157,7 +129,7 @@ class TestCaldavStorage(DavStorageTests):
END:VCALENDAR END:VCALENDAR
''').strip()) ''').strip())
good_item = get_item(item_template=dedent(u''' good_item = format_item(dedent(u'''
BEGIN:VCALENDAR BEGIN:VCALENDAR
VERSION:2.0 VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN PRODID:-//hacksw/handcal//NONSGML v1.0//EN
@ -207,7 +179,10 @@ class TestCaldavStorage(DavStorageTests):
class TestCarddavStorage(DavStorageTests): class TestCarddavStorage(DavStorageTests):
storage_class = CarddavStorage storage_class = CarddavStorage
item_template = VCARD_TEMPLATE
@pytest.fixture
def item_template(self, request):
return VCARD_TEMPLATE
@pytest.mark.parametrize('base,path', [ @pytest.mark.parametrize('base,path', [

View file

@ -16,7 +16,7 @@ import vdirsyncer.storage.http
from vdirsyncer.storage.singlefile import SingleFileStorage from vdirsyncer.storage.singlefile import SingleFileStorage
from . import BaseStorageTests from . import BaseStorageTests
from .. import EVENT_TEMPLATE, assert_item_equals from .. import assert_item_equals
class CombinedStorage(Storage): class CombinedStorage(Storage):
@ -50,7 +50,6 @@ class CombinedStorage(Storage):
class TestHttpStorage(BaseStorageTests): class TestHttpStorage(BaseStorageTests):
storage_class = CombinedStorage storage_class = CombinedStorage
item_template = EVENT_TEMPLATE
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def setup_tmpdir(self, tmpdir, monkeypatch): def setup_tmpdir(self, tmpdir, monkeypatch):
@ -69,7 +68,7 @@ class TestHttpStorage(BaseStorageTests):
r._content = b'' r._content = b''
r.headers['Content-Type'] = 'text/icalendar' r.headers['Content-Type'] = 'text/icalendar'
r.encoding = 'ISO-8859-1' r.encoding = 'utf-8'
return r return r
monkeypatch.setattr(vdirsyncer.storage.http, 'request', _request) monkeypatch.setattr(vdirsyncer.storage.http, 'request', _request)

View file

@ -12,13 +12,12 @@ import pytest
from vdirsyncer.storage.singlefile import SingleFileStorage from vdirsyncer.storage.singlefile import SingleFileStorage
from . import BaseStorageTests from . import BaseStorageTests
from .. import EVENT_TEMPLATE, assert_item_equals from .. import assert_item_equals
class TestSingleFileStorage(BaseStorageTests): class TestSingleFileStorage(BaseStorageTests):
storage_class = SingleFileStorage storage_class = SingleFileStorage
item_template = EVENT_TEMPLATE
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def setup(self, tmpdir): def setup(self, tmpdir):