aboutsummaryrefslogtreecommitdiff
path: root/tests/test_dl.py
diff options
context:
space:
mode:
authorCody Logan <cody@lokken.dev>2023-11-06 16:52:40 -0800
committerCody Logan <cody@lokken.dev>2023-11-06 16:52:40 -0800
commit8c206d1e3857e17f5fd0f86a1ab6cfbc038c17ae (patch)
tree0a47c07d3608a1cd4d38b1bfc333354b91ee0d5a /tests/test_dl.py
parent3d34b09a361dadb50bb4e4ffa18c75928904c30d (diff)
downloadwikiget-8c206d1e3857e17f5fd0f86a1ab6cfbc038c17ae.tar.gz
wikiget-8c206d1e3857e17f5fd0f86a1ab6cfbc038c17ae.zip
Additional download tests
Diffstat (limited to 'tests/test_dl.py')
-rw-r--r--tests/test_dl.py274
1 files changed, 269 insertions, 5 deletions
diff --git a/tests/test_dl.py b/tests/test_dl.py
index 08cf5b4..331b08f 100644
--- a/tests/test_dl.py
+++ b/tests/test_dl.py
@@ -15,12 +15,15 @@
# You should have received a copy of the GNU General Public License
# along with Wikiget. If not, see <https://www.gnu.org/licenses/>.
+import logging
from pathlib import Path
from unittest.mock import MagicMock, Mock, patch
import pytest
+from requests import ConnectionError
-from wikiget.dl import prep_download, process_download
+from wikiget.dl import batch_download, download, prep_download, process_download
+from wikiget.exceptions import ParseError
from wikiget.file import File
from wikiget.wikiget import parse_args
@@ -43,6 +46,7 @@ class TestPrepDownload:
args = parse_args(["File:Example.jpg"])
file = prep_download(args.FILE, args)
+
assert file == expected_file
def test_prep_download_with_existing_file(self, tmp_path: Path) -> None:
@@ -59,16 +63,17 @@ class TestPrepDownload:
class TestProcessDownload:
@patch("wikiget.dl.batch_download")
- def test_batch_download(self, mock_batch_download: MagicMock) -> None:
+ def test_process_batch_download(self, mock_batch_download: MagicMock) -> None:
"""A successful batch download should not return any errors."""
mock_batch_download.return_value = 0
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(
+ def test_process_batch_download_with_errors(
self, mock_batch_download: MagicMock, caplog: pytest.LogCaptureFixture
) -> None:
"""
@@ -79,12 +84,13 @@ class TestProcessDownload:
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
@patch("wikiget.dl.prep_download")
@patch("wikiget.dl.download")
- def test_single_download(
+ def test_process_single_download(
self, mock_download: MagicMock, mock_prep_download: MagicMock
) -> None:
"""A successful download should not return any errors."""
@@ -93,11 +99,12 @@ class TestProcessDownload:
args = parse_args(["File:Example.jpg"])
exit_code = process_download(args)
+
assert exit_code == 0
@patch("wikiget.dl.prep_download")
@patch("wikiget.dl.download")
- def test_single_download_with_errors(
+ def test_process_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."""
@@ -106,4 +113,261 @@ class TestProcessDownload:
args = parse_args(["File:Example.jpg"])
exit_code = process_download(args)
+
+ assert exit_code == 1
+
+ @patch("wikiget.dl.prep_download")
+ def test_process_single_download_parse_error(
+ self, mock_prep_download: MagicMock, caplog: pytest.LogCaptureFixture
+ ) -> None:
+ """
+ If process_download catches a ParseError, it should create an error log message.
+ """
+ mock_prep_download.side_effect = ParseError("error message")
+
+ args = parse_args(["File:Example.jpg"])
+ _ = process_download(args)
+
+ assert mock_prep_download.called
+ assert caplog.record_tuples == [("wikiget.dl", logging.ERROR, "error message")]
+
+ @patch("wikiget.dl.prep_download")
+ def test_process_single_download_file_exists_error(
+ self, mock_prep_download: MagicMock, caplog: pytest.LogCaptureFixture
+ ) -> None:
+ """
+ If process_download catches a FileExistsError, it should create a warning log
+ message.
+ """
+ mock_prep_download.side_effect = FileExistsError("warning message")
+
+ args = parse_args(["File:Example.jpg"])
+ _ = process_download(args)
+
+ assert mock_prep_download.called
+ assert caplog.record_tuples == [
+ ("wikiget.dl", logging.WARNING, "warning message"),
+ ]
+
+ @patch("wikiget.dl.prep_download")
+ def test_process_single_download_other_error(
+ self, mock_prep_download: MagicMock
+ ) -> None:
+ """
+ If process_download catches any other errors, it should return 1.
+ """
+ mock_prep_download.side_effect = ConnectionError
+
+ args = parse_args(["File:Example.jpg"])
+ exit_code = process_download(args)
+
+ assert mock_prep_download.called
assert exit_code == 1
+
+
+class TestBatchDownload:
+ @patch("wikiget.dl.download")
+ @patch("wikiget.dl.prep_download")
+ @patch("wikiget.dl.read_batch_file")
+ def test_batch_download(
+ self,
+ mock_read_batch_file: MagicMock,
+ mock_prep_download: MagicMock,
+ mock_download: MagicMock,
+ caplog: pytest.LogCaptureFixture,
+ ) -> None:
+ caplog.set_level(logging.INFO)
+
+ # set dummy return values for read_batch_file() and download()
+ mock_read_batch_file.return_value = {1: "File:Example.jpg"}
+ mock_download.return_value = 0
+
+ args = parse_args(["-a", "batch.txt"])
+ errors = batch_download(args)
+
+ assert mock_read_batch_file.called
+ assert mock_prep_download.called
+ assert mock_download.called
+ assert caplog.record_tuples == [
+ ("wikiget.dl", logging.INFO, "Processing 'File:Example.jpg' at line 1")
+ ]
+ assert errors == 0
+
+ @patch("wikiget.dl.read_batch_file")
+ def test_batch_download_os_error(
+ self, mock_read_batch_file: MagicMock, caplog: pytest.LogCaptureFixture
+ ) -> None:
+ """
+ If batch_download catches an OSError, it should print an error log message
+ and exit the program.
+ """
+ mock_read_batch_file.side_effect = OSError("error message")
+
+ args = parse_args(["-a", "batch.txt"])
+ with pytest.raises(SystemExit):
+ _ = batch_download(args)
+
+ assert mock_read_batch_file.called
+ assert caplog.record_tuples == [
+ ("wikiget.dl", logging.ERROR, "File could not be read: error message"),
+ ]
+
+ @patch("wikiget.dl.prep_download")
+ @patch("wikiget.dl.read_batch_file")
+ def test_batch_download_parse_error(
+ self,
+ mock_read_batch_file: MagicMock,
+ mock_prep_download: MagicMock,
+ caplog: pytest.LogCaptureFixture,
+ ) -> None:
+ mock_read_batch_file.return_value = {1: "File:Example.jpg"}
+ mock_prep_download.side_effect = ParseError("warning message")
+
+ args = parse_args(["-a", "batch.txt"])
+ errors = batch_download(args)
+
+ assert mock_read_batch_file.called
+ assert mock_prep_download.called
+ assert caplog.record_tuples == [
+ ("wikiget.dl", logging.WARNING, "warning message (line 1)"),
+ ]
+ assert errors == 1
+
+ @patch("wikiget.dl.prep_download")
+ @patch("wikiget.dl.read_batch_file")
+ def test_batch_download_file_exists_error(
+ self,
+ mock_read_batch_file: MagicMock,
+ mock_prep_download: MagicMock,
+ caplog: pytest.LogCaptureFixture,
+ ) -> None:
+ mock_read_batch_file.return_value = {1: "File:Example.jpg"}
+ mock_prep_download.side_effect = FileExistsError("warning message")
+
+ args = parse_args(["-a", "batch.txt"])
+ errors = batch_download(args)
+
+ assert mock_read_batch_file.called
+ assert mock_prep_download.called
+ assert caplog.record_tuples == [
+ ("wikiget.dl", logging.WARNING, "warning message"),
+ ]
+ assert errors == 1
+
+ @patch("wikiget.dl.prep_download")
+ @patch("wikiget.dl.read_batch_file")
+ def test_batch_download_other_error(
+ self,
+ mock_read_batch_file: MagicMock,
+ mock_prep_download: MagicMock,
+ caplog: pytest.LogCaptureFixture,
+ ) -> None:
+ mock_read_batch_file.return_value = {1: "File:Example.jpg"}
+ mock_prep_download.side_effect = ConnectionError
+
+ args = parse_args(["-a", "batch.txt"])
+ errors = batch_download(args)
+
+ assert mock_read_batch_file.called
+ assert mock_prep_download.called
+ assert caplog.record_tuples == [
+ (
+ "wikiget.dl",
+ logging.WARNING,
+ "Unable to download 'File:Example.jpg' (line 1) due to an error",
+ ),
+ ]
+ assert errors == 1
+
+
+class TestDownload:
+ @pytest.fixture
+ def mock_file(self) -> File:
+ file = File("Example.jpg")
+ file.image = Mock()
+ file.image.exists = True
+ file.image.imageinfo = {
+ "url": "https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg",
+ "size": 9022,
+ "sha1": "d01b79a6781c72ac9bfff93e5e2cfbeef4efc840",
+ }
+ file.image.site = Mock()
+ file.image.site.host = "commons.wikimedia.org"
+ return file
+
+ # TODO: test dry run option separately
+ def test_download(self, mock_file: File, caplog: pytest.LogCaptureFixture) -> None:
+ caplog.set_level(logging.INFO)
+
+ args = parse_args(["-n", "File:Example.jpg"])
+ errors = download(mock_file, args)
+
+ assert caplog.record_tuples == [
+ (
+ "wikiget.dl",
+ logging.INFO,
+ "[Example.jpg] Downloading 'Example.jpg' (9022 bytes) from "
+ "commons.wikimedia.org",
+ ),
+ (
+ "wikiget.dl",
+ logging.INFO,
+ "[Example.jpg] "
+ "https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg",
+ ),
+ ("wikiget.dl", logging.WARNING, "[Example.jpg] Dry run; download skipped"),
+ ]
+ assert errors == 0
+
+ def test_download_with_output(
+ self, mock_file: File, caplog: pytest.LogCaptureFixture
+ ) -> None:
+ caplog.set_level(logging.INFO)
+
+ mock_file.dest = "output.jpg"
+ args = parse_args(["-n", "-o", "output.jpg", "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'",
+ )
+ assert errors == 0
+
+ def test_download_os_error(
+ self, mock_file: File, tmp_path: Path, 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"])
+ errors = download(mock_file, args)
+
+ assert caplog.record_tuples == [
+ (
+ "wikiget.dl",
+ logging.ERROR,
+ "[Example.jpg] File could not be written: write error",
+ ),
+ ]
+ assert errors == 1
+
+ def test_download_nonexistent_file(
+ self, mock_file: File, caplog: pytest.LogCaptureFixture
+ ) -> None:
+ mock_file.image.exists = False
+
+ args = parse_args(["File:Example.jpg"])
+ errors = download(mock_file, args)
+
+ assert caplog.record_tuples == [
+ (
+ "wikiget.dl",
+ logging.WARNING,
+ "[Example.jpg] Target does not appear to be a valid file",
+ ),
+ ]
+ assert errors == 1