Skip to content

Commit 23bcc92

Browse files
committed
test_core: add types
1 parent 253db1d commit 23bcc92

File tree

1 file changed

+115
-87
lines changed

1 file changed

+115
-87
lines changed

safeeyes/tests/test_core.py

Lines changed: 115 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,21 @@
2323
from safeeyes import core
2424
from safeeyes import model
2525

26+
from time_machine import TimeMachineFixture
27+
2628
from unittest import mock
2729

2830

2931
class SafeEyesCoreHandle:
3032
callback: typing.Optional[typing.Tuple[typing.Callable, int]] = None
33+
safe_eyes_core: core.SafeEyesCore
34+
time_machine: TimeMachineFixture
3135

32-
def __init__(self, safe_eyes_core: core.SafeEyesCore, time_machine):
36+
def __init__(
37+
self,
38+
safe_eyes_core: core.SafeEyesCore,
39+
time_machine: TimeMachineFixture,
40+
):
3341
self.time_machine = time_machine
3442
self.safe_eyes_core = safe_eyes_core
3543

@@ -40,7 +48,7 @@ def timeout_add_seconds(self, duration: int, callback: typing.Callable) -> int:
4048
print(f"callback registered for {callback} and {duration}")
4149
return 1
4250

43-
def next(self):
51+
def next(self) -> None:
4452
assert self.callback
4553

4654
(callback, duration) = self.callback
@@ -63,7 +71,7 @@ def set_time(self, time_machine):
6371
)
6472

6573
@pytest.fixture(autouse=True)
66-
def monkeypatch_translations(self, monkeypatch):
74+
def monkeypatch_translations(self, monkeypatch: pytest.MonkeyPatch):
6775
monkeypatch.setattr(
6876
core, "_", lambda message: "translated!: " + message, raising=False
6977
)
@@ -73,7 +81,9 @@ def monkeypatch_translations(self, monkeypatch):
7381

