aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorCody Logan <cody@lokken.dev>2023-11-06 12:06:08 -0800
committerCody Logan <cody@lokken.dev>2023-11-06 12:06:08 -0800
commit2d18a4cefff6f26a48966e66774c463dfbbe2492 (patch)
tree2523d2b456f204fffd7aee92008ddb9cda806da3 /tests
parent56dbc899c6bec74cba25483768e855ad7953a432 (diff)
downloadwikiget-2d18a4cefff6f26a48966e66774c463dfbbe2492.tar.gz
wikiget-2d18a4cefff6f26a48966e66774c463dfbbe2492.zip
Be more consistent in use of mocking method
Diffstat (limited to 'tests')
-rw-r--r--tests/test_dl.py82
-rw-r--r--tests/test_wikiget_cli.py45
2 files changed, 59 insertions, 68 deletions
diff --git a/tests/test_dl.py b/tests/test_dl.py
index 6d0b484..f2a942f 100644
--- a/tests/test_dl.py
+++ b/tests/test_dl.py
@@ -60,68 +60,52 @@ class TestPrepDownload:
class TestProcessDownload:
- def test_batch_download(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ @patch("wikiget.dl.batch_download")
+ def test_batch_download(self, mock_batch_download: MagicMock) -> None:
"""A successful batch download should not return any errors."""
+ mock_batch_download.return_value = 0
- 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
+ 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, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
+ self, mock_batch_download: MagicMock, 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.
"""
+ mock_batch_download.return_value = 4
- 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
+ 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:
+ @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."""
+ mock_download.return_value = 0
+ mock_prep_download.return_value = File("Example.jpg")
- 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
+ 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:
+ @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."""
+ mock_download.return_value = 1
+ mock_prep_download.return_value = File("Example.jpg")
- 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
+ args = parse_args(["File:Example.jpg"])
+ exit_code = process_download(args)
+ assert exit_code == 1
diff --git a/tests/test_wikiget_cli.py b/tests/test_wikiget_cli.py
index 86b3780..0306579 100644
--- a/tests/test_wikiget_cli.py
+++ b/tests/test_wikiget_cli.py
@@ -15,6 +15,8 @@
# You should have received a copy of the GNU General Public License
# along with Wikiget. If not, see <https://www.gnu.org/licenses/>.
+from unittest.mock import MagicMock, patch
+
import pytest
from wikiget import USER_AGENT, __version__
@@ -23,47 +25,52 @@ from wikiget.wikiget import cli
class TestCli:
def test_cli_no_params(self, monkeypatch: pytest.MonkeyPatch) -> None:
- monkeypatch.setattr("sys.argv", ["wikiget"])
- with pytest.raises(SystemExit) as e:
- cli()
- assert e.value.code == 2
+ with monkeypatch.context() as m:
+ m.setattr("sys.argv", ["wikiget"])
+ with pytest.raises(SystemExit) as e:
+ cli()
+ assert e.value.code == 2
- def test_cli_completed_successfully(self, monkeypatch: pytest.MonkeyPatch) -> None:
- def mock_process_download(*args, **kwargs) -> int: # noqa: ARG001
- """A successful call to process_download returns 0."""
- return 0
+ @patch("wikiget.wikiget.process_download")
+ def test_cli_completed_successfully(
+ self, mock_process_download: MagicMock, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ # a successful call to process_download returns 0
+ mock_process_download.return_value = 0
with monkeypatch.context() as m:
m.setattr("sys.argv", ["wikiget", "File:Example.jpg"])
- m.setattr("wikiget.wikiget.process_download", mock_process_download)
with pytest.raises(SystemExit) as e:
cli()
assert e.value.code == 0
- def test_cli_completed_with_problems(self, monkeypatch: pytest.MonkeyPatch) -> None:
- def mock_process_download(*args, **kwargs) -> int: # noqa: ARG001
- """An unsuccessful call to process_download returns 1."""
- return 1
+ @patch("wikiget.wikiget.process_download")
+ def test_cli_completed_with_problems(
+ self, mock_process_download: MagicMock, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ # an unsuccessful call to process_download returns 1
+ mock_process_download.return_value = 1
with monkeypatch.context() as m:
m.setattr("sys.argv", ["wikiget", "File:Example.jpg"])
- m.setattr("wikiget.wikiget.process_download", mock_process_download)
with pytest.raises(SystemExit) as e:
cli()
assert e.value.code == 1
+ @patch("wikiget.wikiget.process_download")
def test_cli_logs(
- self, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
+ self,
+ mock_process_download: MagicMock,
+ monkeypatch: pytest.MonkeyPatch,
+ caplog: pytest.LogCaptureFixture,
) -> None:
- def mock_process_download(*args, **kwargs) -> int: # noqa: ARG001
- """A successful call to process_download returns 0."""
- return 0
+ # a successful call to process_download returns 0
+ mock_process_download.return_value = 0
with monkeypatch.context() as m:
m.setattr("sys.argv", ["wikiget", "File:Example.jpg"])
- m.setattr("wikiget.wikiget.process_download", mock_process_download)
with pytest.raises(SystemExit):
cli()