Skip to content

Commit 739977f

Browse files
committed
Clean up prior tests
1 parent 798903c commit 739977f

File tree

8 files changed

+130
-95
lines changed

8 files changed

+130
-95
lines changed

tests/actions/test_action_registry.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
from __future__ import annotations
22

33
import pytest
4-
from polyfactory.factories.pydantic_factory import ModelFactory
54
from streamdeck.actions import Action, ActionRegistry
65
from streamdeck.models import events
76

8-
9-
class DialUpEventFactory(ModelFactory[events.DialUp]):
10-
"""Polyfactory factory for creating a fake dialUp event message based on our Pydantic model."""
11-
12-
class DialDownEventFactory(ModelFactory[events.DialDown]):
13-
"""Polyfactory factory for creating a fake dialDown event message based on our Pydantic model."""
14-
15-
class KeyUpEventFactory(ModelFactory[events.KeyUp]):
16-
"""Polyfactory factory for creating a fake keyUp event message based on our Pydantic model."""
17-
18-
7+
from tests.test_utils.fake_event_factories import (
8+
DialDownEventFactory,
9+
DialUpEventFactory,
10+
KeyUpEventFactory,
11+
)
1912

2013

2114
def test_register_action():

tests/actions/test_event_handler_filtering.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44
from unittest.mock import create_autospec
55

66
import pytest
7-
from polyfactory.factories.pydantic_factory import ModelFactory
87
from streamdeck.actions import Action, ActionRegistry, GlobalAction
98
from streamdeck.models import events
109

10+
from tests.test_utils.fake_event_factories import (
11+
ApplicationDidLaunchEventFactory,
12+
DeviceDidConnectFactory,
13+
KeyDownEventFactory,
14+
)
15+
1116

1217
if TYPE_CHECKING:
1318
from unittest.mock import Mock
1419

20+
from polyfactory.factories.pydantic_factory import ModelFactory
21+
1522

1623

1724
@pytest.fixture
@@ -22,28 +29,6 @@ def dummy_handler(event: events.EventBase) -> None:
2229
return create_autospec(dummy_handler, spec_set=True)
2330

2431

