Skip to content

Commit 6c2cbf5

Browse files
author
Murat Kaan Meral
committed
Fix all model unit tests to use new API
Updated all test calls from old API to new API: - .connect() → .start() - .close() → .stop() - Updated error message expectations to match actual errors All tests now passing: - ✅ 47/47 bidirectional streaming tests passing - ✅ 14/14 type tests - ✅ 33/33 model tests - ✅ 2/2 integration tests
1 parent 272a1fc commit 6c2cbf5

3 files changed

Lines changed: 71 additions & 71 deletions

File tree

tests/strands/experimental/bidirectional_streaming/models/test_gemini_live.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -114,36 +114,36 @@ async def test_connection_lifecycle(mock_genai_client, model, system_prompt, too
114114
mock_client, mock_live_session, mock_live_session_cm = mock_genai_client
115115

116116
# Test basic connection
117-
await model.connect()
117+
await model.start()
118118
assert model._active is True
119119
assert model.connection_id is not None
120120
assert model.live_session == mock_live_session
121121
mock_client.aio.live.connect.assert_called_once()
122122

123123
# Test close
124-
await model.close()
124+
await model.stop()
125125
assert model._active is False
126126
mock_live_session_cm.__aexit__.assert_called_once()
127127

128128
# Test connection with system prompt
129-
await model.connect(system_prompt=system_prompt)
129+
await model.start(system_prompt=system_prompt)
130130
call_args = mock_client.aio.live.connect.call_args
131131
config = call_args.kwargs.get("config", {})
132132
assert config.get("system_instruction") == system_prompt
133-
await model.close()
133+
await model.stop()
134134

135135
# Test connection with tools
136-
await model.connect(tools=[tool_spec])
136+
await model.start(tools=[tool_spec])
137137
call_args = mock_client.aio.live.connect.call_args
138138
config = call_args.kwargs.get("config", {})
139139
assert "tools" in config
140140
assert len(config["tools"]) > 0
141-
await model.close()
141+
await model.stop()
142142

143143
# Test connection with messages
144-
await model.connect(messages=messages)
144+
await model.start(messages=messages)
145145
mock_live_session.send_client_content.assert_called()
146-
await model.close()
146+
await model.stop()
147147

148148

149149
@pytest.mark.asyncio
@@ -155,28 +155,28 @@ async def test_connection_edge_cases(mock_genai_client, api_key, model_id):
155155
model1 = BidiGeminiLiveModel(model_id=model_id, api_key=api_key)
156156
mock_client.aio.live.connect.side_effect = Exception("Connection failed")
157157
with pytest.raises(Exception, match="Connection failed"):
158-
await model1.connect()
158+
await model1.start()
159159

160160
# Reset mock for next tests
161161
mock_client.aio.live.connect.side_effect = None
162162

163163
# Test double connection
164164
model2 = BidiGeminiLiveModel(model_id=model_id, api_key=api_key)
165-
await model2.connect()
165+
await model2.start()
166166
with pytest.raises(RuntimeError, match="Connection already active"):
167-
await model2.connect()
168-
await model2.close()
167+
await model2.start()
168+
await model2.stop()
169169

170170
# Test close when not connected
171171
model3 = BidiGeminiLiveModel(model_id=model_id, api_key=api_key)
172-
await model3.close() # Should not raise
172+
await model3.stop() # Should not raise
173173

174174
# Test close error handling
175175
model4 = BidiGeminiLiveModel(model_id=model_id, api_key=api_key)
176-
await model4.connect()
176+
await model4.start()
177177
mock_live_session_cm.__aexit__.side_effect = Exception("Close failed")
178178
with pytest.raises(Exception, match="Close failed"):
179-
await model4.close()
179+
await model4.stop()
180180

181181

182182
# Send Method Tests
@@ -186,7 +186,7 @@ async def test_connection_edge_cases(mock_genai_client, api_key, model_id):
186186
async def test_send_all_content_types(mock_genai_client, model):
187187
"""Test sending all content types through unified send() method."""
188188
_, mock_live_session, _ = mock_genai_client
189-
await model.connect()
189+
await model.start()
190190

191191
# Test text input
192192
text_input = BidiTextInputEvent(text="Hello", role="user")
@@ -228,7 +228,7 @@ async def test_send_all_content_types(mock_genai_client, model):
228228
await model.send(ToolResultEvent(tool_result))
229229
mock_live_session.send_tool_response.assert_called_once()
230230

231-
await model.close()
231+
await model.stop()
232232

233233

234234
@pytest.mark.asyncio
@@ -242,11 +242,11 @@ async def test_send_edge_cases(mock_genai_client, model):
242242
mock_live_session.send_client_content.assert_not_called()
243243

244244
# Test unknown content type
245-
await model.connect()
245+
await model.start()
246246
unknown_content = {"unknown_field": "value"}
247247
await model.send(unknown_content) # Should not raise, just log warning
248248

249-
await model.close()
249+
await model.stop()
250250

251251

252252
# Receive Method Tests
@@ -263,15 +263,15 @@ async def test_receive_lifecycle_events(mock_genai_client, model, agenerator):
263263
_, mock_live_session, _ = mock_genai_client
264264
mock_live_session.receive.return_value = agenerator([])
265265

266-
await model.connect()
266+
await model.start()
267267

268268
# Collect events
269269
events = []
270270
async for event in model.receive():
271271
events.append(event)
272272
# Close after first event to trigger connection end
273273
if len(events) == 1:
274-
await model.close()
274+
await model.stop()
275275

276276
# Verify connection start and end
277277
assert len(events) >= 2
@@ -290,7 +290,7 @@ async def test_event_conversion(mock_genai_client, model):
290290
)
291291

