Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Implement Transition.__hash__ and __eq__ for 'in' operator
Browse files Browse the repository at this point in the history
Signed-off-by: Jelmer Draaijer <info@jelmert.nl>
  • Loading branch information
foarsitter authored and kmmbvnr committed Dec 21, 2023
1 parent 22bc86b commit cedaeb2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
11 changes: 11 additions & 0 deletions django_fsm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ def has_perm(self, instance, user):
else:
return False

def __hash__(self):
return hash(self.name)

def __eq__(self, other):
if isinstance(other, str):
return other == self.name
if isinstance(other, Transition):
return other.name == self.name

return False


def get_available_FIELD_transitions(instance, field):
"""
Expand Down
25 changes: 24 additions & 1 deletion django_fsm/tests/test_basic_transitions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db import models
from django.test import TestCase

from django_fsm import FSMField, TransitionNotAllowed, transition, can_proceed
from django_fsm import FSMField, TransitionNotAllowed, transition, can_proceed, Transition
from django_fsm.signals import pre_transition, post_transition


Expand Down Expand Up @@ -143,6 +143,29 @@ class TestFieldTransitionsInspect(TestCase):
def setUp(self):
self.model = BlogPost()

def test_in_operator_for_available_transitions(self):
# store the generator in a list, so we can reuse the generator and do multiple asserts
transitions = list(self.model.get_available_state_transitions())

self.assertIn("publish", transitions)
self.assertNotIn("xyz", transitions)

# inline method for faking the name of the transition
def publish():
pass

obj = Transition(
method=publish,
source="",
target="",
on_error="",
conditions="",
permission="",
custom="",
)

self.assertTrue(obj in transitions)

def test_available_conditions_from_new(self):
transitions = self.model.get_available_state_transitions()
actual = set((transition.source, transition.target) for transition in transitions)
Expand Down

0 comments on commit cedaeb2

Please sign in to comment.