25-
class ApplicationDidLaunchEventFactory(ModelFactory[events.ApplicationDidLaunch]):
26-
"""Polyfactory factory for creating fake applicationDidLaunch event message based on our Pydantic model.
27-
28-
ApplicationDidLaunchEvent's hold no unique identifier properties, besides the almost irrelevant `event` name property.
29-
"""
30-
31-
class DeviceDidConnectFactory(ModelFactory[events.DeviceDidConnect]):
32-
"""Polyfactory factory for creating fake deviceDidConnect event message based on our Pydantic model.
33-
34-
DeviceDidConnectEvent's have `device` unique identifier property.
35-
"""
36-
37-
class KeyDownEventFactory(ModelFactory[events.KeyDown]):
38-
"""Polyfactory factory for creating fake keyDown event message based on our Pydantic model.
39-
40-
KeyDownEvent's have the unique identifier properties:
41-
`device`: Identifies the Stream Deck device that this event is associated with.
42-
`action`: Identifies the action that caused the event.
43-
`context`: Identifies the *instance* of an action that caused the event.
44-
"""
45-
46-
4732
@pytest.mark.parametrize(("event_name","event_factory"), [
4833
("keyDown", KeyDownEventFactory),
4934
("deviceDidConnect", DeviceDidConnectFactory),

tests/actions/test_global_action.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
from typing import TYPE_CHECKING
44

55
import pytest
6-
from polyfactory.factories.pydantic_factory import ModelFactory
7-
from streamdeck.actions import Action, GlobalAction, available_event_names
8-
from streamdeck.models import events
6+
from streamdeck.actions import GlobalAction, available_event_names
97

108

119
if TYPE_CHECKING:
1210
from streamdeck.models.events import EventBase
13-
from streamdeck.types import EventNameStr
1411

1512

1613
def test_global_action_register_event_handler():

tests/plugin_manager/__init__.py

Whitespace-only changes.

tests/plugin_manager/conftest.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from __future__ import annotations
2+
3+
import uuid
4+
from unittest.mock import Mock, create_autospec
5+
6+
import pytest
7+
from streamdeck.manager import PluginManager
8+
from streamdeck.websocket import WebSocketClient
9+
10+
11+
@pytest.fixture
12+
def plugin_manager(port_number: int) -> PluginManager:
13+
"""Fixture that provides a configured instance of PluginManager for testing.
14+
15+
Returns:
16+
PluginManager: An instance of PluginManager with test parameters.
17+
"""
18+
plugin_uuid = "test-plugin-uuid"
19+
plugin_registration_uuid = str(uuid.uuid1())
20+
register_event = "registerPlugin"
21+
info = {"some": "info"}
22+
23+
return PluginManager(
24+
port=port_number,
25+
plugin_uuid=plugin_uuid,
26+
plugin_registration_uuid=plugin_registration_uuid,
27+
register_event=register_event,
28+
info=info,
29+
)
30+
31+
32+
@pytest.fixture
33+
def patch_websocket_client(monkeypatch: pytest.MonkeyPatch) -> Mock:
34+
"""Fixture that uses pytest's MonkeyPatch to mock WebSocketClient for the PluginManager run method.
35+
36+
The mocked WebSocketClient can be given fake event messages to yield when listen_forever() is called:
37+
```patch_websocket_client.listen_forever.return_value = [fake_event_json1, fake_event_json2, ...]```
38+
39+
Args:
40+
monkeypatch: pytest's monkeypatch fixture.
41+
42+
Returns:
43+
"""
44+
mock_websocket_client: Mock = create_autospec(WebSocketClient, spec_set=True)
45+
mock_websocket_client.__enter__.return_value = mock_websocket_client
46+
47+
monkeypatch.setattr("streamdeck.manager.WebSocketClient", (lambda *args, **kwargs: mock_websocket_client))
48+
49+
return mock_websocket_client
50+
51+
52+
@pytest.fixture
53+
def mock_command_sender(mocker: pytest_mock.MockerFixture) -> Mock:
54+
"""Fixture that patches the StreamDeckCommandSender.
55+
56+
Args:
57+
mocker: pytest-mock's mocker fixture.
58+
59+
Returns:
60+
Mock: Mocked instance of StreamDeckCommandSender
61+
"""
62+
mock_cmd_sender = Mock()
63+
mocker.patch("streamdeck.manager.StreamDeckCommandSender", return_value=mock_cmd_sender)
64+
return mock_cmd_sender

tests/test_plugin_manager.py renamed to tests/plugin_manager/test_plugin_manager.py

Lines changed: 10 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,31 @@
1-
import uuid
21
from typing import cast
3-
from unittest.mock import MagicMock, Mock
2+
from unittest.mock import Mock
43

54
import pytest
65
import pytest_mock
7-
from polyfactory.factories.pydantic_factory import ModelFactory
86
from streamdeck.actions import Action
97
from streamdeck.manager import PluginManager
108
from streamdeck.models.events import DialRotate, EventBase, event_adapter
11-
from streamdeck.websocket import WebSocketClient
129

13-
14-
class DialRotateEventFactory(ModelFactory[DialRotate]):
15-
"""Polyfactory factory for creating a fake event message based on our Pydantic model."""
16-
17-
18-
@pytest.fixture
19-
def plugin_manager(port_number: int) -> PluginManager:
20-
"""Fixture that provides a configured instance of PluginManager for testing.
21-
22-
Returns:
23-
PluginManager: An instance of PluginManager with test parameters.
24-
"""
25-
plugin_uuid = "test-plugin-uuid"
26-
plugin_registration_uuid = str(uuid.uuid1())
27-
register_event = "registerPlugin"
28-
info = {"some": "info"}
29-
30-
return PluginManager(
31-
port=port_number,
32-
plugin_uuid=plugin_uuid,
33-
plugin_registration_uuid=plugin_registration_uuid,
34-
register_event=register_event,
35-
info=info,
36-
)
10+
from tests.test_utils.fake_event_factories import DialRotateEventFactory
3711

3812

3913
@pytest.fixture
40-
def patch_websocket_client(monkeypatch: pytest.MonkeyPatch) -> tuple[MagicMock, EventBase]:
41-
"""Fixture that uses pytest's MonkeyPatch to mock WebSocketClient and StreamDeckCommandSender for the PluginManager run method.
14+
def mock_websocket_client_with_event(patch_websocket_client: Mock) -> tuple[Mock, EventBase]:
15+
"""Fixture that mocks the WebSocketClient and provides a fake DialRotateEvent message.
4216
4317
Args:
44-
monkeypatch: pytest's monkeypatch fixture.
18+
patch_websocket_client: Mocked instance of the patched WebSocketClient.
4519
4620
Returns:
4721
tuple: Mocked instance of WebSocketClient, and a fake DialRotateEvent.
4822
"""
49-
mock_websocket_client = MagicMock(spec=WebSocketClient)
50-
51-
mock_websocket_client.__enter__.return_value = mock_websocket_client
52-
5323
# Create a fake event message, and convert it to a json string to be passed back by the client.listen_forever() method.
5424
fake_event_message: DialRotate = DialRotateEventFactory.build()
55-
mock_websocket_client.listen_forever.return_value = [fake_event_message.model_dump_json()]
25+
patch_websocket_client.listen_forever.return_value = [fake_event_message.model_dump_json()]
5626

57-
monkeypatch.setattr("streamdeck.manager.WebSocketClient", lambda port: mock_websocket_client)
27+
return patch_websocket_client, fake_event_message
5828

59-
return mock_websocket_client, fake_event_message
60-
61-
62-
@pytest.fixture
63-
def mock_command_sender(mocker: pytest_mock.MockerFixture) -> Mock:
64-
"""Fixture that patches the StreamDeckCommandSender.
65-
66-
Args:
67-
mocker: pytest-mock's mocker fixture.
68-
69-
Returns:
70-
Mock: Mocked instance of StreamDeckCommandSender
71-
"""
72-
mock_cmd_sender = Mock()
73-
mocker.patch("streamdeck.manager.StreamDeckCommandSender", return_value=mock_cmd_sender)
74-
return mock_cmd_sender
7529

7630

7731
@pytest.fixture
@@ -114,7 +68,7 @@ def test_plugin_manager_register_action(plugin_manager: PluginManager):
11468
assert plugin_manager._registry._plugin_actions[0] == action
11569

11670

117-
@pytest.mark.usefixtures("patch_websocket_client")
71+
@pytest.mark.usefixtures("mock_websocket_client_with_event")
11872
def test_plugin_manager_sends_registration_event(
11973
mock_command_sender: Mock, plugin_manager: PluginManager
12074
):
@@ -130,10 +84,10 @@ def test_plugin_manager_sends_registration_event(
13084
@pytest.mark.usefixtures("_spy_action_registry_get_action_handlers")
13185
@pytest.mark.usefixtures("_spy_event_adapter_validate_json")
13286
def test_plugin_manager_process_event(
133-
patch_websocket_client: tuple[MagicMock, EventBase], plugin_manager: PluginManager
87+
mock_websocket_client_with_event: tuple[Mock, EventBase], plugin_manager: PluginManager
13488
):
13589
"""Test that PluginManager processes events correctly, calling event_adapter.validate_json and action_registry.get_action_handlers."""
136-
mock_websocket_client, fake_event_message = patch_websocket_client
90+
mock_websocket_client, fake_event_message = mock_websocket_client_with_event
13791

13892
plugin_manager.run()
13993

tests/test_utils/__init__.py

Whitespace-only changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from polyfactory.factories.pydantic_factory import ModelFactory
2+
from streamdeck.models import events
3+
4+
5+
class DialDownEventFactory(ModelFactory[events.DialDown]):
6+
"""Polyfactory factory for creating a fake dialDown event message based on our Pydantic model."""
7+
8+
9+
class DialUpEventFactory(ModelFactory[events.DialUp]):
10+
"""Polyfactory factory for creating a fake dialUp event message based on our Pydantic model."""
11+
12+
13+
class DialRotateEventFactory(ModelFactory[events.DialRotate]):
14+
"""Polyfactory factory for creating a fake event message based on our Pydantic model."""
15+
16+
17+
class KeyDownEventFactory(ModelFactory[events.KeyDown]):
18+
"""Polyfactory factory for creating fake keyDown event message based on our Pydantic model.
19+
20+
KeyDownEvent's have the unique identifier properties:
21+
`device`: Identifies the Stream Deck device that this event is associated with.
22+
`action`: Identifies the action that caused the event.
23+
`context`: Identifies the *instance* of an action that caused the event.
24+
"""
25+
26+
27+
class KeyUpEventFactory(ModelFactory[events.KeyUp]):
28+
"""Polyfactory factory for creating a fake keyUp event message based on our Pydantic model."""
29+
30+
31+
class ApplicationDidLaunchEventFactory(ModelFactory[events.ApplicationDidLaunch]):
32+
"""Polyfactory factory for creating fake applicationDidLaunch event message based on our Pydantic model.
33+
34+
ApplicationDidLaunchEvent's hold no unique identifier properties, besides the almost irrelevant `event` name property.
35+
"""
36+
37+
38+
class DeviceDidConnectFactory(ModelFactory[events.DeviceDidConnect]):
39+
"""Polyfactory factory for creating fake deviceDidConnect event message based on our Pydantic model.
40+
41+
DeviceDidConnectEvent's have `device` unique identifier property.
42+
"""

0 commit comments

Comments
 (0)