test_feeds.py 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from __future__ import unicode_literals
  2. from unittest import skipUnless
  3. from xml.dom import minidom
  4. from django.conf import settings
  5. from django.contrib.sites.models import Site
  6. from django.contrib.gis.geos import HAS_GEOS
  7. from django.contrib.gis.tests.utils import HAS_SPATIAL_DB
  8. from django.test import TestCase, modify_settings
  9. if HAS_GEOS:
  10. from .models import City
  11. @modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'})
  12. @skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.")
  13. class GeoFeedTest(TestCase):
  14. urls = 'django.contrib.gis.tests.geoapp.urls'
  15. def setUp(self):
  16. Site(id=settings.SITE_ID, domain="example.com", name="example.com").save()
  17. def assertChildNodes(self, elem, expected):
  18. "Taken from syndication/tests.py."
  19. actual = set(n.nodeName for n in elem.childNodes)
  20. expected = set(expected)
  21. self.assertEqual(actual, expected)
  22. def test_geofeed_rss(self):
  23. "Tests geographic feeds using GeoRSS over RSSv2."
  24. # Uses `GEOSGeometry` in `item_geometry`
  25. doc1 = minidom.parseString(self.client.get('/feeds/rss1/').content)
  26. # Uses a 2-tuple in `item_geometry`
  27. doc2 = minidom.parseString(self.client.get('/feeds/rss2/').content)
  28. feed1, feed2 = doc1.firstChild, doc2.firstChild
  29. # Making sure the box got added to the second GeoRSS feed.
  30. self.assertChildNodes(feed2.getElementsByTagName('channel')[0],
  31. ['title', 'link', 'description', 'language',
  32. 'lastBuildDate', 'item', 'georss:box', 'atom:link']
  33. )
  34. # Incrementing through the feeds.
  35. for feed in [feed1, feed2]:
  36. # Ensuring the georss namespace was added to the <rss> element.
  37. self.assertEqual(feed.getAttribute('xmlns:georss'), 'http://www.georss.org/georss')
  38. chan = feed.getElementsByTagName('channel')[0]
  39. items = chan.getElementsByTagName('item')
  40. self.assertEqual(len(items), City.objects.count())
  41. # Ensuring the georss element was added to each item in the feed.
  42. for item in items:
  43. self.assertChildNodes(item, ['title', 'link', 'description', 'guid', 'georss:point'])
  44. def test_geofeed_atom(self):
  45. "Testing geographic feeds using GeoRSS over Atom."
  46. doc1 = minidom.parseString(self.client.get('/feeds/atom1/').content)
  47. doc2 = minidom.parseString(self.client.get('/feeds/atom2/').content)
  48. feed1, feed2 = doc1.firstChild, doc2.firstChild
  49. # Making sure the box got added to the second GeoRSS feed.
  50. 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('xmlns:georss'), 'http://www.georss.org/georss')
  54. entries = feed.getElementsByTagName('entry')
  55. self.assertEqual(len(entries), City.objects.count())
  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. def test_geofeed_w3c(self):
  60. "Testing geographic feeds using W3C Geo."
  61. doc = minidom.parseString(self.client.get('/feeds/w3cgeo1/').content)
  62. feed = doc.firstChild
  63. # Ensuring the geo namespace was added to the <feed> element.
  64. self.assertEqual(feed.getAttribute('xmlns:geo'), 'http://www.w3.org/2003/01/geo/wgs84_pos#')
  65. chan = feed.getElementsByTagName('channel')[0]
  66. items = chan.getElementsByTagName('item')
  67. self.assertEqual(len(items), City.objects.count())
  68. # Ensuring the geo:lat and geo:lon element was added to each item in the feed.
  69. for item in items:
  70. self.assertChildNodes(item, ['title', 'link', 'description', 'guid', 'geo:lat', 'geo:lon'])
  71. # Boxes and Polygons aren't allowed in W3C Geo feeds.
  72. self.assertRaises(ValueError, self.client.get, '/feeds/w3cgeo2/') # Box in <channel>
  73. self.assertRaises(ValueError, self.client.get, '/feeds/w3cgeo3/') # Polygons in <entry>