Skip to content

Commit c7e85c7

Browse files
tests(bigquery): harden system test teardown and unit test auth isolation (#17964)
Following the merge of PR #17953 for the socket leak fix, this PR focuses on the remaining orthogonal test and fixture hardening fixes: 1. **System Test Teardown Resilience:** Wrap TagKey and TagValue resource deletions in `try...except NotFound: pass` during `TestBigQuery.tearDown()` to prevent cascaded teardown failures when tag resources were already deleted. 2. **Unit Test ADC Isolation:** Enable `autouse=True` on the `use_local_magics_context` fixture in `test_magics.py` to prevent credentials mutation across test runs in uncredentialed CI environments. 3. **InteractiveShell Fixture & Fallback:** Use `IPython.core.interactiveshell.InteractiveShell` and add fallback unit test coverage for `magics.Context`. Related: b/540939659 🦕 --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 7d2bc21 commit c7e85c7

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

packages/google-cloud-bigquery/tests/system/test_client.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,16 @@ def _still_in_use(bad_request):
204204
tag_key = key_values.pop()
205205

206206
# Delete tag values first
207-
[
208-
tag_values_client.delete_tag_value(name=tag_value.name).result()
209-
for tag_value in key_values
210-
]
207+
for tag_value in key_values:
208+
try:
209+
tag_values_client.delete_tag_value(name=tag_value.name).result()
210+
except NotFound:
211+
pass
211212

212-
tag_keys_client.delete_tag_key(name=tag_key.name).result()
213+
try:
214+
tag_keys_client.delete_tag_key(name=tag_key.name).result()
215+
except NotFound:
216+
pass
213217

214218
def test_get_service_account_email(self):
215219
client = Config.CLIENT

packages/google-cloud-bigquery/tests/unit/test_magics.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737

3838
bigquery_storage = pytest.importorskip("google.cloud.bigquery_storage")
3939
IPython = pytest.importorskip("IPython")
40-
interactiveshell = pytest.importorskip("IPython.terminal.interactiveshell")
40+
interactiveshell = pytest.importorskip("IPython.core.interactiveshell")
4141
tools = pytest.importorskip("IPython.testing.tools")
4242
io = pytest.importorskip("IPython.utils.io")
4343
pandas = pytest.importorskip("pandas")
4444

4545

46-
@pytest.fixture()
46+
@pytest.fixture(autouse=True)
4747
def use_local_magics_context(monkeypatch):
4848
if magics is not None: # pragma: NO COVER
4949
local_context = magics.Context()
@@ -58,8 +58,7 @@ def use_local_magics_context(monkeypatch):
5858
@pytest.fixture(scope="session")
5959
def ipython():
6060
config = tools.default_config()
61-
config.TerminalInteractiveShell.simple_prompt = True
62-
shell = interactiveshell.TerminalInteractiveShell.instance(config=config)
61+
shell = interactiveshell.InteractiveShell.instance(config=config)
6362
return shell
6463

6564

@@ -143,10 +142,12 @@ def fail_if(name, globals, locals, fromlist, level):
143142
}
144143

145144

146-
def test_context_with_default_credentials():
147-
"""When Application Default Credentials are set, the context credentials
148-
will be created the first time it is called
145+
def test_context_resolves_unset_credentials_and_project():
146+
"""When context credentials and project are unset (None), accessing them
147+
resolves them from Application Default Credentials.
149148
"""
149+
magics.context._credentials = None
150+
magics.context._project = None
150151
assert magics.context._credentials is None
151152
assert magics.context._project is None
152153

0 commit comments

Comments
 (0)