diff options
| author | Cody Logan <cody@lokken.dev> | 2023-11-16 12:01:17 -0800 |
|---|---|---|
| committer | Cody Logan <cody@lokken.dev> | 2023-11-16 12:01:17 -0800 |
| commit | 6178c170d88434937d28026fe592629bd967681e (patch) | |
| tree | 0f373e36661ccaf4e38e81ccc37042a688f767cd | |
| parent | 5e7e0b84c632641ba12041b2aa516b4a27245de6 (diff) | |
| download | wikiget-6178c170d88434937d28026fe592629bd967681e.tar.gz wikiget-6178c170d88434937d28026fe592629bd967681e.zip | |
Code cleanup; reorganize some tests
| -rw-r--r-- | src/wikiget/dl.py | 10 | ||||
| -rw-r--r-- | src/wikiget/version.py | 2 | ||||
| -rw-r--r-- | tests/test_dl.py | 75 |
3 files changed, 58 insertions, 29 deletions
diff --git a/src/wikiget/dl.py b/src/wikiget/dl.py index 9b53d66..c84935b 100644 --- a/src/wikiget/dl.py +++ b/src/wikiget/dl.py @@ -15,10 +15,12 @@ # You should have received a copy of the GNU General Public License # along with Wikiget. If not, see <https://www.gnu.org/licenses/>. +from __future__ import annotations + import logging import sys -from argparse import Namespace from concurrent.futures import ThreadPoolExecutor +from typing import TYPE_CHECKING from mwclient import APIError, InvalidResponse, LoginError, Site from requests import ConnectionError, HTTPError @@ -27,11 +29,15 @@ from tqdm import tqdm import wikiget from wikiget.client import connect_to_site, query_api from wikiget.exceptions import ParseError -from wikiget.file import File from wikiget.logging import FileLogAdapter from wikiget.parse import get_dest, read_batch_file from wikiget.validations import verify_hash +if TYPE_CHECKING: + from argparse import Namespace + + from wikiget.file import File + logger = logging.getLogger(__name__) diff --git a/src/wikiget/version.py b/src/wikiget/version.py index 804c60f..479f200 100644 --- a/src/wikiget/version.py +++ b/src/wikiget/version.py @@ -15,4 +15,4 @@ # You should have received a copy of the GNU General Public License # along with Wikiget. If not, see <https://www.gnu.org/licenses/>. -__version__ = "0.8.0" +__version__ = "0.8.0.dev0" diff --git a/tests/test_dl.py b/tests/test_dl.py index 10f5fd5..a15f397 100644 --- a/tests/test_dl.py +++ b/tests/test_dl.py @@ -17,8 +17,10 @@ """Define tests related to the wikiget.dl module.""" +from __future__ import annotations + import logging -from pathlib import Path +from typing import TYPE_CHECKING from unittest.mock import MagicMock, Mock, patch import pytest @@ -30,6 +32,9 @@ from wikiget.exceptions import ParseError from wikiget.file import File from wikiget.wikiget import parse_args +if TYPE_CHECKING: + from pathlib import Path + class TestPrepDownload: """Define tests related to wikiget.dl.prep_download.""" @@ -348,7 +353,13 @@ class TestDownload: file.image.site.connection = requests.Session() return file - def test_download(self, mock_file: File, caplog: pytest.LogCaptureFixture) -> None: + @patch("wikiget.dl.verify_hash") + def test_download( + self, + mock_verify_hash: MagicMock, + mock_file: File, + caplog: pytest.LogCaptureFixture, + ) -> None: """Test that the correct log messages are created when downloading a file. There should be a series of info-level messages containing the filename, size, @@ -357,10 +368,10 @@ class TestDownload: """ caplog.set_level(logging.INFO) - with patch("wikiget.dl.verify_hash") as mock_verify_hash: - mock_verify_hash.return_value = "d01b79a6781c72ac9bfff93e5e2cfbeef4efc840" - args = parse_args(["File:Example.jpg"]) - errors = download(mock_file, args) + mock_verify_hash.return_value = "d01b79a6781c72ac9bfff93e5e2cfbeef4efc840" + + args = parse_args(["File:Example.jpg"]) + errors = download(mock_file, args) assert caplog.record_tuples == [ ( @@ -392,8 +403,12 @@ class TestDownload: ] assert errors == 0 + @patch("wikiget.dl.verify_hash") def test_download_with_output( - self, mock_file: File, caplog: pytest.LogCaptureFixture + self, + mock_verify_hash: MagicMock, + mock_file: File, + caplog: pytest.LogCaptureFixture, ) -> None: """Test that the correct log messages are created when downloading a file. @@ -402,11 +417,10 @@ class TestDownload: caplog.set_level(logging.INFO) tmp_file = mock_file.dest + mock_verify_hash.return_value = "d01b79a6781c72ac9bfff93e5e2cfbeef4efc840" - with patch("wikiget.dl.verify_hash") as mock_verify_hash: - mock_verify_hash.return_value = "d01b79a6781c72ac9bfff93e5e2cfbeef4efc840" - args = parse_args(["-o", str(tmp_file), "File:Example.jpg"]) - errors = download(mock_file, args) + args = parse_args(["-o", str(tmp_file), "File:Example.jpg"]) + errors = download(mock_file, args) assert caplog.record_tuples[0] == ( "wikiget.dl", @@ -436,18 +450,19 @@ class TestDownload: ] assert errors == 0 + @patch("pathlib.Path.open") def test_download_os_error( - self, mock_file: File, caplog: pytest.LogCaptureFixture + self, mock_open: MagicMock, mock_file: File, caplog: pytest.LogCaptureFixture ) -> None: """Test what happens when an OSError is raised during download. If the downloaded file cannot be created, an error log message should be created with details on the exception. """ - with patch("pathlib.Path.open") as mock_open: - mock_open.side_effect = OSError("write error") - args = parse_args(["File:Example.jpg"]) - errors = download(mock_file, args) + mock_open.side_effect = OSError("write error") + + args = parse_args(["File:Example.jpg"]) + errors = download(mock_file, args) assert caplog.record_tuples == [ ( @@ -458,18 +473,22 @@ class TestDownload: ] assert errors == 1 + @patch("wikiget.dl.verify_hash") def test_download_verify_os_error( - self, mock_file: File, caplog: pytest.LogCaptureFixture + self, + mock_verify_hash: MagicMock, + mock_file: File, + caplog: pytest.LogCaptureFixture, ) -> None: """Test what happens when an OSError is raised during verification. If the downloaded file cannot be read in order to calculate its hash, an error log message should be created with details on the exception. """ - 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) + mock_verify_hash.side_effect = OSError("read error") + + args = parse_args(["File:Example.jpg"]) + errors = download(mock_file, args) assert caplog.record_tuples == [ ( @@ -480,17 +499,21 @@ class TestDownload: ] assert errors == 1 + @patch("wikiget.dl.verify_hash") def test_download_verify_hash_mismatch( - self, mock_file: File, caplog: pytest.LogCaptureFixture + self, + mock_verify_hash: MagicMock, + mock_file: File, + caplog: pytest.LogCaptureFixture, ) -> None: """Test what happens when the downloaded file hash and server hash don't match. An error log message should be created if there's a hash mismatch. """ - 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) + mock_verify_hash.return_value = "mismatch" + + args = parse_args(["File:Example.jpg"]) + errors = download(mock_file, args) assert caplog.record_tuples == [ ( |
