Skip to content

Commit a9ea35e

Browse files
committed
Add error handling and improve tests with fixures
1 parent d893cff commit a9ea35e

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

github_dlr/source.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def download_content(download_url, output_file):
7070
"""Download a single downloadable file given a download URL."""
7171

7272
resp = requests.get(download_url)
73+
resp.raise_for_status()
7374
resp_content = resp.content
7475

7576
with open(output_file, mode="wb") as file:

tests/download_test.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
import os
2-
import shutil
2+
3+
import pytest
4+
import requests
35

46
from github_dlr.source import download_content
57

68

7-
def test_download_content_succes(requests_mock):
9+
def test_download_content_succes(requests_mock, tmp_path):
810
download_url = "https://github.com/AnimeChan/animechan/tree/main/client/public"
911
mock_content = b"This is an image content"
1012

1113
requests_mock.get(download_url, content=mock_content)
1214

13-
# Create a temp dir `tmp` to store the test files
14-
output_dir = "tmp"
15-
output_file = os.path.join(output_dir, "foo.txt")
16-
os.makedirs(output_dir)
15+
# Use tmp_path fixure to create temp file path
16+
output_file = os.path.join(tmp_path, "foo.txt")
17+
print(output_file)
1718

1819
# Download the file content and save it locally
1920
download_content(download_url, output_file)
2021

21-
try:
22-
# Verify the file was written correctly
23-
with open(output_file, "rb") as file:
24-
assert file.read() == mock_content
25-
finally:
26-
if os.path.exists(output_file):
27-
shutil.rmtree(output_dir)
22+
# Verify the file was written correctly
23+
with open(output_file, "rb") as file:
24+
assert file.read() == mock_content
25+
26+
27+
def test_download_content_failure(requests_mock, tmp_path):
28+
download_url = "https://github.com/AnimeChan/animechan/tree/main/client/public"
29+
30+
requests_mock.get(download_url, status_code=404)
31+
32+
# Create a temp dir `tmp` to store the test files
33+
output_file = os.path.join(tmp_path, "foo.txt")
34+
print(output_file)
35+
36+
with pytest.raises(requests.exceptions.RequestException):
37+
download_content(download_url, output_file)

0 commit comments

Comments
 (0)