<commit_before>import datetime

from beancount.core import account
from beancount.core.data import Transaction
from fava.api.filters import AccountFilter, DateFilter, PayeeFilter, TagFilter


def test_account_filter(example_api):
    account_filter = AccountFilter()

    account_filter.set('Assets')
    filtered_entries = account_filter.apply(
        example_api.all_entries, example_api.options)
    assert all(map(
        lambda x: isinstance(x, Transaction),
        filtered_entries))
    assert len(filtered_entries) == 485
    assert all(map(
        lambda x: any(map(
            lambda p: account.has_component(p.account, 'Assets'), x.postings)),
        filtered_entries))


def test_date_filter(example_api):
    date_filter = DateFilter()

    date_filter.set('2017')
    assert date_filter.begin_date == datetime.date(2017, 1, 1)
    assert date_filter.end_date == datetime.date(2018, 1, 1)
    filtered_entries = date_filter.apply(
        example_api.all_entries, example_api.options)
    assert len(filtered_entries) == 81

    date_filter.set('1000')
    filtered_entries = date_filter.apply(
        example_api.all_entries, example_api.options)
    assert len(filtered_entries) == 0

    date_filter.set(None)
    filtered_entries = date_filter.apply(
        example_api.all_entries, example_api.options)
    assert len(filtered_entries) == len(example_api.all_entries)


def test_tag_filter(example_api):
    tag_filter = TagFilter()

    tag_filter.set('test')
    filtered_entries = tag_filter.apply(
        example_api.all_entries, example_api.options)
    assert all(map(
        lambda x: isinstance(x, Transaction),
        filtered_entries))
    assert len(filtered_entries) == 0


def test_payee_filter(example_api):
    payee_filter = PayeeFilter()

    payee_filter.set('BayBook')
    filtered_entries = payee_filter.apply(
        example_api.all_entries, example_api.options)
    assert all(map(
        lambda x: isinstance(x, Transaction),
        filtered_entries))
    assert len(filtered_entries) == 62

    payee_filter.set('asdfasdfasdf, BayBook')
    filtered_entries = payee_filter.apply(
        example_api.all_entries, example_api.options)
    assert len(filtered_entries) == 62

    payee_filter.set('asdfasdfasdvba')
    filtered_entries = payee_filter.apply(
        example_api.all_entries, example_api.options)
    assert len(filtered_entries) == 0
<commit_msg>fix test_api_filters<commit_after>import datetime

from beancount.core import account
from beancount.core.data import Transaction
from fava.api.filters import AccountFilter, DateFilter, PayeeFilter, TagFilter


def test_account_filter(example_api):
    account_filter = AccountFilter()

    account_filter.set('Assets')
    filtered_entries = account_filter.apply(
        example_api.all_entries, example_api.options)
    assert len(filtered_entries) == 537
    assert all(map(
        lambda x: hasattr(x, 'account') and
        account.has_component(x.account, 'Assets') or any(map(
            lambda p: account.has_component(p.account, 'Assets'), x.postings)),
        filtered_entries))


def test_date_filter(example_api):
    date_filter = DateFilter()

    date_filter.set('2017')
    assert date_filter.begin_date == datetime.date(2017, 1, 1)
    assert date_filter.end_date == datetime.date(2018, 1, 1)
    filtered_entries = date_filter.apply(
        example_api.all_entries, example_api.options)
    assert len(filtered_entries) == 81

    date_filter.set('1000')
    filtered_entries = date_filter.apply(
        example_api.all_entries, example_api.options)
    assert len(filtered_entries) == 0

    date_filter.set(None)
    filtered_entries = date_filter.apply(
        example_api.all_entries, example_api.options)
    assert len(filtered_entries) == len(example_api.all_entries)


def test_tag_filter(example_api):
    tag_filter = TagFilter()

    tag_filter.set('test')
    filtered_entries = tag_filter.apply(
        example_api.all_entries, example_api.options)
    assert all(map(
        lambda x: isinstance(x, Transaction),
        filtered_entries))
    assert len(filtered_entries) == 0


def test_payee_filter(example_api):
    payee_filter = PayeeFilter()

    payee_filter.set('BayBook')
    filtered_entries = payee_filter.apply(
        example_api.all_entries, example_api.options)
    assert all(map(
        lambda x: isinstance(x, Transaction),
        filtered_entries))
    assert len(filtered_entries) == 62

    payee_filter.set('asdfasdfasdf, BayBook')
    filtered_entries = payee_filter.apply(
        example_api.all_entries, example_api.options)
    assert len(filtered_entries) == 62

    payee_filter.set('asdfasdfasdvba')
    filtered_entries = payee_filter.apply(
        example_api.all_entries, example_api.options)
    assert len(filtered_entries) == 0

{'fraction_non_alphanumeric': 0.09510982179859097, 'fraction_numerical': 0.01326150020721094, 'mean_word_length': 3.8708375378405653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3069797', 'n_tokens_mistral': 1558, 'n_tokens_neox': 1461, 'n_words': 280}