<commit_before># Copyright (C) 2021 <NAME> <<EMAIL>>
# Licensed under GNU Affero GPL v3 or later

from http import HTTPStatus

from django.test import RequestFactory, TestCase
from django.urls import reverse_lazy
from parameterized import parameterized

from ...models import DebianPopcon, IssueKind
from ...tests.factories import DebianWnppFactory
from ..front_page import (_COLUMN_NAMES, _DEFAULT_COLUMNS, _DEFAULT_ISSUE_KINDS,
                          _INSTANCES_PER_PAGE, _INTERNAL_FIELDS_FOR_COLUMN_NAME, FrontPageView)


class _FrontPageTestCase(TestCase):
    url = reverse_lazy('front_page')


class QueryCountTest(_FrontPageTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        for _ in range(2):
            DebianWnppFactory()

    @parameterized.expand([(col, ) for col in _COLUMN_NAMES])
    def test(self, column_name):
        data = {
            'col[]': [column_name],
        }

        # NOTE: The first query is to retrieve the number of objects for pagination,
        #       the second is retrieving the actual instances.
        with self.assertNumQueries(2):
            response = self.client.get(self.url, data)
            self.assertEqual(response.status_code, HTTPStatus.OK)


class RequestValidationTest(TestCase):  # doesn't need _FrontPageTestCase
    @parameterized.expand([
        ('/?col%5B%5D=description%27&desc=%27&sort=project%27&type%5B%5D=RFP%27', ),  # seen live
        ('/?col%5B%5D=description%27', ),
        ('/?sort=project%27', ),
        ('/?type%5B%5D=RFP%27', ),
        ('/?sort=installs&col%5B%5D=description', ),  # misses "&col[]=installs"
        ('/?sort=users', ),  # misses "&col[]=users"
    ])
    def test_bad_request_for_column(self, url):
        response = self.client.get(url)
        self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST)


class ColumnVisibilityTest(_FrontPageTestCase):
    def test_context_data__default(self):
        expected = {
            f'show_{column_name}': column_name in _DEFAULT_COLUMNS
            for column_name in _COLUMN_NAMES
        }

        response = self.client.get(self.url)

        for k, v in expected.items():
            self.assertEqual(response.context_data[k], v)

    def test_context_data__all(self):
        expected = {f'show_{column_name}': True for column_name in _COLUMN_NAMES}
        data = {'col[]': _COLUMN_NAMES}

        response = self.client.get(self.url, data)

        for k, v in expected.items():
            self.assertEqual(response.context_data[k], v)


class TextBasedFilterTest(_FrontPageTestCase):<commit_msg>front_page.py: Cover column visility effect<commit_after># Copyright (C) 2021 <NAME> <<EMAIL>>
# Licensed under GNU Affero GPL v3 or later

from http import HTTPStatus

from django.test import RequestFactory, TestCase
from django.urls import reverse_lazy
from parameterized import parameterized

from ...models import DebianPopcon, IssueKind
from ...tests.factories import DebianPopconFactory, DebianWnppFactory
from ..front_page import (_COLUMN_NAMES, _DEFAULT_COLUMNS, _DEFAULT_ISSUE_KINDS,
                          _INSTANCES_PER_PAGE, _INTERNAL_FIELDS_FOR_COLUMN_NAME, FrontPageView)


class _FrontPageTestCase(TestCase):
    url = reverse_lazy('front_page')


class QueryCountTest(_FrontPageTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        for _ in range(2):
            DebianWnppFactory()

    @parameterized.expand([(col, ) for col in _COLUMN_NAMES])
    def test(self, column_name):
        data = {
            'col[]': [column_name],
        }

        # NOTE: The first query is to retrieve the number of objects for pagination,
        #       the second is retrieving the actual instances.
        with self.assertNumQueries(2):
            response = self.client.get(self.url, data)
            self.assertEqual(response.status_code, HTTPStatus.OK)


class RequestValidationTest(TestCase):  # doesn't need _FrontPageTestCase
    @parameterized.expand([
        ('/?col%5B%5D=description%27&desc=%27&sort=project%27&type%5B%5D=RFP%27', ),  # seen live
        ('/?col%5B%5D=description%27', ),
        ('/?sort=project%27', ),
        ('/?type%5B%5D=RFP%27', ),
        ('/?sort=installs&col%5B%5D=description', ),  # misses "&col[]=installs"
        ('/?sort=users', ),  # misses "&col[]=users"
    ])
    def test_bad_request_for_column(self, url):
        response = self.client.get(url)
        self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST)


class ColumnVisibilityTest(_FrontPageTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.magic_issue = DebianWnppFactory(
            open_person='2222<EMAIL>',
            popcon=DebianPopconFactory(package='33333'),
            description='44444',
            charge_person='<EMAIL>',
        )

    @parameterized.expand([
        ('reporter', 'open_person'),
        ('description', 'description'),
        ('owner', 'charge_person'),
    ])
    def test_column_visibility(self, external_column_name, model_field):
        for column_name, expected_visible in (
            (external_column_name, True),
            ('project', False),
        ):
            data = {
                'col[]': [column_name],
            }

            response = self.client.get(self.url, data)

            assertion = self.assertContains if expected_visible else self.assertNotContains
            assertion(response, getattr(self.magic_issue, model_field))

    def test_context_data__default(self):
        expected = {
            f'show_{column_name}': column_name in _DEFAULT_COLUMNS
            for column_name in _COLUMN_NAMES
        }

        response = self.client.get(self.url)

        for k, v in expected.items():
            self.assertEqual(response.context_data[k], v)

    def test_context_data__all(self):
        expected = {f'show_{column_name}': True for column_name in _COLUMN_NAMES}
        data = {'col[]': _COLUMN_NAMES}

        response = self.client.get(self.url, data)

        for k, v in expected.items():
            self.assertEqual(response.context_data[k], v)


class TextBasedFilterTest(_FrontPageTestCase):
{'fraction_non_alphanumeric': 0.11451612903225807, 'fraction_numerical': 0.012258064516129033, 'mean_word_length': 3.005813953488372, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1361954', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1866, 'n_words': 392}