示例#1
0
    def test_functions_are_called_correctly(self,
                                            mocker: MockerFixture) -> None:
        mocker.patch("panel.tasks.torrent.Client", MockClient)
        mocker.patch("panel.tasks.torrent.check_and_process_torrent.delay")
        mocker.patch("panel.tasks.torrent.download_movie_info.delay")
        remove_category = mocker.spy(panel.tasks.tests.mocks.MockClient,
                                     "remove_category")
        create_category = mocker.spy(panel.tasks.tests.mocks.MockClient,
                                     "create_category")
        mocker.patch("panel.tasks.torrent.check_and_process_torrent.delay")
        mocker.patch("panel.tasks.torrent.download_movie_info.delay")
        download_from_link = mocker.spy(panel.tasks.tests.mocks.MockClient,
                                        "download_from_link")
        movie_content: MovieContent = MovieContentFactory()
        MovieFactory.create(movie_content=[movie_content])

        download_torrent(movie_content_id=movie_content.id)

        remove_category.assert_called_once_with(str(movie_content.id))
        create_category.assert_called_once_with(str(movie_content.id))
        download_from_link.assert_called_once_with(
            movie_content.torrent_source, category=str(movie_content.id))
        panel.tasks.torrent.check_and_process_torrent.delay.assert_called_once(
        )
        panel.tasks.torrent.download_movie_info.delay.assert_called_once()
示例#2
0
    def test_creates_movie_torrent(self, mocker: MockerFixture) -> None:
        mocker.patch("panel.tasks.torrent.Client", MockClient)
        mocker.patch("panel.tasks.torrent.check_and_process_torrent.delay")
        mocker.patch("panel.tasks.torrent.download_movie_info.delay")
        movie_content: MovieContent = MovieContentFactory()
        MovieFactory.create(movie_content=[movie_content])

        assert not MovieTorrent.objects.filter(
            movie_content=movie_content).exists()

        download_torrent(movie_content_id=movie_content.id)

        assert MovieTorrent.objects.filter(
            movie_content=movie_content).exists()
示例#3
0
    def test_updates_movie_content_and_movie(self, mocker: MockerFixture,
                                             tmp_path: PosixPath) -> None:
        mocker.patch.object(settings, "MEDIA_FOLDER", str(tmp_path))
        mocker.patch("panel.tasks.torrent.fetch_subtitles.delay")
        mocker.patch("os.chmod")
        mocker.patch("panel.tasks.torrent.Client", MockClient)
        run = mocker.patch("panel.tasks.torrent.subprocess.run", autospec=True)
        run.return_value = MockSubprocess(
            stdout=json.dumps(RAW_INFO).encode("UTF-8"))
        movie_content: MovieContent = MovieContentFactory()
        movie: Movie = MovieFactory.create(movie_content=[movie_content])
        movie_content.full_path = str(tmp_path)
        movie_content.save()
        video: PosixPath = tmp_path / "video.mkv"
        video.touch()

        process_videos_in_folder(movie_content.id)
        movie_content.refresh_from_db()
        movie.refresh_from_db()
        hashed_title: str = _hash(video.name)

        assert movie_content.full_path == str(
            (tmp_path / (f"{hashed_title}.mp4")))
        assert movie_content.file_name == hashed_title
        assert movie_content.relative_path == f"{hashed_title}.mp4"
        assert movie_content.file_extension == ".mp4"
        assert movie_content.source_file_name == video.name
        assert movie_content.source_file_extension == video.suffix
        assert movie_content.resolution_width == RAW_INFO["streams"][0][
            "width"]
        assert movie_content.resolution_height == RAW_INFO["streams"][0][
            "height"]
        assert movie_content.is_ready is True
        assert movie.duration == int(float(RAW_INFO["streams"][0]["duration"]))
