added tests

This commit is contained in:
Lorenze Jay
2025-01-24 12:24:54 -08:00
parent 71246e9de1
commit f34f53fae2
4 changed files with 498 additions and 47 deletions

View File

@@ -1600,3 +1600,25 @@ def test_agent_with_knowledge_sources():
# Assert that the agent provides the correct information # Assert that the agent provides the correct information
assert "red" in result.raw.lower() assert "red" in result.raw.lower()
@pytest.mark.vcr(filter_headers=["authorization"])
def test_agent_with_knowledge_sources_works_with_test():
content = "Brandon's favorite color is red and he likes Mexican food."
string_source = StringKnowledgeSource(content=content)
agent = Agent(
role="test role",
goal="test goal",
backstory="test backstory",
llm=LLM(model="gpt-4o-mini"),
knowledge_sources=[string_source],
)
agent.create_agent_executor()
agent_copy = agent.copy()
assert agent_copy.role == agent.role
assert agent_copy.goal == agent.goal
assert agent_copy.backstory == agent.backstory
assert agent_copy.knowledge_sources == agent.knowledge_sources
assert isinstance(agent_copy.llm, LLM)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -14,6 +14,7 @@ from crewai.agent import Agent
from crewai.agents.cache import CacheHandler from crewai.agents.cache import CacheHandler
from crewai.crew import Crew from crewai.crew import Crew
from crewai.crews.crew_output import CrewOutput from crewai.crews.crew_output import CrewOutput
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource
from crewai.memory.contextual.contextual_memory import ContextualMemory from crewai.memory.contextual.contextual_memory import ContextualMemory
from crewai.process import Process from crewai.process import Process
from crewai.project import crew from crewai.project import crew
@@ -555,12 +556,12 @@ def test_crew_with_delegating_agents_should_not_override_task_tools():
_, kwargs = mock_execute_sync.call_args _, kwargs = mock_execute_sync.call_args
tools = kwargs["tools"] tools = kwargs["tools"]
assert any( assert any(isinstance(tool, TestTool) for tool in tools), (
isinstance(tool, TestTool) for tool in tools "TestTool should be present"
), "TestTool should be present" )
assert any( assert any("delegate" in tool.name.lower() for tool in tools), (
"delegate" in tool.name.lower() for tool in tools "Delegation tool should be present"
), "Delegation tool should be present" )
@pytest.mark.vcr(filter_headers=["authorization"]) @pytest.mark.vcr(filter_headers=["authorization"])
@@ -619,12 +620,12 @@ def test_crew_with_delegating_agents_should_not_override_agent_tools():
_, kwargs = mock_execute_sync.call_args _, kwargs = mock_execute_sync.call_args
tools = kwargs["tools"] tools = kwargs["tools"]
assert any( assert any(isinstance(tool, TestTool) for tool in new_ceo.tools), (
isinstance(tool, TestTool) for tool in new_ceo.tools "TestTool should be present"
), "TestTool should be present" )
assert any( assert any("delegate" in tool.name.lower() for tool in tools), (
"delegate" in tool.name.lower() for tool in tools "Delegation tool should be present"
), "Delegation tool should be present" )
@pytest.mark.vcr(filter_headers=["authorization"]) @pytest.mark.vcr(filter_headers=["authorization"])
@@ -748,17 +749,17 @@ def test_task_tools_override_agent_tools_with_allow_delegation():
used_tools = kwargs["tools"] used_tools = kwargs["tools"]
# Confirm AnotherTestTool is present but TestTool is not # Confirm AnotherTestTool is present but TestTool is not
assert any( assert any(isinstance(tool, AnotherTestTool) for tool in used_tools), (
isinstance(tool, AnotherTestTool) for tool in used_tools "AnotherTestTool should be present"
), "AnotherTestTool should be present" )
assert not any( assert not any(isinstance(tool, TestTool) for tool in used_tools), (
isinstance(tool, TestTool) for tool in used_tools "TestTool should not be present among used tools"
), "TestTool should not be present among used tools" )
# Confirm delegation tool(s) are present # Confirm delegation tool(s) are present
assert any( assert any("delegate" in tool.name.lower() for tool in used_tools), (
"delegate" in tool.name.lower() for tool in used_tools "Delegation tool should be present"
), "Delegation tool should be present" )
# Finally, make sure the agent's original tools remain unchanged # Finally, make sure the agent's original tools remain unchanged
assert len(researcher_with_delegation.tools) == 1 assert len(researcher_with_delegation.tools) == 1
@@ -1466,7 +1467,6 @@ def test_dont_set_agents_step_callback_if_already_set():
@pytest.mark.vcr(filter_headers=["authorization"]) @pytest.mark.vcr(filter_headers=["authorization"])
def test_crew_function_calling_llm(): def test_crew_function_calling_llm():
from crewai import LLM from crewai import LLM
from crewai.tools import tool from crewai.tools import tool
@@ -1560,9 +1560,9 @@ def test_code_execution_flag_adds_code_tool_upon_kickoff():
# Verify that exactly one tool was used and it was a CodeInterpreterTool # Verify that exactly one tool was used and it was a CodeInterpreterTool
assert len(used_tools) == 1, "Should have exactly one tool" assert len(used_tools) == 1, "Should have exactly one tool"
assert isinstance( assert isinstance(used_tools[0], CodeInterpreterTool), (
used_tools[0], CodeInterpreterTool "Tool should be CodeInterpreterTool"
), "Tool should be CodeInterpreterTool" )
@pytest.mark.vcr(filter_headers=["authorization"]) @pytest.mark.vcr(filter_headers=["authorization"])
@@ -3107,9 +3107,9 @@ def test_fetch_inputs():
expected_placeholders = {"role_detail", "topic", "field"} expected_placeholders = {"role_detail", "topic", "field"}
actual_placeholders = crew.fetch_inputs() actual_placeholders = crew.fetch_inputs()
assert ( assert actual_placeholders == expected_placeholders, (
actual_placeholders == expected_placeholders f"Expected {expected_placeholders}, but got {actual_placeholders}"
), f"Expected {expected_placeholders}, but got {actual_placeholders}" )
def test_task_tools_preserve_code_execution_tools(): def test_task_tools_preserve_code_execution_tools():
@@ -3182,20 +3182,20 @@ def test_task_tools_preserve_code_execution_tools():
used_tools = kwargs["tools"] used_tools = kwargs["tools"]
# Verify all expected tools are present # Verify all expected tools are present
assert any( assert any(isinstance(tool, TestTool) for tool in used_tools), (
isinstance(tool, TestTool) for tool in used_tools "Task's TestTool should be present"
), "Task's TestTool should be present" )
assert any( assert any(isinstance(tool, CodeInterpreterTool) for tool in used_tools), (
isinstance(tool, CodeInterpreterTool) for tool in used_tools "CodeInterpreterTool should be present"
), "CodeInterpreterTool should be present" )
assert any( assert any("delegate" in tool.name.lower() for tool in used_tools), (
"delegate" in tool.name.lower() for tool in used_tools "Delegation tool should be present"
), "Delegation tool should be present" )
# Verify the total number of tools (TestTool + CodeInterpreter + 2 delegation tools) # Verify the total number of tools (TestTool + CodeInterpreter + 2 delegation tools)
assert ( assert len(used_tools) == 4, (
len(used_tools) == 4 "Should have TestTool, CodeInterpreter, and 2 delegation tools"
), "Should have TestTool, CodeInterpreter, and 2 delegation tools" )
@pytest.mark.vcr(filter_headers=["authorization"]) @pytest.mark.vcr(filter_headers=["authorization"])
@@ -3239,9 +3239,9 @@ def test_multimodal_flag_adds_multimodal_tools():
used_tools = kwargs["tools"] used_tools = kwargs["tools"]
# Check that the multimodal tool was added # Check that the multimodal tool was added
assert any( assert any(isinstance(tool, AddImageTool) for tool in used_tools), (
isinstance(tool, AddImageTool) for tool in used_tools "AddImageTool should be present when agent is multimodal"
), "AddImageTool should be present when agent is multimodal" )
# Verify we have exactly one tool (just the AddImageTool) # Verify we have exactly one tool (just the AddImageTool)
assert len(used_tools) == 1, "Should only have the AddImageTool" assert len(used_tools) == 1, "Should only have the AddImageTool"
@@ -3467,9 +3467,9 @@ def test_crew_guardrail_feedback_in_context():
assert len(execution_contexts) > 1, "Task should have been executed multiple times" assert len(execution_contexts) > 1, "Task should have been executed multiple times"
# Verify that the second execution included the guardrail feedback # Verify that the second execution included the guardrail feedback
assert ( assert "Output must contain the keyword 'IMPORTANT'" in execution_contexts[1], (
"Output must contain the keyword 'IMPORTANT'" in execution_contexts[1] "Guardrail feedback should be included in retry context"
), "Guardrail feedback should be included in retry context" )
# Verify final output meets guardrail requirements # Verify final output meets guardrail requirements
assert "IMPORTANT" in result.raw, "Final output should contain required keyword" assert "IMPORTANT" in result.raw, "Final output should contain required keyword"
@@ -3494,7 +3494,6 @@ def test_before_kickoff_callback():
@before_kickoff @before_kickoff
def modify_inputs(self, inputs): def modify_inputs(self, inputs):
self.inputs_modified = True self.inputs_modified = True
inputs["modified"] = True inputs["modified"] = True
return inputs return inputs
@@ -3596,3 +3595,21 @@ def test_before_kickoff_without_inputs():
# Verify that the inputs were initialized and modified inside the before_kickoff method # Verify that the inputs were initialized and modified inside the before_kickoff method
assert test_crew_instance.received_inputs is not None assert test_crew_instance.received_inputs is not None
assert test_crew_instance.received_inputs.get("modified") is True assert test_crew_instance.received_inputs.get("modified") is True
@pytest.mark.vcr(filter_headers=["authorization"])
def test_crew_with_knowledge_sources_works_with_copy():
content = "Brandon's favorite color is red and he likes Mexican food."
string_source = StringKnowledgeSource(content=content)
crew = Crew(
agents=[researcher, writer],
tasks=[Task(description="test", expected_output="test", agent=researcher)],
knowledge_sources=[string_source],
)
crew_copy = crew.copy()
assert crew_copy.knowledge_sources == crew.knowledge_sources
assert len(crew_copy.agents) == len(crew.agents)
assert len(crew_copy.tasks) == len(crew.tasks)