From 4cfca383d915708dc5745f4718978bd4da4b8d59 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Tue, 19 May 2015 13:29:49 +0200 Subject: [PATCH] Small refactor --- tests/storage/__init__.py | 24 +++++++++++++++++------- tests/storage/dav/test_main.py | 23 ++++++++++++----------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/tests/storage/__init__.py b/tests/storage/__init__.py index 910f3db..75595b6 100644 --- a/tests/storage/__init__.py +++ b/tests/storage/__init__.py @@ -19,6 +19,14 @@ def format_item(item_template, uid=None): class StorageTests(object): + storage_class = None + supports_collections = True + + @pytest.fixture(params=['VEVENT', 'VTODO', 'VCARD']) + def item_type(self, request): + '''Parametrize with all supported item types.''' + return request.param + @pytest.fixture def get_storage_args(self): ''' @@ -32,17 +40,19 @@ class StorageTests(object): def s(self, get_storage_args): return self.storage_class(**get_storage_args()) - @pytest.fixture(params=[EVENT_TEMPLATE, TASK_TEMPLATE, VCARD_TEMPLATE]) - def item_template(self, request): - return request.param - @pytest.fixture - def get_item(self, item_template): - return lambda **kw: format_item(item_template, **kw) + def get_item(self, item_type): + template = { + 'VEVENT': EVENT_TEMPLATE, + 'VTODO': TASK_TEMPLATE, + 'VCARD': VCARD_TEMPLATE, + }[item_type] + + return lambda **kw: format_item(template, **kw) @pytest.fixture def requires_collections(self): - if not getattr(self, 'supports_collections', True): + if not self.supports_collections: pytest.skip('This storage does not support collections.') def test_generic(self, s, get_item): diff --git a/tests/storage/dav/test_main.py b/tests/storage/dav/test_main.py index f5e8100..e48d5d5 100644 --- a/tests/storage/dav/test_main.py +++ b/tests/storage/dav/test_main.py @@ -52,11 +52,10 @@ class DavStorageTests(ServerMixin, StorageTests): class TestCaldavStorage(DavStorageTests): storage_class = CaldavStorage - @pytest.fixture(params=[EVENT_TEMPLATE, TASK_TEMPLATE]) - def item_template(self, request): + @pytest.fixture(params=['VTODO', 'VEVENT']) + def item_type(self, request): return request.param - @pytest.mark.parametrize('item_type', ['VTODO', 'VEVENT']) def test_doesnt_accept_vcard(self, item_type, get_storage_args): s = self.storage_class(item_types=(item_type,), **get_storage_args()) @@ -66,16 +65,18 @@ class TestCaldavStorage(DavStorageTests): pass assert not list(s.list()) - @pytest.mark.parametrize('item_types,calls_num', [ + # The `arg` param is not named `item_types` because that would hit + # https://bitbucket.org/pytest-dev/pytest/issue/745/ + @pytest.mark.parametrize('arg,calls_num', [ (('VTODO',), 1), (('VEVENT',), 1), (('VTODO', 'VEVENT'), 2), (('VTODO', 'VEVENT', 'VJOURNAL'), 3), ((), 1) ]) - def test_item_types_performance(self, get_storage_args, item_types, - calls_num, monkeypatch, get_item): - s = self.storage_class(item_types=item_types, **get_storage_args()) + def test_item_types_performance(self, get_storage_args, arg, calls_num, + monkeypatch): + s = self.storage_class(item_types=arg, **get_storage_args()) old_parse = s._parse_prop_responses calls = [] @@ -163,7 +164,7 @@ class TestCaldavStorage(DavStorageTests): list(s.list()) assert len(calls) == 1 - def test_item_types(self, s): + def test_item_types_general(self, s): event = s.upload(format_item(EVENT_TEMPLATE)) task = s.upload(format_item(TASK_TEMPLATE)) s.item_types = ('VTODO', 'VEVENT') @@ -179,6 +180,6 @@ class TestCaldavStorage(DavStorageTests): class TestCarddavStorage(DavStorageTests): storage_class = CarddavStorage - @pytest.fixture - def item_template(self): - return VCARD_TEMPLATE + @pytest.fixture(params=['VCARD']) + def item_type(self, request): + return request.param