Skip to content

Commit e05dd5b

Browse files
Remove dead code
1 parent 2daa18d commit e05dd5b

2 files changed

Lines changed: 21 additions & 107 deletions

File tree

src/google/adk/integrations/firestore/firestore_session_service.py

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ async def _create_txn(transaction: firestore.AsyncTransaction) -> None:
152152
snap = await session_ref.get(transaction=transaction)
153153
if snap.exists:
154154
from ...errors.already_exists_error import AlreadyExistsError
155+
155156
raise AlreadyExistsError(f"Session {session_id} already exists.")
156157
transaction.set(session_ref, session_data)
157158

@@ -240,7 +241,9 @@ async def list_sessions(
240241
) -> ListSessionsResponse:
241242
"""Lists sessions from Firestore."""
242243
if user_id:
243-
query = self._get_sessions_ref(app_name, user_id).where("appName", "==", app_name)
244+
query = self._get_sessions_ref(app_name, user_id).where(
245+
"appName", "==", app_name
246+
)
244247
docs = await query.get()
245248
else:
246249
query = self.client.collection_group(self.sessions_collection).where(
@@ -320,52 +323,6 @@ async def delete_session(
320323

321324
await session_ref.delete()
322325

323-
324-
async def _update_app_state_transactional(
325-
self, app_name: str, delta: dict[str, Any]
326-
) -> dict[str, Any]:
327-
"""Atomically applies delta to app state inside a transaction."""
328-
from google.cloud import firestore
329-
330-
doc_ref = self.client.collection(self.app_state_collection).document(
331-
app_name
332-
)
333-
334-
@firestore.async_transactional # type: ignore[untyped-decorator]
335-
async def _txn(transaction: firestore.AsyncTransaction) -> dict[str, Any]:
336-
snap = await doc_ref.get(transaction=transaction)
337-
current = snap.to_dict() if snap.exists else {}
338-
current.update(delta)
339-
transaction.set(doc_ref, current, merge=True)
340-
return current
341-
342-
transaction = self.client.transaction()
343-
return cast(dict[str, Any], await _txn(transaction))
344-
345-
async def _update_user_state_transactional(
346-
self, app_name: str, user_id: str, delta: dict[str, Any]
347-
) -> dict[str, Any]:
348-
"""Atomically applies delta to user state inside a transaction."""
349-
from google.cloud import firestore
350-
351-
doc_ref = (
352-
self.client.collection(self.user_state_collection)
353-
.document(app_name)
354-
.collection("users")
355-
.document(user_id)
356-
)
357-
358-
@firestore.async_transactional # type: ignore[untyped-decorator]
359-
async def _txn(transaction: firestore.AsyncTransaction) -> dict[str, Any]:
360-
snap = await doc_ref.get(transaction=transaction)
361-
current = snap.to_dict() if snap.exists else {}
362-
current.update(delta)
363-
transaction.set(doc_ref, current, merge=True)
364-
return current
365-
366-
transaction = self.client.transaction()
367-
return cast(dict[str, Any], await _txn(transaction))
368-
369326
async def append_event(self, session: Session, event: Event) -> Event:
370327
"""Appends an event to a session in Firestore."""
371328
from google.cloud import firestore
@@ -376,7 +333,9 @@ async def append_event(self, session: Session, event: Event) -> Event:
376333
self._apply_temp_state(session, event)
377334
event = self._trim_temp_delta_state(event)
378335

379-
session_ref = self._get_sessions_ref(session.app_name, session.user_id).document(session.id)
336+
session_ref = self._get_sessions_ref(
337+
session.app_name, session.user_id
338+
).document(session.id)
380339

381340
if event.actions and event.actions.state_delta:
382341
state_delta = event.actions.state_delta
@@ -392,7 +351,9 @@ async def append_event(self, session: Session, event: Event) -> Event:
392351
else:
393352
session_updates[key] = value
394353

395-
app_ref = self.client.collection(self.app_state_collection).document(session.app_name)
354+
app_ref = self.client.collection(self.app_state_collection).document(
355+
session.app_name
356+
)
396357
user_ref = (
397358
self.client.collection(self.user_state_collection)
398359
.document(session.app_name)
@@ -403,8 +364,14 @@ async def append_event(self, session: Session, event: Event) -> Event:
403364
@firestore.async_transactional # type: ignore[untyped-decorator]
404365
async def _append_txn(transaction: firestore.AsyncTransaction) -> None:
405366
# 1. Reads
406-
app_snap = await app_ref.get(transaction=transaction) if app_updates else None
407-
user_snap = await user_ref.get(transaction=transaction) if user_updates else None
367+
app_snap = (
368+
await app_ref.get(transaction=transaction) if app_updates else None
369+
)
370+
user_snap = (
371+
await user_ref.get(transaction=transaction)
372+
if user_updates
373+
else None
374+
)
408375

409376
# 2. Writes
410377
if app_updates and app_snap is not None:
@@ -429,7 +396,9 @@ async def _append_txn(transaction: firestore.AsyncTransaction) -> None:
429396
)
430397

431398
event_id = event.id
432-
event_ref = session_ref.collection(self.events_collection).document(event_id)
399+
event_ref = session_ref.collection(self.events_collection).document(
400+
event_id
401+
)
433402
event_data = event.model_dump(exclude_none=True, mode="json")
434403
transaction.set(
435404
event_ref,

tests/unittests/integrations/firestore/test_firestore_session_service.py

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -577,62 +577,7 @@ async def test_append_event_partial(mock_firestore_client):
577577

578578

579579
@pytest.mark.asyncio
580-
async def test_update_app_state_transactional(mock_firestore_client):
581-
service = FirestoreSessionService(client=mock_firestore_client)
582-
583-
app_name = "test_app"
584-
delta = {"new_key": "new_val"}
585-
586-
transaction = mock.MagicMock()
587-
mock_firestore_client.transaction.return_value = transaction
588-
589-
doc_ref = mock.MagicMock()
590-
mock_firestore_client.collection.return_value.document.return_value = doc_ref
591-
592-
doc_snapshot = mock.MagicMock()
593-
doc_snapshot.exists = True
594-
doc_snapshot.to_dict.return_value = {"old_key": "old_val"}
595-
doc_ref.get = mock.AsyncMock(return_value=doc_snapshot)
596-
597-
with mock.patch("google.cloud.firestore.async_transactional", lambda x: x):
598-
result = await service._update_app_state_transactional(app_name, delta)
599-
600-
assert result == {"old_key": "old_val", "new_key": "new_val"}
601-
transaction.set.assert_called_once_with(
602-
doc_ref, {"old_key": "old_val", "new_key": "new_val"}, merge=True
603-
)
604-
605-
606-
@pytest.mark.asyncio
607-
async def test_update_user_state_transactional(mock_firestore_client):
608-
service = FirestoreSessionService(client=mock_firestore_client)
609-
610-
app_name = "test_app"
611-
user_id = "test_user"
612-
delta = {"new_key": "new_val"}
613-
614-
transaction = mock.MagicMock()
615-
mock_firestore_client.transaction.return_value = transaction
616580

617-
doc_ref = mock.MagicMock()
618-
mock_firestore_client.collection.return_value.document.return_value.collection.return_value.document.return_value = (
619-
doc_ref
620-
)
621-
622-
doc_snapshot = mock.MagicMock()
623-
doc_snapshot.exists = True
624-
doc_snapshot.to_dict.return_value = {"old_key": "old_val"}
625-
doc_ref.get = mock.AsyncMock(return_value=doc_snapshot)
626-
627-
with mock.patch("google.cloud.firestore.async_transactional", lambda x: x):
628-
result = await service._update_user_state_transactional(
629-
app_name, user_id, delta
630-
)
631-
632-
assert result == {"old_key": "old_val", "new_key": "new_val"}
633-
transaction.set.assert_called_once_with(
634-
doc_ref, {"old_key": "old_val", "new_key": "new_val"}, merge=True
635-
)
636581

637582

638583
@pytest.mark.asyncio

0 commit comments

Comments
 (0)