aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCody Logan <cody@lokken.dev>2023-11-03 16:37:59 -0700
committerCody Logan <cody@lokken.dev>2023-11-03 16:37:59 -0700
commit5b3fd4383462503c8d9eaae53692e68b159d5536 (patch)
tree0f91bdb274d1335cadc4b83674d6f46fe7a782a5
parentf28e25c999169a0ea0dc6dc12895a4b6dfab148f (diff)
downloadwikiget-5b3fd4383462503c8d9eaae53692e68b159d5536.tar.gz
wikiget-5b3fd4383462503c8d9eaae53692e68b159d5536.zip
Revise query_api test to use mock objects
-rw-r--r--tests/test_client.py48
1 files changed, 26 insertions, 22 deletions
diff --git a/tests/test_client.py b/tests/test_client.py
index 18af3f9..fd47a35 100644
--- a/tests/test_client.py
+++ b/tests/test_client.py
@@ -16,18 +16,22 @@
# along with Wikiget. If not, see <https://www.gnu.org/licenses/>.
import logging
-from unittest.mock import patch
+from unittest.mock import MagicMock, patch
import pytest
-from mwclient import InvalidResponse
+from mwclient import InvalidResponse, Site
+from mwclient.image import Image
from requests import ConnectionError, HTTPError
-from wikiget import DEFAULT_SITE, USER_AGENT
+from wikiget import DEFAULT_SITE
from wikiget.client import connect_to_site, query_api
from wikiget.wikiget import parse_args
-class TestClient:
+class TestConnectSite:
+ # this message is logged when the level is at INFO or below
+ info_msg = f"Connecting to {DEFAULT_SITE}"
+
def test_connect_to_site(self, caplog: pytest.LogCaptureFixture) -> None:
"""
The connect_to_site function should create an info log message recording the
@@ -38,15 +42,15 @@ class TestClient:
with patch("wikiget.client.Site"):
_ = connect_to_site(DEFAULT_SITE, args)
assert caplog.record_tuples == [
- ("wikiget.client", logging.INFO, f"Connecting to {DEFAULT_SITE}"),
+ ("wikiget.client", logging.INFO, self.info_msg),
]
def test_connect_to_site_connection_error(
self, caplog: pytest.LogCaptureFixture
) -> None:
"""
- The connect_to_site function should log an error if a ConnectionError exception
- is raised.
+ The connect_to_site function should log the correct messages if a
+ ConnectionError exception is raised.
"""
caplog.set_level(logging.DEBUG)
args = parse_args(["File:Example.jpg"])
@@ -56,15 +60,15 @@ class TestClient:
_ = connect_to_site(DEFAULT_SITE, args)
assert "Could not connect to specified site" in caplog.text
assert caplog.record_tuples == [
- ("wikiget.client", logging.INFO, f"Connecting to {DEFAULT_SITE}"),
+ ("wikiget.client", logging.INFO, self.info_msg),
("wikiget.client", logging.ERROR, "Could not connect to specified site"),
("wikiget.client", logging.DEBUG, "connection error message"),
]
def test_connect_to_site_http_error(self, caplog: pytest.LogCaptureFixture) -> None:
"""
- The connect_to_site function should log an error if an HTTPError exception
- is raised.
+ The connect_to_site function should log the correct messages if an HTTPError
+ exception is raised.
"""
caplog.set_level(logging.DEBUG)
args = parse_args(["File:Example.jpg"])
@@ -73,14 +77,14 @@ class TestClient:
with pytest.raises(HTTPError):
_ = connect_to_site(DEFAULT_SITE, args)
assert caplog.record_tuples == [
- ("wikiget.client", logging.INFO, f"Connecting to {DEFAULT_SITE}"),
+ ("wikiget.client", logging.INFO, self.info_msg),
(
"wikiget.client",
logging.ERROR,
"Could not find the specified wiki's api.php. "
"Check the value of --path.",
),
- ("wikiget.client", logging.DEBUG, "")
+ ("wikiget.client", logging.DEBUG, ""),
]
def test_connect_to_site_other_error(
@@ -98,15 +102,15 @@ class TestClient:
for record in caplog.records:
assert record.levelname == "ERROR"
- # TODO: don't hit the actual API when doing tests
- @pytest.mark.skip(reason="skip tests that query a live API")
- def test_query_api(self, caplog: pytest.LogCaptureFixture) -> None:
+
+class TestQueryApi:
+ def test_query_api(self) -> None:
"""
- The query_api function should create a debug log message containing the user
- agent we're sending to the API.
+ The query_api function should return an Image object when given a name and a
+ valid Site.
"""
- caplog.set_level(logging.DEBUG)
- args = parse_args(["File:Example.jpg"])
- site = connect_to_site("commons.wikimedia.org", args)
- _ = query_api("Example.jpg", site)
- assert USER_AGENT in caplog.text
+ mock_site = MagicMock(Site)
+ mock_image = MagicMock(Image)
+ mock_site.images = {"Example.jpg": mock_image}
+ image = query_api("Example.jpg", mock_site)
+ assert image == mock_image