diff options
| author | Cody Logan <cody@lokken.dev> | 2023-11-03 10:40:07 -0700 |
|---|---|---|
| committer | Cody Logan <cody@lokken.dev> | 2023-11-03 10:40:07 -0700 |
| commit | 08a5907bd8b34e2f99a0c74e6756c66134ceb7d3 (patch) | |
| tree | 62465f3adaae1c9cca0734f79931783407d8d854 /tests/test_dl.py | |
| parent | bc5d19c8150bf7960839243ceeb6f62a9df54e18 (diff) | |
| download | wikiget-08a5907bd8b34e2f99a0c74e6756c66134ceb7d3.tar.gz wikiget-08a5907bd8b34e2f99a0c74e6756c66134ceb7d3.zip | |
Move from unittest.mock to pytest's monkeypatch where feasible
Diffstat (limited to 'tests/test_dl.py')
| -rw-r--r-- | tests/test_dl.py | 115 |
1 files changed, 60 insertions, 55 deletions
diff --git a/tests/test_dl.py b/tests/test_dl.py index a57c3c1..297c59d 100644 --- a/tests/test_dl.py +++ b/tests/test_dl.py @@ -16,7 +16,6 @@ # along with Wikiget. If not, see <https://www.gnu.org/licenses/>. from pathlib import Path -from unittest.mock import MagicMock, patch import pytest @@ -25,13 +24,11 @@ from wikiget.file import File from wikiget.wikiget import parse_args -# TODO: don't hit the actual API when doing tests class TestPrepDownload: + # TODO: don't hit the actual API when doing tests @pytest.mark.skip(reason="skip tests that query a live API") def test_prep_download(self) -> None: - """ - The prep_download function should create a file object. - """ + """The prep_download function should create a file object.""" args = parse_args(["File:Example.jpg"]) file = prep_download(args.FILE, args) assert file is not None @@ -49,60 +46,68 @@ class TestPrepDownload: class TestProcessDownload: - @patch("wikiget.dl.batch_download") - def test_batch_download(self, mock_batch_download: MagicMock) -> None: - """ - A successful batch download should not return any errors. - """ - args = parse_args(["-a", "batch.txt"]) - mock_batch_download.return_value = 0 - exit_code = process_download(args) - assert mock_batch_download.called - assert exit_code == 0 + def test_batch_download(self, monkeypatch: pytest.MonkeyPatch) -> None: + """A successful batch download should not return any errors.""" + + def mock_batch_download(*args, **kwargs): # noqa: ARG001 + return 0 + + with monkeypatch.context() as m: + m.setattr("wikiget.dl.batch_download", mock_batch_download) + + args = parse_args(["-a", "batch.txt"]) + exit_code = process_download(args) + assert exit_code == 0 - @patch("wikiget.dl.batch_download") def test_batch_download_with_errors( - self, mock_batch_download: MagicMock, caplog: pytest.LogCaptureFixture + self, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture ) -> None: """ Any errors during batch download should create a log message containing the number of errors and result in a non-zero exit code. """ - args = parse_args(["-a", "batch.txt"]) - mock_batch_download.return_value = 4 - exit_code = process_download(args) - assert mock_batch_download.called - assert exit_code == 1 - assert "4 problems encountered during batch processing" in caplog.text - - @patch("wikiget.dl.prep_download") - @patch("wikiget.dl.download") - def test_single_download( - self, mock_download: MagicMock, mock_prep_download: MagicMock - ) -> None: - """ - A successful download should not return any errors. - """ - args = parse_args(["File:Example.jpg"]) - mock_download.return_value = 0 - mock_prep_download.return_value = MagicMock(File) - exit_code = process_download(args) - assert mock_prep_download.called - assert mock_download.called - assert exit_code == 0 - - @patch("wikiget.dl.prep_download") - @patch("wikiget.dl.download") - def test_single_download_with_errors( - self, mock_download: MagicMock, mock_prep_download: MagicMock - ) -> None: - """ - Any errors during download should result in a non-zero exit code. - """ - args = parse_args(["File:Example.jpg"]) - mock_download.return_value = 1 - mock_prep_download.return_value = MagicMock(File) - exit_code = process_download(args) - assert mock_prep_download.called - assert mock_download.called - assert exit_code == 1 + + def mock_batch_download(*args, **kwargs): # noqa: ARG001 + return 4 + + with monkeypatch.context() as m: + m.setattr("wikiget.dl.batch_download", mock_batch_download) + + args = parse_args(["-a", "batch.txt"]) + exit_code = process_download(args) + assert exit_code == 1 + assert "4 problems encountered during batch processing" in caplog.text + + def test_single_download(self, monkeypatch: pytest.MonkeyPatch) -> None: + """A successful download should not return any errors.""" + + def mock_download(*args, **kwargs): # noqa: ARG001 + return 0 + + def mock_prep_download(*args, **kwargs): # noqa ARG001 + return File("Example.jpg") + + with monkeypatch.context() as m: + m.setattr("wikiget.dl.download", mock_download) + m.setattr("wikiget.dl.prep_download", mock_prep_download) + + args = parse_args(["File:Example.jpg"]) + exit_code = process_download(args) + assert exit_code == 0 + + def test_single_download_with_errors(self, monkeypatch: pytest.MonkeyPatch) -> None: + """Any errors during download should result in a non-zero exit code.""" + + def mock_download(*args, **kwargs): # noqa: ARG001 + return 1 + + def mock_prep_download(*args, **kwargs): # noqa ARG001 + return File("Example.jpg") + + with monkeypatch.context() as m: + m.setattr("wikiget.dl.download", mock_download) + m.setattr("wikiget.dl.prep_download", mock_prep_download) + + args = parse_args(["File:Example.jpg"]) + exit_code = process_download(args) + assert exit_code == 1 |
