diff options
| author | Cody Logan <cody@lokken.dev> | 2023-11-07 11:35:39 -0800 |
|---|---|---|
| committer | Cody Logan <cody@lokken.dev> | 2023-11-07 11:35:39 -0800 |
| commit | afd8bcae61290ed7025cbb6e6da4e8dcd1055e4f (patch) | |
| tree | 98ecd50f5902680d6c45bb66b24d4a86c0abb9ba | |
| parent | 14b3f3e4c48183776d3021fa596f30d2a3c1091f (diff) | |
| download | wikiget-afd8bcae61290ed7025cbb6e6da4e8dcd1055e4f.tar.gz wikiget-afd8bcae61290ed7025cbb6e6da4e8dcd1055e4f.zip | |
Test query_api when an APIError is raised
| -rw-r--r-- | tests/test_client.py | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/tests/test_client.py b/tests/test_client.py index e17c17a..4cbf702 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -19,7 +19,7 @@ import logging from unittest.mock import MagicMock, patch, sentinel import pytest -from mwclient import InvalidResponse +from mwclient import APIError, InvalidResponse from requests import ConnectionError, HTTPError from wikiget import DEFAULT_SITE @@ -145,3 +145,31 @@ class TestQueryApi: image = query_api("Example.jpg", mock_site) assert image == sentinel.mock_image + + def test_query_api_error(self, caplog: pytest.LogCaptureFixture) -> None: + """ + The query_api function should log an error if an APIError exception is caught, + as well as debug log entries with additional information about the error. + """ + caplog.set_level(logging.DEBUG) + + mock_site = MagicMock() + mock_site.images = MagicMock() + mock_site.images.__getitem__.side_effect = APIError( + "error code", "error info", "error kwargs" + ) + + with pytest.raises(APIError): + _ = query_api("Example.jpg", mock_site) + + assert caplog.record_tuples == [ + ( + "wikiget.client", + logging.ERROR, + "Access denied. Try providing credentials with " + "--username and --password.", + ), + ("wikiget.client", logging.DEBUG, "error code"), + ("wikiget.client", logging.DEBUG, "error info"), + ("wikiget.client", logging.DEBUG, "error kwargs"), + ] |
