def test_update_items_download_arg_true(self): feed_content = ''' <rss version="2.0"> <channel> <title>Fake Feed</title> <item> <guid isPermaLink="false">/ep123.mp3</guid> <enclosure url="http://example.com/ep123.mp3" type="audio/mpeg" /> </item> </channel> </rss> ''' tree = etree.fromstring(feed_content).find('channel') channel = PodcastChannel.objects.create(url='http://example.com') self.mock.StubOutWithMock(PodcastItem, 'download_file') PodcastItem.download_file() self.mock.StubOutWithMock(model_logger, 'info') model_logger.info('Found 1 new items') self.mock.ReplayAll() channel.update_items(tree, download=True) self.mock.VerifyAll() item = channel.podcast_items.get() self.assertEqual(channel.podcast_items.count(), 1) self.assertEqual(item.guid, '/ep123.mp3')
def test_update_items_partial_item(self): feed_content = ''' <rss version="2.0"> <channel> <title>Fake Feed</title> <item> <guid isPermaLink="false">/ep123.mp3</guid> </item> </channel> </rss> ''' tree = etree.fromstring(feed_content).find('channel') channel = PodcastChannel.objects.create(url='http://example.com') self.mock.StubOutWithMock(PodcastItem, 'download_file') self.mock.StubOutWithMock(model_logger, 'info') model_logger.info('Found 1 new items') self.mock.ReplayAll() channel.update_items(tree) self.mock.VerifyAll() item = channel.podcast_items.get() self.assertEqual(channel.podcast_items.count(), 1) self.assertEqual(item.guid, '/ep123.mp3') self.assertEqual(item.title, '') self.assertEqual(item.description, '') self.assertEqual(item.author, '') self.assertEqual(item.publish_date, None) self.assertEqual(item.url, '') self.assertEqual(item.file_type, '')
def test_update_items_existing_item(self): feed_content = ''' <rss version="2.0"> <channel> <title>Fake Feed</title> <item> <title>Episode 123</title> <description>Another episode</description> <author>[email protected] (Jim Bob)</author> <pubDate>Mon, 25 Feb 2013 07:10:42 -0000</pubDate> <guid isPermaLink="false">/ep123.mp3</guid> <enclosure url="http://example.com/ep123.mp3" type="audio/mpeg" /> </item> </channel> </rss> ''' tree = etree.fromstring(feed_content).find('channel') channel = PodcastChannel.objects.create(url='http://example.com') channel.podcast_items.create(guid='/ep123.mp3', title='Old title') self.mock.StubOutWithMock(PodcastItem, 'download_file') self.mock.StubOutWithMock(model_logger, 'info') model_logger.info('Found 0 new items') self.mock.ReplayAll() channel.update_items(tree) self.mock.VerifyAll() item = channel.podcast_items.get() self.assertEqual(item.title, 'Old title')
def test_update_items_new_item(self): feed_content = ''' <rss version="2.0"> <channel> <title>Fake Feed</title> <item> <title>Episode 123</title> <description>Another episode</description> <author>[email protected] (Jim Bob)</author> <link>http://example.com/ep123/</link> <image><url>http://example.com/feed.jpg</url></image> <pubDate>Mon, 25 Feb 2013 07:10:42 -0000</pubDate> <guid isPermaLink="false">/ep123.mp3</guid> <enclosure url="http://example.com/ep123.mp3" type="audio/mpeg" /> </item> </channel> </rss> ''' tree = etree.fromstring(feed_content).find('channel') channel = PodcastChannel.objects.create(url='http://example.com') self.mock.StubOutWithMock(PodcastItem, 'download_file') self.mock.StubOutWithMock(model_logger, 'info') model_logger.info('Found 1 new items') self.mock.StubOutWithMock(PodcastChannel, 'parse_cover_url') PodcastChannel.parse_cover_url(mox.IsA(etree._Element)).AndReturn( 'http://example.com/feed.jpg') self.mock.ReplayAll() channel.update_items(tree) self.mock.VerifyAll() item = channel.podcast_items.get() self.assertEqual(channel.podcast_items.count(), 1) self.assertEqual(item.guid, '/ep123.mp3') self.assertEqual(item.title, 'Episode 123') self.assertEqual(item.description, 'Another episode') self.assertEqual(item.author, '[email protected] (Jim Bob)') self.assertEqual(item.link, 'http://example.com/ep123/') self.assertEqual(item.publish_date, datetime(2013, 2, 25, 7, 10, 42, tzinfo=timezone.utc)) self.assertEqual(item.url, 'http://example.com/ep123.mp3') self.assertEqual(item.file_type, 'audio/mpeg') self.assertEqual(item.cover_url, 'http://example.com/feed.jpg')
def test_update_channel(self): feed_content = ''' <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> <channel> <title>Fake Feed</title> <link>http://example.com</link> <description>This is a sample feed.</description> <copyright>All rights reserved.</copyright> <pubDate>Sun, 02 Sep 2012 18:17:35 -0700</pubDate> <category>Sample stuff</category> <category>News</category> <image> <url>http://example.com/feed.jpg</url> </image> <item><title>Episode 123</title></item> </channel> </rss> ''' feed_response = Struct(status_code=200, ok=True, content=feed_content, url='http://test.com') self.mock.StubOutWithMock(requests, 'get') requests.get('http://test.com').AndReturn(feed_response) self.mock.StubOutWithMock(PodcastChannel, 'parse_cover_url') PodcastChannel.parse_cover_url(mox.IsA( etree._Element)).AndReturn('http://example.com/feed.jpg') self.mock.StubOutWithMock(PodcastChannel, 'save') PodcastChannel.save() self.mock.StubOutWithMock(PodcastChannel, 'update_items') PodcastChannel.update_items(mox.IsA(etree._Element), download=True) self.mock.StubOutWithMock(model_logger, 'info') model_logger.info('Updating Channel: Old Title') channel = PodcastChannel(url='http://test.com', title='Old Title') self.mock.ReplayAll() channel.update_channel(download=True) self.mock.VerifyAll() self.assertEqual(channel.title, 'Fake Feed') self.assertEqual(channel.description, 'This is a sample feed.') self.assertEqual(channel.cover_url, 'http://example.com/feed.jpg') self.assertEqual(channel.website, 'http://example.com') self.assertEqual(channel.copyright, 'All rights reserved.')
def test_update_items_new_item(self): feed_content = ''' <rss version="2.0"> <channel> <title>Fake Feed</title> <item> <title>Episode 123</title> <description>Another episode</description> <author>[email protected] (Jim Bob)</author> <link>http://example.com/ep123/</link> <image><url>http://example.com/feed.jpg</url></image> <pubDate>Mon, 25 Feb 2013 07:10:42 -0000</pubDate> <guid isPermaLink="false">/ep123.mp3</guid> <enclosure url="http://example.com/ep123.mp3" type="audio/mpeg" /> </item> </channel> </rss> ''' tree = etree.fromstring(feed_content).find('channel') channel = PodcastChannel.objects.create(url='http://example.com') self.mock.StubOutWithMock(PodcastItem, 'download_file') self.mock.StubOutWithMock(model_logger, 'info') model_logger.info('Found 1 new items') self.mock.StubOutWithMock(PodcastChannel, 'parse_cover_url') PodcastChannel.parse_cover_url(mox.IsA( etree._Element)).AndReturn('http://example.com/feed.jpg') self.mock.ReplayAll() channel.update_items(tree) self.mock.VerifyAll() item = channel.podcast_items.get() self.assertEqual(channel.podcast_items.count(), 1) self.assertEqual(item.guid, '/ep123.mp3') self.assertEqual(item.title, 'Episode 123') self.assertEqual(item.description, 'Another episode') self.assertEqual(item.author, '[email protected] (Jim Bob)') self.assertEqual(item.link, 'http://example.com/ep123/') self.assertEqual(item.publish_date, datetime(2013, 2, 25, 7, 10, 42, tzinfo=timezone.utc)) self.assertEqual(item.url, 'http://example.com/ep123.mp3') self.assertEqual(item.file_type, 'audio/mpeg') self.assertEqual(item.cover_url, 'http://example.com/feed.jpg')
def test_update_channel(self): feed_content = ''' <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> <channel> <title>Fake Feed</title> <link>http://example.com</link> <description>This is a sample feed.</description> <copyright>All rights reserved.</copyright> <pubDate>Sun, 02 Sep 2012 18:17:35 -0700</pubDate> <category>Sample stuff</category> <category>News</category> <image> <url>http://example.com/feed.jpg</url> </image> <item><title>Episode 123</title></item> </channel> </rss> ''' feed_response = Struct( status_code=200, ok=True, content=feed_content, url='http://test.com') self.mock.StubOutWithMock(requests, 'get') requests.get('http://test.com').AndReturn(feed_response) self.mock.StubOutWithMock(PodcastChannel, 'parse_cover_url') PodcastChannel.parse_cover_url(mox.IsA(etree._Element)).AndReturn( 'http://example.com/feed.jpg') self.mock.StubOutWithMock(PodcastChannel, 'save') PodcastChannel.save() self.mock.StubOutWithMock(PodcastChannel, 'update_items') PodcastChannel.update_items(mox.IsA(etree._Element), download=True) self.mock.StubOutWithMock(model_logger, 'info') model_logger.info('Updating Channel: Old Title') channel = PodcastChannel(url='http://test.com', title='Old Title') self.mock.ReplayAll() channel.update_channel(download=True) self.mock.VerifyAll() self.assertEqual(channel.title, 'Fake Feed') self.assertEqual(channel.description, 'This is a sample feed.') self.assertEqual(channel.cover_url, 'http://example.com/feed.jpg') self.assertEqual(channel.website, 'http://example.com') self.assertEqual(channel.copyright, 'All rights reserved.')
def test_update_channel_bad_response(self): feed_response = Struct(status_code=404, ok=False, reason='404') self.mock.StubOutWithMock(requests, 'get') requests.get('http://test.com').AndReturn(feed_response) self.mock.StubOutWithMock(model_logger, 'info') model_logger.info('Updating Channel: http://test.com') self.mock.StubOutWithMock(model_logger, 'error') model_logger.error('Failed to retrieve feed. Status 404') self.mock.StubOutWithMock(PodcastChannel, 'parse_cover_url') self.mock.StubOutWithMock(PodcastChannel, 'save') self.mock.StubOutWithMock(PodcastChannel, 'update_items') channel = PodcastChannel(url='http://test.com') self.mock.ReplayAll() channel.update_channel(download=True) self.mock.VerifyAll() self.assertEqual(channel.title, '') self.assertEqual(channel.description, '') self.assertEqual(channel.cover_url, '')
def test_update_items_empty_feed(self): feed_content = ''' <rss version="2.0"> <channel> <title>Fake Feed</title> </channel> </rss> ''' tree = etree.fromstring(feed_content).find('channel') channel = PodcastChannel.objects.create(url='http://example.com') self.mock.StubOutWithMock(PodcastItem, 'download_file') self.mock.StubOutWithMock(model_logger, 'info') model_logger.info('Found 0 new items') self.mock.ReplayAll() channel.update_items(tree) self.mock.VerifyAll() self.assertEqual(channel.podcast_items.count(), 0)