Beispiel #1
0
def test_generate_rss__ok(db_objects, podcast, episode_data, mocked_s3):

    new_episode_data = {
        **episode_data,
        **{
            "source_id": generate_video_id(),
            "status": "new"
        },
    }
    episode_new: Episode = Episode.create(**new_episode_data)

    new_episode_data = {
        **episode_data,
        **{
            "source_id": generate_video_id(),
            "status": "downloading"
        },
    }
    episode_downloading: Episode = Episode.create(**new_episode_data)

    new_episode_data = {
        **episode_data,
        **{
            "source_id": generate_video_id(),
            "status": "published",
            "published_at": datetime.utcnow(),
        },
    }
    episode_published: Episode = Episode.create(**new_episode_data)

    rss_path = generate_rss(podcast.id)

    mocked_s3.upload_file.assert_called_with(
        rss_path,
        f"{podcast.publish_id}.xml",
        remote_path=settings.S3_BUCKET_RSS_PATH)

    with open(rss_path) as file:
        generated_rss_content = file.read()

    assert episode_published.title in generated_rss_content
    assert episode_published.description in generated_rss_content
    assert episode_published.file_name in generated_rss_content

    assert episode_new.source_id not in generated_rss_content
    assert episode_downloading.source_id not in generated_rss_content

    os.remove(rss_path)
Beispiel #2
0
def test_download_sound__episode_downloaded__file_correct__ignore_downloading__ok(
    generate_rss_mock,
    db_objects,
    podcast,
    episode_data,
    mocked_youtube: MockYoutube,
    mocked_s3: MockS3Client,
):
    new_episode_data = {
        **episode_data,
        **{
            "status": "published",
            "source_id": mocked_youtube.video_id,
            "watch_url": mocked_youtube.watch_url,
            "file_size": 1024,
        },
    }
    episode: Episode = Episode.create(**new_episode_data)
    mocked_s3.get_file_size.return_value = episode.file_size
    generate_rss_mock.return_value = f"file_{episode.source_id}.mp3"
    result = download_episode(episode.watch_url, episode.id)

    with db_objects.allow_sync():
        updated_episode: Episode = Episode.select().where(
            Episode.id == episode.id).first()

    generate_rss_mock.assert_called_with(episode.podcast_id)
    assert result == EPISODE_DOWNLOADING_IGNORED
    assert not mocked_youtube.download.called
    assert updated_episode.status == "published"
    assert updated_episode.published_at == updated_episode.created_at
Beispiel #3
0
def test_download_sound__youtube_exception__download_rollback(
    download_audio_mock,
    db_objects,
    podcast,
    episode_data,
    mocked_youtube: MockYoutube,
    mocked_s3: MockS3Client,
):
    new_episode_data = {
        **episode_data,
        **{
            "status": "new",
            "source_id": mocked_youtube.video_id,
            "watch_url": mocked_youtube.watch_url,
            "file_size": 1024,
        },
    }
    episode: Episode = Episode.create(**new_episode_data)

    download_audio_mock.side_effect = YoutubeException(
        "Youtube video is not available")
    result = download_episode(episode.watch_url, episode.id)

    with db_objects.allow_sync():
        updated_episode: Episode = Episode.select().where(
            Episode.id == episode.id).first()

    download_audio_mock.assert_called_with(episode.watch_url,
                                           episode.file_name)

    assert result == EPISODE_DOWNLOADING_ERROR
    assert updated_episode.status == "new"
    assert updated_episode.published_at is None
Beispiel #4
0
def test_download_sound__episode_new__correct_downloading(
    download_audio_mock,
    generate_rss_mock,
    db_objects,
    podcast,
    episode_data,
    mocked_youtube: MockYoutube,
    mocked_s3: MockS3Client,
    mocked_ffmpeg: Mock,
):
    new_episode_data = {
        **episode_data,
        **{
            "status": "new",
            "source_id": mocked_youtube.video_id,
            "watch_url": mocked_youtube.watch_url,
            "file_size": 1024,
        },
    }
    episode: Episode = Episode.create(**new_episode_data)

    download_audio_mock.return_value = episode.file_name
    generate_rss_mock.return_value = f"file_{episode.source_id}.mp3"
    result = download_episode(episode.watch_url, episode.id)

    with db_objects.allow_sync():
        updated_episode: Episode = Episode.select().where(
            Episode.id == episode.id).first()

    generate_rss_mock.assert_called_with(episode.podcast_id)
    download_audio_mock.assert_called_with(episode.watch_url,
                                           episode.file_name)
    mocked_ffmpeg.assert_called_with(episode.file_name)

    assert result == EPISODE_DOWNLOADING_OK
    assert updated_episode.status == "published"
    assert updated_episode.published_at == updated_episode.created_at
Beispiel #5
0
def episode(db_objects, episode_data):
    with db_objects.allow_sync():
        yield Episode.create(**episode_data)