2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-17 18:45:22 +00:00
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
import zipfile
|
2012-05-05 17:47:03 +00:00
|
|
|
from io import BytesIO
|
2008-08-23 19:22:23 +00:00
|
|
|
from xml.dom import minidom
|
2011-10-17 18:45:22 +00:00
|
|
|
|
2011-03-16 18:22:52 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.sites.models import Site
|
2014-08-17 17:06:25 +00:00
|
|
|
from django.test import (
|
2015-01-28 12:35:27 +00:00
|
|
|
TestCase, ignore_warnings, modify_settings, override_settings,
|
|
|
|
skipUnlessDBFeature,
|
2014-08-17 17:06:25 +00:00
|
|
|
)
|
2014-06-07 23:35:57 +00:00
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2015-04-24 15:24:07 +00:00
|
|
|
from .models import City, Country
|
2008-08-23 19:22:23 +00:00
|
|
|
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2014-03-22 17:31:52 +00:00
|
|
|
@modify_settings(INSTALLED_APPS={'append': ['django.contrib.sites', 'django.contrib.sitemaps']})
|
2015-02-10 15:07:44 +00:00
|
|
|
@override_settings(ROOT_URLCONF='gis_tests.geoapp.urls')
|
2014-08-17 17:06:25 +00:00
|
|
|
@skipUnlessDBFeature("gis_enabled")
|
2014-03-22 17:31:52 +00:00
|
|
|
class GeoSitemapTest(TestCase):
|
2010-12-02 05:58:21 +00:00
|
|
|
|
2011-03-16 18:22:52 +00:00
|
|
|
def setUp(self):
|
2013-12-07 18:28:01 +00:00
|
|
|
super(GeoSitemapTest, self).setUp()
|
2011-03-16 18:22:52 +00:00
|
|
|
Site(id=settings.SITE_ID, domain="example.com", name="example.com").save()
|
|
|
|
|
2008-08-23 19:22:23 +00:00
|
|
|
def assertChildNodes(self, elem, expected):
|
2013-03-03 20:03:11 +00:00
|
|
|
"Taken from syndication/tests.py."
|
2013-08-29 23:20:00 +00:00
|
|
|
actual = set(n.nodeName for n in elem.childNodes)
|
2008-08-23 19:22:23 +00:00
|
|
|
expected = set(expected)
|
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
|
2014-12-21 20:19:05 +00:00
|
|
|
@ignore_warnings(category=RemovedInDjango20Warning)
|
2008-08-23 19:22:23 +00:00
|
|
|
def test_geositemap_kml(self):
|
|
|
|
"Tests KML/KMZ geographic sitemaps."
|
|
|
|
for kml_type in ('kml', 'kmz'):
|
2014-12-21 20:19:05 +00:00
|
|
|
# The URL for the sitemaps in urls.py have been updated
|
|
|
|
# with a name but since reversing by Python path is tried first
|
|
|
|
# before reversing by name and works since we're giving
|
|
|
|
# name='django.contrib.gis.sitemaps.views.(kml|kmz)', we need
|
|
|
|
# to silence the erroneous warning until reversing by dotted
|
|
|
|
# path is removed. The test will work without modification when
|
|
|
|
# it's removed.
|
|
|
|
doc = minidom.parseString(self.client.get('/sitemaps/%s.xml' % kml_type).content)
|
2008-08-23 19:22:23 +00:00
|
|
|
|
2014-03-22 17:31:52 +00:00
|
|
|
# Ensuring the right sitemaps namespace is present.
|
2008-08-23 19:22:23 +00:00
|
|
|
urlset = doc.firstChild
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(urlset.getAttribute('xmlns'), 'http://www.sitemaps.org/schemas/sitemap/0.9')
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2008-08-23 19:22:23 +00:00
|
|
|
urls = urlset.getElementsByTagName('url')
|
2013-11-02 21:02:56 +00:00
|
|
|
self.assertEqual(2, len(urls)) # Should only be 2 sitemaps.
|
2008-08-23 19:22:23 +00:00
|
|
|
for url in urls:
|
2014-03-22 17:31:52 +00:00
|
|
|
self.assertChildNodes(url, ['loc'])
|
2008-08-23 19:22:23 +00:00
|
|
|
|
|
|
|
# Getting the relative URL since we don't have a real site.
|
|
|
|
kml_url = url.getElementsByTagName('loc')[0].childNodes[0].data.split('http://example.com')[1]
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2008-08-23 19:22:23 +00:00
|
|
|
if kml_type == 'kml':
|
|
|
|
kml_doc = minidom.parseString(self.client.get(kml_url).content)
|
|
|
|
elif kml_type == 'kmz':
|
|
|
|
# Have to decompress KMZ before parsing.
|
2012-05-05 17:47:03 +00:00
|
|
|
buf = BytesIO(self.client.get(kml_url).content)
|
2008-08-23 19:22:23 +00:00
|
|
|
zf = zipfile.ZipFile(buf)
|
|
|
|
self.assertEqual(1, len(zf.filelist))
|
|
|
|
self.assertEqual('doc.kml', zf.filelist[0].filename)
|
|
|
|
kml_doc = minidom.parseString(zf.read('doc.kml'))
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2008-08-23 19:22:23 +00:00
|
|
|
# Ensuring the correct number of placemarks are in the KML doc.
|
|
|
|
if 'city' in kml_url:
|
|
|
|
model = City
|
|
|
|
elif 'country' in kml_url:
|
|
|
|
model = Country
|
|
|
|
self.assertEqual(model.objects.count(), len(kml_doc.getElementsByTagName('Placemark')))
|