Skip to content

Commit ce23957

Browse files
author
Andrei Neagu
committed
added tests
1 parent f9258fc commit ce23957

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

services/dynamic-sidecar/tests/unit/test_modules_outputs_event_handler.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
# pylint: disable=protected-access
33

44
import asyncio
5+
from collections.abc import AsyncIterable
56
from pathlib import Path
6-
from typing import AsyncIterable
7+
from typing import Any, Final
78
from unittest.mock import Mock
89

910
import aioprocessing
@@ -17,8 +18,16 @@
1718
from simcore_service_dynamic_sidecar.modules.outputs._event_handler import (
1819
EventHandlerObserver,
1920
_EventHandlerProcess,
21+
_PortKeysEventHandler,
2022
)
2123
from simcore_service_dynamic_sidecar.modules.outputs._manager import OutputsManager
24+
from watchdog.events import (
25+
DirModifiedEvent,
26+
FileClosedEvent,
27+
FileCreatedEvent,
28+
FileMovedEvent,
29+
FileSystemEvent,
30+
)
2231

2332

2433
@pytest.fixture
@@ -124,3 +133,62 @@ async def test_event_handler_observer_health_degraded(
124133
await asyncio.sleep(observer_monitor.wait_for_heart_beat_interval_s * 3)
125134
await observer_monitor.stop()
126135
assert outputs_manager.set_all_ports_for_upload.call_count >= 1
136+
137+
138+
_STATE_PATH: Final[Path] = Path("/some/random/fake/path/for/state/")
139+
140+
141+
@pytest.fixture
142+
def mock_state_path() -> Path:
143+
return _STATE_PATH
144+
145+
146+
class _MockAioQueue:
147+
def __init__(self) -> None:
148+
self.items: list[Any] = []
149+
150+
def put(self, item: Any) -> None:
151+
self.items.append(item)
152+
153+
def get(self) -> Any | None:
154+
try:
155+
return self.items.pop()
156+
except IndexError:
157+
return None
158+
159+
160+
@pytest.mark.parametrize(
161+
"event, expected_port_key",
162+
[
163+
(FileCreatedEvent(src_path=f"{_STATE_PATH}/untitled.txt", dest_path=""), None),
164+
(DirModifiedEvent(src_path=f"{_STATE_PATH}", dest_path=""), None),
165+
(FileClosedEvent(src_path=f"{_STATE_PATH}/untitled.txt", dest_path=""), None),
166+
(
167+
FileMovedEvent(
168+
src_path=f"{_STATE_PATH}/untitled.txt",
169+
dest_path=f"{_STATE_PATH}/asdsadsasad.txt",
170+
),
171+
None,
172+
),
173+
(
174+
FileMovedEvent(
175+
src_path=f"{_STATE_PATH}/asdsadsasad.txt",
176+
dest_path=f"{_STATE_PATH}/output_1/asdsadsasad.txt",
177+
),
178+
"output_1",
179+
),
180+
(DirModifiedEvent(src_path=f"{_STATE_PATH}/output_1", dest_path=""), None),
181+
],
182+
)
183+
def test_port_keys_event_handler_triggers_for_events(
184+
mock_state_path: Path, event: FileSystemEvent, expected_port_key: str | None
185+
) -> None:
186+
187+
queue = _MockAioQueue()
188+
189+
event_handler = _PortKeysEventHandler(mock_state_path, queue)
190+
event_handler.handle_set_outputs_port_keys(outputs_port_keys={"output_1"})
191+
event_handler.handle_toggle_event_propagation(is_enabled=True)
192+
193+
event_handler.event_handler(event)
194+
assert queue.get() == expected_port_key

0 commit comments

Comments
 (0)