Skip to content

Commit 50cbd14

Browse files
test: added coverage test (#374)
* test: added coverage test for toolbox-core * nit: added coverage report and generated directory for gitignore * chore: modified coverage test path * chore: modified toolbox-core * chore: modified coverage test * ci: fix coverage collection in cloud build Add editable install and correct working directory for coverage * chore: removed extra test * chore: added coverage test for llamaindex & langchain * chore: fixed test error * chore: fix path * chore: added test for getting coverage as 90% * chore: added test case for llamaindex to get 90% * chore: updated license header
1 parent bd377c4 commit 50cbd14

File tree

6 files changed

+249
-6
lines changed

6 files changed

+249
-6
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ venv
1111
.python-version
1212
**.egg-info/
1313
__pycache__/**
14-
14+
.coverage
15+
**/.coverage
16+
build/
17+
**/build/

packages/toolbox-core/integration.cloudbuild.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ steps:
2626
name: 'python:${_VERSION}'
2727
args:
2828
- install
29+
- '-e'
2930
- 'packages/toolbox-core[test]'
3031
- '--user'
3132
entrypoint: pip
3233
- id: Run integration tests
3334
name: 'python:${_VERSION}'
35+
dir: 'packages/toolbox-core'
3436
env:
3537
- TOOLBOX_URL=$_TOOLBOX_URL
3638
- TOOLBOX_VERSION=$_TOOLBOX_VERSION
@@ -39,7 +41,7 @@ steps:
3941
args:
4042
- '-c'
4143
- >-
42-
python -m pytest packages/toolbox-core/tests/
44+
python -m pytest --cov=src/toolbox_core --cov-report=term --cov-fail-under=90 tests/
4345
entrypoint: /bin/bash
4446
options:
4547
logging: CLOUD_LOGGING_ONLY

packages/toolbox-langchain/integration.cloudbuild.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ steps:
2626
name: 'python:${_VERSION}'
2727
args:
2828
- install
29+
- '-e'
2930
- 'packages/toolbox-langchain[test]'
3031
- '--user'
3132
entrypoint: pip
3233
- id: Run integration tests
3334
name: 'python:${_VERSION}'
35+
dir: 'packages/toolbox-langchain'
3436
env:
3537
- TOOLBOX_URL=$_TOOLBOX_URL
3638
- TOOLBOX_VERSION=$_TOOLBOX_VERSION
@@ -39,7 +41,7 @@ steps:
3941
args:
4042
- '-c'
4143
- >-
42-
python -m pytest packages/toolbox-langchain/tests/
44+
python -m pytest --cov=src/toolbox_langchain --cov-report=term --cov-fail-under=90 tests/
4345
entrypoint: /bin/bash
4446
options:
4547
logging: CLOUD_LOGGING_ONLY

packages/toolbox-langchain/tests/test_client.py

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 Google LLC
1+
# Copyright 2025 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -421,6 +421,69 @@ async def test_aload_toolset_with_args(
421421
strict=True,
422422
)
423423

