| 1 | from swarm import Swarm |
| 2 | from agents import weather_agent |
| 3 | import pytest |
| 4 | |
| 5 | client = Swarm() |
| 6 | |
| 7 | |
| 8 | def run_and_get_tool_calls(agent, query): |
| 9 | message = {"role": "user", "content": query} |
| 10 | response = client.run( |
| 11 | agent=agent, |
| 12 | messages=[message], |
| 13 | execute_tools=False, |
| 14 | ) |
| 15 | return response.messages[-1].get("tool_calls") |
| 16 | |
| 17 | |
| 18 | @pytest.mark.parametrize( |
| 19 | "query", |
| 20 | [ |
| 21 | "What's the weather in NYC?", |
| 22 | "Tell me the weather in London.", |
| 23 | "Do I need an umbrella today? I'm in chicago.", |
| 24 | ], |
| 25 | ) |
| 26 | def test_calls_weather_when_asked(query): |
| 27 | tool_calls = run_and_get_tool_calls(weather_agent, query) |
| 28 | |
| 29 | assert len(tool_calls) == 1 |
| 30 | assert tool_calls[0]["function"]["name"] == "get_weather" |
| 31 | |
| 32 | |
| 33 | @pytest.mark.parametrize( |
| 34 | "query", |
| 35 | [ |
| 36 | "Who's the president of the United States?", |
| 37 | "What is the time right now?", |
| 38 | "Hi!", |
| 39 | ], |
| 40 | ) |
| 41 | def test_does_not_call_weather_when_not_asked(query): |
| 42 | tool_calls = run_and_get_tool_calls(weather_agent, query) |
| 43 | |
| 44 | assert not tool_calls |