<commit_before>import django

from datetime import datetime
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.core.cache import cache
from django.core.urlresolvers import set_urlconf, get_urlconf
from django.test import TestCase
from django.utils import translation

from fluent_blogs.models import Entry
from fluent_blogs.pagetypes.blogpage.models import BlogPage
from fluent_pages.urlresolvers import PageTypeNotMounted


class BlogPageTests(TestCase):
    """
    Testing integration between django-fluent-pages and django-fluent-blogs
    """

    @classmethod
    def setUpClass(cls):
        super(BlogPageTests, cls).setUpClass()

        User = get_user_model()

        Site.objects.get_or_create(id=settings.SITE_ID, defaults=dict(domain='django.localhost', name='django at localhost'))
        cls.user = User.objects.create_superuser("fluent-blogs-admin", '<EMAIL>', 'admin')

        # Testing with other URLconf, this works for every Django version
        cls._old_urlconf = get_urlconf()
        set_urlconf('fluent_blogs.pagetypes.blogpage.tests.urls')

    @classmethod
    def tearDownClass(cls):
        super(BlogPageTests, cls).tearDownClass()
        set_urlconf(cls._old_urlconf)

    def tearDown(self):
        cache.clear()  # BlogPage URLs are stored in cache

    def test_no_blogpage(self):
        """
        When there is no blog page, the system should detect this.
        """
        date = datetime(year=2016, month=5, day=1)
        entry = Entry.objects.language('en').create(author=self.user, slug='foo', publication_date=date)
        self.assertRaises(PageTypeNotMounted, lambda: entry.default_url)
        self.assertEqual(entry.get_absolute_url_format(), '/.../2016/05/{slug}/')  # slug preview should not crash.

    def test_blogpage_fallback_url(self):
        """
        Testing how translated entries appear on a ``BlogPage`` that has no translation except for the default/fallback.
        """
        page = BlogPage.objects.language('en').create(author=self.user, status=BlogPage.PUBLISHED, slug='blog')
        self.assertEqual(page.default_url, '/en/blog/')

        date = datetime(year=2016, month=5, day=1)
        entry = Entry.objects.language('en').create(author=self.user, slug='hello-en', publication_date=date)
        self.assertEqual(entry.default_url, '/en/blog/2016/05/hello-en/')

        # Simulate fetched entry in new language
        with translation.override('nl'):
            entry.set_current_language('nl')
            self.assertEqual(entry.default_url, '/nl/blog/2016/05/hello-en/')

            # Create new language
            entry.create_translation('nl', slug='hello-nl')
            self.assertEqual(entry.default_url, '/nl/blog/2016/05/hello-nl/')<commit_msg>Improve test_blogpage_fallback_url() test<commit_after>import django

from datetime import datetime
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.core.cache import cache
from django.core.urlresolvers import set_urlconf, get_urlconf
from django.test import TestCase
from django.utils import translation

from fluent_blogs.models import Entry
from fluent_blogs.pagetypes.blogpage.models import BlogPage
from fluent_pages.urlresolvers import PageTypeNotMounted


class BlogPageTests(TestCase):
    """
    Testing integration between django-fluent-pages and django-fluent-blogs
    """

    @classmethod
    def setUpClass(cls):
        super(BlogPageTests, cls).setUpClass()

        User = get_user_model()

        Site.objects.get_or_create(id=settings.SITE_ID, defaults=dict(domain='django.localhost', name='django at localhost'))
        cls.user = User.objects.create_superuser("fluent-blogs-admin", '<EMAIL>', 'admin')

        # Testing with other URLconf, this works for every Django version
        cls._old_urlconf = get_urlconf()
        set_urlconf('fluent_blogs.pagetypes.blogpage.tests.urls')

    @classmethod
    def tearDownClass(cls):
        super(BlogPageTests, cls).tearDownClass()
        set_urlconf(cls._old_urlconf)

    def tearDown(self):
        cache.clear()  # BlogPage URLs are stored in cache

    def test_no_blogpage(self):
        """
        When there is no blog page, the system should detect this.
        """
        date = datetime(year=2016, month=5, day=1)
        entry = Entry.objects.language('en').create(author=self.user, slug='foo', publication_date=date)
        self.assertRaises(PageTypeNotMounted, lambda: entry.default_url)
        self.assertEqual(entry.get_absolute_url_format(), '/.../2016/05/{slug}/')  # slug preview should not crash.

    def test_blogpage_fallback_url(self):
        """
        Testing how translated entries appear on a ``BlogPage`` that has no translation except for the default/fallback.
        """
        page = BlogPage.objects.language('en').create(author=self.user, status=BlogPage.PUBLISHED, slug='blogpage')
        self.assertEqual(page.default_url, '/en/blogpage/')

        date = datetime(year=2016, month=5, day=1)
        entry = Entry.objects.language('en').create(author=self.user, slug='hello-en', publication_date=date)
        self.assertEqual(entry.default_url, '/en/blogpage/2016/05/hello-en/')

        # Simulate fetched entry in new language
        with translation.override('nl'):
            entry.set_current_language('nl')
            self.assertEqual(entry.default_url, '/nl/blogpage/2016/05/hello-en/')

            # Create new language
            entry.create_translation('nl', slug='hello-nl')
            self.assertEqual(entry.default_url, '/nl/blogpage/2016/05/hello-nl/')
{'fraction_non_alphanumeric': 0.1112666200139958, 'fraction_numerical': 0.012596221133659902, 'mean_word_length': 3.4698983580922595, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '822985', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1643, 'n_words': 365}