8000 fix(tests): use mock GCS client in artifact service tests to avoid re… · usingsystem007/adk-python@ac97fc6 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac97fc6

Browse files
thiagoneve
10000
scopybara-github
authored andcommitted
fix(tests): use mock GCS client in artifact service tests to avoid real credentials
Copybara import of the project: -- ade1d98 by Thiago Neves <thiagohneves@gmail.com>: fix(tests): use mock GCS client in artifact service tests to avoid real credentials -- becd292 by Thiago Neves <thiagohneves@gmail.com>: test(agents): add unit tests for live_request_queue, readonly_context, and run_config COPYBARA_INTEGRATE_REVIEW=google#641 from thiagoneves:feature/increase-test-coverage 0f7a9fc PiperOrigin-RevId: 756798390
1 parent dc5c23c commit ac97fc6

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pytest
2+
from unittest.mock import MagicMock, AsyncMock, patch
3+
from google.adk.agents.live_request_queue import LiveRequest, LiveRequestQueue
4+
from google.genai import types
5+
6+
7+
@pytest.mark.asyncio
8+
async def test_close_queue():
9+
queue = LiveRequestQueue()
10+
11+
with patch.object(queue._queue, "put_nowait") as mock_put_nowait:
12+
queue.close()
13+
mock_put_nowait.assert_called_once_with(LiveRequest(close=True))
14+
15+
16+
def test_send_content():
17+
queue = LiveRequestQueue()
18+
content = MagicMock(spec=types.Content)
19+
20+
with patch.object(queue._queue, "put_nowait") as mock_put_nowait:
21+
queue.send_content(content)
22+
mock_put_nowait.assert_called_once_with(LiveRequest(content=content))
23+
24+
25+
def test_send_realtime():
26+
queue = LiveRequestQueue()
27+
blob = MagicMock(spec=types.Blob)
28+
29+
with patch.object(queue._queue, "put_nowait") as mock_put_nowait:
30+
queue.send_realtime(blob)
31+
mock_put_nowait.assert_called_once_with(LiveRequest(blob=blob))
32+
33+
34+
def test_send():
35+
queue = LiveRequestQueue()
36+
req = LiveRequest(content=MagicMock(spec=types.Content))
37+
38+
with patch.object(queue._queue, "put_nowait") as mock_put_nowait:
39+
queue.send(req)
40+
mock_put_nowait.assert_called_once_with(req)
41+
42+
43+
@pytest.mark.asyncio
44+
async def test_get():
45+
queue = LiveRequestQueue()
46+
res = MagicMock(spec=types.Content)
47+
48+
with patch.object(queue._queue, "get", return_value=res) as mock_get:
49+
result = await queue.get()
50+
51+
assert result == res
52+
mock_get.assert_called_once()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
from unittest.mock import MagicMock
3+
from types import MappingProxyType
4+
from google.adk.agents.readonly_context import ReadonlyContext
5+
6+
7+
@pytest.fixture
8+
def mock_invocation_context():
9+
mock_context = MagicMock()
10+
mock_context.invocation_id = "test-invocation-id"
11+
mock_context.agent.name = "test-agent-name"
12+
mock_context.session.state = {"key1": "value1", "key2": "value2"}
13+
14+
return mock_context
15+
16+
17+
def test_invocation_id(mock_invocation_context):
18+
readonly_context = ReadonlyContext(mock_invocation_context)
19+
assert readonly_context.invocation_id == "test-invocation-id"
20+
21+
22+
def test_agent_name(mock_invocation_context):
23+
readonly_context = ReadonlyContext(mock_invocation_context)
24+
assert readonly_context.agent_name == "test-agent-name"
25+
26+
27+
def test_state_content(mock_invocation_context):
28+
readonly_context = ReadonlyContext(mock_invocation_context)
29+
state = readonly_context.state
30+
31+
assert isinstance(state, MappingProxyType)
32+
assert state["key1"] == "value1"
33+
assert state["key2"] == "value2"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
import sys
3+
import logging
4+
from unittest.mock import patch, ANY
5+
from google.adk.agents.run_config import RunConfig
6+
7+
8+
def test_validate_max_llm_calls_valid():
9+
value = RunConfig.validate_max_llm_calls(100)
10+
assert value == 100
11+
12+
13+
def test_validate_max_llm_calls_negative():
14+
with patch("google.adk.agents.run_config.logger.warning") as mock_warning:
15+
value = RunConfig.validate_max_llm_calls(-1)
16+
mock_warning.assert_called_once_with(ANY)
17+
assert value == -1
18+
19+
20+
def test_validate_max_llm_calls_warns_on_zero():
21+
with patch("google.adk.agents.run_config.logger.warning") as mock_warning:
22+
value = RunConfig.validate_max_llm_calls(0)
23+
mock_warning.assert_called_once_with(ANY)
24+
assert value == 0
25+
26+
27+
def test_validate_max_llm_calls_too_large():
28+
with pytest.raises(
29+
ValueError, match=f"max_llm_calls should be less than {sys.maxsize}."
30+
):
31+
RunConfig.validate_max_llm_calls(sys.maxsize)

0 commit comments

Comments
 (0)
0