PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/gis/tests/geoapp/test_sitemaps.py

https://code.google.com/p/mango-py/
Python | 96 lines | 68 code | 19 blank | 9 comment | 8 complexity | 53a447a7f779049142a2e698c7b329b8 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import cStringIO
  2. from xml.dom import minidom
  3. import zipfile
  4. from django.conf import settings
  5. from django.contrib.sites.models import Site
  6. from django.test import TestCase
  7. from models import City, Country
  8. class GeoSitemapTest(TestCase):
  9. urls = 'django.contrib.gis.tests.geoapp.urls'
  10. def setUp(self):
  11. Site(id=settings.SITE_ID, domain="example.com", name="example.com").save()
  12. self.old_Site_meta_installed = Site._meta.installed
  13. Site._meta.installed = True
  14. def tearDown(self):
  15. Site._meta.installed = self.old_Site_meta_installed
  16. def assertChildNodes(self, elem, expected):
  17. "Taken from regressiontests/syndication/tests.py."
  18. actual = set([n.nodeName for n in elem.childNodes])
  19. expected = set(expected)
  20. self.assertEqual(actual, expected)
  21. def test_geositemap_index(self):
  22. "Tests geographic sitemap index."
  23. # Getting the geo index.
  24. doc = minidom.parseString(self.client.get('/sitemap.xml').content)
  25. index = doc.firstChild
  26. self.assertEqual(index.getAttribute(u'xmlns'), u'http://www.sitemaps.org/schemas/sitemap/0.9')
  27. self.assertEqual(3, len(index.getElementsByTagName('sitemap')))
  28. def test_geositemap_kml(self):
  29. "Tests KML/KMZ geographic sitemaps."
  30. for kml_type in ('kml', 'kmz'):
  31. doc = minidom.parseString(self.client.get('/sitemaps/%s.xml' % kml_type).content)
  32. # Ensuring the right sitemaps namespaces are present.
  33. urlset = doc.firstChild
  34. self.assertEqual(urlset.getAttribute(u'xmlns'), u'http://www.sitemaps.org/schemas/sitemap/0.9')
  35. self.assertEqual(urlset.getAttribute(u'xmlns:geo'), u'http://www.google.com/geo/schemas/sitemap/1.0')
  36. urls = urlset.getElementsByTagName('url')
  37. self.assertEqual(2, len(urls)) # Should only be 2 sitemaps.
  38. for url in urls:
  39. self.assertChildNodes(url, ['loc', 'geo:geo'])
  40. # Making sure the 'geo:format' element was properly set.
  41. geo_elem = url.getElementsByTagName('geo:geo')[0]
  42. geo_format = geo_elem.getElementsByTagName('geo:format')[0]
  43. self.assertEqual(kml_type, geo_format.childNodes[0].data)
  44. # Getting the relative URL since we don't have a real site.
  45. kml_url = url.getElementsByTagName('loc')[0].childNodes[0].data.split('http://example.com')[1]
  46. if kml_type == 'kml':
  47. kml_doc = minidom.parseString(self.client.get(kml_url).content)
  48. elif kml_type == 'kmz':
  49. # Have to decompress KMZ before parsing.
  50. buf = cStringIO.StringIO(self.client.get(kml_url).content)
  51. zf = zipfile.ZipFile(buf)
  52. self.assertEqual(1, len(zf.filelist))
  53. self.assertEqual('doc.kml', zf.filelist[0].filename)
  54. kml_doc = minidom.parseString(zf.read('doc.kml'))
  55. # Ensuring the correct number of placemarks are in the KML doc.
  56. if 'city' in kml_url:
  57. model = City
  58. elif 'country' in kml_url:
  59. model = Country
  60. self.assertEqual(model.objects.count(), len(kml_doc.getElementsByTagName('Placemark')))
  61. def test_geositemap_georss(self):
  62. "Tests GeoRSS geographic sitemaps."
  63. from feeds import feed_dict
  64. doc = minidom.parseString(self.client.get('/sitemaps/georss.xml').content)
  65. # Ensuring the right sitemaps namespaces are present.
  66. urlset = doc.firstChild
  67. self.assertEqual(urlset.getAttribute(u'xmlns'), u'http://www.sitemaps.org/schemas/sitemap/0.9')
  68. self.assertEqual(urlset.getAttribute(u'xmlns:geo'), u'http://www.google.com/geo/schemas/sitemap/1.0')
  69. # Making sure the correct number of feed URLs were included.
  70. urls = urlset.getElementsByTagName('url')
  71. self.assertEqual(len(feed_dict), len(urls))
  72. for url in urls:
  73. self.assertChildNodes(url, ['loc', 'geo:geo'])
  74. # Making sure the 'geo:format' element was properly set to 'georss'.
  75. geo_elem = url.getElementsByTagName('geo:geo')[0]
  76. geo_format = geo_elem.getElementsByTagName('geo:format')[0]
  77. self.assertEqual('georss', geo_format.childNodes[0].data)