424+
@pytest.mark.asyncio
425+
@patch("toolbox_core.sync_client.ToolboxSyncClient.load_toolset")
426+
async def test_aload_toolset_with_deprecated_args(
427+
self, mock_sync_core_load_toolset, toolbox_client
428+
):
429+
mock_core_tool_instance = create_mock_core_sync_tool(
430+
model_name="MyAsyncSetModel"
431+
)
432+
mock_sync_core_load_toolset.return_value = [mock_core_tool_instance]
433+
434+
auth_tokens_deprecated = {"token_deprecated": lambda: "value_dep"}
435+
auth_headers_deprecated = {"header_deprecated": lambda: "value_head_dep"}
436+
bound_params = {"param1": "value4"}
437+
toolset_name = "my_async_toolset"
438+
439+
# Scenario 2: auth_tokens and auth_headers provided, auth_token_getters is default (empty initially)
440+
with pytest.warns(DeprecationWarning) as record:
441+
await toolbox_client.aload_toolset(
442+
toolset_name,
443+
auth_tokens=auth_tokens_deprecated, # This will be used for auth_token_getters
444+
auth_headers=auth_headers_deprecated, # This will warn as auth_token_getters is now populated
445+
bound_params=bound_params,
446+
)
447+
assert len(record) == 2
448+
messages = sorted([str(r.message) for r in record])
449+
450+
assert (
451+
messages[0]
452+
== "Argument `auth_tokens` is deprecated. Use `auth_token_getters` instead."
453+
)
454+
assert (
455+
messages[1]
456+
== "Both `auth_token_getters` and `auth_headers` are provided. `auth_headers` is deprecated, and `auth_token_getters` will be used."
457+
)
458+
459+
expected_getters_for_call = auth_tokens_deprecated
460+
461+
mock_sync_core_load_toolset.assert_called_with(
462+
name=toolset_name,
463+
auth_token_getters=expected_getters_for_call,
464+
bound_params=bound_params,
465+
strict=False,
466+
)
467+
mock_sync_core_load_toolset.reset_mock()
468+
469+
with pytest.warns(
470+
DeprecationWarning,
471+
match="Argument `auth_headers` is deprecated. Use `auth_token_getters` instead.",
472+
) as record:
473+
await toolbox_client.aload_toolset(
474+
toolset_name,
475+
auth_headers=auth_headers_deprecated,
476+
bound_params=bound_params,
477+
)
478+
assert len(record) == 1
479+
480+
mock_sync_core_load_toolset.assert_called_with(
481+
name=toolset_name,
482+
auth_token_getters=auth_headers_deprecated,
483+
bound_params=bound_params,
484+
strict=False,
485+
)
486+
424487
@patch("toolbox_langchain.client.ToolboxCoreSyncClient")
425488
def test_init_with_client_headers(self, mock_core_client_constructor):
426489
"""Tests that client_headers are passed to the core client during initialization."""
@@ -429,3 +492,27 @@ def test_init_with_client_headers(self, mock_core_client_constructor):
429492
mock_core_client_constructor.assert_called_once_with(
430493
url=URL, client_headers=headers
431494
)
495+
496+
@patch("toolbox_langchain.client.ToolboxCoreSyncClient")
497+
def test_context_manager(self, mock_core_client_constructor):
498+
"""Tests that the client can be used as a context manager."""
499+
with ToolboxClient(URL) as client:
500+
assert isinstance(client, ToolboxClient)
501+
mock_core_client_constructor.return_value.close.assert_not_called()
502+
mock_core_client_constructor.return_value.close.assert_called_once()
503+
504+
@pytest.mark.asyncio
505+
@patch("toolbox_langchain.client.ToolboxCoreSyncClient")
506+
async def test_async_context_manager(self, mock_core_client_constructor):
507+
"""Tests that the client can be used as an async context manager."""
508+
async with ToolboxClient(URL) as client:
509+
assert isinstance(client, ToolboxClient)
510+
mock_core_client_constructor.return_value.close.assert_not_called()
511+
mock_core_client_constructor.return_value.close.assert_called_once()
512+
513+
@patch("toolbox_langchain.client.ToolboxCoreSyncClient")
514+
def test_close(self, mock_core_client_constructor):
515+
"""Tests the close method."""
516+
client = ToolboxClient(URL)
517+
client.close()
518+
mock_core_client_constructor.return_value.close.assert_called_once()

packages/toolbox-llamaindex/integration.cloudbuild.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ steps:
2626
name: 'python:${_VERSION}'
2727
args:
2828
- install
29+
- '-e'
2930
- 'packages/toolbox-llamaindex[test]'
3031
- '--user'
3132
entrypoint: pip
3233
- id: Run integration tests
3334
name: 'python:${_VERSION}'
35+
dir: 'packages/toolbox-llamaindex'
3436
env:
3537
- TOOLBOX_URL=$_TOOLBOX_URL
3638
- TOOLBOX_VERSION=$_TOOLBOX_VERSION
@@ -39,7 +41,7 @@ steps:
3941
args:
4042
- '-c'
4143
- >-
42-
python -m pytest packages/toolbox-llamaindex/tests/
44+
python -m pytest --cov=src/toolbox_llamaindex --cov-report=term --cov-fail-under=90 tests/
4345
entrypoint: /bin/bash
4446
options:
4547
logging: CLOUD_LOGGING_ONLY

