mirror of https://github.com/django/django.git
Improved test coverage for django.contrib.sitemaps.
This commit is contained in:
parent
5bb9b9a388
commit
0eefda493b
|
@ -4,6 +4,7 @@ from django.urls import reverse
|
|||
|
||||
class TestModel(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
lastmod = models.DateTimeField(null=True)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return '/testmodel/%s/' % self.id
|
||||
|
|
|
@ -45,3 +45,15 @@ class GenericViewsSitemapTests(SitemapTestsBase):
|
|||
</urlset>
|
||||
""" % expected
|
||||
self.assertXMLEqual(response.content.decode(), expected_content)
|
||||
|
||||
def test_generic_sitemap_lastmod(self):
|
||||
test_model = TestModel.objects.first()
|
||||
TestModel.objects.update(lastmod=datetime(2013, 3, 13, 10, 0, 0))
|
||||
response = self.client.get('/generic-lastmod/sitemap.xml')
|
||||
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<url><loc>%s/testmodel/%s/</loc><lastmod>2013-03-13</lastmod></url>
|
||||
</urlset>
|
||||
""" % (self.base_url, test_model.pk)
|
||||
self.assertXMLEqual(response.content.decode(), expected_content)
|
||||
self.assertEqual(response['Last-Modified'], 'Wed, 13 Mar 2013 10:00:00 GMT')
|
||||
|
|
|
@ -27,6 +27,26 @@ class HTTPSitemapTests(SitemapTestsBase):
|
|||
""" % self.base_url
|
||||
self.assertXMLEqual(response.content.decode(), expected_content)
|
||||
|
||||
def test_sitemap_not_callable(self):
|
||||
"""A sitemap may not be callable."""
|
||||
response = self.client.get('/simple-not-callable/index.xml')
|
||||
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap>
|
||||
</sitemapindex>
|
||||
""" % self.base_url
|
||||
self.assertXMLEqual(response.content.decode(), expected_content)
|
||||
|
||||
def test_paged_sitemap(self):
|
||||
"""A sitemap may have multiple pages."""
|
||||
response = self.client.get('/simple-paged/index.xml')
|
||||
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap><loc>{0}/simple/sitemap-simple.xml</loc></sitemap><sitemap><loc>{0}/simple/sitemap-simple.xml?p=2</loc></sitemap>
|
||||
</sitemapindex>
|
||||
""".format(self.base_url)
|
||||
self.assertXMLEqual(response.content.decode(), expected_content)
|
||||
|
||||
@override_settings(TEMPLATES=[{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [os.path.join(os.path.dirname(__file__), 'templates')],
|
||||
|
@ -52,6 +72,21 @@ class HTTPSitemapTests(SitemapTestsBase):
|
|||
""" % (self.base_url, date.today())
|
||||
self.assertXMLEqual(response.content.decode(), expected_content)
|
||||
|
||||
def test_no_section(self):
|
||||
response = self.client.get('/simple/sitemap-simple2.xml')
|
||||
self.assertEqual(str(response.context['exception']), "No sitemap available for section: 'simple2'")
|
||||
self.assertEqual(response.status_code, 404)
|
||||
|
||||
def test_empty_page(self):
|
||||
response = self.client.get('/simple/sitemap-simple.xml?p=0')
|
||||
self.assertEqual(str(response.context['exception']), 'Page 0 empty')
|
||||
self.assertEqual(response.status_code, 404)
|
||||
|
||||
def test_page_not_int(self):
|
||||
response = self.client.get('/simple/sitemap-simple.xml?p=test')
|
||||
self.assertEqual(str(response.context['exception']), "No page 'test'")
|
||||
self.assertEqual(response.status_code, 404)
|
||||
|
||||
def test_simple_sitemap(self):
|
||||
"A simple sitemap can be rendered"
|
||||
response = self.client.get('/simple/sitemap.xml')
|
||||
|
|
|
@ -21,6 +21,11 @@ class SimpleSitemap(Sitemap):
|
|||
return [object()]
|
||||
|
||||
|
||||
class SimplePagedSitemap(Sitemap):
|
||||
def items(self):
|
||||
return [object() for x in range(Sitemap.limit + 1)]
|
||||
|
||||
|
||||
class SimpleI18nSitemap(Sitemap):
|
||||
changefreq = "never"
|
||||
priority = 0.5
|
||||
|
@ -35,9 +40,6 @@ class EmptySitemap(Sitemap):
|
|||
priority = 0.5
|
||||
location = '/location/'
|
||||
|
||||
def items(self):
|
||||
return []
|
||||
|
||||
|
||||
class FixedLastmodSitemap(SimpleSitemap):
|
||||
lastmod = datetime(2013, 3, 13, 10, 0, 0)
|
||||
|
@ -80,6 +82,14 @@ simple_i18nsitemaps = {
|
|||
'simple': SimpleI18nSitemap,
|
||||
}
|
||||
|
||||
simple_sitemaps_not_callable = {
|
||||
'simple': SimpleSitemap(),
|
||||
}
|
||||
|
||||
simple_sitemaps_paged = {
|
||||
'simple': SimplePagedSitemap,
|
||||
}
|
||||
|
||||
empty_sitemaps = {
|
||||
'empty': EmptySitemap,
|
||||
}
|
||||
|
@ -118,9 +128,17 @@ generic_sitemaps = {
|
|||
'generic': GenericSitemap({'queryset': TestModel.objects.order_by('pk').all()}),
|
||||
}
|
||||
|
||||
generic_sitemaps_lastmod = {
|
||||
'generic': GenericSitemap({
|
||||
'queryset': TestModel.objects.order_by('pk').all(),
|
||||
'date_field': 'lastmod',
|
||||
}),
|
||||
}
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^simple/index\.xml$', views.index, {'sitemaps': simple_sitemaps}),
|
||||
url(r'^simple-paged/index\.xml$', views.index, {'sitemaps': simple_sitemaps_paged}),
|
||||
url(r'^simple-not-callable/index\.xml$', views.index, {'sitemaps': simple_sitemaps_not_callable}),
|
||||
url(r'^simple/custom-index\.xml$', views.index,
|
||||
{'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_index.xml'}),
|
||||
url(r'^simple/sitemap-(?P<section>.+)\.xml$', views.sitemap,
|
||||
|
@ -165,6 +183,9 @@ urlpatterns = [
|
|||
url(r'^generic/sitemap\.xml$', views.sitemap,
|
||||
{'sitemaps': generic_sitemaps},
|
||||
name='django.contrib.sitemaps.views.sitemap'),
|
||||
url(r'^generic-lastmod/sitemap\.xml$', views.sitemap,
|
||||
{'sitemaps': generic_sitemaps_lastmod},
|
||||
name='django.contrib.sitemaps.views.sitemap'),
|
||||
url(r'^cached/index\.xml$', cache_page(1)(views.index),
|
||||
{'sitemaps': simple_sitemaps, 'sitemap_url_name': 'cached_sitemap'}),
|
||||
url(r'^cached/sitemap-(?P<section>.+)\.xml', cache_page(1)(views.sitemap),
|
||||
|
|
Loading…
Reference in New Issue