Skip to content

Commit 3d67cc6

Browse files
author
Rafael Pierre
committed
fix tests
1 parent cb31507 commit 3d67cc6

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/agents/memory/providers/redis.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,36 @@ async def add_item(self, item: TResponseInputItem) -> None:
144144

145145
await pipeline.execute()
146146

147+
async def add_items(self, items: list[TResponseInputItem]) -> None:
148+
"""Add multiple items to the session's conversation history.
149+
150+
Args:
151+
items: List of response input items to add
152+
"""
153+
if not items:
154+
return
155+
156+
client = await self._get_redis_client()
157+
158+
# Ensure session exists first
159+
await self._ensure_session_exists(client)
160+
161+
# Serialize all items and add them to the messages list in one rpush call
162+
serialized_items = [json.dumps(item) for item in items]
163+
pipeline = client.pipeline()
164+
pipeline.rpush(self.messages_key, *serialized_items)
165+
166+
# Update session timestamp
167+
current_time = time.time()
168+
pipeline.hset(self.session_key, "updated_at", str(current_time))
169+
170+
# Set expiration on both keys if TTL is configured
171+
if self.ttl:
172+
pipeline.expire(self.session_key, self.ttl)
173+
pipeline.expire(self.messages_key, self.ttl)
174+
175+
await pipeline.execute()
176+
147177
async def pop_item(self) -> TResponseInputItem | None:
148178
"""Remove and return the most recent item from the session.
149179

tests/test_redis_session.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,10 @@ async def test_add_items(
188188
mock_time.return_value = 1234567890.0
189189
mock_from_url.return_value = mock_redis
190190

191-
# Create a proper async context manager mock
192-
mock_pipeline = AsyncMock()
193-
mock_context_manager = AsyncMock()
194-
mock_context_manager.__aenter__.return_value = mock_pipeline
195-
mock_context_manager.__aexit__.return_value = None
196-
197-
# Make pipeline() return the context manager directly (not a coroutine)
198-
mock_redis.pipeline = MagicMock(return_value=mock_context_manager)
191+
# Create a mock pipeline that's returned directly (not as context manager)
192+
mock_pipeline = MagicMock()
193+
mock_pipeline.execute = AsyncMock()
194+
mock_redis.pipeline = MagicMock(return_value=mock_pipeline)
199195

200196
# Mock _ensure_session_exists method
201197
redis_session._ensure_session_exists = AsyncMock()

0 commit comments

Comments
 (0)