test: unshadow Discovery class and restore test_discovery_http_is_closed - #2828
reginaldalfret wants to merge 2 commits into
Conversation
…http_is_closed Fixes googleapis#2757
There was a problem hiding this comment.
Code Review
This pull request refactors and relocates the test_discovery_http_is_closed test in tests/test_discovery.py to mock build_http and verify that the discovery HTTP client is closed. The review feedback correctly identifies that build_http is called twice during the service creation process. Returning the same mock instance for both calls conflates the discovery and service clients, which could lead to false positives. It is recommended to use side_effect to return separate mock instances for each call to ensure the test is robust.
| mock_http = mock.Mock() | ||
| mock_http.request.return_value = ( | ||
| httplib2.Response({"status": "200"}), | ||
| read_datafile("plus.json", "rb"), | ||
| ) | ||
| mock_build_http.return_value = mock_http | ||
| build( | ||
| "plus", | ||
| "v1", | ||
| developerKey="test_key", | ||
| static_discovery=False, | ||
| ) | ||
| mock_http.close.assert_called_once() |
There was a problem hiding this comment.
In build(), build_http() is called twice: once to create the temporary HTTP client for fetching the discovery document, and once inside build_from_document() to create the HTTP client for the service itself.
By returning the same mock_http instance for both calls, the test conflates the two clients. If a bug or future change causes the service's HTTP client to be closed instead of the discovery HTTP client, the test would still pass (a false positive).
Using side_effect to return separate mock instances for the discovery client and the service client makes the test more robust and ensures we are specifically asserting that the discovery HTTP client is closed.
mock_discovery_http = mock.Mock()
mock_service_http = mock.Mock()
mock_discovery_http.request.return_value = (
httplib2.Response({"status": "200"}),
read_datafile("plus.json", "rb"),
)
mock_build_http.side_effect = [mock_discovery_http, mock_service_http]
build(
"plus",
"v1",
developerKey="test_key",
static_discovery=False,
)
mock_discovery_http.close.assert_called_once()
Description
Fixes #2757
In \ ests/test_discovery.py, a duplicate \class Discovery(unittest.TestCase)\ defined at line 498 was completely shadowed by the active \class Discovery(unittest.TestCase)\ defined further down at line 1551. Consequently, \ est_discovery_http_is_closed\ was dead code and never executed by \pytest.
This PR:
Validation