From 899ca54fe2a289bee4dfbed9f5ef6fc9024def72 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Mon, 11 Aug 2008 20:49:19 +0000 Subject: [PATCH] Added a couple of extra syndication tests, and generally sanitized the existing ones. Refs #6547. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8310 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/syndication/feeds.py | 2 +- tests/regressiontests/syndication/feeds.py | 23 ++++++++++++++++ .../syndication/fixtures/feeddata.json | 26 +++++++++++++++++++ tests/regressiontests/syndication/models.py | 8 ++++++ tests/regressiontests/syndication/tests.py | 21 +++++++++++++-- tests/regressiontests/syndication/urls.py | 22 ++++++---------- 6 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 tests/regressiontests/syndication/feeds.py create mode 100644 tests/regressiontests/syndication/fixtures/feeddata.json diff --git a/django/contrib/syndication/feeds.py b/django/contrib/syndication/feeds.py index d0e8ac9db7..d4cb42711f 100644 --- a/django/contrib/syndication/feeds.py +++ b/django/contrib/syndication/feeds.py @@ -81,7 +81,7 @@ class Feed(object): current_site = Site.objects.get_current() else: current_site = RequestSite(self.request) - + link = self.__get_dynamic_attr('link', obj) link = add_domain(current_site.domain, link) diff --git a/tests/regressiontests/syndication/feeds.py b/tests/regressiontests/syndication/feeds.py new file mode 100644 index 0000000000..3c5d5a51b5 --- /dev/null +++ b/tests/regressiontests/syndication/feeds.py @@ -0,0 +1,23 @@ +from django.core.exceptions import ObjectDoesNotExist +from django.contrib.syndication import feeds +from django.utils.feedgenerator import Atom1Feed + +class ComplexFeed(feeds.Feed): + def get_object(self, bits): + if len(bits) != 1: + raise ObjectDoesNotExist + return None + +class TestRssFeed(feeds.Feed): + link = "/blog/" + title = 'My blog' + + def items(self): + from models import Entry + return Entry.objects.all() + + def item_link(self, item): + return "/blog/%s/" % item.pk + +class TestAtomFeed(TestRssFeed): + feed_type = Atom1Feed diff --git a/tests/regressiontests/syndication/fixtures/feeddata.json b/tests/regressiontests/syndication/fixtures/feeddata.json new file mode 100644 index 0000000000..de752f66bf --- /dev/null +++ b/tests/regressiontests/syndication/fixtures/feeddata.json @@ -0,0 +1,26 @@ +[ + { + "model": "syndication.entry", + "pk": 1, + "fields": { + "title": "My first entry", + "date": "2008-01-01 12:30:00" + } + }, + { + "model": "syndication.entry", + "pk": 2, + "fields": { + "title": "My second entry", + "date": "2008-01-02 12:30:00" + } + }, + { + "model": "syndication.entry", + "pk": 3, + "fields": { + "title": "My third entry", + "date": "2008-01-02 13:30:00" + } + } +] \ No newline at end of file diff --git a/tests/regressiontests/syndication/models.py b/tests/regressiontests/syndication/models.py index e69de29bb2..99e14ade8a 100644 --- a/tests/regressiontests/syndication/models.py +++ b/tests/regressiontests/syndication/models.py @@ -0,0 +1,8 @@ +from django.db import models + +class Entry(models.Model): + title = models.CharField(max_length=200) + date = models.DateTimeField() + + def __unicode__(self): + return self.title \ No newline at end of file diff --git a/tests/regressiontests/syndication/tests.py b/tests/regressiontests/syndication/tests.py index 6a9dd643d7..142cf47d03 100644 --- a/tests/regressiontests/syndication/tests.py +++ b/tests/regressiontests/syndication/tests.py @@ -1,14 +1,31 @@ # -*- coding: utf-8 -*- +from xml.dom import minidom from django.test import TestCase from django.test.client import Client +from models import Entry class SyndicationFeedTest(TestCase): + fixtures = ['feeddata.json'] + + def test_rss_feed(self): + response = self.client.get('/syndication/feeds/rss/') + doc = minidom.parseString(response.content) + self.assertEqual(len(doc.getElementsByTagName('channel')), 1) + self.assertEqual(len(doc.getElementsByTagName('item')), Entry.objects.count()) + + def test_atom_feed(self): + response = self.client.get('/syndication/feeds/atom/') + doc = minidom.parseString(response.content) + self.assertEqual(len(doc.getElementsByTagName('feed')), 1) + self.assertEqual(len(doc.getElementsByTagName('entry')), Entry.objects.count()) + def test_complex_base_url(self): """ Tests that that the base url for a complex feed doesn't raise a 500 exception. """ - c = Client() - response = c.get('/syndication/feeds/complex/') + response = self.client.get('/syndication/feeds/complex/') self.assertEquals(response.status_code, 404) + + diff --git a/tests/regressiontests/syndication/urls.py b/tests/regressiontests/syndication/urls.py index 24644ffd53..ce3b5056ba 100644 --- a/tests/regressiontests/syndication/urls.py +++ b/tests/regressiontests/syndication/urls.py @@ -1,18 +1,12 @@ +from feeds import TestRssFeed, TestAtomFeed, ComplexFeed from django.conf.urls.defaults import patterns -from django.core.exceptions import ObjectDoesNotExist -from django.contrib.syndication import feeds - - -class ComplexFeed(feeds.Feed): - def get_object(self, bits): - if len(bits) != 1: - raise ObjectDoesNotExist - return None - +feed_dict = { + 'complex': ComplexFeed, + 'rss': TestRssFeed, + 'atom': TestAtomFeed, + +} urlpatterns = patterns('', - (r'^feeds/(?P.*)/$', 'django.contrib.syndication.views.feed', { - 'feed_dict': dict( - complex = ComplexFeed, - )}), + (r'^feeds/(?P.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feed_dict}) )