diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-04 08:08:27 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
| commit | 7119f40c9881666b6f9b5cf7df09ee1d21cc8344 (patch) | |
| tree | fa50869f5614295f462d9bf77fec59365c621609 /tests/sitemaps_tests | |
| parent | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (diff) | |
Refs #33476 -- Refactored code to strictly match 88 characters line length.
Diffstat (limited to 'tests/sitemaps_tests')
| -rw-r--r-- | tests/sitemaps_tests/test_generic.py | 25 | ||||
| -rw-r--r-- | tests/sitemaps_tests/test_http.py | 136 | ||||
| -rw-r--r-- | tests/sitemaps_tests/test_https.py | 26 | ||||
| -rw-r--r-- | tests/sitemaps_tests/test_utils.py | 5 |
4 files changed, 115 insertions, 77 deletions
diff --git a/tests/sitemaps_tests/test_generic.py b/tests/sitemaps_tests/test_generic.py index 33a8ee5592..2865c77902 100644 --- a/tests/sitemaps_tests/test_generic.py +++ b/tests/sitemaps_tests/test_generic.py @@ -40,24 +40,25 @@ class GenericViewsSitemapTests(SitemapTestsBase): for pk in TestModel.objects.values_list("id", flat=True): expected += "<url><loc>%s/testmodel/%s/</loc></url>" % (self.base_url, pk) expected_content = ( - """<?xml version="1.0" encoding="UTF-8"?> -<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> -%s -</urlset> -""" - % expected - ) + '<?xml version="1.0" encoding="UTF-8"?>\n' + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' + 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' + "%s\n" + "</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" xmlns:xhtml="http://www.w3.org/1999/xhtml"> -<url><loc>%s/testmodel/%s/</loc><lastmod>2013-03-13</lastmod></url> -</urlset> -""" % ( + expected_content = ( + '<?xml version="1.0" encoding="UTF-8"?>\n' + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' + 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' + "<url><loc>%s/testmodel/%s/</loc><lastmod>2013-03-13</lastmod></url>\n" + "</urlset>" + ) % ( self.base_url, test_model.pk, ) diff --git a/tests/sitemaps_tests/test_http.py b/tests/sitemaps_tests/test_http.py index 76707e16e3..d1c345b53e 100644 --- a/tests/sitemaps_tests/test_http.py +++ b/tests/sitemaps_tests/test_http.py @@ -82,11 +82,14 @@ class HTTPSitemapTests(SitemapTestsBase): def test_simple_sitemap_section(self): "A simple sitemap section can be rendered" response = self.client.get("/simple/sitemap-simple.xml") - expected_content = """<?xml version="1.0" encoding="UTF-8"?> -<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> -<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> -</urlset> -""" % ( + expected_content = ( + '<?xml version="1.0" encoding="UTF-8"?>\n' + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' + 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' + "<url><loc>%s/location/</loc><lastmod>%s</lastmod>" + "<changefreq>never</changefreq><priority>0.5</priority></url>\n" + "</urlset>" + ) % ( self.base_url, date.today(), ) @@ -113,11 +116,14 @@ class HTTPSitemapTests(SitemapTestsBase): def test_simple_sitemap(self): "A simple sitemap can be rendered" response = self.client.get("/simple/sitemap.xml") - expected_content = """<?xml version="1.0" encoding="UTF-8"?> -<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> -<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> -</urlset> -""" % ( + expected_content = ( + '<?xml version="1.0" encoding="UTF-8"?>\n' + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' + 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' + "<url><loc>%s/location/</loc><lastmod>%s</lastmod>" + "<changefreq>never</changefreq><priority>0.5</priority></url>\n" + "</urlset>" + ) % ( self.base_url, date.today(), ) @@ -256,13 +262,13 @@ class HTTPSitemapTests(SitemapTestsBase): # doesn't raise an exception. response = self.client.get("/simple/sitemap.xml") expected_content = ( - """<?xml version="1.0" encoding="UTF-8"?> -<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> -<url><loc>http://testserver/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> -</urlset> -""" - % date.today() - ) + '<?xml version="1.0" encoding="UTF-8"?>\n' + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' + 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' + "<url><loc>http://testserver/location/</loc><lastmod>%s</lastmod>" + "<changefreq>never</changefreq><priority>0.5</priority></url>\n" + "</urlset>" + ) % date.today() self.assertXMLEqual(response.content.decode(), expected_content) @ignore_warnings(category=RemovedInDjango50Warning) @@ -335,13 +341,15 @@ class HTTPSitemapTests(SitemapTestsBase): """ with self.assertNoLogs("django.template", "DEBUG"): response = self.client.get("/simple/i18n.xml") - expected_content = """<?xml version="1.0" encoding="UTF-8"?> -<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> -<url><loc>{0}/en/i18n/testmodel/{1}/</loc><changefreq>never</changefreq><priority>0.5</priority></url><url><loc>{0}/pt/i18n/testmodel/{1}/</loc><changefreq>never</changefreq><priority>0.5</priority></url> -</urlset> -""".format( - self.base_url, self.i18n_model.pk - ) + expected_content = ( + '<?xml version="1.0" encoding="UTF-8"?>\n' + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' + 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' + "<url><loc>{0}/en/i18n/testmodel/{1}/</loc><changefreq>never</changefreq>" + "<priority>0.5</priority></url><url><loc>{0}/pt/i18n/testmodel/{1}/</loc>" + "<changefreq>never</changefreq><priority>0.5</priority></url>\n" + "</urlset>" + ).format(self.base_url, self.i18n_model.pk) self.assertXMLEqual(response.content.decode(), expected_content) @override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese"))) @@ -363,11 +371,13 @@ class HTTPSitemapTests(SitemapTestsBase): """.replace( "\n", "" ) - expected_content = f"""<?xml version="1.0" encoding="UTF-8"?> -<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> -{expected_urls} -</urlset> -""" + expected_content = ( + f'<?xml version="1.0" encoding="UTF-8"?>\n' + f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' + f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' + f"{expected_urls}\n" + f"</urlset>" + ) self.assertXMLEqual(response.content.decode(), expected_content) @override_settings( @@ -391,11 +401,13 @@ class HTTPSitemapTests(SitemapTestsBase): """.replace( "\n", "" ) - expected_content = f"""<?xml version="1.0" encoding="UTF-8"?> -<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> -{expected_urls} -</urlset> -""" + expected_content = ( + f'<?xml version="1.0" encoding="UTF-8"?>\n' + f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' + f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' + f"{expected_urls}\n" + f"</urlset>" + ) self.assertXMLEqual(response.content.decode(), expected_content) @override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese"))) @@ -419,19 +431,23 @@ class HTTPSitemapTests(SitemapTestsBase): """.replace( "\n", "" ) - expected_content = f"""<?xml version="1.0" encoding="UTF-8"?> -<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> -{expected_urls} -</urlset> -""" + expected_content = ( + f'<?xml version="1.0" encoding="UTF-8"?>\n' + f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' + f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' + f"{expected_urls}\n" + f"</urlset>" + ) self.assertXMLEqual(response.content.decode(), expected_content) def test_sitemap_without_entries(self): response = self.client.get("/sitemap-without-entries/sitemap.xml") - expected_content = """<?xml version="1.0" encoding="UTF-8"?> -<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> - -</urlset>""" + expected_content = ( + '<?xml version="1.0" encoding="UTF-8"?>\n' + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' + 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n\n' + "</urlset>" + ) self.assertXMLEqual(response.content.decode(), expected_content) def test_callable_sitemod_partial(self): @@ -448,11 +464,15 @@ class HTTPSitemapTests(SitemapTestsBase): <sitemap><loc>http://example.com/simple/sitemap-callable-lastmod.xml</loc></sitemap> </sitemapindex> """ - expected_content_sitemap = """<?xml version="1.0" encoding="UTF-8"?> - <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> - <url><loc>http://example.com/location/</loc><lastmod>2013-03-13</lastmod></url><url><loc>http://example.com/location/</loc></url> - </urlset> - """ + expected_content_sitemap = ( + '<?xml version="1.0" encoding="UTF-8"?>\n' + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' + 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' + "<url><loc>http://example.com/location/</loc>" + "<lastmod>2013-03-13</lastmod></url><url>" + "<loc>http://example.com/location/</loc></url>\n" + "</urlset>" + ) self.assertXMLEqual(index_response.content.decode(), expected_content_index) self.assertXMLEqual(sitemap_response.content.decode(), expected_content_sitemap) @@ -474,11 +494,16 @@ class HTTPSitemapTests(SitemapTestsBase): <sitemap><loc>http://example.com/simple/sitemap-callable-lastmod.xml</loc><lastmod>2014-03-13T10:00:00</lastmod></sitemap> </sitemapindex> """ - expected_content_sitemap = """<?xml version="1.0" encoding="UTF-8"?> - <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> - <url><loc>http://example.com/location/</loc><lastmod>2013-03-13</lastmod></url><url><loc>http://example.com/location/</loc><lastmod>2014-03-13</lastmod></url> - </urlset> - """ + expected_content_sitemap = ( + '<?xml version="1.0" encoding="UTF-8"?>\n' + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' + 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' + "<url><loc>http://example.com/location/</loc>" + "<lastmod>2013-03-13</lastmod></url>" + "<url><loc>http://example.com/location/</loc>" + "<lastmod>2014-03-13</lastmod></url>\n" + "</urlset>" + ) self.assertXMLEqual(index_response.content.decode(), expected_content_index) self.assertXMLEqual(sitemap_response.content.decode(), expected_content_sitemap) @@ -494,7 +519,10 @@ class DeprecatedTests(SitemapTestsBase): ] ) def test_simple_sitemap_custom_index_warning(self): - msg = "Calling `__str__` on SitemapIndexItem is deprecated, use the `location` attribute instead." + msg = ( + "Calling `__str__` on SitemapIndexItem is deprecated, use the `location` " + "attribute instead." + ) with self.assertRaisesMessage(RemovedInDjango50Warning, msg): self.client.get("/simple/custom-index.xml") diff --git a/tests/sitemaps_tests/test_https.py b/tests/sitemaps_tests/test_https.py index e2923cf6f4..2eae71e4cc 100644 --- a/tests/sitemaps_tests/test_https.py +++ b/tests/sitemaps_tests/test_https.py @@ -25,11 +25,14 @@ class HTTPSSitemapTests(SitemapTestsBase): def test_secure_sitemap_section(self): "A secure sitemap section can be rendered" response = self.client.get("/secure/sitemap-simple.xml") - expected_content = """<?xml version="1.0" encoding="UTF-8"?> -<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> -<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> -</urlset> -""" % ( + expected_content = ( + '<?xml version="1.0" encoding="UTF-8"?>\n' + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' + 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' + "<url><loc>%s/location/</loc><lastmod>%s</lastmod>" + "<changefreq>never</changefreq><priority>0.5</priority></url>\n" + "</urlset>" + ) % ( self.base_url, date.today(), ) @@ -56,11 +59,14 @@ class HTTPSDetectionSitemapTests(SitemapTestsBase): def test_sitemap_section_with_https_request(self): "A sitemap section requested in HTTPS is rendered with HTTPS links" response = self.client.get("/simple/sitemap-simple.xml", **self.extra) - expected_content = """<?xml version="1.0" encoding="UTF-8"?> -<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> -<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> -</urlset> -""" % ( + expected_content = ( + '<?xml version="1.0" encoding="UTF-8"?>\n' + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' + 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' + "<url><loc>%s/location/</loc><lastmod>%s</lastmod>" + "<changefreq>never</changefreq><priority>0.5</priority></url>\n" + "</urlset>" + ) % ( self.base_url.replace("http://", "https://"), date.today(), ) diff --git a/tests/sitemaps_tests/test_utils.py b/tests/sitemaps_tests/test_utils.py index e83b50cd73..3aba9b5a1a 100644 --- a/tests/sitemaps_tests/test_utils.py +++ b/tests/sitemaps_tests/test_utils.py @@ -34,7 +34,10 @@ class PingGoogleTests(SitemapTestsBase): @override_settings(ROOT_URLCONF="sitemaps_tests.urls.empty") def test_get_sitemap_full_url_not_detected(self): - msg = "You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected." + msg = ( + "You didn't provide a sitemap_url, and the sitemap URL couldn't be " + "auto-detected." + ) with self.assertRaisesMessage(SitemapNotFound, msg): _get_sitemap_full_url(None) |
