示例#1
0
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_load_config_smoke_test(monkeypatch):
    def mock_load_known_podcasts(_, __):
        return []

    monkeypatch.setattr(podcast.channel_config,
                        'load_known_podcasts', mock_load_known_podcasts)

    actual = load_channel_config(RadioDirectory(
        'directory'), 'tests/data/test_channel_config')
    expected = [
        Channel(
            ChannelInfo(
                name='podcast_name_1',
                url='podcast_url_1',
                directory='podcast_dir_1'),
            known_podcasts=[]),
        Channel(
            ChannelInfo(
                name='podcast_name_2',
                url='podcast_url_2',
                directory='podcast_dir_2'),
            known_podcasts=[]),
    ]

    assert actual == expected
示例#3
0
def test_read_from_empty_file():
    channel_info = channel_info_factory()

    with tempfile.TemporaryDirectory() as tmpdirname:
        result = load_known_podcasts(RadioDirectory(tmpdirname), channel_info)

    assert result == []
示例#4
0
def radio_factory(directory: RadioDirectory = None,
                  channels: typing.List[Channel] = None):
    if channels is None:
        channels = [channel_factory()]

    if directory is None:
        directory = RadioDirectory('tmp')

    return Radio(
        directory=directory,
        channels=channels,
    )
示例#5
0
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)
示例#6
0
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)
示例#7
0
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)
示例#8
0
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,
        )]
示例#9
0
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)
示例#10
0
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
示例#11
0
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)
示例#12
0
def load_radio(directory: RadioDirectory, config: str) -> Radio:

    return Radio(channels=load_channel_config(directory, config),
                 directory=RadioDirectory(directory))
示例#13
0
def test_radio():
    with tempfile.TemporaryDirectory() as tmpdirname:
        radio = radio_factory(RadioDirectory(tmpdirname))
        save_radio(radio)
示例#14
0
def test_save_to_missing_directory():
    channel = channel_factory()

    with tempfile.TemporaryDirectory() as tmpdirname:
        save_known_podcasts(RadioDirectory(tmpdirname), channel)