Add Django-1.2.1
[frozenviper.git] / Django-1.2.1 / django / contrib / gis / tests / geoapp / test_feeds.py
blobeb039145e4670d02c01f8c4d19f5cf96d98e7daf
1 import unittest
2 from xml.dom import minidom
4 from django.test import Client
5 from models import City
7 class GeoFeedTest(unittest.TestCase):
8 client = Client()
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)
16 def test_geofeed_rss(self):
17 "Tests geographic feeds using GeoRSS over RSSv2."
18 # Uses `GEOSGeometry` in `item_geometry`
19 doc1 = minidom.parseString(self.client.get('/geoapp/feeds/rss1/').content)
20 # Uses a 2-tuple in `item_geometry`
21 doc2 = minidom.parseString(self.client.get('/geoapp/feeds/rss2/').content)
22 feed1, feed2 = doc1.firstChild, doc2.firstChild
24 # Making sure the box got added to the second GeoRSS feed.
25 self.assertChildNodes(feed2.getElementsByTagName('channel')[0],
26 ['title', 'link', 'description', 'language',
27 'lastBuildDate', 'item', 'georss:box', 'atom:link']
30 # Incrementing through the feeds.
31 for feed in [feed1, feed2]:
32 # Ensuring the georss namespace was added to the <rss> element.
33 self.assertEqual(feed.getAttribute(u'xmlns:georss'), u'http://www.georss.org/georss')
34 chan = feed.getElementsByTagName('channel')[0]
35 items = chan.getElementsByTagName('item')
36 self.assertEqual(len(items), City.objects.count())
38 # Ensuring the georss element was added to each item in the feed.
39 for item in items:
40 self.assertChildNodes(item, ['title', 'link', 'description', 'guid', 'georss:point'])
42 def test_geofeed_atom(self):
43 "Testing geographic feeds using GeoRSS over Atom."
44 doc1 = minidom.parseString(self.client.get('/geoapp/feeds/atom1/').content)
45 doc2 = minidom.parseString(self.client.get('/geoapp/feeds/atom2/').content)
46 feed1, feed2 = doc1.firstChild, doc2.firstChild
48 # Making sure the box got added to the second GeoRSS feed.
49 self.assertChildNodes(feed2, ['title', 'link', 'id', 'updated', 'entry', 'georss:box'])
51 for feed in [feed1, feed2]:
52 # Ensuring the georsss namespace was added to the <feed> element.
53 self.assertEqual(feed.getAttribute(u'xmlns:georss'), u'http://www.georss.org/georss')
54 entries = feed.getElementsByTagName('entry')
55 self.assertEqual(len(entries), City.objects.count())
57 # Ensuring the georss element was added to each entry in the feed.
58 for entry in entries:
59 self.assertChildNodes(entry, ['title', 'link', 'id', 'summary', 'georss:point'])
61 def test_geofeed_w3c(self):
62 "Testing geographic feeds using W3C Geo."
63 doc = minidom.parseString(self.client.get('/geoapp/feeds/w3cgeo1/').content)
64 feed = doc.firstChild
65 # Ensuring the geo namespace was added to the <feed> element.
66 self.assertEqual(feed.getAttribute(u'xmlns:geo'), u'http://www.w3.org/2003/01/geo/wgs84_pos#')
67 chan = feed.getElementsByTagName('channel')[0]
68 items = chan.getElementsByTagName('item')
69 self.assertEqual(len(items), City.objects.count())
71 # Ensuring the geo:lat and geo:lon element was added to each item in the feed.
72 for item in items:
73 self.assertChildNodes(item, ['title', 'link', 'description', 'guid', 'geo:lat', 'geo:lon'])
75 # Boxes and Polygons aren't allowed in W3C Geo feeds.
76 self.assertRaises(ValueError, self.client.get, '/geoapp/feeds/w3cgeo2/') # Box in <channel>
77 self.assertRaises(ValueError, self.client.get, '/geoapp/feeds/w3cgeo3/') # Polygons in <entry>