fix: Update test implementation for inject_date feature

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-05-21 03:40:31 +00:00
parent 9973011be5
commit 98df434eb9
2 changed files with 63 additions and 55 deletions

View File

@@ -256,16 +256,7 @@ class Agent(BaseAgent):
else:
print(f"Error during reasoning process: {str(e)}")
if self.inject_date:
from datetime import datetime
try:
current_date: str = datetime.now().strftime(self.date_format)
task.description += f"\n\nCurrent Date: {current_date}"
except Exception as e:
if hasattr(self, '_logger'):
self._logger.log("warning", f"Failed to inject date: {str(e)}")
else:
print(f"Warning: Failed to inject date: {str(e)}")
self._inject_date_to_task(task)
if self.tools_handler:
self.tools_handler.last_used_tool = {} # type: ignore # Incompatible types in assignment (expression has type "dict[Never, Never]", variable has type "ToolCalling")
@@ -627,6 +618,20 @@ class Agent(BaseAgent):
return description
def _inject_date_to_task(self, task):
"""Inject the current date into the task description if inject_date is enabled."""
if self.inject_date:
from datetime import datetime
try:
current_date: str = datetime.now().strftime(self.date_format)
task.description += f"\n\nCurrent Date: {current_date}"
except Exception as e:
if hasattr(self, '_logger'):
self._logger.log("warning", f"Failed to inject date: {str(e)}")
else:
print(f"Warning: Failed to inject date: {str(e)}")
def _validate_docker_installation(self) -> None:
"""Check if Docker is installed and running."""
if not shutil.which("docker"):

View File

@@ -10,26 +10,29 @@ def test_agent_inject_date():
Tests that when inject_date=True, the current date is added to the task description.
"""
agent = Agent(
role="test_agent",
goal="test_goal",
backstory="test_backstory",
inject_date=True,
)
task = Task(
description="Test task",
expected_output="Test output",
agent=agent,
)
with patch.object(Agent, 'execute_task', return_value="Task executed") as mock_execute:
agent.execute_task(task)
with patch('datetime.datetime') as mock_datetime:
mock_datetime.now.return_value = datetime(2025, 1, 1)
called_task = mock_execute.call_args[0][0]
agent = Agent(
role="test_agent",
goal="test_goal",
backstory="test_backstory",
inject_date=True,
)
current_date = datetime.now().strftime("%Y-%m-%d")
assert f"Current Date: {current_date}" in called_task.description
task = Task(
description="Test task",
expected_output="Test output",
agent=agent,
)
# Store original description
original_description = task.description
agent._inject_date_to_task(task)
assert "Current Date: 2025-01-01" in task.description
assert task.description != original_description
def test_agent_without_inject_date():
@@ -41,6 +44,7 @@ def test_agent_without_inject_date():
role="test_agent",
goal="test_goal",
backstory="test_backstory",
# inject_date is False by default
)
task = Task(
@@ -51,13 +55,9 @@ def test_agent_without_inject_date():
original_description = task.description
with patch.object(Agent, 'execute_task', return_value="Task executed") as mock_execute:
agent.execute_task(task)
called_task = mock_execute.call_args[0][0]
assert "Current Date:" not in called_task.description
assert called_task.description == original_description
agent._inject_date_to_task(task)
assert task.description == original_description
def test_agent_inject_date_custom_format():
@@ -65,27 +65,30 @@ def test_agent_inject_date_custom_format():
Tests that when inject_date=True with a custom date_format, the date is formatted correctly.
"""
agent = Agent(
role="test_agent",
goal="test_goal",
backstory="test_backstory",
inject_date=True,
date_format="%d/%m/%Y",
)
task = Task(
description="Test task",
expected_output="Test output",
agent=agent,
)
with patch.object(Agent, 'execute_task', return_value="Task executed") as mock_execute:
agent.execute_task(task)
with patch('datetime.datetime') as mock_datetime:
mock_datetime.now.return_value = datetime(2025, 1, 1)
called_task = mock_execute.call_args[0][0]
agent = Agent(
role="test_agent",
goal="test_goal",
backstory="test_backstory",
inject_date=True,
date_format="%d/%m/%Y",
)
current_date = datetime.now().strftime("%d/%m/%Y")
assert f"Current Date: {current_date}" in called_task.description
task = Task(
description="Test task",
expected_output="Test output",
agent=agent,
)
# Store original description
original_description = task.description
agent._inject_date_to_task(task)
assert "Current Date: 01/01/2025" in task.description
assert task.description != original_description
def test_agent_inject_date_invalid_format():
@@ -109,6 +112,6 @@ def test_agent_inject_date_invalid_format():
original_description = task.description
agent.execute_task(task)
agent._inject_date_to_task(task)
assert task.description == original_description