2022-02-07 17:11:43 +00:00
|
|
|
import typing as t
|
|
|
|
import unittest
|
|
|
|
|
2023-01-06 18:02:17 +00:00
|
|
|
from tutor.core.hooks import actions, contexts
|
2022-02-07 17:11:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PluginActionsTests(unittest.TestCase):
|
|
|
|
def setUp(self) -> None:
|
|
|
|
self.side_effect_int = 0
|
|
|
|
|
|
|
|
def run(self, result: t.Any = None) -> t.Any:
|
2023-01-06 18:02:17 +00:00
|
|
|
with contexts.enter("tests"):
|
2022-02-07 17:11:43 +00:00
|
|
|
return super().run(result=result)
|
|
|
|
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
def test_do(self) -> None:
|
2023-04-28 15:11:14 +00:00
|
|
|
action: actions.Action[int] = actions.Action()
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
|
|
|
|
@action.add()
|
2022-02-07 17:11:43 +00:00
|
|
|
def _test_action_1(increment: int) -> None:
|
|
|
|
self.side_effect_int += increment
|
|
|
|
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
@action.add()
|
2022-02-07 17:11:43 +00:00
|
|
|
def _test_action_2(increment: int) -> None:
|
|
|
|
self.side_effect_int += increment * 2
|
|
|
|
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
action.do(1)
|
2022-02-07 17:11:43 +00:00
|
|
|
self.assertEqual(3, self.side_effect_int)
|
|
|
|
|
|
|
|
def test_priority(self) -> None:
|
2023-04-28 15:11:14 +00:00
|
|
|
action: actions.Action[[]] = actions.Action()
|
|
|
|
|
|
|
|
@action.add(priority=2)
|
2022-02-07 17:11:43 +00:00
|
|
|
def _test_action_1() -> None:
|
|
|
|
self.side_effect_int += 4
|
|
|
|
|
2023-04-28 15:11:14 +00:00
|
|
|
@action.add(priority=1)
|
2022-02-07 17:11:43 +00:00
|
|
|
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
|
2023-04-28 15:11:14 +00:00
|
|
|
action.do()
|
2022-02-07 17:11:43 +00:00
|
|
|
self.assertEqual(6, self.side_effect_int)
|
|
|
|
|
|
|
|
def test_equal_priority(self) -> None:
|
2023-04-28 15:11:14 +00:00
|
|
|
action: actions.Action[[]] = actions.Action()
|
|
|
|
|
|
|
|
@action.add(priority=2)
|
2022-02-07 17:11:43 +00:00
|
|
|
def _test_action_1() -> None:
|
|
|
|
self.side_effect_int += 4
|
|
|
|
|
2023-04-28 15:11:14 +00:00
|
|
|
@action.add(priority=2)
|
2022-02-07 17:11:43 +00:00
|
|
|
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
|
2023-04-28 15:11:14 +00:00
|
|
|
action.do()
|
2022-02-07 17:11:43 +00:00
|
|
|
self.assertEqual(4, self.side_effect_int)
|