Skip to content

Commit b002198

Browse files
committed
add action registry tests
1 parent a2b6771 commit b002198

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

tests/test_action_registry.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from django.core.exceptions import ImproperlyConfigured
2+
from django.test import SimpleTestCase
3+
4+
from djangocms_form_builder import actions as actions_module
5+
from djangocms_form_builder.actions import FormAction
6+
7+
8+
class DummyAction(FormAction):
9+
verbose_name = "Dummy action"
10+
11+
def execute(self, form, request): # pragma: no cover - trivial
12+
return None
13+
14+
15+
class NoVerboseAction(FormAction):
16+
verbose_name = None
17+
18+
def execute(self, form, request): # pragma: no cover - trivial
19+
return None
20+
21+
22+
class NotAnAction:
23+
pass
24+
25+
26+
class ActionRegistryTests(SimpleTestCase):
27+
def setUp(self):
28+
super().setUp()
29+
# Backup the original registry so we can safely modify it
30+
self._orig_registry = actions_module._action_registry.copy()
31+
32+
def tearDown(self):
33+
# Restore the registry to its original state
34+
actions_module._action_registry.clear()
35+
actions_module._action_registry.update(self._orig_registry)
36+
super().tearDown()
37+
38+
def test_register_adds_action_and_getters_work(self):
39+
action_hash = actions_module.get_hash(DummyAction)
40+
self.assertIsNone(actions_module.get_action_class(action_hash))
41+
42+
actions_module.register(DummyAction)
43+
44+
# get_action_class returns the class
45+
self.assertIs(actions_module.get_action_class(action_hash), DummyAction)
46+
# get_registered_actions contains our (hash, verbose_name) pair
47+
choices = dict(actions_module.get_registered_actions())
48+
self.assertIn(action_hash, choices)
49+
self.assertEqual(choices[action_hash], DummyAction.verbose_name)
50+
51+
def test_unregister_removes_action(self):
52+
action_hash = actions_module.get_hash(DummyAction)
53+
actions_module.register(DummyAction)
54+
self.assertIsNotNone(actions_module.get_action_class(action_hash))
55+
56+
actions_module.unregister(DummyAction)
57+
self.assertIsNone(actions_module.get_action_class(action_hash))
58+
59+
choices = dict(actions_module.get_registered_actions())
60+
self.assertNotIn(action_hash, choices)
61+
62+
def test_register_rejects_non_subclass(self):
63+
with self.assertRaises(ImproperlyConfigured):
64+
actions_module.register(NotAnAction) # type: ignore[arg-type]
65+
66+
def test_register_rejects_missing_verbose_name(self):
67+
with self.assertRaises(ImproperlyConfigured):
68+
actions_module.register(NoVerboseAction)
69+
70+
def test_register_is_idempotent(self):
71+
# capture current size
72+
size_before = len(actions_module._action_registry)
73+
actions_module.register(DummyAction)
74+
size_after_first = len(actions_module._action_registry)
75+
actions_module.register(DummyAction)
76+
size_after_second = len(actions_module._action_registry)
77+
78+
self.assertEqual(size_after_first, size_before + 1)
79+
self.assertEqual(size_after_second, size_after_first)
80+
81+
actions_module.unregister(DummyAction)
82+
self.assertEqual(len(actions_module._action_registry), size_before)
83+
84+
def test_unregister_is_safe_when_absent(self):
85+
size_before = len(actions_module._action_registry)
86+
actions_module.unregister(DummyAction) # should not raise
87+
self.assertEqual(len(actions_module._action_registry), size_before)
88+
89+
def test_get_registered_actions_empty_fallback(self):
90+
# Clear registry to simulate no actions registered
91+
actions_module._action_registry.clear()
92+
choices = actions_module.get_registered_actions()
93+
94+
# Expect a single-choice tuple with an empty options tuple
95+
self.assertIsInstance(choices, tuple)
96+
self.assertEqual(len(choices), 1)
97+
self.assertIsInstance(choices[0], tuple)
98+
self.assertEqual(len(choices[0]), 2)
99+
self.assertIsInstance(choices[0][1], tuple)
100+
self.assertEqual(choices[0][1], ())

tests/test_actions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,5 +257,4 @@ def test_redirect_action_sets_redirect_url(self):
257257
form = plugin.get_form_class()({}, request=request)
258258
form.cleaned_data = {}
259259
form.save()
260-
261-
self.assertTrue(form.Meta.options.get("redirect"))
260+
self.assertEqual(form.Meta.options.get("redirect"), "/home/")

0 commit comments

Comments
 (0)