From 0eefda493b293c129d5bf52b5606c6b1a85e197f Mon Sep 17 00:00:00 2001 From: Anton Samarchyan Date: Fri, 17 Mar 2017 15:25:50 -0400 Subject: [PATCH] Improved test coverage for django.contrib.sitemaps. --- tests/sitemaps_tests/models.py | 1 + tests/sitemaps_tests/test_generic.py | 12 ++++++++++ tests/sitemaps_tests/test_http.py | 35 ++++++++++++++++++++++++++++ tests/sitemaps_tests/urls/http.py | 27 ++++++++++++++++++--- 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): """ % 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 = """ + +%s/testmodel/%s/2013-03-13 + +""" % (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 = """ + +%s/simple/sitemap-simple.xml + +""" % 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 = """ + +{0}/simple/sitemap-simple.xml{0}/simple/sitemap-simple.xml?p=2 + +""".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
.+)\.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
.+)\.xml', cache_page(1)(views.sitemap),