示例#4
0
    def test_fetches_subtitles(
        self, tmp_path: PosixPath, mocker: MockerFixture
    ) -> None:
        mocker.patch("panel.tasks.subtitles.requests", MockRequest)
        mocker.patch("panel.tasks.subtitles.get_hash", lambda x: "somehash")
        mocker.patch(
            "panel.tasks.subtitles._get_response_from_api", _mock_get_response_from_api
        )
        mocker.patch.object(settings, "SUBTITLE_LANGS", "eng,ger")
        subtitle = tmp_path / "test.srt"
        subtitle.touch()
        subtitle_dir: str = "vtt_subtitles"
        movie_content: MovieContent = MovieContentFactory(full_path=str(tmp_path))
        MovieFactory.create(movie_content=[movie_content])
        langs: List[str] = get_subtitle_language()

        assert not movie_content.movie_subtitle.exists()

        fetch_subtitles(
            movie_content_id=movie_content.id, limit=2, delete_original=True
        )

        assert (tmp_path / subtitle_dir).is_dir()
        for lang in langs:
            assert (tmp_path / subtitle_dir / f"{lang}1.vtt").is_file()
            assert (tmp_path / subtitle_dir / f"{lang}2.vtt").is_file()
            assert not (tmp_path / subtitle_dir / f"{lang}1.srt").is_file()
            assert not (tmp_path / subtitle_dir / f"{lang}2.srt").is_file()
            assert not (tmp_path / subtitle_dir / f"{lang}3.vtt").is_file()
            assert not (tmp_path / subtitle_dir / f"{lang}3.srt").is_file()

        assert movie_content.movie_subtitle.count() == 5
        assert movie_content.movie_subtitle.first().full_path == str(
            tmp_path / subtitle_dir / f"{langs[0]}1.vtt"
        )

        assert {i.full_path for i in movie_content.movie_subtitle.all()}.issubset(
            {str(i) for i in (tmp_path / subtitle_dir).iterdir()}
        )

        assert movie_content.movie_subtitle.last().full_path == str(
            tmp_path / subtitle_dir / "org1.vtt"
        )
示例#5
0
    def test_sub_functions_are_called(self, tmp_path: PosixPath,
                                      mocker: MockerFixture) -> None:
        mocker.patch.object(settings, "MEDIA_FOLDER", str(tmp_path))
        mocker.patch("panel.tasks.torrent.fetch_subtitles.delay")
        mocker.patch("os.chmod")
        mocker.patch("panel.tasks.torrent.Client", MockClient)
        get_root_path = mocker.spy(panel.tasks.torrent, "_get_root_path")
        get_videos_from_folder = mocker.spy(panel.tasks.torrent,
                                            "get_videos_from_folder")
        process_videos = mocker.spy(panel.tasks.torrent, "_process_videos")
        run = mocker.patch("panel.tasks.torrent.subprocess.run", autospec=True)
        run.return_value = MockSubprocess(
            stdout=json.dumps(RAW_INFO).encode("UTF-8"))
        movie_content: MovieContent = MovieContentFactory()
        MovieFactory.create(movie_content=[movie_content])
        movie_content.full_path = str(tmp_path)
        movie_content.save()
        video: PosixPath = tmp_path / "video.mkv"
        video.touch()
        hashed_title: str = _hash(video.name)

        process_videos_in_folder(movie_content_id=movie_content.id,
                                 delete_original=True)

        get_root_path.assert_called_once_with(
            movie_content_id=movie_content.id)
        get_videos_from_folder.assert_called_once_with(root_path=str(tmp_path))
        process_videos.assert_called_once_with(videos={video},
                                               delete_original=True)
        os.chmod.assert_called_once_with(
            str((tmp_path / f"{hashed_title}.mp4")), 0o644)
        panel.tasks.torrent.fetch_subtitles.delay.assert_called_once_with(
            movie_content_id=movie_content.id,
            limit=5,
            delete_original=settings.DELETE_ORIGINAL_FILES,
        )
示例#6
0
def movie(movie_content: MovieContent) -> Movie:
    return MovieFactory.create(movie_content=[movie_content], is_ready=True)