Files @ ea0e3d3a73b7
Branch filter:

Location: NPO-Accounting/import2ledger/tests/date_hooks.py

Brett Smith
dynload: Pre-configure objects and skip unconfigured ones.

Now that all importers and hooks receive the configuration object, they can
tell us at initialization time whether or not they actually have enough
configuration to be useful. Initialize them immediately on loading, and
only use importers and hooks that are actually configured.
import datetime

class DateHookTestBase:
    DATE_FMT_ISO = '%Y-%m-%d'
    DATE_FMT_LEDGER = '%d/%m/%Y'

    def _format_config_date(self, section, key, value, date_fmt):
        if value is not None:
            section[key] = value.strftime(date_fmt)

    def new_config(
            self,
            fmt=DATE_FMT_ISO,
            default_date=datetime.date(2017, 3, 1),
            start_date=datetime.date(2017, 2, 1),
            end_date=datetime.date(2017, 4, 1)
    ):
        section = {'date format': fmt.replace('%', '%%')}
        self._format_config_date(section, 'default date', default_date, fmt)
        self._format_config_date(section, 'import start date', start_date, fmt)
        self._format_config_date(section, 'import end date', end_date, fmt)
        return {'Dates': section}