<commit_before># -*- coding: utf-8 -*-

"""Akvo RSR is covered by the GNU Affero General Public License.

See more details in the license.txt file located at the root folder of the Akvo RSR module.
For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
"""

from django.test import TestCase

from akvo.rsr.models import ProjectEditorValidation, ProjectEditorValidationSet
from akvo.rsr.templatetags.project_editor import mandatory_or_hidden, invalidate_validation_cache


class TemplateTagsTestCase(TestCase):
    """Tests for template tags."""

    @classmethod
    def setUpClass(cls):
        cls.tearDownClass()
        MANDATORY = ProjectEditorValidation.MANDATORY_ACTION
        HIDDEN = ProjectEditorValidation.HIDDEN_ACTION
        validation1 = [
            ('rsr_project.title', MANDATORY),
            ('rsr_project.subtitle', MANDATORY),
            ('rsr_project.date_start_planned||rsr_project.date_start_actual', MANDATORY),
            ('rsr_project.current_image', MANDATORY),
            ('rsr_project.current_image||rsr_project.test_image', MANDATORY),
            ('rsr_partnership', MANDATORY),
            ('rsr_partnership.organisation', MANDATORY),
            ('rsr_partnership.iati_organisation_role', MANDATORY),
            ('rsr_project.iati_activity_id', HIDDEN),
            ('rsr_project.hierarchy', HIDDEN),
            ('rsr_transaction', HIDDEN),
            ('rsr_planneddisbursement', HIDDEN),
        ]

        validation2 = [
            ('rsr_project.title', MANDATORY),
            ('rsr_project.iati_activity_id', MANDATORY),
            ('rsr_project.date_start_planned||rsr_project.date_start_actual', MANDATORY),
            ('rsr_partnership', MANDATORY),
            ('rsr_partnership.organisation', MANDATORY),
            ('rsr_partnership.iati_organisation_role', MANDATORY),
            ('rsr_transaction.transaction_type', MANDATORY),
            ('rsr_transaction.transaction_date', MANDATORY),
            ('rsr_transaction.value', MANDATORY),
            ('rsr_transaction.value_date', MANDATORY),
            ('rsr_projectdocument.url||rsr_projectdocument.document', MANDATORY),
            ('rsr_planneddisbursement.value', MANDATORY),
            ('rsr_planneddisbursement.period_start', MANDATORY),
            ('rsr_planneddisbursement.period_end', MANDATORY),
            ('rsr_planneddisbursement.value_date', MANDATORY)
        ]

        for i, validations in enumerate([validation1, validation2], start=1):
            validation_set = ProjectEditorValidationSet.objects.create(name='validation-{}'.format(i))
            setattr(cls, 'validation{}'.format(i), validation_set)
            for validation, action in validations:
                ProjectEditorValidation.objects.create(
                    validation=validation,
                    action=action,
                    validation_set=validation_set,
                )

        cls.validations = ProjectEditorValidation.objects.all()
        id_1, id_2 = ProjectEditorValidationSet.objects.values_list('id', flat=True)
        cls.ids = {'id_1': id_1, 'id_2': id_2}


    @classmethod
    def tearDownClass(cls):
        ProjectEditorValidation.objects.all().delete()
        ProjectEditorValidationSet.objects.all().delete()
        cls.validations = None
        invalidate_validation_cache()

    def _indications(self, field):
        return mandatory_or_hidden(self.validations, field).strip()

    def assertIndications(self, field, expected_indications):
        self.assertEqual(self._indications(field), expected_indications)

    def test_current_image_field(self):
        field = 'rsr_project.current_image.2'
        expected_indications = (
            'mandatory-{id_1} mandatory-{id_1} mandatory-{id_1}-or-test_image'.format(**self.ids)
        )
        self.assertIndications(field, expected_indications)

    def test_current_image(self):
        field = 'rsr_project.current_image'
        expected_indications = ''
        self.assertIndications(field, expected_indications)

    def test_title(self):
        field = 'rsr_project.title.2'
        expected_indications = 'mandatory-{id_1} mandatory-{id_2}'.format(**self.ids)<commit_msg>[#3105] flake8 fix<commit_after># -*- coding: utf-8 -*-

"""Akvo RSR is covered by the GNU Affero General Public License.

See more details in the license.txt file located at the root folder of the Akvo RSR module.
For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
"""

from django.test import TestCase

from akvo.rsr.models import ProjectEditorValidation, ProjectEditorValidationSet
from akvo.rsr.templatetags.project_editor import mandatory_or_hidden, invalidate_validation_cache


class TemplateTagsTestCase(TestCase):
    """Tests for template tags."""

    @classmethod
    def setUpClass(cls):
        cls.tearDownClass()
        MANDATORY = ProjectEditorValidation.MANDATORY_ACTION
        HIDDEN = ProjectEditorValidation.HIDDEN_ACTION
        validation1 = [
            ('rsr_project.title', MANDATORY),
            ('rsr_project.subtitle', MANDATORY),
            ('rsr_project.date_start_planned||rsr_project.date_start_actual', MANDATORY),
            ('rsr_project.current_image', MANDATORY),
            ('rsr_project.current_image||rsr_project.test_image', MANDATORY),
            ('rsr_partnership', MANDATORY),
            ('rsr_partnership.organisation', MANDATORY),
            ('rsr_partnership.iati_organisation_role', MANDATORY),
            ('rsr_project.iati_activity_id', HIDDEN),
            ('rsr_project.hierarchy', HIDDEN),
            ('rsr_transaction', HIDDEN),
            ('rsr_planneddisbursement', HIDDEN),
        ]

        validation2 = [
            ('rsr_project.title', MANDATORY),
            ('rsr_project.iati_activity_id', MANDATORY),
            ('rsr_project.date_start_planned||rsr_project.date_start_actual', MANDATORY),
            ('rsr_partnership', MANDATORY),
            ('rsr_partnership.organisation', MANDATORY),
            ('rsr_partnership.iati_organisation_role', MANDATORY),
            ('rsr_transaction.transaction_type', MANDATORY),
            ('rsr_transaction.transaction_date', MANDATORY),
            ('rsr_transaction.value', MANDATORY),
            ('rsr_transaction.value_date', MANDATORY),
            ('rsr_projectdocument.url||rsr_projectdocument.document', MANDATORY),
            ('rsr_planneddisbursement.value', MANDATORY),
            ('rsr_planneddisbursement.period_start', MANDATORY),
            ('rsr_planneddisbursement.period_end', MANDATORY),
            ('rsr_planneddisbursement.value_date', MANDATORY)
        ]

        for i, validations in enumerate([validation1, validation2], start=1):
            validation_set = ProjectEditorValidationSet.objects.create(name='validation-{}'.format(i))
            setattr(cls, 'validation{}'.format(i), validation_set)
            for validation, action in validations:
                ProjectEditorValidation.objects.create(
                    validation=validation,
                    action=action,
                    validation_set=validation_set,
                )

        cls.validations = ProjectEditorValidation.objects.all()
        id_1, id_2 = ProjectEditorValidationSet.objects.values_list('id', flat=True)
        cls.ids = {'id_1': id_1, 'id_2': id_2}

    @classmethod
    def tearDownClass(cls):
        ProjectEditorValidation.objects.all().delete()
        ProjectEditorValidationSet.objects.all().delete()
        cls.validations = None
        invalidate_validation_cache()

    def _indications(self, field):
        return mandatory_or_hidden(self.validations, field).strip()

    def assertIndications(self, field, expected_indications):
        self.assertEqual(self._indications(field), expected_indications)

    def test_current_image_field(self):
        field = 'rsr_project.current_image.2'
        expected_indications = (
            'mandatory-{id_1} mandatory-{id_1} mandatory-{id_1}-or-test_image'.format(**self.ids)
        )
        self.assertIndications(field, expected_indications)

    def test_current_image(self):
        field = 'rsr_project.current_image'
        expected_indications = ''
        self.assertIndications(field, expected_indications)

    def test_title(self):
        field = 'rsr_project.title.2'
        expected_indications = 'mandatory-{id_1} mandatory-{id_2}'.format(**self.ids)
{'fraction_non_alphanumeric': 0.10721040189125296, 'fraction_numerical': 0.00508274231678487, 'mean_word_length': 3.017568850902184, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4873345', 'n_tokens_mistral': 2542, 'n_tokens_neox': 2460, 'n_words': 403}