PageRenderTime 163ms CodeModel.GetById 91ms app.highlight 10ms RepoModel.GetById 60ms app.codeStats 0ms

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

https://code.google.com/p/mango-py/
Python | 88 lines | 58 code | 18 blank | 12 comment | 12 complexity | 0c0b53f66ac8ac8936fe63b2d18f65d6 MD5 | raw file
 1from xml.dom import minidom
 2from django.conf import settings
 3from django.contrib.sites.models import Site
 4from django.test import TestCase
 5
 6from models import City
 7
 8
 9class GeoFeedTest(TestCase):
10
11    urls = 'django.contrib.gis.tests.geoapp.urls'
12
13    def setUp(self):
14        Site(id=settings.SITE_ID, domain="example.com", name="example.com").save()
15        self.old_Site_meta_installed = Site._meta.installed
16        Site._meta.installed = True
17
18    def tearDown(self):
19        Site._meta.installed = self.old_Site_meta_installed
20
21    def assertChildNodes(self, elem, expected):
22        "Taken from regressiontests/syndication/tests.py."
23        actual = set([n.nodeName for n in elem.childNodes])
24        expected = set(expected)
25        self.assertEqual(actual, expected)
26
27    def test_geofeed_rss(self):
28        "Tests geographic feeds using GeoRSS over RSSv2."
29        # Uses `GEOSGeometry` in `item_geometry`
30        doc1 = minidom.parseString(self.client.get('/feeds/rss1/').content)
31        # Uses a 2-tuple in `item_geometry`
32        doc2 = minidom.parseString(self.client.get('/feeds/rss2/').content)
33        feed1, feed2 = doc1.firstChild, doc2.firstChild
34
35        # Making sure the box got added to the second GeoRSS feed.
36        self.assertChildNodes(feed2.getElementsByTagName('channel')[0],
37                              ['title', 'link', 'description', 'language',
38                               'lastBuildDate', 'item', 'georss:box', 'atom:link']
39                              )
40
41        # Incrementing through the feeds.
42        for feed in [feed1, feed2]:
43            # Ensuring the georss namespace was added to the <rss> element.
44            self.assertEqual(feed.getAttribute(u'xmlns:georss'),  u'http://www.georss.org/georss')
45            chan = feed.getElementsByTagName('channel')[0]
46            items = chan.getElementsByTagName('item')
47            self.assertEqual(len(items), City.objects.count())
48
49            # Ensuring the georss element was added to each item in the feed.
50            for item in items:
51                self.assertChildNodes(item, ['title', 'link', 'description', 'guid', 'georss:point'])
52
53    def test_geofeed_atom(self):
54        "Testing geographic feeds using GeoRSS over Atom."
55        doc1 = minidom.parseString(self.client.get('/feeds/atom1/').content)
56        doc2 = minidom.parseString(self.client.get('/feeds/atom2/').content)
57        feed1, feed2 = doc1.firstChild, doc2.firstChild
58
59        # Making sure the box got added to the second GeoRSS feed.
60        self.assertChildNodes(feed2, ['title', 'link', 'id', 'updated', 'entry', 'georss:box'])
61
62        for feed in [feed1, feed2]:
63            # Ensuring the georsss namespace was added to the <feed> element.
64            self.assertEqual(feed.getAttribute(u'xmlns:georss'),  u'http://www.georss.org/georss')
65            entries = feed.getElementsByTagName('entry')
66            self.assertEqual(len(entries), City.objects.count())
67
68            # Ensuring the georss element was added to each entry in the feed.
69            for entry in entries:
70                self.assertChildNodes(entry, ['title', 'link', 'id', 'summary', 'georss:point'])
71
72    def test_geofeed_w3c(self):
73        "Testing geographic feeds using W3C Geo."
74        doc = minidom.parseString(self.client.get('/feeds/w3cgeo1/').content)
75        feed = doc.firstChild
76        # Ensuring the geo namespace was added to the <feed> element.
77        self.assertEqual(feed.getAttribute(u'xmlns:geo'), u'http://www.w3.org/2003/01/geo/wgs84_pos#')
78        chan = feed.getElementsByTagName('channel')[0]
79        items = chan.getElementsByTagName('item')
80        self.assertEqual(len(items), City.objects.count())
81
82        # Ensuring the geo:lat and geo:lon element was added to each item in the feed.
83        for item in items:
84            self.assertChildNodes(item, ['title', 'link', 'description', 'guid', 'geo:lat', 'geo:lon'])
85
86        # Boxes and Polygons aren't allowed in W3C Geo feeds.
87        self.assertRaises(ValueError, self.client.get, '/feeds/w3cgeo2/') # Box in <channel>
88        self.assertRaises(ValueError, self.client.get, '/feeds/w3cgeo3/') # Polygons in <entry>