Skip to content

Commit

Permalink
Increase test coverage of download.py
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit74 committed Feb 9, 2024
1 parent a2f4650 commit b588a79
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ check = [
]
test = [
"pytest==8.0.0",
"pytest-mock==3.12.0",
"coverage==7.4.1",
]
dev = [
Expand Down
83 changes: 82 additions & 1 deletion tests/download/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_no_output_supplied(valid_http_url):
stream_file(url=valid_http_url)


def test_first_block_download(valid_http_url):
def test_first_block_download_default_session(valid_http_url):
byte_stream = io.BytesIO()
size, ret = stream_file(
url=valid_http_url, byte_stream=byte_stream, only_first_block=True
Expand All @@ -70,6 +70,29 @@ def test_first_block_download(valid_http_url):
assert len(byte_stream.read()) <= expected


def test_first_block_download_custom_session(mocker, valid_http_url):
byte_stream = io.BytesIO()
custom_session = mocker.Mock(spec=requests.Session)

expected_response = requests.Response()
expected_response.status_code = 200
expected_response.raw = io.BytesIO(b"Whatever\n")
custom_session.get.return_value = expected_response

mocker.patch("requests.Session")
stream_file(
url=valid_http_url,
byte_stream=byte_stream,
only_first_block=True,
session=custom_session,
)
# check that custom session has been used
custom_session.get.assert_called_once_with(
valid_http_url, stream=True, proxies=None, headers=None
)
requests.Session.assert_not_called() # pyright: ignore


@pytest.mark.slow
def test_user_agent():
ua = "zimscraperlib-test"
Expand Down Expand Up @@ -190,3 +213,61 @@ def test_youtube_download_contextmanager(tmp_path):
)
assert yt_downloader.executor._shutdown
assert tmp_path.joinpath("video.mp4").exists() # videmo doesn't offer webm


@pytest.fixture
def target_dir() -> pathlib.Path:
return pathlib.Path("adir1")


@pytest.fixture
def filepath() -> pathlib.Path:
return pathlib.Path("adir2/afile")


@pytest.fixture
def custom_outtmpl() -> str:
return "custom.%(ext)s"


def test_get_options_target_dir(target_dir):
options = BestWebm.get_options(target_dir=target_dir)
assert options["outtmpl"] == "adir1/video.%(ext)s"


def test_get_options_filepath(filepath):
options = BestWebm.get_options(filepath=filepath)
assert options["outtmpl"] == "adir2/afile"


def test_get_options_target_dir_filepath(target_dir, filepath):
options = BestWebm.get_options(target_dir=target_dir, filepath=filepath)
assert options["outtmpl"] == "adir1/adir2/afile"


def test_get_options_override_outtmpl_no_other_vars(custom_outtmpl):
original = BestWebm.get_options()
overriden = BestWebm.get_options(outtmpl=custom_outtmpl)
assert "outtmpl" in original
assert "outtmpl" in overriden
for key, value in original.items():
if key != "outtmpl":
assert overriden[key] == value
else:
assert overriden[key] == custom_outtmpl


def test_get_options_override_outtmpl_other_vars(target_dir, filepath, custom_outtmpl):
original = BestWebm.get_options(target_dir=target_dir, filepath=filepath)
overriden = BestWebm.get_options(
target_dir=target_dir,
filepath=filepath,
outtmpl=custom_outtmpl,
)
assert "outtmpl" in original
assert "outtmpl" in overriden
for key, value in original.items():
if key != "outtmpl":
assert overriden[key] == value
else:
assert overriden[key] == custom_outtmpl

0 comments on commit b588a79

Please sign in to comment.