better mocks

This commit is contained in:
Lorenze Jay
2025-01-24 15:30:13 -08:00
parent 079692de35
commit 4ff9d4963c
2 changed files with 128 additions and 118 deletions

View File

@@ -19,6 +19,7 @@ from crewai.tools.tool_usage import ToolUsage
from crewai.tools.tool_usage_events import ToolUsageFinished
from crewai.utilities import Printer, RPMController
from crewai.utilities.events import Emitter
from tests.utilities.test_converter import mock_agent
def test_agent_llm_creation_with_env_vars():
@@ -1616,9 +1617,7 @@ def test_agent_with_knowledge_sources_works_with_copy():
mock_knowledge_instance = MockKnowledge.return_value
mock_knowledge_instance.sources = [string_source]
mock_knowledge_instance.query.return_value = [{"content": content}]
# Mock the save method specifically
mock_knowledge_instance.save.return_value = None
# Mock initialize_knowledge_storage to avoid connection issues
mock_knowledge_instance.initialize_knowledge_storage.return_value = None
agent = Agent(
@@ -1629,12 +1628,23 @@ def test_agent_with_knowledge_sources_works_with_copy():
knowledge_sources=[string_source],
)
agent_copy = agent.copy()
with patch(
"crewai.agents.agent_builder.base_agent.BaseAgent.copy",
) as mock_agent_copy:
mock_agent_copy.return_value = agent_copy
with patch.object(Agent, "copy") as mock_copy:
# Configure the mock to return a copy with the same attributes
mock_copy.return_value = Agent(
role=agent.role,
goal=agent.goal,
backstory=agent.backstory,
llm=agent.llm,
knowledge_sources=agent.knowledge_sources,
)
# Call copy method
agent_copy = agent.copy()
# Verify the mock was called
mock_copy.assert_called_once()
# Verify the copied agent has the correct attributes
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