From 965a999ae5a03ca595f3842feacfbce5dc12f20a Mon Sep 17 00:00:00 2001 From: wrwrwr Date: Sat, 29 Nov 2014 09:36:39 +0100 Subject: [PATCH] [1.7.x] Fixed #23929 -- Added more tests for create_default_site. Backport of 1f98ec2e53e4636863396ab54f671f4546f9ba4c from master --- django/contrib/sites/tests.py | 61 +++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/django/contrib/sites/tests.py b/django/contrib/sites/tests.py index 8f80087c5b..d6eae94928 100644 --- a/django/contrib/sites/tests.py +++ b/django/contrib/sites/tests.py @@ -1,10 +1,13 @@ from __future__ import unicode_literals +from django.apps import apps from django.conf import settings from django.core.exceptions import ObjectDoesNotExist, ValidationError +from django.db import router from django.http import HttpRequest from django.test import TestCase, modify_settings, override_settings +from .management import create_default_site from .middleware import CurrentSiteMiddleware from .models import Site from .requests import RequestSite @@ -17,12 +20,6 @@ class SitesFrameworkTests(TestCase): def setUp(self): Site(id=settings.SITE_ID, domain="example.com", name="example.com").save() - def test_save_another(self): - # Regression for #17415 - # On some backends the sequence needs reset after save with explicit ID. - # Test that there is no sequence collisions by saving another site. - Site(domain="example2.com", name="example2.com").save() - def test_site_manager(self): # Make sure that get_current() does not return a deleted Site object. s = Site.objects.get_current() @@ -82,6 +79,58 @@ class SitesFrameworkTests(TestCase): self.assertRaises(ValidationError, site.full_clean) +class JustOtherRouter(object): + def allow_migrate(self, db, model): + return db == 'other' + + +@modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'}) +class CreateDefaultSiteTests(TestCase): + multi_db = True + + def setUp(self): + self.app_config = apps.get_app_config('sites') + # Delete the site created as part of the default migration process. + Site.objects.all().delete() + + def test_basic(self): + """ + #15346, #15573 - create_default_site() creates an example site only if + none exist. + """ + create_default_site(self.app_config, verbosity=0) + self.assertEqual(Site.objects.count(), 1) + + create_default_site(self.app_config, verbosity=0) + self.assertEqual(Site.objects.count(), 1) + + def test_multi_db(self): + """ + #16353, #16828 - The default site creation should respect db routing. + """ + old_routers = router.routers + router.routers = [JustOtherRouter()] + try: + create_default_site(self.app_config, db='default', verbosity=0) + create_default_site(self.app_config, db='other', verbosity=0) + self.assertFalse(Site.objects.using('default').exists()) + self.assertTrue(Site.objects.using('other').exists()) + finally: + router.routers = old_routers + + def test_save_another(self): + """ + #17415 - Another site can be created right after the default one. + + On some backends the sequence needs to be reset after saving with an + explicit ID. Test that there isn't a sequence collisions by saving + another site. This test is only meaningful with databases that use + sequences for automatic primary keys such as PostgreSQL and Oracle. + """ + create_default_site(self.app_config, verbosity=0) + Site(domain='example2.com', name='example2.com').save() + + class MiddlewareTest(TestCase): def test_request(self):