def test_delete_podcast_from_channel(): new_podcast = new_podcast_factory() channel = channel_factory(known_podcasts=[new_podcast]) radio = radio_factory(channels=[channel]) channel_id = get_channel_id(channel) podcast_id = get_podcast_id(new_podcast) not_podcast_id = "123" assert podcast_id != not_podcast_id def first_podcast_on_radio_type(radio): assert len(radio.channels) == 1 assert len(radio.channels[0].known_podcasts) == 1 return type(radio.channels[0].known_podcasts[0].status).__name__ with mock.patch('podcast.delete._delete_podcast_file'): result, _ = delete_podcast(radio, channel_id, not_podcast_id) assert first_podcast_on_radio_type(result) == 'NewStatus' with mock.patch('podcast.delete._delete_podcast_file'): result, _ = delete_podcast(radio, channel_id, podcast_id) assert first_podcast_on_radio_type(result) == 'DeletedStatus'
def test_unmerged_podcasts_from_feed(): channel = channel_factory(known_podcasts=[]) new_podcast = new_podcast_factory() assert channel.known_podcasts == [] with mock.patch('podcast.update.unmerged_podcasts_from_feed', return_value=[new_podcast]): channel = update_channel(channel) assert channel.known_podcasts == [new_podcast]
def test_smoke(): channel = channel_factory() # Now save some fake data with tempfile.TemporaryDirectory() as tmpdirname: save_known_podcasts(RadioDirectory(tmpdirname), channel) result = load_known_podcasts( RadioDirectory(tmpdirname), channel.channel_info) # Now, check that loading the channel gives us the expected results assert result == channel.known_podcasts
def test_smoke(monkeypatch): # Create a channel with 3 unmerged podcasts channel = channel_factory() def mock_podcast_from_feed(_): with open('tests/data/test_feed', 'rb') as f: return _podcasts_from_feed(f) monkeypatch.setattr(podcast.feeds, '_podcasts_from_feed', mock_podcast_from_feed) actual = unmerged_podcasts_from_feed(channel, 2) assert actual == EXPECTED_FROM_TEST_FEED
def test_download_channel_fail(): channel = channel_factory(known_podcasts=[requested_podcast_factory()]) with mock.patch( 'podcast.download._download_from_url', return_value=False) as mock_download_podcast: new_channel = download_channel( RadioDirectory('tmp'), channel) assert len(mock_download_podcast.mock_calls) == 1 assert channel == new_channel assert get_types(channel) == get_types(new_channel)
def test_merge_podcast(): # Create a channel with 3 unmerged podcasts channel = channel_factory(known_podcasts=[]) unmerged_podcasts = [ unmerged_podcast_factory(), unmerged_podcast_factory(), unmerged_podcast_factory(), ] podcasts = merge_podcasts(channel, unmerged_podcasts) assert len(podcasts) == 1 assert type(podcasts[0].status).__name__ == 'RequestedStatus'
def test_download_channel_none_requested(): channel = channel_factory(known_podcasts=[ new_podcast_factory(), new_podcast_factory(), new_podcast_factory(), ]) with mock.patch( 'podcast.download.download_podcast') as mock_download_podcast: new_channel = download_channel( RadioDirectory('tmp'), channel) assert len(mock_download_podcast.mock_calls) == 0 assert channel == new_channel assert get_types(channel) == get_types(new_channel)
def test_delete_podcast(): directory = RadioDirectory('tmp') podcast = new_podcast_factory() channel = channel_factory(known_podcasts=[podcast]) radio = radio_factory( directory=directory, channels=[channel], ) channel_id = get_channel_id(channel) podcast_id = get_podcast_id(podcast) with mock.patch('podcast.delete._delete_podcast_file') as mock_delete: delete_podcast(radio, channel_id, podcast_id) mock_delete.mock_calls == [mock.call( directory, channel, podcast, )]
def test_download_channel_success(): channel = channel_factory(known_podcasts=[requested_podcast_factory()]) with mock.patch( 'podcast.download._download_from_url', return_value=True) as mock_download_podcast: new_channel = download_channel( RadioDirectory('tmp'), channel) assert len(mock_download_podcast.mock_calls) == 1 expected = channel._replace( known_podcasts=[ channel.known_podcasts[0]._replace(status=NewStatus()) ]) assert channel == expected assert get_types(new_channel) == get_types(expected) # Let's test the tests assert get_types(new_channel) != get_types(channel)
def test_download_location(): podcast_data = Podcast( status=RequestedStatus(), data=podcast_data_factory( audio_link={ 'length': u'0', 'href': u'http://feed.thisamericanlife.org/~r/talpodcast/~5/R0qvREKxypU/597.mp3', # noqa 'type': u'audio/mpeg', 'rel': u'enclosure', })) channel = channel_factory() actual = download_location( RadioDirectory('dir'), channel, podcast_data) expected = 'dir/{0}/597.mp3'.format(DEFAULT_CHANNEL_INFO_DIRECTORY) assert actual == expected
def test_move_to_trash(): channel = channel_factory() new_podcast = new_podcast_factory() with tempfile.TemporaryDirectory() as tmpdirname: filename = download_location(RadioDirectory(tmpdirname), channel, new_podcast) trash_filename = trash_location(RadioDirectory(tmpdirname), channel, new_podcast) os.makedirs(os.path.dirname(filename)) open(filename, 'a').close() assert os.path.exists(filename) assert not os.path.exists(trash_filename) assert _delete_podcast_file(RadioDirectory(tmpdirname), channel, new_podcast) assert not os.path.exists(filename) assert os.path.exists(trash_filename)
def test_missing_file(): channel = channel_factory() new_podcast = new_podcast_factory() with tempfile.TemporaryDirectory() as tmpdirname: assert not _delete_podcast_file(RadioDirectory(tmpdirname), channel, new_podcast)
def test_save_to_missing_directory(): channel = channel_factory() with tempfile.TemporaryDirectory() as tmpdirname: save_known_podcasts(RadioDirectory(tmpdirname), channel)