<commit_before>from unittest import mock

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

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


NODE_BASE_FIELDS = (
    'parent',
    'parent_id',
    'slug',
    'url',

    # MPTT fields
    'level',
    'lft',
    'rght',
    'tree_id',

    # Polymorphic fields
    'polymorphic_ctype',
    'polymorphic_ctype_id',

    # Incoming foreign keys
    'children',  # FK from self. The other end of "parent".
)


class RouteTest(TestCase):
    """Test Route fields."""
    def test_fields(self):
        """Check the Route model has the expected fields."""
        expected = (
            'id',
            'routeredirect',
            'page',
        ) + NODE_BASE_FIELDS
        fields = Route._meta.get_all_field_names()
        self.assertCountEqual(fields, expected)


class RouteValidateOnSave(TestCase):
    """Check validation of Route slugs and ancestry on save."""
    def test_create_root_with_slug(self):
        """Root must not have a slug."""
        root_route = RouteFactory.build(slug='slug', parent=None)

        with self.assertRaises(ValueError):
            root_route.save()

    def test_create_leaf_without_slug(self):
        """Leaf Routes must have a slug."""
        root_route = RootRouteFactory.create()
        leaf = RouteFactory.build(slug='', parent=root_route)
<commit_msg>Add fields missing from test as of django 1.8<commit_after>from unittest import mock

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

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


NODE_BASE_FIELDS = (
    'parent',
    'parent_id',
    'slug',
    'url',

    # MPTT fields
    'level',
    'lft',
    'rght',
    'tree_id',

    # Polymorphic fields
    'polymorphic_ctype',
    'polymorphic_ctype_id',

    # Incoming foreign keys
    'children',  # FK from self. The other end of "parent".
)


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

            # Incoming foreign keys from subclasses in tests
            'routewithhandler',
            'routewithouthandler',
        ) + NODE_BASE_FIELDS
        fields = Route._meta.get_all_field_names()
        self.assertCountEqual(fields, expected)


class RouteValidateOnSave(TestCase):
    """Check validation of Route slugs and ancestry on save."""
    def test_create_root_with_slug(self):
        """Root must not have a slug."""
        root_route = RouteFactory.build(slug='slug', parent=None)

        with self.assertRaises(ValueError):
            root_route.save()

    def test_create_leaf_without_slug(self):
        """Leaf Routes must have a slug."""
        root_route = RootRouteFactory.create()
        leaf = RouteFactory.build(slug='', parent=root_route)

{'fraction_non_alphanumeric': 0.10516066212268745, 'fraction_numerical': 0.0006491398896462187, 'mean_word_length': 2.78159509202454, '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': '909432', 'n_tokens_mistral': 927, 'n_tokens_neox': 896, 'n_words': 245}