Skip to content

test: remove duplicate Discovery class that shadowed test_discovery_http_is_closed - #2819

Open
Tatamis wants to merge 3 commits into
googleapis:mainfrom
Tatamis:fix/duplicate-discovery-test-class
Open

Tatamis wants to merge 3 commits into
googleapis:mainfrom
Tatamis:fix/duplicate-discovery-test-class

Conversation

@Tatamis

@Tatamis Tatamis commented Sep 17, 2026

Copy link
Copy Markdown

Fixes #2757.

tests/test_discovery.py defined class Discovery(unittest.TestCase) twice: once near line 498 (added in #1038 to test that build() closes the httplib2.Http it creates internally) and once further down (pre-existing). In Python, the second class definition silently overwrites the first at import time, so test_discovery_http_is_closed was never collected by pytest and has been dead code since 2020.

The dead test was also broken on its own terms: HttpMock.close is a plain method, not a mock object, so calling .assert_called_once() on it raises AttributeError rather than testing anything — confirmed by isolating and running it directly.

Fix

Removes the duplicate class and moves a corrected version of the test into the real Discovery class, patching httplib2.Http directly so the assertion actually exercises the close-the-internal-http-client behavior in discovery.py (the discovery_http.close() call made when build() creates its own http client to fetch the discovery document).

Verified the new test both passes against current code and fails (as expected) if that discovery_http.close() call is removed.

Test plan

  • pytest tests/test_discovery.py — 167 passed (up from 166), same 5 pre-existing unrelated failures (credentials/cache/mtls tests that fail in this sandboxed environment regardless of this change).
  • black --check clean.

…ttp_is_closed

tests/test_discovery.py defined class Discovery(unittest.TestCase)
twice: once near line 498 (added in googleapis#1038 to test that build() closes
the httplib2.Http it creates internally) and once near line 1544
(pre-existing). The second definition silently overwrote the first,
so test_discovery_http_is_closed was never collected by pytest and
had been dead code since 2020.

The dead test was also broken on its own terms: HttpMock.close is a
plain method, not a mock, so calling .assert_called_once() on it
raises AttributeError rather than testing anything.

Move a corrected version of the test into the real Discovery class,
patching httplib2.Http directly so the assertion exercises the actual
close-the-internal-http-client behavior in discovery.py.
@Tatamis
Tatamis requested a review from a team as a code owner September 17, 2026 10:55
@google-cla

google-cla Bot commented Sep 17, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@product-auto-label product-auto-label Bot added the size: s Pull request size is small. label Sep 17, 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 use @mock.patch for mocking httplib2.Http instead of relying on HttpMock. The feedback suggests two improvements: first, to mock the response content as bytes by reading the datafile with 'rb' to better align with Python 3 behavior; second, to use a mock with a proper spec for credentials instead of a generic MagicMock to ensure type-checking compatibility.

Comment thread tests/test_discovery.py Outdated
Comment thread tests/test_discovery.py Outdated
Tatamis and others added 2 commits September 17, 2026 13:57
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
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