test: Improve flow persistence test cases and logging

This commit is contained in:
Lorenze Jay
2025-02-19 13:40:32 -08:00
parent 73ee7ce1c9
commit a88e910a7a

View File

@@ -13,6 +13,7 @@ from crewai.flow.persistence.sqlite import SQLiteFlowPersistence
class TestState(FlowState):
"""Test state model with required id field."""
counter: int = 0
message: str = ""
@@ -73,7 +74,6 @@ def test_flow_state_restoration(tmp_path):
# First flow execution to create initial state
class RestorableFlow(Flow[TestState]):
@start()
@persist(persistence)
def set_message(self):
@@ -89,10 +89,7 @@ def test_flow_state_restoration(tmp_path):
# Test case 1: Restore using restore_uuid with field override
flow2 = RestorableFlow(persistence=persistence)
flow2.kickoff(inputs={
"id": original_uuid,
"counter": 43
})
flow2.kickoff(inputs={"id": original_uuid, "counter": 43})
# Verify state restoration and selective field override
assert flow2.state.id == original_uuid
@@ -101,10 +98,7 @@ def test_flow_state_restoration(tmp_path):
# Test case 2: Restore using kwargs['id']
flow3 = RestorableFlow(persistence=persistence)
flow3.kickoff(inputs={
"id": original_uuid,
"message": "Updated message"
})
flow3.kickoff(inputs={"id": original_uuid, "message": "Updated message"})
# Verify state restoration and selective field override
assert flow3.state.id == original_uuid
@@ -175,8 +169,12 @@ def test_multiple_method_persistence(tmp_path):
assert final_state.counter == 99999
assert final_state.message == "Step 99999"
def test_persist_decorator_verbose_logging(tmp_path, caplog):
"""Test that @persist decorator's verbose parameter controls logging."""
# Set logging level to ensure we capture all logs
caplog.set_level("INFO")
db_path = os.path.join(tmp_path, "test_flows.db")
persistence = SQLiteFlowPersistence(db_path)
@@ -192,7 +190,7 @@ def test_persist_decorator_verbose_logging(tmp_path, caplog):
flow = QuietFlow(persistence=persistence)
flow.kickoff()
assert "Saving flow state to memory for ID: test-uuid-1" not in caplog.text
assert "Saving flow state" not in caplog.text
# Clear the log
caplog.clear()
@@ -209,4 +207,4 @@ def test_persist_decorator_verbose_logging(tmp_path, caplog):
flow = VerboseFlow(persistence=persistence)
flow.kickoff()
assert "Saving flow state to memory for ID: test-uuid-2" in caplog.text
assert "Saving flow state" in caplog.text