summaryrefslogtreecommitdiff
path: root/tests/sitemaps_tests
diff options
context:
space:
mode:
authorAnton Samarchyan <anton.samarchyan@savoirfairelinux.com>2017-03-17 15:25:50 -0400
committerTim Graham <timograham@gmail.com>2017-07-26 15:01:46 -0400
commit0eefda493b293c129d5bf52b5606c6b1a85e197f (patch)
tree95bce3c2b32ed6991683cb54b894d20a5f8682e6 /tests/sitemaps_tests
parent5bb9b9a3881fb1a334efb2c1357a20bdce68a960 (diff)
downloaddjango-0eefda493b293c129d5bf52b5606c6b1a85e197f.tar.gz
Improved test coverage for django.contrib.sitemaps.
Diffstat (limited to 'tests/sitemaps_tests')
-rw-r--r--tests/sitemaps_tests/models.py1
-rw-r--r--tests/sitemaps_tests/test_generic.py12
-rw-r--r--tests/sitemaps_tests/test_http.py35
-rw-r--r--tests/sitemaps_tests/urls/http.py27
4 files changed, 72 insertions, 3 deletions
diff --git a/tests/sitemaps_tests/models.py b/tests/sitemaps_tests/models.py
index 29b3e8cde7..77ac601841 100644
--- a/tests/sitemaps_tests/models.py
+++ b/tests/sitemaps_tests/models.py
@@ -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
diff --git a/tests/sitemaps_tests/test_generic.py b/tests/sitemaps_tests/test_generic.py
index 9f8fa20924..141e2e2a39 100644
--- a/tests/sitemaps_tests/test_generic.py
+++ b/tests/sitemaps_tests/test_generic.py
@@ -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')
diff --git a/tests/sitemaps_tests/test_http.py b/tests/sitemaps_tests/test_http.py
index 42962bb01c..45b95d0958 100644
--- a/tests/sitemaps_tests/test_http.py
+++ b/tests/sitemaps_tests/test_http.py
@@ -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')
diff --git a/tests/sitemaps_tests/urls/http.py b/tests/sitemaps_tests/urls/http.py
index e2fc991a29..66e05301f5 100644
--- a/tests/sitemaps_tests/urls/http.py
+++ b/tests/sitemaps_tests/urls/http.py
@@ -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),