2011-10-18 02:45:22 +08:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
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
|
2010-10-11 20:55:17 +08:00
|
|
|
import zipfile
|
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
|
2010-12-02 13:58:21 +08:00
|
|
|
from django.test import TestCase
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2011-10-18 02:45:22 +08:00
|
|
|
from .models import City, Country
|
2008-08-24 03:22:23 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2010-12-02 13:58:21 +08:00
|
|
|
class GeoSitemapTest(TestCase):
|
|
|
|
|
|
|
|
urls = 'django.contrib.gis.tests.geoapp.urls'
|
2008-08-24 03:22:23 +08:00
|
|
|
|
2011-03-17 02:22:52 +08:00
|
|
|
def setUp(self):
|
|
|
|
Site(id=settings.SITE_ID, domain="example.com", name="example.com").save()
|
|
|
|
self.old_Site_meta_installed = Site._meta.installed
|
|
|
|
Site._meta.installed = True
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
Site._meta.installed = self.old_Site_meta_installed
|
|
|
|
|
2008-08-24 03:22:23 +08:00
|
|
|
def assertChildNodes(self, elem, expected):
|
|
|
|
"Taken from regressiontests/syndication/tests.py."
|
|
|
|
actual = set([n.nodeName for n in elem.childNodes])
|
|
|
|
expected = set(expected)
|
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
|
|
|
|
def test_geositemap_index(self):
|
|
|
|
"Tests geographic sitemap index."
|
|
|
|
# Getting the geo index.
|
2010-12-02 13:58:21 +08:00
|
|
|
doc = minidom.parseString(self.client.get('/sitemap.xml').content)
|
2008-08-24 03:22:23 +08:00
|
|
|
index = doc.firstChild
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(index.getAttribute('xmlns'), 'http://www.sitemaps.org/schemas/sitemap/0.9')
|
2008-08-24 03:22:23 +08:00
|
|
|
self.assertEqual(3, len(index.getElementsByTagName('sitemap')))
|
|
|
|
|
|
|
|
def test_geositemap_kml(self):
|
|
|
|
"Tests KML/KMZ geographic sitemaps."
|
|
|
|
for kml_type in ('kml', 'kmz'):
|
2010-12-02 13:58:21 +08:00
|
|
|
doc = minidom.parseString(self.client.get('/sitemaps/%s.xml' % kml_type).content)
|
2008-08-24 03:22:23 +08:00
|
|
|
|
|
|
|
# Ensuring the right sitemaps namespaces are present.
|
|
|
|
urlset = doc.firstChild
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(urlset.getAttribute('xmlns'), 'http://www.sitemaps.org/schemas/sitemap/0.9')
|
|
|
|
self.assertEqual(urlset.getAttribute('xmlns:geo'), 'http://www.google.com/geo/schemas/sitemap/1.0')
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-08-24 03:22:23 +08:00
|
|
|
urls = urlset.getElementsByTagName('url')
|
|
|
|
self.assertEqual(2, len(urls)) # Should only be 2 sitemaps.
|
|
|
|
for url in urls:
|
|
|
|
self.assertChildNodes(url, ['loc', 'geo:geo'])
|
|
|
|
# Making sure the 'geo:format' element was properly set.
|
|
|
|
geo_elem = url.getElementsByTagName('geo:geo')[0]
|
|
|
|
geo_format = geo_elem.getElementsByTagName('geo:format')[0]
|
|
|
|
self.assertEqual(kml_type, geo_format.childNodes[0].data)
|
|
|
|
|
|
|
|
# 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)
|
2008-08-24 03:22:23 +08: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 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')))
|
|
|
|
|
|
|
|
def test_geositemap_georss(self):
|
|
|
|
"Tests GeoRSS geographic sitemaps."
|
2011-10-18 02:45:22 +08:00
|
|
|
from .feeds import feed_dict
|
2008-08-24 03:22:23 +08:00
|
|
|
|
2010-12-02 13:58:21 +08:00
|
|
|
doc = minidom.parseString(self.client.get('/sitemaps/georss.xml').content)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-08-24 03:22:23 +08:00
|
|
|
# Ensuring the right sitemaps namespaces are present.
|
|
|
|
urlset = doc.firstChild
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(urlset.getAttribute('xmlns'), 'http://www.sitemaps.org/schemas/sitemap/0.9')
|
|
|
|
self.assertEqual(urlset.getAttribute('xmlns:geo'), 'http://www.google.com/geo/schemas/sitemap/1.0')
|
2008-08-24 03:22:23 +08:00
|
|
|
|
|
|
|
# Making sure the correct number of feed URLs were included.
|
|
|
|
urls = urlset.getElementsByTagName('url')
|
|
|
|
self.assertEqual(len(feed_dict), len(urls))
|
|
|
|
|
|
|
|
for url in urls:
|
|
|
|
self.assertChildNodes(url, ['loc', 'geo:geo'])
|
|
|
|
# Making sure the 'geo:format' element was properly set to 'georss'.
|
|
|
|
geo_elem = url.getElementsByTagName('geo:geo')[0]
|
|
|
|
geo_format = geo_elem.getElementsByTagName('geo:format')[0]
|
|
|
|
self.assertEqual('georss', geo_format.childNodes[0].data)
|