292292
_, _, _ = mock_genai_client
293-
await model.connect()
293+
await model.start()
294294

295295
# Test text output (converted to transcript)
296296
mock_text = unittest.mock.Mock()
@@ -360,7 +360,7 @@ async def test_event_conversion(mock_genai_client, model):
360360
assert isinstance(interrupt_event, BidiInterruptionEvent)
361361
assert interrupt_event.reason == "user_speech"
362362

363-
await model.close()
363+
await model.stop()
364364

365365

366366
# Helper Method Tests

tests/strands/experimental/bidirectional_streaming/models/test_novasonic.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async def nova_model(model_id, region):
5757
yield model
5858
# Cleanup
5959
if model._active:
60-
await model.close()
60+
await model.stop()
6161

6262

6363
# Initialization and Connection Tests
@@ -82,14 +82,14 @@ async def test_connection_lifecycle(nova_model, mock_client, mock_stream):
8282
nova_model.client = mock_client
8383

8484
# Test basic connection
85-
await nova_model.connect(system_prompt="Test system prompt")
85+
await nova_model.start(system_prompt="Test system prompt")
8686
assert nova_model._active
8787
assert nova_model.stream == mock_stream
8888
assert nova_model.connection_id is not None
8989
assert mock_client.invoke_model_with_bidirectional_stream.called
9090

9191
# Test close
92-
await nova_model.close()
92+
await nova_model.stop()
9393
assert not nova_model._active
9494
assert mock_stream.input_stream.close.called
9595

@@ -101,10 +101,10 @@ async def test_connection_lifecycle(nova_model, mock_client, mock_stream):
101101
"inputSchema": {"json": json.dumps({"type": "object", "properties": {}})}
102102
}
103103
]
104-
await nova_model.connect(system_prompt="You are helpful", tools=tools)
104+
await nova_model.start(system_prompt="You are helpful", tools=tools)
105105
# Verify initialization events were sent (connectionStart, promptStart, system prompt)
106106
assert mock_stream.input_stream.send.call_count >= 3
107-
await nova_model.close()
107+
await nova_model.stop()
108108

109109

