diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/conftest.py | 28 | ||||
| -rw-r--r-- | tests/test_dl.py | 69 |
2 files changed, 37 insertions, 60 deletions
diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..94fc053 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,28 @@ +# wikiget - CLI tool for downloading files from Wikimedia sites +# Copyright (C) 2023 Cody Logan +# SPDX-License-Identifier: GPL-3.0-or-later +# +# Wikiget is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Wikiget is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Wikiget. If not, see <https://www.gnu.org/licenses/>. + +import pytest +import requests_mock as rm + + +@pytest.fixture +def mock_get(requests_mock: rm.Mocker) -> None: + # fake the download request for File:Example.jpg + requests_mock.get( + "https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg", + text="data", + ) diff --git a/tests/test_dl.py b/tests/test_dl.py index cfcefef..d39c352 100644 --- a/tests/test_dl.py +++ b/tests/test_dl.py @@ -21,7 +21,6 @@ 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 @@ -282,10 +281,11 @@ class TestBatchDownload: assert errors == 1 +@pytest.mark.usefixtures("mock_get") class TestDownload: @pytest.fixture - def mock_file(self) -> File: - file = File("Example.jpg") + def mock_file(self, tmp_path: Path) -> File: + file = File(name="Example.jpg", dest=str(tmp_path / "Example.jpg")) file.image = Mock() file.image.exists = True file.image.imageinfo = { @@ -298,23 +298,9 @@ class TestDownload: file.image.site.connection = requests.Session() return file - def test_download( - self, - mock_file: File, - requests_mock: rm.Mocker, - tmp_path: Path, - caplog: pytest.LogCaptureFixture, - ) -> None: + def test_download(self, mock_file: File, caplog: pytest.LogCaptureFixture) -> None: caplog.set_level(logging.INFO) - # 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 = "d01b79a6781c72ac9bfff93e5e2cfbeef4efc840" args = parse_args(["File:Example.jpg"]) @@ -351,22 +337,11 @@ class TestDownload: assert errors == 0 def test_download_with_output( - self, - mock_file: File, - requests_mock: rm.Mocker, - tmp_path: Path, - caplog: pytest.LogCaptureFixture, + self, mock_file: File, caplog: pytest.LogCaptureFixture ) -> None: caplog.set_level(logging.INFO) - # 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 + tmp_file = mock_file.dest with patch("wikiget.dl.verify_hash") as mock_verify_hash: mock_verify_hash.return_value = "d01b79a6781c72ac9bfff93e5e2cfbeef4efc840" @@ -401,10 +376,8 @@ class TestDownload: assert errors == 0 def test_download_os_error( - self, mock_file: File, tmp_path: Path, caplog: pytest.LogCaptureFixture + self, mock_file: File, caplog: pytest.LogCaptureFixture ) -> None: - mock_file.dest = str(tmp_path / "Example.jpg") - with patch("wikiget.dl.open") as mock_open: mock_open.side_effect = OSError("write error") args = parse_args(["File:Example.jpg"]) @@ -420,20 +393,8 @@ 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, + self, mock_file: File, 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"]) @@ -449,20 +410,8 @@ class TestDownload: assert errors == 1 def test_download_verify_hash_mismatch( - self, - mock_file: File, - requests_mock: rm.Mocker, - tmp_path: Path, - caplog: pytest.LogCaptureFixture, + self, mock_file: File, 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"]) |
