Skip to content

Commit a071af9

Browse files
committed
test(fix): main integration test now use fixtures properly
1 parent 3dbe514 commit a071af9

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

tests/conftest.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
from pathlib import Path
3+
from typing import Final
34
from unittest.mock import AsyncMock, create_autospec
45

56
import pytest
@@ -8,7 +9,10 @@
89
from async_rutube_downloader.downloader import Downloader
910
from async_rutube_downloader.utils.type_hints import APIResponseDict
1011

11-
RUTUBE_LINK = "https://rutube.ru/video/2ce725b3dc1a243f8456458975ecd872/"
12+
RUTUBE_LINK: Final[str] = (
13+
"https://rutube.ru/video/2ce725b3dc1a243f8456458975ecd872/"
14+
)
15+
RUTUBE_ID: Final[str] = "365ae8f40a2ffd2a5901ace4db799de7"
1216
API_RESPONSE_FIXTURE = Path("tests/fixtures/api_response_fixture.json")
1317
MASTER_PLAYLIST_FIXTURE = Path("tests/fixtures/master_playlist_fixture.m3u8")
1418
VIDEO_FILE_PLAYLIST_FIXTURE = Path(
@@ -33,12 +37,6 @@ def get_response_mock(mocked_session: AsyncMock) -> AsyncMock:
3337
return get_response_mock
3438

3539

36-
@pytest.fixture(scope="session")
37-
def url() -> str:
38-
"""Real Rutube URL."""
39-
return RUTUBE_LINK
40-
41-
4240
@pytest.fixture(scope="session")
4341
def api_response_fixture() -> APIResponseDict:
4442
"""Real json from some Rutube video."""
@@ -64,10 +62,14 @@ def downloader(
6462
get_response_mock: AsyncMock,
6563
api_response_fixture: APIResponseDict,
6664
master_playlist_fixture: str,
65+
video_file_playlist_fixture: str,
6766
) -> Downloader:
6867
"""Downloader object Fixture with ClientSession and session.get mocked."""
6968
get_response_mock.json.return_value = api_response_fixture
70-
get_response_mock.text.return_value = master_playlist_fixture
69+
get_response_mock.text.side_effect = (
70+
master_playlist_fixture,
71+
video_file_playlist_fixture,
72+
)
7173
downloader = Downloader(RUTUBE_LINK, session=mocked_session)
7274
return downloader
7375

@@ -76,7 +78,7 @@ def downloader(
7678
def cli_single_url_fixture(monkeypatch: pytest.MonkeyPatch) -> None:
7779
argv = [
7880
"async_rutube_downloader",
79-
"365ae8f40a2ffd2a5901ace4db799de7",
81+
RUTUBE_ID,
8082
]
8183
monkeypatch.setattr("sys.argv", argv)
8284

tests/test_downloader.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
from unittest.mock import AsyncMock, patch
55

66
import aiofiles
7-
import m3u8
87
import pytest
98
from m3u8 import M3U8
109

1110
from async_rutube_downloader.downloader import Downloader
1211
from async_rutube_downloader.settings import (
1312
TEST_VIDEO_ID,
1413
URL_FOR_ID_TEMPLATE,
14+
VIDEO_FORMAT,
1515
FULL_HD_1080p,
1616
)
1717
from async_rutube_downloader.utils.exceptions import (
@@ -20,30 +20,27 @@
2020
QualityError,
2121
)
2222
from async_rutube_downloader.utils.type_hints import APIResponseDict, Qualities
23+
from tests.conftest import RUTUBE_LINK
2324
from tests.test_utils import validate_qualities
2425

2526
# There is few protected methods calls, through mangled names,
2627
# it's not a good practice.
2728

2829

2930
@pytest.mark.asyncio
30-
async def test_download_video(
31-
downloader: Downloader, video_file_playlist_fixture: str
32-
) -> None:
33-
prev_get_calls = 2 # fetch_video_info and MasterPlaylist
31+
async def test_download_video(downloader: Downloader, tmp_path: Path) -> None:
32+
get_calls = 3
33+
# 3 is _get_api_response, __get_master_playlist, and __get_selected_quality
34+
downloader._upload_directory = tmp_path
3435
await downloader.fetch_video_info()
35-
# FIXME: why is this necessary? autoselect_quality should do it.
36-
downloader._selected_quality = m3u8.loads(
37-
video_file_playlist_fixture, downloader.url
38-
)
3936
with patch.object(aiofiles, "open") as aiofiles_open:
4037
await downloader.download_video()
4138
aiofiles_open.assert_called_once_with(
42-
Path.cwd() / f"{downloader._filename}.mp4", mode="wb"
39+
tmp_path / f"{downloader._filename}.{VIDEO_FORMAT}", mode="wb"
4340
)
4441
assert (
4542
downloader._session.get.call_count # type: ignore
46-
== len(downloader._selected_quality.segments) + prev_get_calls
43+
== len(downloader._selected_quality.segments) + get_calls # type: ignore
4744
)
4845

4946

@@ -72,7 +69,14 @@ async def test_select_quality_raise_error_master_playlist_not_initialized(
7269

7370
@pytest.mark.parametrize(
7471
"value",
75-
[("1920", "1080"), (list(), list()), (dict(), dict()), (object, object)],
72+
[
73+
("1920", "1080"),
74+
(1920.0, 1080.0),
75+
(list(), list()),
76+
(tuple(), tuple()),
77+
(dict(), dict()),
78+
(object, object),
79+
],
7680
)
7781
@pytest.mark.asyncio
7882
async def test_validate_selected_quality(
@@ -139,17 +143,17 @@ async def test_get_api_response(
139143

140144

141145
@pytest.mark.asyncio
142-
async def test_create_downloader(url: str, mocked_session: AsyncMock) -> None:
146+
async def test_create_downloader(mocked_session: AsyncMock) -> None:
143147
"""Create correct Downloader object."""
144148

145149
def dummy_callback(arg: int, arg2: int): ...
146150

147151
loop = asyncio.new_event_loop()
148152

149-
obj = Downloader(url, loop, dummy_callback, session=mocked_session)
153+
obj = Downloader(RUTUBE_LINK, loop, dummy_callback, session=mocked_session)
150154

151155
assert isinstance(obj, Downloader)
152-
assert obj.url == url
156+
assert obj.url == RUTUBE_LINK
153157
assert obj._loop == loop
154158
assert obj._callback == dummy_callback
155159

0 commit comments

Comments
 (0)