Skip to content

Commit 8550090

Browse files
committed
refactor: improve test_autoagents.py for clarity and validation
- Updated comments for better understanding of test cases. - Modified task configuration to create a valid config before simulating error conditions. - Changed patch references from 'praisonaiagents.agents.autoagents.supports_structured_outputs' to 'praisonaiagents.llm.supports_structured_outputs' for consistency across tests. These changes enhance the readability and maintainability of the test suite while ensuring accurate validation scenarios.
1 parent 72b0e6f commit 8550090

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/praisonai/tests/unit/test_autoagents.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def test_validate_config_failure_not_taskconfig(self):
139139
auto_agents.instructions = "Test instructions"
140140
auto_agents.max_agents = 3
141141

142-
# Create a config with a non-TaskConfig task
142+
# Create a valid config first, then modify it to bypass Pydantic validation
143143
config = AutoAgentsConfig(
144144
main_instruction="Test",
145145
process_type="sequential",
@@ -150,12 +150,19 @@ def test_validate_config_failure_not_taskconfig(self):
150150
goal="Goal",
151151
backstory="Story",
152152
tools=[],
153-
tasks=["This is a string task"] # This will cause validation to fail
153+
tasks=[
154+
TaskConfig(
155+
name="Valid Task",
156+
description="Valid description",
157+
expected_output="Valid output",
158+
tools=[]
159+
)
160+
]
154161
)
155162
]
156163
)
157164

158-
# Mock the task to bypass Pydantic validation for testing
165+
# Now manually replace the task with a string to simulate the error condition
159166
config.agents[0].tasks = ["This is a string task"]
160167

161168
is_valid, error_msg = auto_agents._validate_config(config)
@@ -213,7 +220,7 @@ def test_validate_config_failure_missing_description(self, sample_invalid_config
213220
assert is_valid is False
214221
assert "has no description" in error_msg
215222

216-
@patch('praisonaiagents.agents.autoagents.supports_structured_outputs')
223+
@patch('praisonaiagents.llm.supports_structured_outputs')
217224
@patch('praisonaiagents.agents.autoagents.get_openai_client')
218225
def test_generate_config_openai_success(self, mock_get_client, mock_supports_structured, sample_valid_config, mock_tools):
219226
"""Test successful config generation using OpenAI structured output"""
@@ -244,7 +251,7 @@ def test_generate_config_openai_success(self, mock_get_client, mock_supports_str
244251
mock_client.parse_structured_output.assert_called_once()
245252
mock_create.assert_called_once_with(sample_valid_config)
246253

247-
@patch('praisonaiagents.agents.autoagents.supports_structured_outputs')
254+
@patch('praisonaiagents.llm.supports_structured_outputs')
248255
@patch('praisonaiagents.agents.autoagents.LLM')
249256
def test_generate_config_llm_success(self, mock_llm_class, mock_supports_structured, sample_valid_config, mock_tools):
250257
"""Test successful config generation using generic LLM"""
@@ -275,7 +282,7 @@ def test_generate_config_llm_success(self, mock_llm_class, mock_supports_structu
275282
mock_llm.get_response.assert_called_once()
276283
mock_create.assert_called_once_with(sample_valid_config)
277284

278-
@patch('praisonaiagents.agents.autoagents.supports_structured_outputs')
285+
@patch('praisonaiagents.llm.supports_structured_outputs')
279286
@patch('praisonaiagents.agents.autoagents.LLM')
280287
def test_generate_config_with_markdown_response(self, mock_llm_class, mock_supports_structured, sample_valid_config, mock_tools):
281288
"""Test config generation when LLM returns markdown-wrapped JSON"""
@@ -307,7 +314,7 @@ def test_generate_config_with_markdown_response(self, mock_llm_class, mock_suppo
307314
# The config should have been generated successfully despite markdown wrapping
308315
mock_create.assert_called_once_with(sample_valid_config)
309316

310-
@patch('praisonaiagents.agents.autoagents.supports_structured_outputs')
317+
@patch('praisonaiagents.llm.supports_structured_outputs')
311318
@patch('praisonaiagents.agents.autoagents.get_openai_client')
312319
def test_generate_config_retry_on_validation_failure(self, mock_get_client, mock_supports_structured, sample_valid_config, mock_tools):
313320
"""Test retry mechanism when validation fails"""
@@ -361,7 +368,7 @@ def test_generate_config_retry_on_validation_failure(self, mock_get_client, mock
361368
assert "Configuration validation failed" in str(mock_warning.call_args)
362369
mock_create.assert_called_once_with(sample_valid_config)
363370

364-
@patch('praisonaiagents.agents.autoagents.supports_structured_outputs')
371+
@patch('praisonaiagents.llm.supports_structured_outputs')
365372
@patch('praisonaiagents.agents.autoagents.get_openai_client')
366373
def test_generate_config_max_retries_exceeded(self, mock_get_client, mock_supports_structured, mock_tools):
367374
"""Test that max retries are properly enforced"""
@@ -403,7 +410,7 @@ def test_generate_config_max_retries_exceeded(self, mock_get_client, mock_suppor
403410
# Verify all retries were attempted
404411
assert mock_client.parse_structured_output.call_count == 3
405412

406-
@patch('praisonaiagents.agents.autoagents.supports_structured_outputs')
413+
@patch('praisonaiagents.llm.supports_structured_outputs')
407414
@patch('praisonaiagents.agents.autoagents.get_openai_client')
408415
def test_max_agents_truncation(self, mock_get_client, mock_supports_structured, mock_tools):
409416
"""Test that agents are truncated when exceeding max_agents"""
@@ -463,7 +470,7 @@ def test_max_agents_truncation(self, mock_get_client, mock_supports_structured,
463470
assert call_args.agents[0].name == "Agent 0"
464471
assert call_args.agents[1].name == "Agent 1"
465472

466-
@patch('praisonaiagents.agents.autoagents.supports_structured_outputs')
473+
@patch('praisonaiagents.llm.supports_structured_outputs')
467474
@patch('praisonaiagents.agents.autoagents.get_openai_client')
468475
def test_insufficient_agents_warning(self, mock_get_client, mock_supports_structured, mock_tools):
469476
"""Test warning when fewer agents than max_agents are generated"""
@@ -530,7 +537,7 @@ def test_max_agents_validation(self):
530537
with pytest.raises(ValueError, match="max_agents cannot exceed 10"):
531538
AutoAgents(instructions="Test", max_agents=11)
532539

533-
@patch('praisonaiagents.agents.autoagents.supports_structured_outputs')
540+
@patch('praisonaiagents.llm.supports_structured_outputs')
534541
@patch('praisonaiagents.agents.autoagents.LLM')
535542
def test_retry_with_previous_response_in_prompt(self, mock_llm_class, mock_supports_structured, sample_valid_config, mock_tools):
536543
"""Test that retry includes previous response and error in prompt"""
@@ -583,7 +590,7 @@ def test_retry_with_previous_response_in_prompt(self, mock_llm_class, mock_suppo
583590
assert "Error:" in second_call_kwargs['prompt']
584591
mock_create.assert_called_once_with(sample_valid_config)
585592

586-
@patch('praisonaiagents.agents.autoagents.supports_structured_outputs')
593+
@patch('praisonaiagents.llm.supports_structured_outputs')
587594
@patch('praisonaiagents.agents.autoagents.OpenAIClient')
588595
@patch('praisonaiagents.agents.autoagents.get_openai_client')
589596
def test_custom_api_key_and_base_url(self, mock_get_client, mock_openai_class, mock_supports_structured, sample_valid_config, mock_tools):

0 commit comments

Comments
 (0)