From 6e78ab6a3b07a099315e3268dce8ea22a8796104 Mon Sep 17 00:00:00 2001 From: jaca Date: Sun, 11 Sep 2022 13:11:32 +0200 Subject: [PATCH] add conftest --- src/icalendar/tests/conftest.py | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/icalendar/tests/conftest.py diff --git a/src/icalendar/tests/conftest.py b/src/icalendar/tests/conftest.py new file mode 100644 index 0000000..838c506 --- /dev/null +++ b/src/icalendar/tests/conftest.py @@ -0,0 +1,43 @@ +import os +import logging + +import pytest +import icalendar + +LOGGER = logging.getLogger(__name__) + +class DataSource: + '''A collection of parsed ICS elements (e.g calendars, timezones, events)''' + def __init__(self, data_source_folder, parser): + self._parser = parser + self._data_source_folder = data_source_folder + + def __getattr__(self, attribute): + if not attribute in self.__dict__: + source_file = attribute.replace('-', '_') + '.ics' + source_path = os.path.join(self.__dict__['_data_source_folder'], source_file) + with open(source_path, 'rb') as f: + try: + raw_ics = f.read() + source = self.__dict__['_parser'](raw_ics) + source.raw_ics = raw_ics + self.__dict__[attribute] = source + except ValueError as error: + LOGGER.error(f'Could not load {source_file} due to {error}') + return self.__dict__[attribute] + + def __repr__(self): + return repr(self.__dict__) + +HERE = os.path.dirname(__file__) +TIMEZONES_FOLDER = os.path.join(HERE, 'timezones') +EVENTS_FOLDER = os.path.join(HERE, 'events') + +@pytest.fixture +def timezones(): + return DataSource(TIMEZONES_FOLDER, icalendar.Timezone.from_ical) + +@pytest.fixture +def events(): + return DataSource(EVENTS_FOLDER, icalendar.Event.from_ical) +