Example #1
0
class Filters:
    """The possible entry filters."""

    __slots__ = ("account", "filter", "time")

    def __init__(self, options: BeancountOptions,
                 fava_options: FavaOptions) -> None:
        self.account = AccountFilter(options, fava_options)
        self.filter = AdvancedFilter(options, fava_options)
        self.time = TimeFilter(options, fava_options)

    def set(
        self,
        account: str | None = None,
        filter: str | None = None,  # pylint: disable=redefined-builtin
        time: str | None = None,
    ) -> bool:
        """Set the filters and check if one of them changed."""
        return any([
            self.account.set(account),
            self.filter.set(filter),
            self.time.set(time),
        ])

    def apply(self, entries: Entries) -> Entries:
        """Apply the filters to the entries."""
        entries = self.account.apply(entries)
        entries = self.filter.apply(entries)
        entries = self.time.apply(entries)
        return entries
Example #2
0
def test_time_filter(example_ledger):
    time_filter = TimeFilter(example_ledger.options,
                             example_ledger.fava_options)

    time_filter.set("2017")
    assert time_filter.begin_date == datetime.date(2017, 1, 1)
    assert time_filter.end_date == datetime.date(2018, 1, 1)
    filtered_entries = time_filter.apply(example_ledger.all_entries)
    assert len(filtered_entries) == 83

    time_filter.set("1000")
    filtered_entries = time_filter.apply(example_ledger.all_entries)
    assert not filtered_entries

    time_filter.set(None)
    filtered_entries = time_filter.apply(example_ledger.all_entries)
    assert len(filtered_entries) == len(example_ledger.all_entries)

    with pytest.raises(FilterException):
        time_filter.set("no_date")
Example #3
0
def test_time_filter(example_ledger):
    time_filter = TimeFilter(example_ledger.options,
                             example_ledger.fava_options)

    time_filter.set('2017')
    assert time_filter.begin_date == datetime.date(2017, 1, 1)
    assert time_filter.end_date == datetime.date(2018, 1, 1)
    filtered_entries = time_filter.apply(example_ledger.all_entries)
    assert len(filtered_entries) == 82

    time_filter.set('1000')
    filtered_entries = time_filter.apply(example_ledger.all_entries)
    assert not filtered_entries

    time_filter.set(None)
    filtered_entries = time_filter.apply(example_ledger.all_entries)
    assert len(filtered_entries) == len(example_ledger.all_entries)

    with pytest.raises(FilterException):
        time_filter.set('no_date')
Example #4
0
def test_time_filter(example_ledger):
    time_filter = TimeFilter()

    time_filter.set('2017')
    assert time_filter.begin_date == datetime.date(2017, 1, 1)
    assert time_filter.end_date == datetime.date(2018, 1, 1)
    filtered_entries = time_filter.apply(example_ledger.all_entries,
                                         example_ledger.options)
    assert len(filtered_entries) == 82

    time_filter.set('1000')
    filtered_entries = time_filter.apply(example_ledger.all_entries,
                                         example_ledger.options)
    assert len(filtered_entries) == 0

    time_filter.set(None)
    filtered_entries = time_filter.apply(example_ledger.all_entries,
                                         example_ledger.options)
    assert len(filtered_entries) == len(example_ledger.all_entries)

    with pytest.raises(FilterException):
        time_filter.set('no_date')