110110
@pytest.mark.asyncio
@@ -114,15 +114,15 @@ async def test_connection_edge_cases(nova_model, mock_client, mock_stream, model
114114
nova_model.client = mock_client
115115

116116
# Test double connection
117-
await nova_model.connect()
117+
await nova_model.start()
118118
with pytest.raises(RuntimeError, match="Connection already active"):
119-
await nova_model.connect()
120-
await nova_model.close()
119+
await nova_model.start()
120+
await nova_model.stop()
121121

122122
# Test close when already closed
123123
model2 = BidiNovaSonicModel(model_id=model_id, region=region)
124-
await model2.close() # Should not raise
125-
await model2.close() # Second call should also be safe
124+
await model2.stop() # Should not raise
125+
await model2.stop() # Second call should also be safe
126126

127127

128128
# Send Method Tests
@@ -140,7 +140,7 @@ async def test_send_all_content_types(nova_model, mock_client, mock_stream):
140140
with patch.object(nova_model, "_initialize_client", new_callable=AsyncMock):
141141
nova_model.client = mock_client
142142

143-
await nova_model.connect()
143+
await nova_model.start()
144144

145145
# Test text content
146146
text_event = BidiTextInputEvent(text="Hello, Nova!", role="user")
@@ -171,7 +171,7 @@ async def test_send_all_content_types(nova_model, mock_client, mock_stream):
171171
# Should send contentStart, toolResult, and contentEnd
172172
assert mock_stream.input_stream.send.called
173173

174-
await nova_model.close()
174+
await nova_model.stop()
175175

176176

177177
@pytest.mark.asyncio
@@ -190,7 +190,7 @@ async def test_send_edge_cases(nova_model, mock_client, mock_stream, caplog):
190190
await nova_model.send(text_event) # Should not raise
191191

192192
# Test image content (not supported, base64 encoded, no encoding parameter)
193-
await nova_model.connect()
193+
await nova_model.start()
194194
import base64
195195
image_b64 = base64.b64encode(b"image data").decode('utf-8')
196196
image_event = BidiImageInputEvent(
@@ -201,7 +201,7 @@ async def test_send_edge_cases(nova_model, mock_client, mock_stream, caplog):
201201
# Should log warning about unsupported image input
202202
assert any("not supported" in record.message.lower() for record in caplog.records)
203203

204-
await nova_model.close()
204+
await nova_model.stop()
205205

206206

207207
# Receive and Event Conversion Tests
@@ -220,7 +220,7 @@ async def mock_wait_for(*args, **kwargs):
220220
raise asyncio.TimeoutError()
221221

222222
with patch("asyncio.wait_for", side_effect=mock_wait_for):
223-
await nova_model.connect()
223+
await nova_model.start()
224224

225225
events = []
226226
async for event in nova_model.receive():
@@ -333,7 +333,7 @@ async def test_audio_connection_lifecycle(nova_model, mock_client, mock_stream):
333333
with patch.object(nova_model, "_initialize_client", new_callable=AsyncMock):
334334
nova_model.client = mock_client
335335

336-
await nova_model.connect()
336+
await nova_model.start()
337337

338338
# Start audio connection
339339
await nova_model._start_audio_connection()
@@ -343,7 +343,7 @@ async def test_audio_connection_lifecycle(nova_model, mock_client, mock_stream):
343343
await nova_model._end_audio_input()
344344
assert not nova_model.audio_connection_active
345345

346-
await nova_model.close()
346+
await nova_model.stop()
347347

348348

349349
@pytest.mark.asyncio
@@ -355,7 +355,7 @@ async def test_silence_detection(nova_model, mock_client, mock_stream):
355355
nova_model.client = mock_client
356356
nova_model.silence_threshold = 0.1 # Short threshold for testing
357357

358-
await nova_model.connect()
358+
await nova_model.start()
359359

360360
# Send audio to start connection (base64 encoded)
361361
import base64
@@ -376,7 +376,7 @@ async def test_silence_detection(nova_model, mock_client, mock_stream):
376376
# Audio connection should be ended
377377
assert not nova_model.audio_connection_active
378378

379-
await nova_model.close()
379+
await nova_model.stop()
380380

381381

382382
# Helper Method Tests
@@ -458,10 +458,10 @@ async def mock_error(*args, **kwargs):
458458

459459
mock_stream.await_output.side_effect = mock_error
460460

461-
await nova_model.connect()
461+
await nova_model.start()
462462

463463
# Wait a bit for response processor to handle error
464464
await asyncio.sleep(0.1)
465465

466466
# Should still be able to close cleanly
467-
await nova_model.close()
467+
await nova_model.stop()

0 commit comments

Comments
 (0)