<commit_before>import random
import string
from django.db import IntegrityError
from django.db.models.fields import FieldDoesNotExist
from django.core.exceptions import ImproperlyConfigured, ValidationError

from armstrong.core.arm_sections.models import BaseSection, Section
from ._utils import ArmSectionsTestCase, override_settings
from .support.models import ComplexArticle, MultipleManyToManyModel


class ModelTestCase(ArmSectionsTestCase):
    def setUp(self):
        super(ModelTestCase, self).setUp()

        self.pro_sports = Section.objects.get(slug='pro')
        self.weather = Section.objects.get(slug='weather')
        self.college = Section.objects.get(slug='college')

        self.complex_article = ComplexArticle.objects.create(
            title="Test Complex Article",
            slug='test_complex_article',
            primary_section=self.pro_sports)
        self.complex_article.related_sections = [self.weather]

        self.multiple_m2m_article = MultipleManyToManyModel.objects.create(
            title="Test Multple Many To Many Article",
            slug='test_multiple_m2m_article',
            primary_section=self.pro_sports)
        self.multiple_m2m_article.more_sections = [self.weather]

    def test_unicode_repr(self):
        self.assertEqual(u"%s" % self.weather, "Weather (weather/)")

    def test_subclass_must_define_parent_field_required_by_mptt(self):
        class NewSection(BaseSection):
            pass

        with self.assertRaises(FieldDoesNotExist):
            NewSection(title="one", slug="one")

    def test_too_long_slug_raises_error(self):
        s = Section.objects.create(
            title="a",
            slug=''.join(random.choice(string.lowercase) for i in range(201)))

        # KLUDGE use full_clean() to trigger the error because SQLite does not
        # enforce VARCAR length and save() will succeed when it shouldn't
        self.assertRaises(ValidationError, s.full_clean)

    def test_too_long_full_slug_raises_error(self):
        s1 = Section.objects.create(
            title="Parent",
            slug=''.join(random.choice(string.lowercase) for i in range(200)))
        s2 = Section.objects.create(
            parent=s1,
            title="Child",
            slug=''.join(random.choice(string.lowercase) for i in range(54)))

        # KLUDGE use full_clean() to trigger the error because SQLite does not
        # enforce VARCAR length and save() will succeed when it shouldn't
        self.assertRaises(ValidationError, s2.full_clean)
        self.assertEqual(len(s2.full_slug), 256)  # includes separating slashes

    @override_settings(ARMSTRONG_SECTION_ITEM_MODEL='tests.support.models.ComplexCommon')
    def test_item_related_name_returns_many_to_many_field_name(self):
        self.assertEqual(self.weather.item_related_name, 'related_sections')<commit_msg>tests Django 1.7 - remove app loading warning<commit_after>import random
import string
from django.db import IntegrityError
from django.db.models.fields import FieldDoesNotExist
from django.core.exceptions import ImproperlyConfigured, ValidationError

from armstrong.core.arm_sections.models import BaseSection, Section
from ._utils import ArmSectionsTestCase, override_settings
from .support.models import ComplexArticle, MultipleManyToManyModel


class ModelTestCase(ArmSectionsTestCase):
    def setUp(self):
        super(ModelTestCase, self).setUp()

        self.pro_sports = Section.objects.get(slug='pro')
        self.weather = Section.objects.get(slug='weather')
        self.college = Section.objects.get(slug='college')

        self.complex_article = ComplexArticle.objects.create(
            title="Test Complex Article",
            slug='test_complex_article',
            primary_section=self.pro_sports)
        self.complex_article.related_sections = [self.weather]

        self.multiple_m2m_article = MultipleManyToManyModel.objects.create(
            title="Test Multple Many To Many Article",
            slug='test_multiple_m2m_article',
            primary_section=self.pro_sports)
        self.multiple_m2m_article.more_sections = [self.weather]

    def test_unicode_repr(self):
        self.assertEqual(u"%s" % self.weather, "Weather (weather/)")

    def test_subclass_must_define_parent_field_required_by_mptt(self):
        class NewSection(BaseSection):
            class Meta:
                app_label = 'newsection'

        with self.assertRaises(FieldDoesNotExist):
            NewSection(title="one", slug="one")

    def test_too_long_slug_raises_error(self):
        s = Section.objects.create(
            title="a",
            slug=''.join(random.choice(string.lowercase) for i in range(201)))

        # KLUDGE use full_clean() to trigger the error because SQLite does not
        # enforce VARCAR length and save() will succeed when it shouldn't
        self.assertRaises(ValidationError, s.full_clean)

    def test_too_long_full_slug_raises_error(self):
        s1 = Section.objects.create(
            title="Parent",
            slug=''.join(random.choice(string.lowercase) for i in range(200)))
        s2 = Section.objects.create(
            parent=s1,
            title="Child",
            slug=''.join(random.choice(string.lowercase) for i in range(54)))

        # KLUDGE use full_clean() to trigger the error because SQLite does not
        # enforce VARCAR length and save() will succeed when it shouldn't
        self.assertRaises(ValidationError, s2.full_clean)
        self.assertEqual(len(s2.full_slug), 256)  # includes separating slashes

    @override_settings(ARMSTRONG_SECTION_ITEM_MODEL='tests.support.models.ComplexCommon')
    def test_item_related_name_returns_many_to_many_field_name(self):
        self.assertEqual(self.weather.item_related_name, 'related_sections')
{'fraction_non_alphanumeric': 0.09100310237849017, 'fraction_numerical': 0.006894174422612892, 'mean_word_length': 3.3895612708018152, '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': '59071', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1639, 'n_words': 338}