mirror of
https://github.com/ChristianLight/tutor.git
synced 2024-11-14 09:14:14 +00:00
71ed7a8618
The hooks API had several issues which are summarized in this comment: https://github.com/openedx/wg-developer-experience/issues/125#issuecomment-1313553526 1. "consts" was a bad name 2. "hooks.filters" and "hooks.Filters" could easily be confused 3. docs made it difficult to understand that plugin developers should use the catalog To address these issues, we: 1. move "consts.py" to "catalog.py" 2. Remove "hooks.actions", "hooks.filters", "hooks.contexts" from the API. 3. re-organize the docs and give better usage examples in the catalog. This change is a partial fix for https://github.com/openedx/wg-developer-experience/issues/125
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
import typing as t
|
|
import unittest
|
|
|
|
from tutor.core.hooks import actions, contexts
|
|
|
|
|
|
class PluginActionsTests(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.side_effect_int = 0
|
|
|
|
def tearDown(self) -> None:
|
|
super().tearDown()
|
|
actions.clear_all(context="tests")
|
|
|
|
def run(self, result: t.Any = None) -> t.Any:
|
|
with contexts.enter("tests"):
|
|
return super().run(result=result)
|
|
|
|
def test_do(self) -> None:
|
|
action: actions.Action[int] = actions.get("test-action")
|
|
|
|
@action.add()
|
|
def _test_action_1(increment: int) -> None:
|
|
self.side_effect_int += increment
|
|
|
|
@action.add()
|
|
def _test_action_2(increment: int) -> None:
|
|
self.side_effect_int += increment * 2
|
|
|
|
action.do(1)
|
|
self.assertEqual(3, self.side_effect_int)
|
|
|
|
def test_priority(self) -> None:
|
|
@actions.add("test-action", priority=2)
|
|
def _test_action_1() -> None:
|
|
self.side_effect_int += 4
|
|
|
|
@actions.add("test-action", priority=1)
|
|
def _test_action_2() -> None:
|
|
self.side_effect_int = self.side_effect_int // 2
|
|
|
|
# Action 2 must be performed before action 1
|
|
self.side_effect_int = 4
|
|
actions.do("test-action")
|
|
self.assertEqual(6, self.side_effect_int)
|
|
|
|
def test_equal_priority(self) -> None:
|
|
@actions.add("test-action", priority=2)
|
|
def _test_action_1() -> None:
|
|
self.side_effect_int += 4
|
|
|
|
@actions.add("test-action", priority=2)
|
|
def _test_action_2() -> None:
|
|
self.side_effect_int = self.side_effect_int // 2
|
|
|
|
# Action 2 must be performed after action 1
|
|
self.side_effect_int = 4
|
|
actions.do("test-action")
|
|
self.assertEqual(4, self.side_effect_int)
|