1 from django
.conf
import settings
2 from django
.contrib
.sites
.models
import Site
, RequestSite
, get_current_site
3 from django
.core
.exceptions
import ObjectDoesNotExist
4 from django
.http
import HttpRequest
5 from django
.test
import TestCase
8 class SitesFrameworkTests(TestCase
):
11 Site(id=settings
.SITE_ID
, domain
="example.com", name
="example.com").save()
12 self
.old_Site_meta_installed
= Site
._meta
.installed
13 Site
._meta
.installed
= True
16 Site
._meta
.installed
= self
.old_Site_meta_installed
18 def test_save_another(self
):
19 # Regression for #17415
20 # On some backends the sequence needs reset after save with explicit ID.
21 # Test that there is no sequence collisions by saving another site.
22 Site(domain
="example2.com", name
="example2.com").save()
24 def test_site_manager(self
):
25 # Make sure that get_current() does not return a deleted Site object.
26 s
= Site
.objects
.get_current()
27 self
.assertTrue(isinstance(s
, Site
))
29 self
.assertRaises(ObjectDoesNotExist
, Site
.objects
.get_current
)
31 def test_site_cache(self
):
32 # After updating a Site object (e.g. via the admin), we shouldn't return a
33 # bogus value from the SITE_CACHE.
34 site
= Site
.objects
.get_current()
35 self
.assertEqual(u
"example.com", site
.name
)
36 s2
= Site
.objects
.get(id=settings
.SITE_ID
)
37 s2
.name
= "Example site"
39 site
= Site
.objects
.get_current()
40 self
.assertEqual(u
"Example site", site
.name
)
42 def test_get_current_site(self
):
43 # Test that the correct Site object is returned
44 request
= HttpRequest()
46 "SERVER_NAME": "example.com",
49 site
= get_current_site(request
)
50 self
.assertTrue(isinstance(site
, Site
))
51 self
.assertEqual(site
.id, settings
.SITE_ID
)
53 # Test that an exception is raised if the sites framework is installed
54 # but there is no matching Site
56 self
.assertRaises(ObjectDoesNotExist
, get_current_site
, request
)
58 # A RequestSite is returned if the sites framework is not installed
59 Site
._meta
.installed
= False
60 site
= get_current_site(request
)
61 self
.assertTrue(isinstance(site
, RequestSite
))
62 self
.assertEqual(site
.name
, u
"example.com")