web/lib/django/contrib/gis/tests/geoapp/test_feeds.py
changeset 0 0d40e90630ef
child 29 cc9b7e14412b
equal deleted inserted replaced
-1:000000000000 0:0d40e90630ef
       
     1 import unittest
       
     2 from xml.dom import minidom
       
     3 
       
     4 from django.test import Client
       
     5 from models import City
       
     6 
       
     7 class GeoFeedTest(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_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
       
    23 
       
    24         # Making sure the box got added to the second GeoRSS feed.
       
    25         self.assertChildNodes(feed2.getElementsByTagName('channel')[0], 
       
    26                               ['title', 'link', 'description', 'language', 'lastBuildDate', 'item', 'georss:box']
       
    27                               )
       
    28         
       
    29         # Incrementing through the feeds.
       
    30         for feed in [feed1, feed2]:
       
    31             # Ensuring the georss namespace was added to the <rss> element.
       
    32             self.assertEqual(feed.getAttribute(u'xmlns:georss'),  u'http://www.georss.org/georss')
       
    33             chan = feed.getElementsByTagName('channel')[0]
       
    34             items = chan.getElementsByTagName('item')
       
    35             self.assertEqual(len(items), City.objects.count())
       
    36                 
       
    37             # Ensuring the georss element was added to each item in the feed.
       
    38             for item in items:
       
    39                 self.assertChildNodes(item, ['title', 'link', 'description', 'guid', 'georss:point'])
       
    40 
       
    41     def test_geofeed_atom(self):
       
    42         "Testing geographic feeds using GeoRSS over Atom."
       
    43         doc1 = minidom.parseString(self.client.get('/geoapp/feeds/atom1/').content)
       
    44         doc2 = minidom.parseString(self.client.get('/geoapp/feeds/atom2/').content)
       
    45         feed1, feed2 = doc1.firstChild, doc2.firstChild
       
    46 
       
    47         # Making sure the box got added to the second GeoRSS feed.
       
    48         self.assertChildNodes(feed2, ['title', 'link', 'id', 'updated', 'entry', 'georss:box'])        
       
    49 
       
    50         for feed in [feed1, feed2]:
       
    51             # Ensuring the georsss namespace was added to the <feed> element.
       
    52             self.assertEqual(feed.getAttribute(u'xmlns:georss'),  u'http://www.georss.org/georss')
       
    53             entries = feed.getElementsByTagName('entry')
       
    54             self.assertEqual(len(entries), City.objects.count())
       
    55             
       
    56             # Ensuring the georss element was added to each entry in the feed.
       
    57             for entry in entries:
       
    58                 self.assertChildNodes(entry, ['title', 'link', 'id', 'summary', 'georss:point'])
       
    59 
       
    60     def test_geofeed_w3c(self):
       
    61         "Testing geographic feeds using W3C Geo."
       
    62         doc = minidom.parseString(self.client.get('/geoapp/feeds/w3cgeo1/').content)
       
    63         feed = doc.firstChild
       
    64         # Ensuring the geo namespace was added to the <feed> element.
       
    65         self.assertEqual(feed.getAttribute(u'xmlns:geo'), u'http://www.w3.org/2003/01/geo/wgs84_pos#')
       
    66         chan = feed.getElementsByTagName('channel')[0]
       
    67         items = chan.getElementsByTagName('item')
       
    68         self.assertEqual(len(items), City.objects.count())
       
    69 
       
    70         # Ensuring the geo:lat and geo:lon element was added to each item in the feed.
       
    71         for item in items:
       
    72             self.assertChildNodes(item, ['title', 'link', 'description', 'guid', 'geo:lat', 'geo:lon'])
       
    73 
       
    74         # Boxes and Polygons aren't allowed in W3C Geo feeds.
       
    75         self.assertRaises(ValueError, self.client.get, '/geoapp/feeds/w3cgeo2/') # Box in <channel>
       
    76         self.assertRaises(ValueError, self.client.get, '/geoapp/feeds/w3cgeo3/') # Polygons in <entry>