packages/toolbox-llamaindex/tests/test_client.py

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 Google LLC
1+
# Copyright 2025 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -431,3 +431,150 @@ def test_init_with_client_headers(self, mock_core_client_constructor):
431431
mock_core_client_constructor.assert_called_once_with(
432432
url=URL, client_headers=headers
433433
)
434+
435+
@patch("toolbox_llamaindex.client.ToolboxCoreSyncClient")
436+
def test_context_manager(self, mock_core_client_constructor):
437+
"""Tests that the client can be used as a context manager."""
438+
with ToolboxClient(URL) as client:
439+
assert isinstance(client, ToolboxClient)
440+
mock_core_client_constructor.return_value.close.assert_not_called()
441+
mock_core_client_constructor.return_value.close.assert_called_once()
442+
443+
@pytest.mark.asyncio
444+
@patch("toolbox_llamaindex.client.ToolboxCoreSyncClient")
445+
async def test_async_context_manager(self, mock_core_client_constructor):
446+
"""Tests that the client can be used as an async context manager."""
447+
async with ToolboxClient(URL) as client:
448+
assert isinstance(client, ToolboxClient)
449+
mock_core_client_constructor.return_value.close.assert_not_called()
450+
mock_core_client_constructor.return_value.close.assert_called_once()
451+
452+
@patch("toolbox_llamaindex.client.ToolboxCoreSyncClient")
453+
def test_close(self, mock_core_client_constructor):
454+
"""Tests the close method."""
455+
client = ToolboxClient(URL)
456+
client.close()
457+
mock_core_client_constructor.return_value.close.assert_called_once()
458+
459+
@patch("toolbox_core.sync_client.ToolboxSyncClient.load_toolset")
460+
def test_load_toolset_with_deprecated_args(
461+
self, mock_core_load_toolset, toolbox_client
462+
):
463+
mock_core_tool_instance = create_mock_core_sync_tool(model_name="MySetModel")
464+
mock_core_load_toolset.return_value = [mock_core_tool_instance]
465+
466+
auth_tokens_deprecated = {"token_deprecated": lambda: "value_dep"}
467+
auth_headers_deprecated = {"header_deprecated": lambda: "value_head_dep"}
468+
bound_params = {"param1": "value4"}
469+
toolset_name = "my_toolset"
470+
471+
# Scenario 2: auth_tokens and auth_headers provided, auth_token_getters is default (empty initially)
472+
with pytest.warns(DeprecationWarning) as record:
473+
toolbox_client.load_toolset(
474+
toolset_name,
475+
auth_tokens=auth_tokens_deprecated, # This will be used for auth_token_getters
476+
auth_headers=auth_headers_deprecated, # This will warn as auth_token_getters is now populated
477+
bound_params=bound_params,
478+
)
479+
assert len(record) == 2
480+
messages = sorted([str(r.message) for r in record])
481+
482+
assert (
483+
messages[0]
484+
== "Argument `auth_tokens` is deprecated. Use `auth_token_getters` instead."
485+
)
486+
assert (
487+
messages[1]
488+
== "Both `auth_token_getters` and `auth_headers` are provided. `auth_headers` is deprecated, and `auth_token_getters` will be used."
489+
)
490+
491+
expected_getters_for_call = auth_tokens_deprecated
492+
493+
mock_core_load_toolset.assert_called_with(
494+
name=toolset_name,
495+
auth_token_getters=expected_getters_for_call,
496+
bound_params=bound_params,
497+
strict=False,
498+
)
499+
mock_core_load_toolset.reset_mock()
500+
501+
with pytest.warns(
502+
DeprecationWarning,
503+
match="Argument `auth_headers` is deprecated. Use `auth_token_getters` instead.",
504+
) as record:
505+
toolbox_client.load_toolset(
506+
toolset_name,
507+
auth_headers=auth_headers_deprecated,
508+
bound_params=bound_params,
509+
)
510+
assert len(record) == 1
511+
512+
mock_core_load_toolset.assert_called_with(
513+
name=toolset_name,
514+
auth_token_getters=auth_headers_deprecated,
515+
bound_params=bound_params,
516+
strict=False,
517+
)
518+
519+
@pytest.mark.asyncio
520+
@patch("toolbox_core.sync_client.ToolboxSyncClient.load_toolset")
521+
async def test_aload_toolset_with_deprecated_args(
522+
self, mock_sync_core_load_toolset, toolbox_client
523+
):
524+
mock_core_tool_instance = create_mock_core_sync_tool(
525+
model_name="MyAsyncSetModel"
526+
)
527+
mock_sync_core_load_toolset.return_value = [mock_core_tool_instance]
528+
529+
auth_tokens_deprecated = {"token_deprecated": lambda: "value_dep"}
530+
auth_headers_deprecated = {"header_deprecated": lambda: "value_head_dep"}
531+
bound_params = {"param1": "value4"}
532+
toolset_name = "my_async_toolset"
533+
534+
# Scenario 2: auth_tokens and auth_headers provided, auth_token_getters is default (empty initially)
535+
with pytest.warns(DeprecationWarning) as record:
536+
await toolbox_client.aload_toolset(
537+
toolset_name,
538+
auth_tokens=auth_tokens_deprecated, # This will be used for auth_token_getters
539+
auth_headers=auth_headers_deprecated, # This will warn as auth_token_getters is now populated
540+
bound_params=bound_params,
541+
)
542+
assert len(record) == 2
543+
messages = sorted([str(r.message) for r in record])
544+
545+
assert (
546+
messages[0]
547+
== "Argument `auth_tokens` is deprecated. Use `auth_token_getters` instead."
548+
)
549+
assert (
550+
messages[1]
551+
== "Both `auth_token_getters` and `auth_headers` are provided. `auth_headers` is deprecated, and `auth_token_getters` will be used."
552+
)
553+
554+
expected_getters_for_call = auth_tokens_deprecated
555+
556+
mock_sync_core_load_toolset.assert_called_with(
557+
name=toolset_name,
558+
auth_token_getters=expected_getters_for_call,
559+
bound_params=bound_params,
560+
strict=False,
561+
)
562+
mock_sync_core_load_toolset.reset_mock()
563+
564+
with pytest.warns(
565+
DeprecationWarning,
566+
match="Argument `auth_headers` is deprecated. Use `auth_token_getters` instead.",
567+
) as record:
568+
await toolbox_client.aload_toolset(
569+
toolset_name,
570+
auth_headers=auth_headers_deprecated,
571+
bound_params=bound_params,
572+
)
573+
assert len(record) == 1
574+
575+
mock_sync_core_load_toolset.assert_called_with(
576+
name=toolset_name,
577+
auth_token_getters=auth_headers_deprecated,
578+
bound_params=bound_params,
579+
strict=False,
580+
)

0 commit comments

Comments
 (0)