|
1 | 1 | import os |
2 | | -import shutil |
| 2 | + |
| 3 | +import pytest |
| 4 | +import requests |
3 | 5 |
|
4 | 6 | from github_dlr.source import download_content |
5 | 7 |
|
6 | 8 |
|
7 | | -def test_download_content_succes(requests_mock): |
| 9 | +def test_download_content_succes(requests_mock, tmp_path): |
8 | 10 | download_url = "https://github.com/AnimeChan/animechan/tree/main/client/public" |
9 | 11 | mock_content = b"This is an image content" |
10 | 12 |
|
11 | 13 | requests_mock.get(download_url, content=mock_content) |
12 | 14 |
|
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) |
17 | 18 |
|
18 | 19 | # Download the file content and save it locally |
19 | 20 | download_content(download_url, output_file) |
20 | 21 |
|
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