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