"""This modules holds filters tests for the ``common`` app."""

from django.template import loader
from django.test import TestCase

import django_filters

from .limpyd_base.models import DjangoTestModel
from .test_cases import MTPTemplateTestCaseMixin

from ..filters import MTPFilterSet


class MTPFilterSetTestCase(MTPTemplateTestCaseMixin, TestCase):
    """Test the ``MTPFilterSet`` class."""

    class TestFilterSetWithoutHelpText(MTPFilterSet):
        """A ``FilterSet`` which contains a field without help_text."""

        class Meta:
            model = DjangoTestModel
            fields = ['some_field']

    class TestFilterSetWithHelpText(MTPFilterSet):
        """A ``FilterSet`` which contains a field with help_text."""

        some_field = django_filters.CharFilter(
            label='Some Field',
            help_text='Fake help text'
        )

        class Meta:
            model = DjangoTestModel
            fields = ['some_field']

    @classmethod
    def setUpClass(cls):
        """Initialize the filtersets to test."""

        super().setUpClass()

        cls.filterset_without_help_text = cls.TestFilterSetWithoutHelpText()
        cls.filterset_with_help_text = cls.TestFilterSetWithHelpText()

    def test_field_help_text(self):
        """Test the field help_text value."""

        self.assertEqual(
            self.filterset_without_help_text.filters['some_field'].extra['help_text'],
            ''
        )

        self.assertEqual(
            self.filterset_with_help_text.filters['some_field'].extra['help_text'],
            'Fake help text'
        )

    def test_rendering_form(self):
        """Test the rendering of the filterset form with ``extras/form.html`` template."""

        form_template = loader.get_template('extras/form.html')

        form_html = self.render_template(
            form_template,
            {
                'form': self.filterset_with_help_text.form,
                'display': 'inline',
            }
        )

        self.assertInHTML(
            '<p class="help-block">Fake help text</p>',
            form_html
        )

        form_html = self.render_template(
            form_template,
            {
                'form': self.filterset_without_help_text.form,
                'display': 'inline',
            }
        )

        self.assertInHTML(
            '<p class="help-block"></p>',
            form_html
        )
