django-enumfield icon indicating copy to clipboard operation
django-enumfield copied to clipboard

Transition validation failure leaves field in unvalidated state

Open thismatters opened this issue 1 year ago • 0 comments

Hello, I'm doing some unittesting around a workflow which is enabled by django-enumfield enum __transitions__, and I'm seeing something unexpected. Suppose we had:

class StatusEnum(Enum):
    NEW = 0
    PENDING = 1
    UNREACHABLE = 3

    __default__ = NEW

    __transitions__ = {
        NEW: (PENDING,),
        PENDING: (NEW,),
        UNREACHABLE: (PENDING,),

    }


class WorkflowModel(models.Model):
    status = EnumField(StatusEnum)

Then, if I were to run something like:

assert workflow_instance.status == StatusEnum.NEW

try:
    workflow_instance.status = StatusEnum.UNREACHABLE
except InvalidStatusOperationError:
    pass

assert workflow_instance.status == StatusEnum.UNREACHABLE

No AssertionError would raise! The status of the field will appear as UNREACHABLE, even though that should be impossible as the change did not pass validation. The behavior I expected was that the field value would be returned to it's pre-validation value (namely NEW). As I write this I'm realizing that the existing behavior permits bypassing validation, which may be a feature, and not a bug; so a change may not really be necessary.

Thanks for this package, it is great and necessary!

thismatters avatar Feb 19 '24 16:02 thismatters