2008-08-16 20:40:47 +00:00
|
|
|
from xml.dom import minidom
|
2011-10-17 18:45:22 +00:00
|
|
|
|
2011-03-16 18:22:52 +00:00
|
|
|
from django.conf import settings
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.contrib.sites.models import Site
|
2017-05-04 14:14:35 +00:00
|
|
|
from django.test import TestCase, modify_settings, override_settings
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2015-04-24 15:24:07 +00:00
|
|
|
from .models import City
|
2008-08-16 20:40:47 +00:00
|
|
|
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2013-12-23 09:37:34 +00:00
|
|
|
@modify_settings(INSTALLED_APPS={"append": "django.contrib.sites"})
|
2015-02-10 15:07:44 +00:00
|
|
|
@override_settings(ROOT_URLCONF="gis_tests.geoapp.urls")
|
2010-12-02 05:58:21 +00:00
|
|
|
class GeoFeedTest(TestCase):
|
2014-07-26 17:15:54 +00:00
|
|
|
fixtures = ["initial"]
|
2010-12-02 05:58:21 +00:00
|
|
|
|
2018-11-24 01:59:38 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2011-03-16 18:22:52 +00:00
|
|
|
Site(id=settings.SITE_ID, domain="example.com", name="example.com").save()
|
|
|
|
|
2008-08-16 20:40:47 +00:00
|
|
|
def assertChildNodes(self, elem, expected):
|
2013-03-03 20:03:11 +00:00
|
|
|
"Taken from syndication/tests.py."
|
2017-06-01 23:08:59 +00:00
|
|
|
actual = {n.nodeName for n in elem.childNodes}
|
2008-08-16 20:40:47 +00:00
|
|
|
expected = set(expected)
|
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
|
|
|
|
def test_geofeed_rss(self):
|
|
|
|
"Tests geographic feeds using GeoRSS over RSSv2."
|
|
|
|
# Uses `GEOSGeometry` in `item_geometry`
|
2010-12-02 05:58:21 +00:00
|
|
|
doc1 = minidom.parseString(self.client.get("/feeds/rss1/").content)
|
2008-08-16 20:40:47 +00:00
|
|
|
# Uses a 2-tuple in `item_geometry`
|
2010-12-02 05:58:21 +00:00
|
|
|
doc2 = minidom.parseString(self.client.get("/feeds/rss2/").content)
|
2008-08-16 20:40:47 +00:00
|
|
|
feed1, feed2 = doc1.firstChild, doc2.firstChild
|
|
|
|
|
|
|
|
# Making sure the box got added to the second GeoRSS feed.
|
2010-01-29 18:07:49 +00:00
|
|
|
self.assertChildNodes(
|
|
|
|
feed2.getElementsByTagName("channel")[0],
|
|
|
|
[
|
|
|
|
"title",
|
|
|
|
"link",
|
|
|
|
"description",
|
|
|
|
"language",
|
|
|
|
"lastBuildDate",
|
|
|
|
"item",
|
|
|
|
"georss:box",
|
|
|
|
"atom:link",
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
2008-08-16 20:40:47 +00:00
|
|
|
)
|
2010-01-29 18:07:49 +00:00
|
|
|
|
2008-08-16 20:40:47 +00:00
|
|
|
# Incrementing through the feeds.
|
|
|
|
for feed in [feed1, feed2]:
|
|
|
|
# Ensuring the georss namespace was added to the <rss> element.
|
2013-11-02 19:37:48 +00:00
|
|
|
self.assertEqual(
|
|
|
|
feed.getAttribute("xmlns:georss"), "http://www.georss.org/georss"
|
|
|
|
)
|
2008-08-16 20:40:47 +00:00
|
|
|
chan = feed.getElementsByTagName("channel")[0]
|
|
|
|
items = chan.getElementsByTagName("item")
|
|
|
|
self.assertEqual(len(items), City.objects.count())
|
2010-01-29 18:07:49 +00:00
|
|
|
|
2008-08-16 20:40:47 +00:00
|
|
|
# Ensuring the georss element was added to each item in the feed.
|
|
|
|
for item in items:
|
|
|
|
self.assertChildNodes(
|
|
|
|
item, ["title", "link", "description", "guid", "georss:point"]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_geofeed_atom(self):
|
|
|
|
"Testing geographic feeds using GeoRSS over Atom."
|
2010-12-02 05:58:21 +00:00
|
|
|
doc1 = minidom.parseString(self.client.get("/feeds/atom1/").content)
|
|
|
|
doc2 = minidom.parseString(self.client.get("/feeds/atom2/").content)
|
2008-08-16 20:40:47 +00:00
|
|
|
feed1, feed2 = doc1.firstChild, doc2.firstChild
|
|
|
|
|
|
|
|
# Making sure the box got added to the second GeoRSS feed.
|
2010-01-29 18:07:49 +00:00
|
|
|
self.assertChildNodes(
|
|
|
|
feed2, ["title", "link", "id", "updated", "entry", "georss:box"]
|
|
|
|
)
|
2008-08-16 20:40:47 +00:00
|
|
|
|
|
|
|
for feed in [feed1, feed2]:
|
|
|
|
# Ensuring the georsss namespace was added to the <feed> element.
|
2013-11-02 19:37:48 +00:00
|
|
|
self.assertEqual(
|
|
|
|
feed.getAttribute("xmlns:georss"), "http://www.georss.org/georss"
|
|
|
|
)
|
2008-08-16 20:40:47 +00:00
|
|
|
entries = feed.getElementsByTagName("entry")
|
|
|
|
self.assertEqual(len(entries), City.objects.count())
|
2010-01-29 18:07:49 +00:00
|
|
|
|
2008-08-16 20:40:47 +00:00
|
|
|
# Ensuring the georss element was added to each entry in the feed.
|
|
|
|
for entry in entries:
|
|
|
|
self.assertChildNodes(
|
|
|
|
entry, ["title", "link", "id", "summary", "georss:point"]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_geofeed_w3c(self):
|
|
|
|
"Testing geographic feeds using W3C Geo."
|
2010-12-02 05:58:21 +00:00
|
|
|
doc = minidom.parseString(self.client.get("/feeds/w3cgeo1/").content)
|
2008-08-16 20:40:47 +00:00
|
|
|
feed = doc.firstChild
|
|
|
|
# Ensuring the geo namespace was added to the <feed> element.
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(
|
|
|
|
feed.getAttribute("xmlns:geo"), "http://www.w3.org/2003/01/geo/wgs84_pos#"
|
|
|
|
)
|
2008-08-16 20:40:47 +00:00
|
|
|
chan = feed.getElementsByTagName("channel")[0]
|
|
|
|
items = chan.getElementsByTagName("item")
|
|
|
|
self.assertEqual(len(items), City.objects.count())
|
|
|
|
|
|
|
|
# Ensuring the geo:lat and geo:lon element was added to each item in the feed.
|
|
|
|
for item in items:
|
|
|
|
self.assertChildNodes(
|
|
|
|
item, ["title", "link", "description", "guid", "geo:lat", "geo:lon"]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Boxes and Polygons aren't allowed in W3C Geo feeds.
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ValueError): # Box in <channel>
|
|
|
|
self.client.get("/feeds/w3cgeo2/")
|
|
|
|
with self.assertRaises(ValueError): # Polygons in <entry>
|
|
|
|
self.client.get("/feeds/w3cgeo3/")
|