<commit_before>from __future__ import print_function

from unittest import TestCase

from indexdigest.database import Database
from indexdigest.linters.linter_0028_data_too_old import check_data_too_old
from indexdigest.test import DatabaseTestMixin


class LimitedViewDatabase(Database, DatabaseTestMixin):
    """
    Limit test to tables
    """
    def get_tables(self):
        return ['0028_data_too_old', '0028_data_ok', '0028_data_empty']


class TestLinter(TestCase, DatabaseTestMixin):

    @property
    def connection(self):
        return LimitedViewDatabase.connect_dsn(self.DSN)

    def test_data_too_old(self):
        reports = list(check_data_too_old(self.connection))

        print(list(map(str, reports)))

        assert len(reports) == 1

        assert str(reports[0]).startswith('0028_data_too_old: "0028_data_too_old" has rows added 18')  # .. 184 days ago
        assert str(reports[0]).endswith('consider changing retention policy')
        self.assertAlmostEquals(reports[0].context['diff_days'], 184)
        assert reports[0].table_name == '0028_data_too_old'

        assert 'data_since' in reports[0].context
        assert 'data_until' in reports[0].context<commit_msg>Add test_data_too_old_with_custom_threshold - customize INDEX_DIGEST_DATA_TOO_OLD_THRESHOLD_DAYS<commit_after>from __future__ import print_function

from unittest import TestCase

from indexdigest.database import Database
from indexdigest.linters.linter_0028_data_too_old import check_data_too_old
from indexdigest.test import DatabaseTestMixin


class LimitedViewDatabase(Database, DatabaseTestMixin):
    """
    Limit test to tables
    """
    def get_tables(self):
        return ['0028_data_too_old', '0028_data_ok', '0028_data_empty']


class TestLinter(TestCase, DatabaseTestMixin):

    @property
    def connection(self):
        return LimitedViewDatabase.connect_dsn(self.DSN)

    def test_data_too_old(self):
        reports = list(check_data_too_old(self.connection))

        print(list(map(str, reports)))

        assert len(reports) == 1

        assert str(reports[0]).startswith('0028_data_too_old: "0028_data_too_old" has rows added 18')  # .. 184 days ago
        assert str(reports[0]).endswith('consider changing retention policy')
        self.assertAlmostEquals(reports[0].context['diff_days'], 184)
        assert reports[0].table_name == '0028_data_too_old'

        assert 'data_since' in reports[0].context
        assert 'data_until' in reports[0].context

    def test_data_too_old_with_custom_threshold(self):
        env = {
            'INDEX_DIGEST_DATA_TOO_OLD_THRESHOLD_DAYS': 365 * 86400
        }

        reports = list(check_data_too_old(self.connection, env))

        print(list(map(str, reports)))
        assert len(reports) == 0
{'fraction_non_alphanumeric': 0.09244604316546763, 'fraction_numerical': 0.0341726618705036, 'mean_word_length': 4.112132352941177, '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': '2761526', 'n_tokens_mistral': 948, 'n_tokens_neox': 833, 'n_words': 172}