mirror of
https://github.com/EvolutionAPI/adk-python.git
synced 2025-07-14 09:51:25 -06:00
fix(tests): use mock GCS client in artifact service tests to avoid real credentials
Copybara import of the project: -- ade1d98e030a966183f56cb5c9c1b04cf51f5337 by Thiago Neves <thiagohneves@gmail.com>: fix(tests): use mock GCS client in artifact service tests to avoid real credentials -- becd2925feebf60196129b029a0ab8d490f7b19e by Thiago Neves <thiagohneves@gmail.com>: test(agents): add unit tests for live_request_queue, readonly_context, and run_config COPYBARA_INTEGRATE_REVIEW=https://github.com/google/adk-python/pull/641 from thiagoneves:feature/increase-test-coverage 0f7a9fc55d97902e190a394f099324fbeb1541af PiperOrigin-RevId: 756798390
This commit is contained in:
parent
dc5c23c675
commit
ac97fc638f
52
tests/unittests/agents/test_live_request_queue.py
Normal file
52
tests/unittests/agents/test_live_request_queue.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import pytest
|
||||||
|
from unittest.mock import MagicMock, AsyncMock, patch
|
||||||
|
from google.adk.agents.live_request_queue import LiveRequest, LiveRequestQueue
|
||||||
|
from google.genai import types
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_close_queue():
|
||||||
|
queue = LiveRequestQueue()
|
||||||
|
|
||||||
|
with patch.object(queue._queue, "put_nowait") as mock_put_nowait:
|
||||||
|
queue.close()
|
||||||
|
mock_put_nowait.assert_called_once_with(LiveRequest(close=True))
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_content():
|
||||||
|
queue = LiveRequestQueue()
|
||||||
|
content = MagicMock(spec=types.Content)
|
||||||
|
|
||||||
|
with patch.object(queue._queue, "put_nowait") as mock_put_nowait:
|
||||||
|
queue.send_content(content)
|
||||||
|
mock_put_nowait.assert_called_once_with(LiveRequest(content=content))
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_realtime():
|
||||||
|
queue = LiveRequestQueue()
|
||||||
|
blob = MagicMock(spec=types.Blob)
|
||||||
|
|
||||||
|
with patch.object(queue._queue, "put_nowait") as mock_put_nowait:
|
||||||
|
queue.send_realtime(blob)
|
||||||
|
mock_put_nowait.assert_called_once_with(LiveRequest(blob=blob))
|
||||||
|
|
||||||
|
|
||||||
|
def test_send():
|
||||||
|
queue = LiveRequestQueue()
|
||||||
|
req = LiveRequest(content=MagicMock(spec=types.Content))
|
||||||
|
|
||||||
|
with patch.object(queue._queue, "put_nowait") as mock_put_nowait:
|
||||||
|
queue.send(req)
|
||||||
|
mock_put_nowait.assert_called_once_with(req)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get():
|
||||||
|
queue = LiveRequestQueue()
|
||||||
|
res = MagicMock(spec=types.Content)
|
||||||
|
|
||||||
|
with patch.object(queue._queue, "get", return_value=res) as mock_get:
|
||||||
|
result = await queue.get()
|
||||||
|
|
||||||
|
assert result == res
|
||||||
|
mock_get.assert_called_once()
|
33
tests/unittests/agents/test_readonly_context.py
Normal file
33
tests/unittests/agents/test_readonly_context.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import pytest
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
from types import MappingProxyType
|
||||||
|
from google.adk.agents.readonly_context import ReadonlyContext
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_invocation_context():
|
||||||
|
mock_context = MagicMock()
|
||||||
|
mock_context.invocation_id = "test-invocation-id"
|
||||||
|
mock_context.agent.name = "test-agent-name"
|
||||||
|
mock_context.session.state = {"key1": "value1", "key2": "value2"}
|
||||||
|
|
||||||
|
return mock_context
|
||||||
|
|
||||||
|
|
||||||
|
def test_invocation_id(mock_invocation_context):
|
||||||
|
readonly_context = ReadonlyContext(mock_invocation_context)
|
||||||
|
assert readonly_context.invocation_id == "test-invocation-id"
|
||||||
|
|
||||||
|
|
||||||
|
def test_agent_name(mock_invocation_context):
|
||||||
|
readonly_context = ReadonlyContext(mock_invocation_context)
|
||||||
|
assert readonly_context.agent_name == "test-agent-name"
|
||||||
|
|
||||||
|
|
||||||
|
def test_state_content(mock_invocation_context):
|
||||||
|
readonly_context = ReadonlyContext(mock_invocation_context)
|
||||||
|
state = readonly_context.state
|
||||||
|
|
||||||
|
assert isinstance(state, MappingProxyType)
|
||||||
|
assert state["key1"] == "value1"
|
||||||
|
assert state["key2"] == "value2"
|
31
tests/unittests/agents/test_run_config.py
Normal file
31
tests/unittests/agents/test_run_config.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import pytest
|
||||||
|
import sys
|
||||||
|
import logging
|
||||||
|
from unittest.mock import patch, ANY
|
||||||
|
from google.adk.agents.run_config import RunConfig
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_max_llm_calls_valid():
|
||||||
|
value = RunConfig.validate_max_llm_calls(100)
|
||||||
|
assert value == 100
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_max_llm_calls_negative():
|
||||||
|
with patch("google.adk.agents.run_config.logger.warning") as mock_warning:
|
||||||
|
value = RunConfig.validate_max_llm_calls(-1)
|
||||||
|
mock_warning.assert_called_once_with(ANY)
|
||||||
|
assert value == -1
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_max_llm_calls_warns_on_zero():
|
||||||
|
with patch("google.adk.agents.run_config.logger.warning") as mock_warning:
|
||||||
|
value = RunConfig.validate_max_llm_calls(0)
|
||||||
|
mock_warning.assert_called_once_with(ANY)
|
||||||
|
assert value == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_max_llm_calls_too_large():
|
||||||
|
with pytest.raises(
|
||||||
|
ValueError, match=f"max_llm_calls should be less than {sys.maxsize}."
|
||||||
|
):
|
||||||
|
RunConfig.validate_max_llm_calls(sys.maxsize)
|
Loading…
Reference in New Issue
Block a user