2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-10-18 02:45:22 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
import zipfile
|
2012-05-06 01:47:03 +08:00
|
|
|
from io import BytesIO
|
2008-08-24 03:22:23 +08:00
|
|
|
from xml.dom import minidom
|
2011-10-18 02:45:22 +08:00
|
|
|
|
2011-03-17 02:22:52 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.sites.models import Site
|
2014-08-18 01:06:25 +08:00
|
|
|
from django.test import (
|
2015-08-18 01:45:07 +08:00
|
|
|
TestCase, modify_settings, override_settings, skipUnlessDBFeature,
|
2014-08-18 01:06:25 +08:00
|
|
|
)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2015-04-24 23:24:07 +08:00
|
|
|
from .models import City, Country
|
2008-08-24 03:22:23 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2014-03-23 01:31:52 +08:00
|
|
|
@modify_settings(INSTALLED_APPS={'append': ['django.contrib.sites', 'django.contrib.sitemaps']})
|
2015-02-10 23:07:44 +08:00
|
|
|
@override_settings(ROOT_URLCONF='gis_tests.geoapp.urls')
|
2014-08-18 01:06:25 +08:00
|
|
|
@skipUnlessDBFeature("gis_enabled")
|
2014-03-23 01:31:52 +08:00
|
|
|
class GeoSitemapTest(TestCase):
|
2010-12-02 13:58:21 +08:00
|
|
|
|
2011-03-17 02:22:52 +08:00
|
|
|
def setUp(self):
|
2013-12-08 02:28:01 +08:00
|
|
|
super(GeoSitemapTest, self).setUp()
|
2011-03-17 02:22:52 +08:00
|
|
|
Site(id=settings.SITE_ID, domain="example.com", name="example.com").save()
|
|
|
|
|
2008-08-24 03:22:23 +08:00
|
|
|
def assertChildNodes(self, elem, expected):
|
2013-03-04 04:03:11 +08:00
|
|
|
"Taken from syndication/tests.py."
|
2013-08-30 07:20:00 +08:00
|
|
|
actual = set(n.nodeName for n in elem.childNodes)
|
2008-08-24 03:22:23 +08:00
|
|
|
expected = set(expected)
|
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
|
|
|
|
def test_geositemap_kml(self):
|
|
|
|
"Tests KML/KMZ geographic sitemaps."
|
|
|
|
for kml_type in ('kml', 'kmz'):
|
2014-12-22 04:19:05 +08:00
|
|
|
doc = minidom.parseString(self.client.get('/sitemaps/%s.xml' % kml_type).content)
|
2008-08-24 03:22:23 +08:00
|
|
|
|
2014-03-23 01:31:52 +08:00
|
|
|
# Ensuring the right sitemaps namespace is present.
|
2008-08-24 03:22:23 +08:00
|
|
|
urlset = doc.firstChild
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(urlset.getAttribute('xmlns'), 'http://www.sitemaps.org/schemas/sitemap/0.9')
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-08-24 03:22:23 +08:00
|
|
|
urls = urlset.getElementsByTagName('url')
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(2, len(urls)) # Should only be 2 sitemaps.
|
2008-08-24 03:22:23 +08:00
|
|
|
for url in urls:
|
2014-03-23 01:31:52 +08:00
|
|
|
self.assertChildNodes(url, ['loc'])
|
2008-08-24 03:22:23 +08: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 20:55:17 +08:00
|
|
|
|
2008-08-24 03:22:23 +08: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-06 01:47:03 +08:00
|
|
|
buf = BytesIO(self.client.get(kml_url).content)
|
2015-07-02 05:37:10 +08:00
|
|
|
with zipfile.ZipFile(buf) as zf:
|
|
|
|
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 20:55:17 +08:00
|
|
|
|
2008-08-24 03:22:23 +08: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')))
|