diff options
| author | Cody Logan <cody@lokken.dev> | 2023-11-07 10:36:25 -0800 |
|---|---|---|
| committer | Cody Logan <cody@lokken.dev> | 2023-11-07 10:36:25 -0800 |
| commit | 749e5afaa1d85d9cf39cf96deb9b980a741f72da (patch) | |
| tree | 9b657e2a5dcf943fcf7167c947a0e4573046d271 | |
| parent | af02ca9a73807a2e59249e6c80af57d39d444e69 (diff) | |
| download | wikiget-749e5afaa1d85d9cf39cf96deb9b980a741f72da.tar.gz wikiget-749e5afaa1d85d9cf39cf96deb9b980a741f72da.zip | |
Full coverage on dl module
| -rw-r--r-- | pyproject.toml | 1 | ||||
| -rw-r--r-- | tests/test_dl.py | 91 |
2 files changed, 85 insertions, 7 deletions
diff --git a/pyproject.toml b/pyproject.toml index 45c1493..9c88c2e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -200,5 +200,6 @@ module = [ "mwclient", "mwclient.image", "pytest", + "requests_mock", ] ignore_missing_imports = true diff --git a/tests/test_dl.py b/tests/test_dl.py index 31964d9..cfcefef 100644 --- a/tests/test_dl.py +++ b/tests/test_dl.py @@ -21,6 +21,7 @@ from unittest.mock import MagicMock, Mock, patch import pytest import requests +import requests_mock as rm from mwclient import Site from wikiget.dl import batch_download, download, prep_download, process_download @@ -300,7 +301,7 @@ class TestDownload: def test_download( self, mock_file: File, - requests_mock, + requests_mock: rm.Mocker, tmp_path: Path, caplog: pytest.LogCaptureFixture, ) -> None: @@ -350,20 +351,38 @@ class TestDownload: assert errors == 0 def test_download_with_output( - self, mock_file: File, caplog: pytest.LogCaptureFixture + self, + mock_file: File, + requests_mock: rm.Mocker, + tmp_path: Path, + caplog: pytest.LogCaptureFixture, ) -> None: caplog.set_level(logging.INFO) - mock_file.dest = "output.jpg" - # TODO: remove dry run option from this test - args = parse_args(["-n", "-o", "output.jpg", "File:Example.jpg"]) - errors = download(mock_file, args) + # fake the download request + requests_mock.get( + "https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg", + text="data", + ) + # save to a temp directory + tmp_file = str(tmp_path / "Example.jpg") + mock_file.dest = tmp_file + + with patch("wikiget.dl.verify_hash") as mock_verify_hash: + mock_verify_hash.return_value = "d01b79a6781c72ac9bfff93e5e2cfbeef4efc840" + args = parse_args(["-o", tmp_file, "File:Example.jpg"]) + errors = download(mock_file, args) assert caplog.record_tuples[0] == ( "wikiget.dl", logging.INFO, "[Example.jpg] Downloading 'Example.jpg' (9022 bytes) from " - "commons.wikimedia.org to 'output.jpg'", + f"commons.wikimedia.org to '{tmp_file}'", + ) + assert caplog.record_tuples[5] == ( + "wikiget.dl", + logging.INFO, + f"[Example.jpg] 'Example.jpg' downloaded to '{tmp_file}'", ) assert errors == 0 @@ -400,6 +419,64 @@ class TestDownload: ] assert errors == 1 + def test_download_verify_os_error( + self, + mock_file: File, + requests_mock: rm.Mocker, + tmp_path: Path, + caplog: pytest.LogCaptureFixture, + ) -> None: + # fake the download request + requests_mock.get( + "https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg", + text="data", + ) + # save to a temp directory + mock_file.dest = str(tmp_path / "Example.jpg") + + with patch("wikiget.dl.verify_hash") as mock_verify_hash: + mock_verify_hash.side_effect = OSError("read error") + args = parse_args(["File:Example.jpg"]) + errors = download(mock_file, args) + + assert caplog.record_tuples == [ + ( + "wikiget.dl", + logging.ERROR, + "[Example.jpg] File downloaded but could not be verified: read error", + ) + ] + assert errors == 1 + + def test_download_verify_hash_mismatch( + self, + mock_file: File, + requests_mock: rm.Mocker, + tmp_path: Path, + caplog: pytest.LogCaptureFixture, + ) -> None: + # fake the download request + requests_mock.get( + "https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg", + text="data", + ) + # save to a temp directory + mock_file.dest = str(tmp_path / "Example.jpg") + + with patch("wikiget.dl.verify_hash") as mock_verify_hash: + mock_verify_hash.return_value = "mismatch" + args = parse_args(["File:Example.jpg"]) + errors = download(mock_file, args) + + assert caplog.record_tuples == [ + ( + "wikiget.dl", + logging.ERROR, + "[Example.jpg] Hash mismatch! Downloaded file may be corrupt.", + ) + ] + assert errors == 1 + def test_download_nonexistent_file( self, mock_file: File, caplog: pytest.LogCaptureFixture ) -> None: |
