@@ -1403,80 +1403,6 @@ def test_toggle_simulate_mode_on_immediate(
14031403 mock_async_start_workflow .assert_called_once ()
14041404
14051405
1406- @override_settings (AUTOMATION_WORKFLOW_HISTORY_MAX_DAYS = 7 )
1407- @pytest .mark .django_db
1408- def test_clear_old_history_deletes_history_older_than_max_days (data_fixture ):
1409- workflow = data_fixture .create_automation_workflow ()
1410-
1411- with freeze_time ("2025-02-01 12:00:00" ):
1412- old_history = data_fixture .create_automation_workflow_history (
1413- workflow = workflow ,
1414- status = HistoryStatusChoices .SUCCESS ,
1415- )
1416-
1417- with freeze_time ("2025-02-02 12:00:00" ):
1418- recent_history = data_fixture .create_automation_workflow_history (
1419- workflow = workflow ,
1420- status = HistoryStatusChoices .SUCCESS ,
1421- )
1422-
1423- # This is 8 days after old_history was created, so it should be deleted.
1424- with freeze_time ("2025-02-09 12:00:00" ):
1425- AutomationWorkflowHandler ()._clear_old_history (workflow )
1426-
1427- assert workflow .workflow_histories .filter (id = old_history .id ).exists () is False
1428- assert workflow .workflow_histories .filter (id = recent_history .id ).exists () is True
1429-
1430-
1431- @override_settings (AUTOMATION_WORKFLOW_HISTORY_MAX_ENTRIES = 3 )
1432- @pytest .mark .django_db
1433- def test_clear_old_history_keeps_only_max_entries (data_fixture ):
1434- workflow = data_fixture .create_automation_workflow ()
1435-
1436- histories = []
1437- day = 10
1438- for i in range (5 ):
1439- day += i
1440- with freeze_time (f"2025-02-{ day } 12:00:00" ):
1441- histories .append (
1442- data_fixture .create_automation_workflow_history (
1443- workflow = workflow ,
1444- status = HistoryStatusChoices .SUCCESS ,
1445- )
1446- )
1447-
1448- with freeze_time (f"2025-02-16 12:00:00" ):
1449- AutomationWorkflowHandler ()._clear_old_history (workflow )
1450-
1451- assert workflow .workflow_histories .all ().count () == 3
1452-
1453- # The two oldest should be deleted
1454- for history in histories [:2 ]:
1455- assert workflow .workflow_histories .filter (id = history .id ).exists () is False
1456-
1457- # The three newest should be kept
1458- for history in histories [2 :]:
1459- assert workflow .workflow_histories .filter (id = history .id ).exists () is True
1460-
1461-
1462- @override_settings (
1463- AUTOMATION_WORKFLOW_HISTORY_MAX_DAYS = 3 ,
1464- AUTOMATION_WORKFLOW_HISTORY_MAX_ENTRIES = 1 ,
1465- )
1466- @pytest .mark .django_db
1467- def test_clear_old_history_keeps_entries (data_fixture ):
1468- workflow = data_fixture .create_automation_workflow ()
1469-
1470- with freeze_time ("2025-02-01 12:00:00" ):
1471- history = data_fixture .create_automation_workflow_history (workflow = workflow )
1472-
1473- with freeze_time ("2025-02-02 12:00:00" ):
1474- AutomationWorkflowHandler ()._clear_old_history (workflow )
1475-
1476- # history is within limits, so it should be kept
1477- assert workflow .workflow_histories .filter (id = history .id ).exists () is True
1478-
1479-
14801406@pytest .mark .django_db
14811407@patch (f"{ WORKFLOWS_MODULE } .handler.AutomationWorkflowHandler.before_run" )
14821408@patch (f"{ WORKFLOWS_MODULE } .handler.start_workflow_celery_task" )
@@ -1755,28 +1681,23 @@ def test_async_start_workflow_unexpected_error_creates_history(
17551681
17561682@override_settings (AUTOMATION_WORKFLOW_TIMEOUT_HOURS = 1 )
17571683@pytest .mark .django_db
1758- def test_before_run_marks_timed_out_started_history_as_failed (data_fixture ):
1759- original_workflow = data_fixture .create_automation_workflow ()
1760- published_workflow = data_fixture .create_automation_workflow (
1761- state = WorkflowState .LIVE
1762- )
1763- published_workflow .automation .published_from = original_workflow
1764- published_workflow .automation .save ()
1684+ def test_mark_failure_for_timed_out_history (data_fixture ):
1685+ workflow = data_fixture .create_automation_workflow ()
17651686
1766- with freeze_time ("2026-03-10 10 :00:00" ):
1687+ with freeze_time ("2026-04-16 12 :00:00" ):
17671688 timed_out_history = data_fixture .create_automation_workflow_history (
1768- workflow = original_workflow ,
1689+ workflow = workflow ,
17691690 status = HistoryStatusChoices .STARTED ,
17701691 )
17711692 node_history = AutomationNodeHistory .objects .create (
17721693 workflow_history = timed_out_history ,
1773- node = original_workflow .get_trigger (),
1694+ node = workflow .get_trigger (),
17741695 started_on = timed_out_history .started_on ,
17751696 status = HistoryStatusChoices .STARTED ,
17761697 )
17771698
1778- with freeze_time ("2026-03-10 12 :00:00 " ):
1779- AutomationWorkflowHandler ().before_run ( published_workflow )
1699+ with freeze_time ("2026-04-16 13 :00:01 " ):
1700+ AutomationWorkflowHandler ().mark_failure_for_timed_out_history ( )
17801701
17811702 error_message = "This workflow took too long and was timed out."
17821703
@@ -1817,60 +1738,6 @@ def test_async_start_workflow_unknown_exception(
18171738 )
18181739
18191740
1820- @override_settings (AUTOMATION_WORKFLOW_HISTORY_MAX_ENTRIES = 2 )
1821- @pytest .mark .django_db
1822- def test_clear_old_history_excludes_started_workflows_max_entries (data_fixture ):
1823- workflow = data_fixture .create_automation_workflow ()
1824-
1825- # Create three history entries
1826- with freeze_time ("2026-03-10 12:00:00" ):
1827- started_history = data_fixture .create_automation_workflow_history (
1828- workflow = workflow , status = HistoryStatusChoices .STARTED
1829- )
1830-
1831- with freeze_time ("2026-03-10 13:00:00" ):
1832- data_fixture .create_automation_workflow_history (
1833- workflow = workflow , status = HistoryStatusChoices .SUCCESS
1834- )
1835-
1836- with freeze_time ("2026-03-10 14:00:00" ):
1837- data_fixture .create_automation_workflow_history (
1838- workflow = workflow , status = HistoryStatusChoices .SUCCESS
1839- )
1840-
1841- # Although max entries is 2 and the oldest history should be deleted,
1842- # the oldest one is still kept because its status is STARTED.
1843- with freeze_time ("2026-03-10 15:00:00" ):
1844- AutomationWorkflowHandler ()._clear_old_history (workflow )
1845-
1846- assert workflow .workflow_histories .filter (id = started_history .id ).exists () is True
1847- assert workflow .workflow_histories .count () == 3
1848-
1849-
1850- @override_settings (AUTOMATION_WORKFLOW_HISTORY_MAX_DAYS = 1 )
1851- @pytest .mark .django_db
1852- def test_clear_old_history_excludes_started_workflows_max_days (data_fixture ):
1853- workflow = data_fixture .create_automation_workflow ()
1854-
1855- with freeze_time ("2026-03-10 12:00:00" ):
1856- history_1 = data_fixture .create_automation_workflow_history (
1857- workflow = workflow , status = HistoryStatusChoices .STARTED
1858- )
1859-
1860- with freeze_time ("2026-03-11 12:00:00" ):
1861- history_2 = data_fixture .create_automation_workflow_history (
1862- workflow = workflow , status = HistoryStatusChoices .SUCCESS
1863- )
1864-
1865- # After 2 days, both history entries are older than MAX_DAYS, but since
1866- # history_1 hasn't finished yet it shouldn't be deleted.
1867- with freeze_time ("2026-03-13 12:00:00" ):
1868- AutomationWorkflowHandler ()._clear_old_history (workflow )
1869-
1870- assert workflow .workflow_histories .filter (id = history_1 .id ).exists () is True
1871- assert workflow .workflow_histories .filter (id = history_2 .id ).exists () is False
1872-
1873-
18741741@pytest .mark .django_db
18751742def test_ensure_published_for_run_creates_new_clone (data_fixture ):
18761743 workflow = data_fixture .create_automation_workflow ()
@@ -2006,14 +1873,14 @@ def test_clear_old_history_deletes_orphaned_automations(data_fixture):
20061873
20071874 # 12 hours later but within 1 day, so history survives
20081875 with freeze_time ("2026-04-21 00:00:00" ):
2009- handler ._clear_old_history ( workflow )
1876+ handler .clear_old_history ( )
20101877
20111878 assert Automation .objects .filter (id = clone_automation_id ).exists ()
20121879
20131880 # 2 days later, so history should have been deleted, and the cloned
20141881 # automation should be pruned as well.
20151882 with freeze_time ("2026-04-22 12:00:00" ):
2016- handler ._clear_old_history ( workflow )
1883+ handler .clear_old_history ( )
20171884
20181885 assert not Automation .objects .filter (id = clone_automation_id ).exists ()
20191886
@@ -2033,7 +1900,7 @@ def test_clear_old_history_keeps_live_published_automation_when_newer_test_clone
20331900 assert test_clone_workflow .automation_id > published_workflow .automation_id
20341901
20351902 with freeze_time ("2026-04-27 12:01:00" ):
2036- handler ._clear_old_history ( workflow )
1903+ handler .clear_old_history ( )
20371904
20381905 assert Automation .objects .filter (id = published_workflow .automation_id ).exists ()
20391906 assert not Automation .objects .filter (id = test_clone_workflow .automation_id ).exists ()
0 commit comments