mirror of
https://github.com/EvolutionAPI/adk-python.git
synced 2025-12-19 03:42:22 -06:00
feat! Update session service interface to be async.
Also keep the sync version in the InMemorySessionService as create_session_sync() as a temporary migration option. PiperOrigin-RevId: 759252188
This commit is contained in:
committed by
Copybara-Service
parent
5b3204c356
commit
1804ca39a6
@@ -15,7 +15,7 @@
|
||||
import re
|
||||
import this
|
||||
from typing import Any
|
||||
|
||||
import uuid
|
||||
from dateutil.parser import isoparse
|
||||
from google.adk.events import Event
|
||||
from google.adk.events import EventActions
|
||||
@@ -124,9 +124,7 @@ class MockApiClient:
|
||||
this.session_dict: dict[str, Any] = {}
|
||||
this.event_dict: dict[str, list[Any]] = {}
|
||||
|
||||
async def async_request(
|
||||
self, http_method: str, path: str, request_dict: dict[str, Any]
|
||||
):
|
||||
def request(self, http_method: str, path: str, request_dict: dict[str, Any]):
|
||||
"""Mocks the API Client request method."""
|
||||
if http_method == 'GET':
|
||||
if re.match(SESSION_REGEX, path):
|
||||
@@ -212,52 +210,46 @@ def mock_vertex_ai_session_service():
|
||||
return service
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_empty_session():
|
||||
def test_get_empty_session():
|
||||
session_service = mock_vertex_ai_session_service()
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
assert await session_service.get_session(
|
||||
assert session_service.get_session(
|
||||
app_name='123', user_id='user', session_id='0'
|
||||
)
|
||||
assert str(excinfo.value) == 'Session not found: 0'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_and_delete_session():
|
||||
def test_get_and_delete_session():
|
||||
session_service = mock_vertex_ai_session_service()
|
||||
|
||||
assert (
|
||||
await session_service.get_session(
|
||||
session_service.get_session(
|
||||
app_name='123', user_id='user', session_id='1'
|
||||
)
|
||||
== MOCK_SESSION
|
||||
)
|
||||
|
||||
await session_service.delete_session(
|
||||
app_name='123', user_id='user', session_id='1'
|
||||
)
|
||||
session_service.delete_session(app_name='123', user_id='user', session_id='1')
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
assert await session_service.get_session(
|
||||
assert session_service.get_session(
|
||||
app_name='123', user_id='user', session_id='1'
|
||||
)
|
||||
assert str(excinfo.value) == 'Session not found: 1'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_sessions():
|
||||
def test_list_sessions():
|
||||
session_service = mock_vertex_ai_session_service()
|
||||
sessions = await session_service.list_sessions(app_name='123', user_id='user')
|
||||
sessions = session_service.list_sessions(app_name='123', user_id='user')
|
||||
assert len(sessions.sessions) == 2
|
||||
assert sessions.sessions[0].id == '1'
|
||||
assert sessions.sessions[1].id == '2'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_session():
|
||||
def test_create_session():
|
||||
session_service = mock_vertex_ai_session_service()
|
||||
|
||||
state = {'key': 'value'}
|
||||
session = await session_service.create_session(
|
||||
session = session_service.create_session(
|
||||
app_name='123', user_id='user', state=state
|
||||
)
|
||||
assert session.state == state
|
||||
@@ -266,17 +258,16 @@ async def test_create_session():
|
||||
assert session.last_update_time is not None
|
||||
|
||||
session_id = session.id
|
||||
assert session == await session_service.get_session(
|
||||
assert session == session_service.get_session(
|
||||
app_name='123', user_id='user', session_id=session_id
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_session_with_custom_session_id():
|
||||
def test_create_session_with_custom_session_id():
|
||||
session_service = mock_vertex_ai_session_service()
|
||||
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
await session_service.create_session(
|
||||
session_service.create_session(
|
||||
app_name='123', user_id='user', session_id='1'
|
||||
)
|
||||
assert str(excinfo.value) == (
|
||||
|
||||
Reference in New Issue
Block a user