|
| 1 | +from copy import deepcopy |
| 2 | +from pathlib import Path |
| 3 | +from unittest.mock import AsyncMock |
| 4 | + |
| 5 | +import pytest |
| 6 | +from hypothesis import given, settings |
| 7 | +from hypothesis import strategies as st |
| 8 | +from transitions import Machine |
| 9 | +from transitions.extensions import HierarchicalAsyncGraphMachine |
| 10 | +from transitions.extensions.asyncio import HierarchicalAsyncMachine |
| 11 | +from transitions.extensions.markup import HierarchicalMarkupMachine |
| 12 | + |
| 13 | +from nextline_schedule.auto.state_machine.config import CONFIG |
| 14 | + |
| 15 | +SELF_LITERAL = Machine.self_literal |
| 16 | + |
| 17 | + |
| 18 | +def test_model_default() -> None: |
| 19 | + machine = HierarchicalAsyncMachine(model=None, **CONFIG) # type: ignore |
| 20 | + assert not machine.models |
| 21 | + |
| 22 | + |
| 23 | +def test_model_self_literal() -> None: |
| 24 | + machine = HierarchicalAsyncMachine(model=SELF_LITERAL, **CONFIG) # type: ignore |
| 25 | + assert machine.models[0] is machine |
| 26 | + assert len(machine.models) == 1 |
| 27 | + |
| 28 | + |
| 29 | +def test_restore_from_markup() -> None: |
| 30 | + machine = HierarchicalMarkupMachine(model=None, **CONFIG) # type: ignore |
| 31 | + assert isinstance(machine.markup, dict) |
| 32 | + markup = deepcopy(machine.markup) |
| 33 | + del markup['models'] # type: ignore |
| 34 | + rebuild = HierarchicalMarkupMachine(model=None, **markup) # type: ignore |
| 35 | + assert rebuild.markup == machine.markup |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.skip |
| 39 | +def test_graph(tmp_path: Path) -> None: # pragma: no cover |
| 40 | + FILE_NAME = 'states.png' |
| 41 | + path = tmp_path / FILE_NAME |
| 42 | + # print(f'Saving the state diagram to {path}...') |
| 43 | + machine = HierarchicalAsyncGraphMachine(model=SELF_LITERAL, **CONFIG) # type: ignore |
| 44 | + machine.get_graph().draw(path, prog='dot') |
| 45 | + |
| 46 | + |
| 47 | +STATE_MAP = { |
| 48 | + 'created': { |
| 49 | + 'start': {'dest': 'off'}, |
| 50 | + }, |
| 51 | + 'off': { |
| 52 | + 'turn_on': {'dest': 'auto_waiting'}, |
| 53 | + }, |
| 54 | + 'auto_waiting': { |
| 55 | + 'turn_off': {'dest': 'off', 'before': 'cancel_task'}, |
| 56 | + 'on_initialized': {'dest': 'auto_pulling'}, |
| 57 | + 'on_finished': {'dest': 'auto_pulling'}, |
| 58 | + 'on_raised': {'dest': 'off'}, |
| 59 | + }, |
| 60 | + 'auto_pulling': { |
| 61 | + 'run': {'dest': 'auto_running'}, |
| 62 | + 'turn_off': {'dest': 'off', 'before': 'cancel_task'}, |
| 63 | + 'on_raised': {'dest': 'off'}, |
| 64 | + }, |
| 65 | + 'auto_running': { |
| 66 | + 'on_finished': {'dest': 'auto_pulling'}, |
| 67 | + 'turn_off': {'dest': 'off', 'before': 'cancel_task'}, |
| 68 | + 'on_raised': {'dest': 'off'}, |
| 69 | + }, |
| 70 | +} |
| 71 | + |
| 72 | +TRIGGERS = list({trigger for v in STATE_MAP.values() for trigger in v.keys()}) |
| 73 | + |
| 74 | + |
| 75 | +@settings(max_examples=200) |
| 76 | +@given(triggers=st.lists(st.sampled_from(TRIGGERS))) |
| 77 | +async def test_transitions(triggers: list[str]) -> None: |
| 78 | + machine = HierarchicalAsyncMachine(model=SELF_LITERAL, **CONFIG) # type: ignore |
| 79 | + assert machine.is_created() |
| 80 | + |
| 81 | + for trigger in triggers: |
| 82 | + prev = machine.state |
| 83 | + if (map_ := STATE_MAP[prev].get(trigger)) is None: |
| 84 | + await getattr(machine, trigger)() |
| 85 | + assert machine.state == prev |
| 86 | + continue |
| 87 | + |
| 88 | + if before := map_.get('before'): |
| 89 | + setattr(machine, before, AsyncMock()) |
| 90 | + |
| 91 | + assert await getattr(machine, trigger)() is True |
| 92 | + dest = map_['dest'] |
| 93 | + assert getattr(machine, f'is_{dest}')() |
| 94 | + |
| 95 | + if before: |
| 96 | + assert getattr(machine, before).call_count == 1 |
| 97 | + assert getattr(machine, before).await_count == 1 |
0 commit comments