-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_features.py
228 lines (174 loc) · 5.71 KB
/
test_features.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# ruff: noqa: D100, D101, D102, D103, D104, D107, T201
from __future__ import annotations
import time
from typing import TYPE_CHECKING, TypeAlias
import pytest
from immutable import Immutable
from redux import CombineReducerRegisterAction, CombineReducerUnregisterAction, Store
from redux.combine_reducers import combine_reducers
from redux.main import CreateStoreOptions
if TYPE_CHECKING:
from redux_pytest.fixtures import StoreMonitor, StoreSnapshot
from redux.basic_types import (
BaseAction,
BaseCombineReducerState,
BaseEvent,
CombineReducerAction,
CompleteReducerResult,
FinishAction,
InitAction,
InitializationActionError,
ReducerResult,
ReducerType,
)
class CountAction(BaseAction): ...
class IncrementAction(CountAction): ...
class DecrementByTwoAction(CountAction): ...
class DoNothingAction(CountAction): ...
class CountStateType(Immutable):
count: int
class StateType(BaseCombineReducerState):
straight: CountStateType
base10: CountStateType
inverse: CountStateType
ActionType: TypeAlias = InitAction | FinishAction | CountAction | CombineReducerAction
class SleepEvent(BaseEvent):
duration: float
class PrintEvent(BaseEvent):
message: str
# Reducers
# --------
def straight_reducer(
state: CountStateType | None,
action: ActionType,
) -> CountStateType:
if state is None:
if isinstance(action, InitAction):
return CountStateType(count=0)
raise InitializationActionError(action)
if isinstance(action, IncrementAction):
return CountStateType(count=state.count + 1)
if isinstance(action, DecrementByTwoAction):
return CountStateType(count=state.count - 2)
return state
def base10_reducer(
state: CountStateType | None,
action: ActionType,
) -> CountStateType:
if state is None:
if isinstance(action, InitAction):
return CountStateType(count=10)
raise InitializationActionError(action)
if isinstance(action, IncrementAction):
return CountStateType(count=state.count + 1)
if isinstance(action, DecrementByTwoAction):
return CountStateType(count=state.count - 2)
return state
def inverse_reducer(
state: CountStateType | None,
action: ActionType,
) -> ReducerResult[CountStateType, ActionType, SleepEvent]:
if state is None:
if isinstance(action, InitAction):
return CountStateType(count=0)
raise InitializationActionError(action)
if isinstance(action, IncrementAction):
return CountStateType(count=state.count - 1)
if isinstance(action, DecrementByTwoAction):
return CountStateType(count=state.count + 2)
if isinstance(action, DoNothingAction):
return CompleteReducerResult(
state=state,
actions=[IncrementAction()],
events=[SleepEvent(duration=0.1)],
)
return state
Reducer: TypeAlias = tuple[
ReducerType[StateType, ActionType, SleepEvent | PrintEvent],
str,
]
@pytest.fixture
def reducer() -> Reducer:
return combine_reducers(
state_type=StateType,
action_type=ActionType, # pyright: ignore [reportArgumentType]
event_type=SleepEvent | PrintEvent, # pyright: ignore [reportArgumentType]
straight=straight_reducer,
base10=base10_reducer,
)
@pytest.fixture
def store(reducer: Reducer) -> Store:
return Store(
reducer[0],
CreateStoreOptions(
threads=2,
action_middlewares=[lambda action: print(action) or action],
event_middlewares=[lambda event: print(event) or event],
),
)
def test_general(
store: Store,
reducer: Reducer,
store_snapshot: StoreSnapshot,
store_monitor: StoreMonitor,
) -> None:
_, reducer_id = reducer
store_snapshot.take(title='initialization')
with pytest.raises(InitializationActionError):
store.dispatch(IncrementAction())
store_monitor.dispatched_actions.reset_mock()
store.dispatch(InitAction())
store_monitor.dispatched_actions.assert_called_once_with(InitAction())
# Event Subscription
# ------------------
store.subscribe(lambda _: store_snapshot.take(title='subscription'))
def event_handler(event: SleepEvent) -> None:
time.sleep(event.duration)
def event_handler_without_parameter() -> None:
time.sleep(0.1)
def never_called_event_handler() -> None:
pytest.fail('This should never be called')
store.subscribe_event(SleepEvent, event_handler)
store.subscribe_event(
SleepEvent,
event_handler_without_parameter,
)
unsubscribe = store.subscribe_event(PrintEvent, never_called_event_handler)
unsubscribe()
# Autorun
# -------
@store.autorun(lambda state: state.base10)
def render(base10_value: CountStateType) -> int:
store_snapshot.take(title='autorun')
return base10_value.count
render.subscribe(lambda _: store_snapshot.take(title='autorun_subscription'))
# Dispatch
# --------
store_snapshot.take()
store.dispatch(IncrementAction())
store_snapshot.take()
store.dispatch(
CombineReducerRegisterAction(
_id=reducer_id,
key='inverse',
reducer=inverse_reducer,
),
)
store.dispatch(DoNothingAction())
store_snapshot.take()
store.dispatch(
CombineReducerUnregisterAction(
_id=reducer_id,
key='straight',
),
)
store_snapshot.take()
store.dispatch(DecrementByTwoAction())
store_snapshot.take()
store.dispatch(
with_state=lambda state: DecrementByTwoAction() if state else IncrementAction(),
)
store_snapshot.take()
# Finish
# ------
store.dispatch(FinishAction())