Skip to content

Commit

Permalink
test(tests): fix not using patched functions correctly in assertions
Browse files Browse the repository at this point in the history
We were incorrectly calling assertion methods on the original name for the patched function, not the patched object, leading to typing warnings. This is now fixed.
  • Loading branch information
jacksonj04 committed Nov 12, 2024
1 parent 46e60af commit dfa3455
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions tests/client/test_advanced_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def test_invoke_called_with_default_params_when_optional_parameters_not_provided
parameters and return the response
"""

with patch.object(self.client, "invoke"):
with patch.object(self.client, "invoke") as patched_invoke:
response = self.client.advanced_search(SearchParameters())

self.client.invoke.assert_called_with(
patched_invoke.assert_called_with(
"/judgments/search/search-v2.xqy",
json.dumps(
{
Expand All @@ -50,7 +50,7 @@ def test_invoke_called_with_default_params_when_optional_parameters_not_provided
),
)

assert response == self.client.invoke.return_value
assert response == patched_invoke.return_value

def test_invoke_called_with_all_params_when_all_parameters_provided(self):
"""
Expand All @@ -60,7 +60,7 @@ def test_invoke_called_with_all_params_when_all_parameters_provided(self):
Then it should call the MarkLogic module with all the parameters
and return the response
"""
with patch.object(self.client, "invoke"):
with patch.object(self.client, "invoke") as patched_invoke:
response = self.client.advanced_search(
SearchParameters(
query="test query",
Expand All @@ -80,7 +80,7 @@ def test_invoke_called_with_all_params_when_all_parameters_provided(self):
),
)

self.client.invoke.assert_called_with(
patched_invoke.assert_called_with(
"/judgments/search/search-v2.xqy",
json.dumps(
{
Expand All @@ -103,7 +103,7 @@ def test_invoke_called_with_all_params_when_all_parameters_provided(self):
),
)

assert response == self.client.invoke.return_value
assert response == patched_invoke.return_value

def test_exception_raised_when_invoke_raises_an_exception(self):
"""
Expand All @@ -113,8 +113,8 @@ def test_exception_raised_when_invoke_raises_an_exception(self):
Then it should raise that same exception
"""
exception = Exception("Error message from MarkLogic")
with patch.object(self.client, "invoke"):
self.client.invoke.side_effect = exception
with patch.object(self.client, "invoke") as patched_invoke:
patched_invoke.side_effect = exception
with pytest.raises(Exception) as e:
self.client.advanced_search(SearchParameters(query="test query"))
assert e.value == exception
Expand Down Expand Up @@ -177,7 +177,7 @@ def test_user_can_view_unpublished_and_show_unpublished_is_true(
When the advanced_search method is called with the show_unpublished parameter set to True
Then it should call the MarkLogic module with the expected query parameters
"""
with patch.object(self.client, "invoke"), patch.object(
with patch.object(self.client, "invoke") as patched_invoke, patch.object(
self.client,
"user_can_view_unpublished_judgments",
return_value=True,
Expand All @@ -193,7 +193,7 @@ def test_user_can_view_unpublished_and_show_unpublished_is_true(
show_unpublished=True,
),
)
assert '"show_unpublished": "true"' in self.client.invoke.call_args.args[1]
assert '"show_unpublished": "true"' in patched_invoke.call_args.args[1]

def test_user_cannot_view_unpublished_but_show_unpublished_is_true(
self,
Expand All @@ -204,7 +204,7 @@ def test_user_cannot_view_unpublished_but_show_unpublished_is_true(
When the advanced_search method is called with the show_unpublished parameter set to True
Then it should call the MarkLogic module with the show_unpublished parameter set to False and log a warning
"""
with patch.object(self.client, "invoke"), patch.object(
with patch.object(self.client, "invoke") as patched_invoke, patch.object(
self.client,
"user_can_view_unpublished_judgments",
return_value=False,
Expand All @@ -221,7 +221,7 @@ def test_user_cannot_view_unpublished_but_show_unpublished_is_true(
),
)

assert '"show_unpublished": "false"' in self.client.invoke.call_args.args[1]
assert '"show_unpublished": "false"' in patched_invoke.call_args.args[1]
mock_logging.assert_called()

def test_no_page_0(self):
Expand All @@ -231,11 +231,11 @@ def test_no_page_0(self):
When the advanced_search method is called with the page parameter set to 0
Then it should call the MarkLogic module with the page parameter set to 1
"""
with patch.object(self.client, "invoke"):
with patch.object(self.client, "invoke") as patched_invoke:
self.client.advanced_search(
SearchParameters(
page=0,
),
)

assert ', "page": 1,' in self.client.invoke.call_args.args[1]
assert ', "page": 1,' in patched_invoke.call_args.args[1]

0 comments on commit dfa3455

Please sign in to comment.