-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_action_registry.py
More file actions
91 lines (64 loc) · 2.83 KB
/
test_action_registry.py
File metadata and controls
91 lines (64 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from __future__ import annotations
import pytest
from streamdeck.actions import Action, ActionRegistry
from streamdeck.models import events
from tests.test_utils.fake_event_factories import (
DialDownEventFactory,
DialUpEventFactory,
KeyUpEventFactory,
)
def test_register_action():
"""Test that an action can be registered."""
registry = ActionRegistry()
action = Action("my-fake-action-uuid")
assert len(registry._plugin_actions) == 0
registry.register(action)
assert len(registry._plugin_actions) == 1
assert registry._plugin_actions[0] == action
def test_get_action_handlers_no_handlers():
"""Test that getting action handlers when there are no handlers yields nothing."""
registry = ActionRegistry()
action = Action("my-fake-action-uuid")
registry.register(action)
fake_event_data: events.DialUp = DialUpEventFactory.build()
handlers = list(registry.get_action_handlers(event_name=fake_event_data.event, event_action_uuid=fake_event_data.action))
assert len(handlers) == 0
def test_get_action_handlers_with_handlers():
"""Test that registered event handlers can be retrieved correctly."""
registry = ActionRegistry()
action = Action("my-fake-action-uuid")
@action.on("dialDown")
def dial_down_handler(event: events.EventBase):
pass
registry.register(action)
fake_event_data: events.DialDown = DialDownEventFactory.build(action=action.uuid)
handlers = list(registry.get_action_handlers(event_name=fake_event_data.event, event_action_uuid=fake_event_data.action))
# handlers = list(registry.get_action_handlers("dialDown"))
assert len(handlers) == 1
assert handlers[0] == dial_down_handler
def test_get_action_handlers_multiple_actions():
"""Test that multiple actions with registered handlers return all handlers."""
registry = ActionRegistry()
action1 = Action("fake-action-uuid-1")
action2 = Action("fake-action-uuid-2")
@action1.on("keyUp")
def key_up_handler1(event):
pass
@action2.on("keyUp")
def key_up_handler2(event):
pass
registry.register(action1)
registry.register(action2)
fake_event_data: events.KeyUp = KeyUpEventFactory.build(action=action1.uuid)
# Notice no action uuid is passed in here, so we should get all handlers for the event.
handlers = list(registry.get_action_handlers(event_name=fake_event_data.event))
assert len(handlers) == 2
assert key_up_handler1 in handlers
assert key_up_handler2 in handlers
def test_get_action_handlers_event_not_available():
"""Test that a KeyError is raised if an unavailable event name is provided."""
registry = ActionRegistry()
action = Action("my-fake-action-uuid")
registry.register(action)
with pytest.raises(KeyError):
list(registry.get_action_handlers("nonExistentEvent"))