Make _create_bogus_item a fixture.

Also rename the "storage" fixture to "get_storage".
This commit is contained in:
Markus Unterwaditzer 2014-08-06 22:03:30 +02:00
parent 6841b25264
commit 2e2082fb55
4 changed files with 60 additions and 64 deletions

View file

@ -28,24 +28,28 @@ class BaseStorageTests(object):
raise NotImplementedError()
@pytest.fixture
def storage(self, storage_args):
def get_storage(self, storage_args):
def inner(**kw):
return self.storage_class(**storage_args(**kw))
return inner
@pytest.fixture
def s(self, storage):
return storage()
def s(self, get_storage):
return get_storage()
def _create_bogus_item(self, item_template=None):
# 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))
@pytest.fixture
def get_item(self):
def inner(item_template=None):
# 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))
def test_generic(self, s):
items = [self._create_bogus_item() for i in range(1, 10)]
return inner
def test_generic(self, s, get_item):
items = [get_item() for i in range(1, 10)]
hrefs = []
for item in items:
hrefs.append(s.upload(item))
@ -61,67 +65,67 @@ class BaseStorageTests(object):
def test_empty_get_multi(self, s):
assert list(s.get_multi([])) == []
def test_upload_already_existing(self, s):
item = self._create_bogus_item()
def test_upload_already_existing(self, s, get_item):
item = get_item()
s.upload(item)
with pytest.raises(exceptions.PreconditionFailed):
s.upload(item)
def test_upload(self, s):
item = self._create_bogus_item()
def test_upload(self, s, get_item):
item = get_item()
href, etag = s.upload(item)
assert_item_equals(s.get(href)[0], item)
def test_update(self, s):
item = self._create_bogus_item()
def test_update(self, s, get_item):
item = get_item()
href, etag = s.upload(item)
assert_item_equals(s.get(href)[0], item)
new_item = self._create_bogus_item()
new_item = get_item()
new_etag = s.update(href, new_item, etag)
# See https://github.com/untitaker/vdirsyncer/issues/48
assert isinstance(new_etag, (bytes, text_type))
assert_item_equals(s.get(href)[0], new_item)
def test_update_nonexisting(self, s):
item = self._create_bogus_item()
def test_update_nonexisting(self, s, get_item):
item = get_item()
with pytest.raises(exceptions.PreconditionFailed):
s.update('huehue', item, '"123"')
def test_wrong_etag(self, s):
item = self._create_bogus_item()
def test_wrong_etag(self, s, get_item):
item = get_item()
href, etag = s.upload(item)
with pytest.raises(exceptions.PreconditionFailed):
s.update(href, item, '"lolnope"')
with pytest.raises(exceptions.PreconditionFailed):
s.delete(href, '"lolnope"')
def test_delete(self, s):
href, etag = s.upload(self._create_bogus_item())
def test_delete(self, s, get_item):
href, etag = s.upload(get_item())
s.delete(href, etag)
assert not list(s.list())
def test_delete_nonexisting(self, s):
def test_delete_nonexisting(self, s, get_item):
with pytest.raises(exceptions.PreconditionFailed):
s.delete('1', '"123"')
def test_list(self, s):
def test_list(self, s, get_item):
assert not list(s.list())
href, etag = s.upload(self._create_bogus_item())
href, etag = s.upload(get_item())
assert list(s.list()) == [(href, etag)]
def test_has(self, s):
def test_has(self, s, get_item):
assert not s.has('asd')
href, etag = s.upload(self._create_bogus_item())
href, etag = s.upload(get_item())
assert s.has(href)
assert not s.has('asd')
def test_update_others_stay_the_same(self, s):
def test_update_others_stay_the_same(self, s, get_item):
info = dict([
s.upload(self._create_bogus_item()),
s.upload(self._create_bogus_item()),
s.upload(self._create_bogus_item()),
s.upload(self._create_bogus_item())
s.upload(get_item()),
s.upload(get_item()),
s.upload(get_item()),
s.upload(get_item())
])
assert dict(
@ -135,7 +139,7 @@ class BaseStorageTests(object):
class SupportsCollections(object):
def test_discover(self, storage_args):
def test_discover(self, storage_args, get_item):
collections = set()
def main():
@ -147,7 +151,7 @@ class SupportsCollections(object):
s = self.storage_class(**storage_args(collection=collection))
# radicale ignores empty collections during discovery
item = self._create_bogus_item()
item = get_item()
s.upload(item)
collections.add(s.collection)
@ -176,8 +180,8 @@ class SupportsCollections(object):
assert 'collection argument must not be given' in str(excinfo.value)
def test_collection_arg(self, storage):
s = storage(collection='test2')
def test_collection_arg(self, get_storage):
s = get_storage(collection='test2')
# Can't do stronger assertion because of radicale, which needs a
# fileextension to guess the collection type.
assert 'test2' in s.collection

View file

@ -53,12 +53,6 @@ class DavStorageTests(ServerMixin, StorageTests):
pass
assert not list(s.list())
def test_wrong_etag(self, s):
super(DavStorageTests, self).test_wrong_etag(s)
def test_update_nonexisting(self, s):
super(DavStorageTests, self).test_update_nonexisting(s)
def test_dav_empty_get_multi_performance(self, s, monkeypatch):
def breakdown(*a, **kw):
raise AssertionError('Expected not to be called.')
@ -73,9 +67,9 @@ class TestCaldavStorage(DavStorageTests):
item_template = TASK_TEMPLATE
def test_both_vtodo_and_vevent(self, s):
task = self._create_bogus_item(item_template=TASK_TEMPLATE)
event = self._create_bogus_item(item_template=EVENT_TEMPLATE)
def test_both_vtodo_and_vevent(self, s, get_item):
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([
@ -84,18 +78,16 @@ class TestCaldavStorage(DavStorageTests):
])
@pytest.mark.parametrize('item_type', ['VTODO', 'VEVENT'])
def test_item_types_correctness(self, item_type, storage_args):
def test_item_types_correctness(self, item_type, storage_args, get_item):
other_item_type = 'VTODO' if item_type == 'VEVENT' else 'VEVENT'
s = self.storage_class(item_types=(item_type,), **storage_args())
try:
s.upload(self._create_bogus_item(
item_template=templates[other_item_type]))
s.upload(self._create_bogus_item(
item_template=templates[other_item_type]))
s.upload(get_item(item_template=templates[other_item_type]))
s.upload(get_item(item_template=templates[other_item_type]))
except (exceptions.Error, requests.exceptions.HTTPError):
pass
href, etag = \
s.upload(self._create_bogus_item(
s.upload(get_item(
item_template=templates[item_type]))
((href2, etag2),) = s.list()
assert href2 == href
@ -109,9 +101,9 @@ class TestCaldavStorage(DavStorageTests):
()
])
def test_item_types_performance(self, storage_args, item_types,
monkeypatch):
monkeypatch, get_item):
s = self.storage_class(item_types=item_types, **storage_args())
item = self._create_bogus_item()
item = get_item()
href, etag = s.upload(item)
old_dav_query = s._dav_query
@ -131,13 +123,13 @@ class TestCaldavStorage(DavStorageTests):
@pytest.mark.xfail(dav_server == 'radicale',
reason='Radicale doesn\'t support timeranges.')
def test_timerange_correctness(self, storage_args):
def test_timerange_correctness(self, storage_args, get_item):
start_date = datetime.datetime(2013, 9, 10)
end_date = datetime.datetime(2013, 9, 13)
s = self.storage_class(start_date=start_date, end_date=end_date,
**storage_args())
too_old_item = self._create_bogus_item(item_template=dedent(u'''
too_old_item = get_item(item_template=dedent(u'''
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
@ -151,7 +143,7 @@ class TestCaldavStorage(DavStorageTests):
END:VCALENDAR
''').strip())
too_new_item = self._create_bogus_item(item_template=dedent(u'''
too_new_item = get_item(item_template=dedent(u'''
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
@ -165,7 +157,7 @@ class TestCaldavStorage(DavStorageTests):
END:VCALENDAR
''').strip())
good_item = self._create_bogus_item(item_template=dedent(u'''
good_item = get_item(item_template=dedent(u'''
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN

View file

@ -78,16 +78,16 @@ class TestHttpStorage(BaseStorageTests):
return {'url': 'http://localhost:123/collection.txt',
'path': self.tmpfile}
def test_update(self, s):
def test_update(self, s, get_item):
'''The original testcase tries to fetch with the old href. But this
storage doesn't have real hrefs, so the href might change if the
underlying UID changes. '''
item = self._create_bogus_item()
item = get_item()
href, etag = s.upload(item)
assert_item_equals(s.get(href)[0], item)
new_item = self._create_bogus_item()
new_item = get_item()
s.update(href, new_item, etag)
((new_href, new_etag),) = s.list()
assert_item_equals(s.get(new_href)[0], new_item)

View file

@ -42,16 +42,16 @@ class TestSingleFileStorage(BaseStorageTests):
with pytest.raises(IOError):
s = self.storage_class(str(tmpdir) + '/foo.ics', create=False)
def test_update(self, s):
def test_update(self, s, get_item):
'''The original testcase tries to fetch with the old href. But this
storage doesn't have real hrefs, so the href might change if the
underlying UID changes. '''
item = self._create_bogus_item()
item = get_item()
href, etag = s.upload(item)
assert_item_equals(s.get(href)[0], item)
new_item = self._create_bogus_item()
new_item = get_item()
s.update(href, new_item, etag)
((new_href, new_etag),) = s.list()
assert_item_equals(s.get(new_href)[0], new_item)