Skip to content

test: unshadow Discovery class and restore test_discovery_http_is_closed - #2828

Open
reginaldalfret wants to merge 2 commits into
googleapis:mainfrom
reginaldalfret:fix-2757-discovery-http-closed-test
Open

reginaldalfret wants to merge 2 commits into
googleapis:mainfrom
reginaldalfret:fix-2757-discovery-http-closed-test

Conversation

@reginaldalfret

Copy link
Copy Markdown

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:

  1. Removes the shadowed duplicate \class Discovery\ definition at line 498.
  2. Restores \ est_discovery_http_is_closed\ inside the active \Discovery\ test class.
  3. Fixes the test case by mocking \googleapiclient.discovery.build_http\ to verify that when \�uild()\ creates an internal HTTP client (i.e. when \http\ is not passed by the caller), \discovery_http.close()\ is properly called after building the service.

Validation

  • \pytest tests/test_discovery.py -k test_discovery_http_is_closed\ PASSED.
  • Full \Discovery\ test suite (38 passed, 2 skipped) PASSED.
  • \ lake8 tests/test_discovery.py\ passed with 0 errors on modified lines.
  • \git diff --check\ passed cleanly.

@reginaldalfret
reginaldalfret requested a review from a team as a code owner September 24, 2026 19:24
@product-auto-label product-auto-label Bot added the size: s Pull request size is small. label Sep 24, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tests/test_discovery.py Outdated
Comment on lines +1547 to +1559
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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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()

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: s Pull request size is small.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: connection leak test test_discovery_http_is_closed is shadowed and syntactically invalid

1 participant