def test_returns_folder_from_base_dir(self, tmp_path: PosixPath, mocker: MockerFixture) -> None: mocker.patch.object(settings, "BASE_DIR", str(tmp_path)) mocker.patch.object(settings, "MEDIA_FOLDER", str(tmp_path)) folder_name: str = "s0med1r" (tmp_path / folder_name).mkdir(exist_ok=True) result: str = _change_and_move_parent_folder(f"/{folder_name}") assert result != f"/{folder_name}" assert Path(result).name == _hash(folder_name) assert not (tmp_path / folder_name).exists() assert (tmp_path / _hash(folder_name)).exists()
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"]))
def test_copy_mp4_to_new_hash_mp4(tmp_path: PosixPath) -> None: video: PosixPath = tmp_path / "video test name.mkv" video.touch() _copy_mp4_to_new_hash_mp4(video) assert (tmp_path / (_hash(video.name) + ".mp4")).exists()
def test_returns_folder_under_media_folder(self, tmp_path: PosixPath, mocker: MockerFixture) -> None: mocker.patch("panel.tasks.torrent.Client", MockClient) mocker.patch.object(settings, "MEDIA_FOLDER", str(tmp_path)) _torrents: List[Dict[str, Any]] = copy.deepcopy(TORRENTS) torrent_folder: PosixPath = tmp_path / "new folder" torrent_folder.mkdir(exist_ok=True) (tmp_path / _hash(torrent_folder.name)).mkdir(exist_ok=True) movie_torrent: MovieTorrent = MovieTorrentFactory(id=1) _torrents[0][ "content_path"] = "/some/nonexist/folder/" + torrent_folder.name mocker.patch.object(panel.tasks.tests.mocks, "TORRENTS", _torrents) mocker.patch("panel.tasks.torrent.Client", MockClient) result: str = _get_root_path(movie_torrent.movie_content.id) assert result == str(tmp_path / _hash(torrent_folder.name))
def test_without_delete_original(self, tmp_path: PosixPath, mocker: MockerFixture) -> None: run = mocker.patch("panel.tasks.torrent.subprocess.run", autospec=True) run.return_value = MockSubprocess(stdout=b'{"stream":[]}') video1: PosixPath = tmp_path / "video.mkv" video2: PosixPath = tmp_path / "test.mp4" video1.touch() video2.touch() result: List[VideoDetail] = _process_videos({video1, video2}) assert len(result) == 2 assert result[0].source_file_name in {video1.name, video2.name} assert result[1].source_file_name in {video1.name, video2.name} assert result[1].file_name in {_hash(video1.name), _hash(video2.name)} assert video1.exists() assert video2.exists() assert (tmp_path / (_hash(video2.name) + ".mp4")).exists()
def test_get_video_detail(tmp_path: PosixPath, mocker: MockerFixture) -> None: run = mocker.patch("panel.tasks.torrent.subprocess.run", autospec=True) raw: Dict[str, str] = {"stream": "test"} run.return_value = MockSubprocess(stdout=json.dumps(raw).encode("UTF-8")) video: PosixPath = tmp_path / "video.mp4" video.touch() result: VideoDetail = _get_video_detail(video) assert result.full_path == str(video.parent / (_hash(video.name) + ".mp4")) assert result.file_name == _hash(video.name) assert result.file_extension == ".mp4" assert result.source_file_name == video.name assert result.source_file_extension == video.suffix assert result.width == 0 assert result.height == 0 assert result.duration == 0 assert result.raw == raw
def test_returns_new_folder(self, tmp_path: PosixPath, mocker: MockerFixture) -> None: mocker.patch.object(settings, "MEDIA_FOLDER", str(tmp_path)) old_folder: PosixPath = tmp_path / "old_folder" old_folder.mkdir(exist_ok=True) result: str = _change_and_move_parent_folder(str(old_folder)) assert Path(result).is_dir() assert Path(result).name == _hash("old_folder") assert not old_folder.exists()
def test_with_delete_original(self, tmp_path: PosixPath, mocker: MockerFixture) -> None: run = mocker.patch("panel.tasks.torrent.subprocess.run", autospec=True) run.return_value = MockSubprocess(stdout=b'{"stream":[]}') video1: PosixPath = tmp_path / "video.mkv" video2: PosixPath = tmp_path / "test.mp4" video1.touch() video2.touch() _process_videos({video1, video2}, delete_original=True) assert not video1.exists() assert not video2.exists() assert (tmp_path / (_hash(video2.name) + ".mp4")).exists()
def test_convert_video_to_mp4(tmp_path: PosixPath, mocker: MockerFixture) -> None: mocker.patch("panel.tasks.torrent.subprocess.run") video_file: PosixPath = tmp_path / "video.mp4" video_file.touch() new_video_path: Path = video_file.parent / (_hash(video_file.name) + ".mp4") _convert_video_to_mp4(video_file) subprocess.run.assert_called_once_with( ["ffmpeg", "-i", str(video_file), "-c", "copy", str(new_video_path)], stderr=-2, stdout=-1, )
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, )