Skip to content

Commit b29afb7

Browse files
Fix tests
1 parent 7760b70 commit b29afb7

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

tests/unittests/sessions/test_firestore_session_service.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,50 @@ async def test_append_event(mock_firestore_client):
164164
batch.set.assert_called_once() # For event
165165
batch.update.assert_called_once() # For session updateTime
166166
batch.commit.assert_called_once()
167+
168+
169+
@pytest.mark.asyncio
170+
async def test_append_event_with_state_delta(mock_firestore_client):
171+
service = FirestoreSessionService(client=mock_firestore_client)
172+
app_name = "test_app"
173+
user_id = "test_user"
174+
from google.adk.sessions.session import Session
175+
176+
session = Session(id="test_session", app_name=app_name, user_id=user_id)
177+
178+
# Using MagicMock for Event to bypass complex pydantic validation for test
179+
event = mock.MagicMock()
180+
event.partial = False
181+
event.id = "test_event_id"
182+
# Mock actions.state_delta
183+
event.actions.state_delta = {
184+
"_app_my_key": "app_val",
185+
"_user_my_key": "user_val",
186+
"session_key": "session_val",
187+
}
188+
# Mock model_dump to return valid event data
189+
event.model_dump.return_value = {"id": "test_event_id", "author": "user"}
190+
191+
await service.append_event(session, event)
192+
193+
mock_firestore_client.batch.assert_called_once()
194+
batch = mock_firestore_client.batch.return_value
195+
196+
# Verify app state set
197+
# In code: batch.set(app_ref, app_updates, merge=True)
198+
# But app_ref is a mock! Which mock?
199+
# It's mock_firestore_client.collection().document()
200+
# In fixture: collection_ref = mock.AsyncMock()
201+
# doc_ref = mock.AsyncMock()
202+
# client.collection.return_value = collection_ref
203+
# collection_ref.document.return_value = doc_ref
204+
# So batch.set is called with app_ref (which is doc_ref)
205+
batch.set.assert_called()
206+
207+
# Verify session state updated in memory
208+
assert session.state["session_key"] == "session_val"
209+
210+
# Verify batch update was called for session
211+
batch.update.assert_called_once()
212+
213+
batch.commit.assert_called_once()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
from unittest import mock
18+
19+
import pytest
20+
from google.adk.agents.base_agent import BaseAgent
21+
from google.adk.firestore_database_runner import create_firestore_runner
22+
23+
24+
@pytest.fixture
25+
def mock_agent():
26+
agent = mock.MagicMock(spec=BaseAgent)
27+
agent.name = "test_agent"
28+
return agent
29+
30+
31+
def test_create_firestore_runner_with_arg(mock_agent, monkeypatch):
32+
monkeypatch.delenv("ADK_GCS_BUCKET_NAME", raising=False)
33+
34+
# Mock GcsArtifactService to avoid real client init
35+
with mock.patch(
36+
"google.adk.firestore_database_runner.GcsArtifactService"
37+
) as mock_gcs:
38+
runner = create_firestore_runner(mock_agent, gcs_bucket_name="test_bucket")
39+
40+
assert runner is not None
41+
mock_gcs.assert_called_once_with(bucket_name="test_bucket")
42+
43+
44+
def test_create_firestore_runner_with_env(mock_agent, monkeypatch):
45+
monkeypatch.setenv("ADK_GCS_BUCKET_NAME", "env_bucket")
46+
47+
with mock.patch(
48+
"google.adk.firestore_database_runner.GcsArtifactService"
49+
) as mock_gcs:
50+
runner = create_firestore_runner(mock_agent)
51+
52+
assert runner is not None
53+
mock_gcs.assert_called_once_with(bucket_name="env_bucket")
54+
55+
56+
def test_create_firestore_runner_missing_bucket(mock_agent, monkeypatch):
57+
monkeypatch.delenv("ADK_GCS_BUCKET_NAME", raising=False)
58+
59+
with pytest.raises(
60+
ValueError, match="Required property 'ADK_GCS_BUCKET_NAME' is not set"
61+
):
62+
create_firestore_runner(mock_agent)

0 commit comments

Comments
 (0)