Repository navigation
test: restore shadowed discovery http close test - #2848
reginaldalfret wants to merge 1 commit into
Conversation
Resolve googleapis#2757 by removing the duplicate class Discovery(unittest.TestCase) definition at line 498 which was shadowed by the active definition at line 1551. In the active Discovery test suite, reinstate test_discovery_http_is_closed with proper mock patching for build_http(), and add test_provided_http_is_not_closed to verify caller-provided HTTP instances are not closed. Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>
There was a problem hiding this comment.
Code Review
This pull request refactors and expands the HTTP discovery tests in tests/test_discovery.py. It moves and updates test_discovery_http_is_closed to mock the HTTP builder and assert that the HTTP client is closed, and adds a new test test_provided_http_is_not_closed to ensure a user-provided HTTP client is not closed. The review feedback points out that using a plain mock.Mock() for credentials can cause test failures in environments where HAS_UNIVERSE is false, and suggests using a mock with a specific credentials spec and resetting the universe domain to prevent this issue.
| creds = mock.Mock() | ||
| service = build("youtube", "v3", credentials=creds) |
There was a problem hiding this comment.
Using a plain mock.Mock() for credentials can cause test failures in environments where HAS_UNIVERSE is False. In those environments, _check_api_core_compatible_with_credentials_universe is executed and accesses credentials.universe_domain. Since creds is a plain mock, getattr(creds, "universe_domain", None) returns a new Mock object (which is truthy and not equal to "googleapis.com"), triggering an unexpected APICoreVersionError.
To make the test robust and consistent with other tests in this suite, use mock.Mock(spec=google.auth.credentials.Credentials) and call _reset_universe_domain(creds).
| creds = mock.Mock() | |
| service = build("youtube", "v3", credentials=creds) | |
| creds = mock.Mock(spec=google.auth.credentials.Credentials) | |
| _reset_universe_domain(creds) | |
| service = build("youtube", "v3", credentials=creds) |
Fixes #2757
Description
In \ ests/test_discovery.py, \class Discovery(unittest.TestCase)\ was defined twice:
Because the second class definition shadowed the first at import time, \ est_discovery_http_is_closed\ was dead code and never executed by test runners. Additionally, the shadowed test attempted to assert \http.close\ without patching \googleapiclient.discovery.build_http().
This PR:
Verification
Verified with pytest:
\\�ash
pytest tests/test_discovery.py -k 'test_discovery_http_is_closed or test_provided_http_is_not_closed' -v
\
Result: 2 passed in 0.42s.