-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_with_state.py
200 lines (140 loc) · 4.97 KB
/
test_with_state.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
"""Tests for `with_state` decorator."""
from __future__ import annotations
import inspect
import re
from dataclasses import replace
from typing import TYPE_CHECKING
import pytest
from immutable import Immutable
from redux.basic_types import (
BaseAction,
FinishAction,
FinishEvent,
InitAction,
InitializationActionError,
StoreOptions,
)
from redux.main import Store
if TYPE_CHECKING:
from pytest_mock import MockerFixture
class _StateType(Immutable):
value: int
class _IncrementAction(BaseAction): ...
Action = _IncrementAction | InitAction | FinishAction
StoreType = Store[_StateType, Action, FinishEvent]
def _reducer(
state: _StateType | None,
action: Action,
) -> _StateType:
if state is None:
if isinstance(action, InitAction):
return _StateType(value=0)
raise InitializationActionError(action)
if isinstance(action, _IncrementAction):
return replace(state, value=state.value + 1)
return state
@pytest.fixture(name='store')
def _() -> StoreType:
return Store(_reducer, options=StoreOptions(auto_init=False))
def test_name_attr(store: StoreType) -> None:
"""Test `with_state` decorator name attribute."""
@store.with_state(lambda state: state.value)
def decorated(value: int) -> int:
return value
assert decorated.__name__ == 'decorated'
assert decorated.__qualname__ == 'test_name_attr.<locals>.decorated'
inline_decorated = store.with_state(lambda state: state.value)(
lambda value: value,
)
assert inline_decorated.__name__ == '<lambda>'
store.dispatch(InitAction())
store.dispatch(FinishAction())
def test_repr(store: StoreType) -> None:
"""Test `with_state` decorator `__repr__` method."""
@store.with_state(lambda state: state.value)
def func(value: int) -> int:
return value
assert re.match(
r'.*<function test_repr\.<locals>\.func at .*>$',
repr(func),
)
store.dispatch(InitAction())
store.dispatch(FinishAction())
def test_signature(store: StoreType) -> None:
"""Test `with_state` decorator `__signature__` attribute."""
@store.with_state(lambda state: state.value)
def func(
value: int,
some_positional_parameter: str,
some_positional_parameter_with_default: int = 0,
*,
some_keyword_parameter: bool,
some_keyword_parameter_with_default: int = 1,
) -> int:
_ = (
some_positional_parameter,
some_positional_parameter_with_default,
some_keyword_parameter,
some_keyword_parameter_with_default,
)
return value
signature = inspect.signature(func)
assert len(signature.parameters) == 4
assert 'some_positional_parameter' in signature.parameters
assert (
signature.parameters['some_positional_parameter'].default
is inspect.Parameter.empty
)
assert signature.parameters['some_positional_parameter'].annotation == 'str'
assert 'some_positional_parameter_with_default' in signature.parameters
assert signature.parameters['some_positional_parameter_with_default'].default == 0
assert (
signature.parameters['some_positional_parameter_with_default'].annotation
== 'int'
)
assert 'some_keyword_parameter' in signature.parameters
assert (
signature.parameters['some_keyword_parameter'].default
is inspect.Parameter.empty
)
assert signature.parameters['some_keyword_parameter'].annotation == 'bool'
assert 'some_keyword_parameter_with_default' in signature.parameters
assert signature.parameters['some_keyword_parameter_with_default'].default == 1
assert (
signature.parameters['some_keyword_parameter_with_default'].annotation == 'int'
)
assert 'value' not in signature.parameters
assert signature.return_annotation == 'int'
store.dispatch(InitAction())
store.dispatch(FinishAction())
def test_with_state(store: StoreType) -> None:
"""Test `with_state` decorator."""
counter = 0
@store.with_state(lambda state: state.value)
def check(value: int) -> int:
nonlocal counter
assert value == counter
counter += 1
return value
store.dispatch(InitAction())
for i in range(10):
assert check() == i
store.dispatch(_IncrementAction())
store.dispatch(FinishAction())
def test_with_state_for_uninitialized_store(
store: StoreType,
mocker: MockerFixture,
) -> None:
"""Test `with_state` decorator for uninitialized store."""
class X:
def check(self: X, value: int) -> None:
assert value == 0
instance = X()
check_spy = mocker.spy(instance, 'check')
check = store.with_state(lambda state: state.value)(instance.check)
with pytest.raises(RuntimeError, match=r'^Store has not been initialized yet.$'):
check()
store.dispatch(InitAction())
check()
store.dispatch(FinishAction())
check_spy.assert_called_once_with(0)