app/django/contrib/gis/tests/geoapp/test_sitemaps.py
changeset 323 ff1a9aa48cfd
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
       
     1 import unittest, zipfile, cStringIO
       
     2 from xml.dom import minidom
       
     3 
       
     4 from django.test import Client
       
     5 from models import City, Country
       
     6 
       
     7 class GeoSitemapTest(unittest.TestCase):
       
     8     client = Client()
       
     9 
       
    10     def assertChildNodes(self, elem, expected):
       
    11         "Taken from regressiontests/syndication/tests.py."
       
    12         actual = set([n.nodeName for n in elem.childNodes])
       
    13         expected = set(expected)
       
    14         self.assertEqual(actual, expected)
       
    15 
       
    16     def test_geositemap_index(self):
       
    17         "Tests geographic sitemap index."
       
    18         # Getting the geo index.
       
    19         doc = minidom.parseString(self.client.get('/geoapp/sitemap.xml').content)
       
    20         index = doc.firstChild
       
    21         self.assertEqual(index.getAttribute(u'xmlns'), u'http://www.sitemaps.org/schemas/sitemap/0.9')
       
    22         self.assertEqual(3, len(index.getElementsByTagName('sitemap')))
       
    23 
       
    24     def test_geositemap_kml(self):
       
    25         "Tests KML/KMZ geographic sitemaps."
       
    26         for kml_type in ('kml', 'kmz'):
       
    27             doc = minidom.parseString(self.client.get('/geoapp/sitemaps/%s.xml' % kml_type).content)
       
    28 
       
    29             # Ensuring the right sitemaps namespaces are present.
       
    30             urlset = doc.firstChild
       
    31             self.assertEqual(urlset.getAttribute(u'xmlns'), u'http://www.sitemaps.org/schemas/sitemap/0.9')
       
    32             self.assertEqual(urlset.getAttribute(u'xmlns:geo'), u'http://www.google.com/geo/schemas/sitemap/1.0')
       
    33         
       
    34             urls = urlset.getElementsByTagName('url')
       
    35             self.assertEqual(2, len(urls)) # Should only be 2 sitemaps.
       
    36             for url in urls:
       
    37                 self.assertChildNodes(url, ['loc', 'geo:geo'])
       
    38                 # Making sure the 'geo:format' element was properly set.
       
    39                 geo_elem = url.getElementsByTagName('geo:geo')[0]
       
    40                 geo_format = geo_elem.getElementsByTagName('geo:format')[0]
       
    41                 self.assertEqual(kml_type, geo_format.childNodes[0].data)
       
    42 
       
    43                 # Getting the relative URL since we don't have a real site.
       
    44                 kml_url = url.getElementsByTagName('loc')[0].childNodes[0].data.split('http://example.com')[1]
       
    45                 
       
    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                 
       
    56                 # Ensuring the correct number of placemarks are in the KML doc.
       
    57                 if 'city' in kml_url:
       
    58                     model = City
       
    59                 elif 'country' in kml_url:
       
    60                     model = Country
       
    61                 self.assertEqual(model.objects.count(), len(kml_doc.getElementsByTagName('Placemark')))
       
    62 
       
    63     def test_geositemap_georss(self):
       
    64         "Tests GeoRSS geographic sitemaps."
       
    65         from feeds import feed_dict
       
    66 
       
    67         doc = minidom.parseString(self.client.get('/geoapp/sitemaps/georss.xml').content)
       
    68    
       
    69         # Ensuring the right sitemaps namespaces are present.
       
    70         urlset = doc.firstChild
       
    71         self.assertEqual(urlset.getAttribute(u'xmlns'), u'http://www.sitemaps.org/schemas/sitemap/0.9')
       
    72         self.assertEqual(urlset.getAttribute(u'xmlns:geo'), u'http://www.google.com/geo/schemas/sitemap/1.0')
       
    73 
       
    74         # Making sure the correct number of feed URLs were included.
       
    75         urls = urlset.getElementsByTagName('url')
       
    76         self.assertEqual(len(feed_dict), len(urls))
       
    77 
       
    78         for url in urls:
       
    79             self.assertChildNodes(url, ['loc', 'geo:geo'])
       
    80             # Making sure the 'geo:format' element was properly set to 'georss'.
       
    81             geo_elem = url.getElementsByTagName('geo:geo')[0]
       
    82             geo_format = geo_elem.getElementsByTagName('geo:format')[0]
       
    83             self.assertEqual('georss', geo_format.childNodes[0].data)