def test_subscribe(self): self.mock.StubOutWithMock(PodcastChannel, 'update_channel') PodcastChannel.update_channel() self.mock.ReplayAll() sub_channel = PodcastChannel.subscribe('http://test.com') self.mock.VerifyAll() self.assertEqual(sub_channel.url, 'http://test.com')
def test_save_model(self): channel = PodcastChannel() self.mock.StubOutWithMock(channel, "save") self.mock.StubOutWithMock(channel, "update_channel") channel.save() channel.update_channel() self.mock.ReplayAll() self.admin.save_model(self.request, channel, None, None) self.mock.VerifyAll()
def test_subscribe(self): self.mock.StubOutWithMock(PodcastChannel, 'subscribe') PodcastChannel.subscribe('newurl.com') self.mock.ReplayAll() call_command('podcast', 'newurl.com', subscribe=True, stdout=self.stdout, stderr=self.stderr) self.mock.VerifyAll() self.assertEqual(len(self.stderr.lines), 0) self.assertEqual(len(self.stdout.lines), 0)
def test_unsubscribe(self): self.mock.StubOutWithMock(PodcastChannel, 'delete') PodcastChannel.delete() self.mock.ReplayAll() call_command('podcast', 'test-url', unsubscribe=True, stdout=self.stdout, stderr=self.stderr) self.mock.VerifyAll() self.assertEqual(len(self.stderr.lines), 0) self.assertEqual(len(self.stdout.lines), 1) self.assertEqual( self.stdout.lines[0], '"Test url" unsubscribed and deleted.\n')
def test_subscribe_failed_request(self): self.mock.StubOutWithMock(PodcastChannel, 'subscribe') PodcastChannel.subscribe('newurl.com').AndRaise( requests.exceptions.RequestException) self.mock.ReplayAll() call_command('podcast', 'newurl.com', subscribe=True, stdout=self.stdout, stderr=self.stderr) self.mock.VerifyAll() self.assertEqual(len(self.stderr.lines), 1) self.assertEqual(len(self.stdout.lines), 0) self.assertEqual( self.stderr.lines[0], '"newurl.com" is not a valid url. Skipping.\n')
def test_unsubscribe(self): self.mock.StubOutWithMock(PodcastChannel, 'delete') PodcastChannel.delete() self.mock.ReplayAll() call_command('podcast', 'test-url', unsubscribe=True, stdout=self.stdout, stderr=self.stderr) self.mock.VerifyAll() self.assertEqual(len(self.stderr.lines), 0) self.assertEqual(len(self.stdout.lines), 1) self.assertEqual(self.stdout.lines[0], '"Test url" unsubscribed and deleted.\n')
def test_subscribe_failed_request(self): self.mock.StubOutWithMock(PodcastChannel, 'subscribe') PodcastChannel.subscribe('newurl.com').AndRaise( requests.exceptions.RequestException) self.mock.ReplayAll() call_command('podcast', 'newurl.com', subscribe=True, stdout=self.stdout, stderr=self.stderr) self.mock.VerifyAll() self.assertEqual(len(self.stderr.lines), 1) self.assertEqual(len(self.stdout.lines), 0) self.assertEqual(self.stderr.lines[0], '"newurl.com" is not a valid url. Skipping.\n')
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_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_channels(self): channel1 = PodcastChannel() channel2 = PodcastChannel() qs = [channel1, channel2] self.mock.StubOutWithMock(channel1, 'update_channel') self.mock.StubOutWithMock(channel2, 'update_channel') channel1.update_channel() channel2.update_channel() self.mock.ReplayAll() self.admin.update_channels(self.request, qs) self.mock.VerifyAll()
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_parse_cover_url_media_thumbnail(self): feed_content = ''' <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/"> <channel> <title>Fake Feed</title> <media:thumbnail url="http://example.com/feed.jpg" /> <item><title>Episode 123</title></item> </channel> </rss> ''' tree = etree.fromstring(feed_content).find('channel') self.assertEqual(PodcastChannel().parse_cover_url(tree), 'http://example.com/feed.jpg')
def test_parse_cover_url_itunes_image(self): feed_content = ''' <rss version="2.0" xmlns:itunes="http://www.itunes.com/podcast-1.0"> <channel> <title>Fake Feed</title> <itunes:image href="http://example.com/feed.jpg" /> <item><title>Episode 123</title></item> </channel> </rss> ''' tree = etree.fromstring(feed_content).find('channel') self.assertEqual(PodcastChannel().parse_cover_url(tree), 'http://example.com/feed.jpg')
def test_save_model(self): channel = PodcastChannel() self.mock.StubOutWithMock(channel, 'save') self.mock.StubOutWithMock(channel, 'update_channel') channel.save() channel.update_channel() self.mock.ReplayAll() self.admin.save_model(self.request, channel, None, None) self.mock.VerifyAll()
def test_parse_cover_url_image_tage(self): feed_content = ''' <rss version="2.0"> <channel> <title>Fake Feed</title> <image> <url>http://example.com/feed.jpg</url> </image> <item><title>Episode 123</title></item> </channel> </rss> ''' tree = etree.fromstring(feed_content).find('channel') self.assertEqual(PodcastChannel().parse_cover_url(tree), 'http://example.com/feed.jpg')
def test_update_channels(self): channel1 = PodcastChannel() channel2 = PodcastChannel() qs = [channel1, channel2] self.mock.StubOutWithMock(channel1, "update_channel") self.mock.StubOutWithMock(channel2, "update_channel") channel1.update_channel() channel2.update_channel() self.mock.ReplayAll() self.admin.update_channels(self.request, qs) self.mock.VerifyAll()
def test_unicode(self): channel = PodcastChannel(url='http://example.com', title='Fake Feed') self.assertEqual(unicode(channel), u'Fake Feed')
def subscribe(self, *args, **options): for url in args: try: PodcastChannel.subscribe(url) except RequestException: self.stderr.write('"%s" is not a valid url. Skipping.' % url)
def test_unicode(self): channel = PodcastChannel(title='Fake Feed') item = PodcastItem(channel=channel, title='Episode 123') self.assertEqual(unicode(item), 'Fake Feed - Episode 123')
def test_unicode_wo_title(self): channel = PodcastChannel(url='http://example.com') self.assertEqual(unicode(channel), u'http://example.com')