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
This commit is contained in:
parent
efaa891b1f
commit
899ca54fe2
|
@ -81,7 +81,7 @@ class Feed(object):
|
||||||
current_site = Site.objects.get_current()
|
current_site = Site.objects.get_current()
|
||||||
else:
|
else:
|
||||||
current_site = RequestSite(self.request)
|
current_site = RequestSite(self.request)
|
||||||
|
|
||||||
link = self.__get_dynamic_attr('link', obj)
|
link = self.__get_dynamic_attr('link', obj)
|
||||||
link = add_domain(current_site.domain, link)
|
link = add_domain(current_site.domain, link)
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
|
@ -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
|
|
@ -1,14 +1,31 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from xml.dom import minidom
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.test.client import Client
|
from django.test.client import Client
|
||||||
|
from models import Entry
|
||||||
|
|
||||||
class SyndicationFeedTest(TestCase):
|
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):
|
def test_complex_base_url(self):
|
||||||
"""
|
"""
|
||||||
Tests that that the base url for a complex feed doesn't raise a 500
|
Tests that that the base url for a complex feed doesn't raise a 500
|
||||||
exception.
|
exception.
|
||||||
"""
|
"""
|
||||||
c = Client()
|
response = self.client.get('/syndication/feeds/complex/')
|
||||||
response = c.get('/syndication/feeds/complex/')
|
|
||||||
self.assertEquals(response.status_code, 404)
|
self.assertEquals(response.status_code, 404)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,12 @@
|
||||||
|
from feeds import TestRssFeed, TestAtomFeed, ComplexFeed
|
||||||
from django.conf.urls.defaults import patterns
|
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('',
|
urlpatterns = patterns('',
|
||||||
(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {
|
(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feed_dict})
|
||||||
'feed_dict': dict(
|
|
||||||
complex = ComplexFeed,
|
|
||||||
)}),
|
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue