From 8b923f0cee8fed90d6eef313429eb593877e0f2d Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Wed, 1 Nov 2023 11:32:53 -0700 Subject: Test connect_to_site using mocking --- tests/test_client.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'tests/test_client.py') diff --git a/tests/test_client.py b/tests/test_client.py index 41ce948..832bf21 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -16,6 +16,7 @@ # along with Wikiget. If not, see . import logging +from unittest.mock import MagicMock, patch import pytest @@ -25,15 +26,19 @@ from wikiget.wikiget import construct_parser # TODO: don't hit the actual API when doing tests -@pytest.mark.skip(reason="skip tests that query a live API") class TestQueryApi: - args = construct_parser().parse_args(["File:Example.jpg"]) - - def test_connect_to_site(self, caplog: pytest.LogCaptureFixture) -> None: + @patch("mwclient.Site.__new__") + def test_connect_to_site( + self, mock_site: MagicMock, caplog: pytest.LogCaptureFixture + ) -> None: caplog.set_level(logging.DEBUG) - _ = connect_to_site("commons.wikimedia.org", self.args) + mock_site.return_value = MagicMock() + args = construct_parser().parse_args(["File:Example.jpg"]) + _ = connect_to_site("commons.wikimedia.org", args) + assert mock_site.called assert "Connecting to commons.wikimedia.org" in caplog.text + @pytest.mark.skip(reason="skip tests that query a live API") def test_query_api(self, caplog: pytest.LogCaptureFixture) -> None: caplog.set_level(logging.DEBUG) site = connect_to_site("commons.wikimedia.org", self.args) -- cgit v1.2.3 From f61853c366a1554f545b34783e1aa282022598b9 Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Wed, 1 Nov 2023 11:33:13 -0700 Subject: Add some explanatory comments to tests --- tests/test_client.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'tests/test_client.py') diff --git a/tests/test_client.py b/tests/test_client.py index 832bf21..cf6e29c 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -31,6 +31,10 @@ class TestQueryApi: def test_connect_to_site( self, mock_site: MagicMock, caplog: pytest.LogCaptureFixture ) -> None: + """ + The connect_to_site function should create a debug log message recording the + name of the site we're connecting to. + """ caplog.set_level(logging.DEBUG) mock_site.return_value = MagicMock() args = construct_parser().parse_args(["File:Example.jpg"]) @@ -40,7 +44,12 @@ class TestQueryApi: @pytest.mark.skip(reason="skip tests that query a live API") def test_query_api(self, caplog: pytest.LogCaptureFixture) -> None: + """ + The query_api function should create a debug log message containing the user + agent we're sending to the API. + """ caplog.set_level(logging.DEBUG) - site = connect_to_site("commons.wikimedia.org", self.args) + args = construct_parser().parse_args(["File:Example.jpg"]) + site = connect_to_site("commons.wikimedia.org", args) _ = query_api("Example.jpg", site) assert USER_AGENT in caplog.text -- cgit v1.2.3