import pytest from django.db import transaction from crm_core.models import Account from workflows.models import FlowDefinition @pytest.mark.django_db(transaction=True) def test_flow_failure_rolls_back_triggering_record(): """ Test that an active record-triggered flow failure rolls back the triggering record outside of implicit test transactions. """ # Create an active flow that intentionally fails failing_dag = { "nodes": [ {"id": "start", "type": "trigger", "data": {}}, {"id": "step_fail", "type": "subflow", "data": {"flow_id": 999999}} # Will raise ValidationError ], "edges": [ {"source": "start", "target": "step_fail"} ] } FlowDefinition.objects.create( name="Failing Trigger Flow", trigger_type="record_created", trigger_object="Account", is_active=True, graph=failing_dag ) account_name = "Doomed Transaction Account" # Attempt to create Account in an atomic block with pytest.raises(Exception): with transaction.atomic(): Account.objects.create(name=account_name) # Verify that the Account was rolled back and does not exist in the database exists = Account.objects.filter(name=account_name).exists() assert not exists, "Account creation MUST be rolled back when record-triggered flow fails!"