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_wikiget_cli.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_wikiget_cli.py')
| -rw-r--r-- | tests/test_wikiget_cli.py | 84 |
1 files changed, 44 insertions, 40 deletions
diff --git a/tests/test_wikiget_cli.py b/tests/test_wikiget_cli.py index 99d113e..86b3780 100644 --- a/tests/test_wikiget_cli.py +++ b/tests/test_wikiget_cli.py @@ -15,55 +15,59 @@ # 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__ from wikiget.wikiget import cli -def test_cli_no_params(monkeypatch: pytest.MonkeyPatch): - monkeypatch.setattr("sys.argv", ["wikiget"]) - with pytest.raises(SystemExit) as e: - cli() - assert e.value.code == 2 +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 + + 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 + + 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_successfully( - mock_process_download: MagicMock, monkeypatch: pytest.MonkeyPatch -) -> None: - monkeypatch.setattr("sys.argv", ["wikiget", "File:Example.jpg"]) - mock_process_download.return_value = 0 - with pytest.raises(SystemExit) as e: - cli() - assert mock_process_download.called - assert e.value.code == 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 == 1 -@patch("wikiget.wikiget.process_download") -def test_cli_completed_with_problems( - mock_process_download: MagicMock, monkeypatch: pytest.MonkeyPatch -) -> None: - monkeypatch.setattr("sys.argv", ["wikiget", "File:Example.jpg"]) - mock_process_download.return_value = 1 - with pytest.raises(SystemExit) as e: - cli() - assert mock_process_download.called - assert e.value.code == 1 + def test_cli_logs( + self, 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 + with monkeypatch.context() as m: + m.setattr("sys.argv", ["wikiget", "File:Example.jpg"]) + m.setattr("wikiget.wikiget.process_download", mock_process_download) -@patch("wikiget.wikiget.process_download") -def test_cli_logs( - mock_process_download: MagicMock, - monkeypatch: pytest.MonkeyPatch, - caplog: pytest.LogCaptureFixture, -) -> None: - monkeypatch.setattr("sys.argv", ["wikiget", "File:Example.jpg"]) - mock_process_download.return_value = 0 - with pytest.raises(SystemExit): - cli() - assert mock_process_download.called - assert f"Starting download session using wikiget {__version__}" in caplog.text - assert USER_AGENT in caplog.text + with pytest.raises(SystemExit): + cli() + assert ( + f"Starting download session using wikiget {__version__}" in caplog.text + ) + assert USER_AGENT in caplog.text |
