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_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.')