<commit_before>from unittest import mock

from django.db.utils import IntegrityError
from django.test import TestCase
from incuna_test_utils.utils import field_names

from .factories import ChildRouteFactory, RouteFactory
from .. import handlers
from ..models import Route


NODE_BASE_FIELDS = (
    'url',

    # Polymorphic fields
    'polymorphic_ctype',
)


class RouteTest(TestCase):
    """Test Route fields."""
    def test_fields(self):
        """Check the Route model has the expected fields."""
        expected = (
            'id',
            'routeredirect',
            'urlredirect',

            # Incoming foreign keys from subclasses in tests
            'mockviewroute',
            'routewithnoview',
            'routewithview',
            'routewithurlconf',
            'routewithnourlconf',
            'urlconfroute',
        ) + NODE_BASE_FIELDS
        fields = field_names(Route)
        self.assertCountEqual(fields, expected)


class RouteUniqueness(TestCase):
    """Check uniqueness conditions on Route are enforced in the DB."""
    def test_unique_url(self):
        """Only one Route can exist with a particular url."""
        Route.objects.create(url='/')

        with self.assertRaises(IntegrityError):
            Route.objects.create(url='/')


class RouteCheckTest(TestCase):
    """Test Route.check()."""
    def test_defer_to_handler(self):
        """Route.check returns the result of Route.handler_class().check()."""
        with mock.patch('conman.routes.models.Route.handler_class') as handler:
            errors = Route.check()

        self.assertEqual(errors, handler.check(Route))


class RouteGetDescendantsTest(TestCase):
    """
    Test Route().get_descendants().

    All of these tests assert use of only one query:
        * Get the decendants of a Route:

              SELECT "routes_route"."id",
                     "routes_route"."polymorphic_ctype_id",
                     "routes_route"."url"<commit_msg>Add comments and an assertion to RouteCheckTest<commit_after>from unittest import mock

from django.db.utils import IntegrityError
from django.test import TestCase
from incuna_test_utils.utils import field_names

from .factories import ChildRouteFactory, RouteFactory
from .. import handlers
from ..models import Route


NODE_BASE_FIELDS = (
    'url',

    # Polymorphic fields
    'polymorphic_ctype',
)


class RouteTest(TestCase):
    """Test Route fields."""
    def test_fields(self):
        """Check the Route model has the expected fields."""
        expected = (
            'id',
            'routeredirect',
            'urlredirect',

            # Incoming foreign keys from subclasses in tests
            'mockviewroute',
            'routewithnoview',
            'routewithview',
            'routewithurlconf',
            'routewithnourlconf',
            'urlconfroute',
        ) + NODE_BASE_FIELDS
        fields = field_names(Route)
        self.assertCountEqual(fields, expected)


class RouteUniqueness(TestCase):
    """Check uniqueness conditions on Route are enforced in the DB."""
    def test_unique_url(self):
        """Only one Route can exist with a particular url."""
        Route.objects.create(url='/')

        with self.assertRaises(IntegrityError):
            Route.objects.create(url='/')


class RouteCheckTest(TestCase):
    """Test Route.check()."""
    def test_defer_to_handler(self):
        """Route.check returns the result of Route.handler_class().check()."""
        with mock.patch('conman.routes.models.Route.handler_class') as handler:
            errors = Route.check()

        # Ensure the handler's check method is called...
        handler.check.assert_called_once_with(Route)
        # ... and that the return value is passed back through.
        self.assertEqual(errors, handler.check(Route))


class RouteGetDescendantsTest(TestCase):
    """
    Test Route().get_descendants().

    All of these tests assert use of only one query:
        * Get the decendants of a Route:

              SELECT "routes_route"."id",
                     "routes_route"."polymorphic_ctype_id",
                     "routes_route"."url"
{'fraction_non_alphanumeric': 0.10192307692307692, 'fraction_numerical': 0.0, 'mean_word_length': 2.5503412969283277, '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': '2212168', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1099, 'n_words': 304}