# HG changeset patch # User Dirk Olmes # Date 1272356555 -7200 # Node ID fd4c8bfa62d6ac09db4ebfa5e89d9ef9cfb83695 # Parent 2da2b691345d9b18762396a21602c1364565a12c FeedUpdater throws an exception if the URL could not be retrieved successfully. Includes unit tests. diff -r 2da2b691345d -r fd4c8bfa62d6 FeedUpdater.py --- a/FeedUpdater.py Tue Apr 27 03:42:08 2010 +0200 +++ b/FeedUpdater.py Tue Apr 27 10:22:35 2010 +0200 @@ -4,6 +4,8 @@ from FeedEntry import FeedEntry import feedparser +STATUS_OK = 200 + def updateAllFeeds(session): allFeeds = session.query(Feed) for feed in allFeeds: @@ -16,10 +18,16 @@ self.feed = feed def update(self): - result = feedparser.parse(self.feed.rss_url) + result = self.getFeed() for entry in result.entries: self.processEntry(entry) + def getFeed(self): + result = feedparser.parse(self.feed.rss_url) + if result["status"] is not STATUS_OK: + raise FeedUpdateException() + return result + def processEntry(self, entry): feedEntry = FeedEntry.findById(entry.id, self.session) if feedEntry is None: @@ -34,3 +42,6 @@ new.summary = entry.summary new.feed = self.feed self.session.add(new) + +class FeedUpdateException(Exception): + pass \ No newline at end of file diff -r 2da2b691345d -r fd4c8bfa62d6 tests/FeedUpdaterTests.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/FeedUpdaterTests.py Tue Apr 27 10:22:35 2010 +0200 @@ -0,0 +1,24 @@ + +import unittest +from Feed import Feed +from FeedUpdater import FeedUpdater +from FeedUpdater import FeedUpdateException + +class FeedUpdaterTests(unittest.TestCase): + def testFeedContainsInvalidUrl(self): + feed = Feed("invalid", "http://thisurldoesnotexist.xxx") + try: + FeedUpdater(None, feed).update() + self.fail("updating an invalid URL must fail") + except FeedUpdateException: + # this one was expected + pass + + def testFeedContainsValidUrl(self): + feed = Feed("valid", "http://www.joelonsoftware.com/rss.xml") + result = FeedUpdater(None, feed).getFeed() + self.assertEqual("Joel on Software", result["feed"].title) + +if __name__ == "__main__": + #import sys;sys.argv = ['', 'Test.testName'] + unittest.main() \ No newline at end of file diff -r 2da2b691345d -r fd4c8bfa62d6 tests/__init__.py