7482
@pytest.fixture
7583
def sequential_threading(
76-
self, monkeypatch: pytest.MonkeyPatch, time_machine
84+
self,
85+
monkeypatch: pytest.MonkeyPatch,
86+
time_machine: TimeMachineFixture,
7787
) -> typing.Generator[SequentialThreadingFixture]:
7888
"""This fixture allows stopping threads at any point.
7989
@@ -116,12 +126,12 @@ def create_handle(safe_eyes_core: core.SafeEyesCore) -> SafeEyesCoreHandle:
116126

117127
def run_next_break(
118128
self,
119-
sequential_threading_handle,
120-
time_machine,
121-
safe_eyes_core,
129+
sequential_threading_handle: SafeEyesCoreHandle,
130+
time_machine: TimeMachineFixture,
131+
safe_eyes_core: core.SafeEyesCore,
122132
context,
123-
break_duration,
124-
break_name_translated,
133+
break_duration: int,
134+
break_name_translated: str,
125135
initial: bool = False,
126136
):
127137
"""Run one entire cycle of safe_eyes_core.
@@ -208,7 +218,7 @@ def run_next_break(
208218

209219
assert context["state"] == model.State.BREAK
210220

211-
def assert_datetime(self, string):
221+
def assert_datetime(self, string: str):
212222
if not string.endswith("+00:00"):
213223
string += "+00:00"
214224
assert datetime.datetime.now(
@@ -230,18 +240,21 @@ def test_create_empty(self):
230240
safe_eyes_core = core.SafeEyesCore(context)
231241
safe_eyes_core.initialize(config)
232242

233-
def test_start_empty(self, sequential_threading):
234-
context = {}
235-
config = {
236-
"short_breaks": [],
237-
"long_breaks": [],
238-
"short_break_interval": 15,
239-
"long_break_interval": 75,
240-
"long_break_duration": 60,
241-
"short_break_duration": 15,
242-
"random_order": False,
243-
"postpone_duration": 5,
244-
}
243+
def test_start_empty(self, sequential_threading: SequentialThreadingFixture):
244+
context: dict[str, typing.Any] = {}
245+
config = model.Config(
246+
user_config={
247+
"short_breaks": [],
248+
"long_breaks": [],
249+
"short_break_interval": 15,
250+
"long_break_interval": 75,
251+
"long_break_duration": 60,
252+
"short_break_duration": 15,
253+
"random_order": False,
254+
"postpone_duration": 5,
255+
},
256+
system_config={},
257+
)
245258
on_update_next_break = mock.Mock()
246259
safe_eyes_core = core.SafeEyesCore(context)
247260
safe_eyes_core.on_update_next_break += mock
@@ -253,29 +266,32 @@ def test_start_empty(self, sequential_threading):
253266

254267
on_update_next_break.assert_not_called()
255268

256-
def test_start(self, sequential_threading, time_machine):
257-
context = {
269+
def test_start(self, sequential_threading: SequentialThreadingFixture):
270+
context: dict[str, typing.Any] = {
258271
"session": {},
259272
}
260-
config = {
261-
"short_breaks": [
262-
{"name": "break 1"},
263-
{"name": "break 2"},
264-
{"name": "break 3"},
265-
{"name": "break 4"},
266-
],
267-
"long_breaks": [
268-
{"name": "long break 1"},
269-
{"name": "long break 2"},
270-
{"name": "long break 3"},
271-
],
272-
"short_break_interval": 15,
273-
"long_break_interval": 75,
274-
"long_break_duration": 60,
275-
"short_break_duration": 15,
276-
"random_order": False,
277-
"postpone_duration": 5,
278-
}
273+
config = model.Config(
274+
user_config={
275+
"short_breaks": [
276+
{"name": "break 1"},
277+
{"name": "break 2"},
278+
{"name": "break 3"},
279+
{"name": "break 4"},
280+
],
281+
"long_breaks": [
282+
{"name": "long break 1"},
283+
{"name": "long break 2"},
284+
{"name": "long break 3"},
285+
],
286+
"short_break_interval": 15,
287+
"long_break_interval": 75,
288+
"long_break_duration": 60,
289+
"short_break_duration": 15,
290+
"random_order": False,
291+
"postpone_duration": 5,
292+
},
293+
system_config={},
294+
)
279295
on_update_next_break = mock.Mock()
280296
safe_eyes_core = core.SafeEyesCore(context)
281297
safe_eyes_core.on_update_next_break += on_update_next_break
@@ -300,35 +316,42 @@ def test_start(self, sequential_threading, time_machine):
300316
safe_eyes_core.stop()
301317
assert context["state"] == model.State.STOPPED
302318

303-
def test_full_run_with_defaults(self, sequential_threading, time_machine):
304-
context = {
319+
def test_full_run_with_defaults(
320+
self,
321+
sequential_threading: SequentialThreadingFixture,
322+
time_machine: TimeMachineFixture,
323+
):
324+
context: dict[str, typing.Any] = {
305325
"session": {},
306326
}
307327
short_break_duration = 15 # seconds
308328
short_break_interval = 15 # minutes
309329
pre_break_warning_time = 10 # seconds
310330
long_break_duration = 60 # seconds
311331
long_break_interval = 75 # minutes
312-
config = {
313-
"short_breaks": [
314-
{"name": "break 1"},
315-
{"name": "break 2"},
316-
{"name": "break 3"},
317-
{"name": "break 4"},
318-
],
319-
"long_breaks": [
320-
{"name": "long break 1"},
321-
{"name": "long break 2"},
322-
{"name": "long break 3"},
323-
],
324-
"short_break_interval": short_break_interval,
325-
"long_break_interval": long_break_interval,
326-
"long_break_duration": long_break_duration,
327-
"short_break_duration": short_break_duration,
328-
"pre_break_warning_time": pre_break_warning_time,
329-
"random_order": False,
330-
"postpone_duration": 5,
331-
}
332+
config = model.Config(
333+
user_config={
334+
"short_breaks": [
335+
{"name": "break 1"},
336+
{"name": "break 2"},
337+
{"name": "break 3"},
338+
{"name": "break 4"},
339+
],
340+
"long_breaks": [
341+
{"name": "long break 1"},
342+
{"name": "long break 2"},
343+
{"name": "long break 3"},
344+
],
345+
"short_break_interval": short_break_interval,
346+
"long_break_interval": long_break_interval,
347+
"long_break_duration": long_break_duration,
348+
"short_break_duration": short_break_duration,
349+
"pre_break_warning_time": pre_break_warning_time,
350+
"random_order": False,
351+
"postpone_duration": 5,
352+
},
353+
system_config={},
354+
)
332355

333356
self.assert_datetime("2024-08-25T13:00:00")
334357

@@ -420,37 +443,42 @@ def test_full_run_with_defaults(self, sequential_threading, time_machine):
420443
assert context["state"] == model.State.STOPPED
421444

422445
def test_long_duration_is_bigger_than_short_interval(
423-
self, sequential_threading, time_machine
446+
self,
447+
sequential_threading: SequentialThreadingFixture,
448+
time_machine: TimeMachineFixture,
424449
):
425450
"""Example taken from https://github.com/slgobinath/SafeEyes/issues/640."""
426-
context = {
451+
context: dict[str, typing.Any] = {
427452
"session": {},
428453
}
429454
short_break_duration = 300 # seconds = 5min
430455
short_break_interval = 25 # minutes
431456
pre_break_warning_time = 10 # seconds
432457
long_break_duration = 1800 # seconds = 30min
433458
long_break_interval = 100 # minutes
434-
config = {
435-
"short_breaks": [
436-
{"name": "break 1"},
437-
{"name": "break 2"},
438-
{"name": "break 3"},
439-
{"name": "break 4"},
440-
],
441-
"long_breaks": [
442-
{"name": "long break 1"},
443-
{"name": "long break 2"},
444-
{"name": "long break 3"},
445-
],
446-
"short_break_interval": short_break_interval,
447-
"long_break_interval": long_break_interval,
448-
"long_break_duration": long_break_duration,
449-
"short_break_duration": short_break_duration,
450-
"pre_break_warning_time": pre_break_warning_time,
451-
"random_order": False,
452-
"postpone_duration": 5,
453-
}
459+
config = model.Config(
460+
user_config={
461+
"short_breaks": [
462+
{"name": "break 1"},
463+
{"name": "break 2"},
464+
{"name": "break 3"},
465+
{"name": "break 4"},
466+
],
467+
"long_breaks": [
468+
{"name": "long break 1"},
469+
{"name": "long break 2"},
470+
{"name": "long break 3"},
471+
],
472+
"short_break_interval": short_break_interval,
473+
"long_break_interval": long_break_interval,
474+
"long_break_duration": long_break_duration,
475+
"short_break_duration": short_break_duration,
476+
"pre_break_warning_time": pre_break_warning_time,
477+
"random_order": False,
478+
"postpone_duration": 5,
479+
},
480+
system_config={},
481+
)
454482

455483
self.assert_datetime("2024-08-25T13:00:00")
456484

0 commit comments

Comments
 (0)