-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_combinations.py
80 lines (55 loc) · 1.73 KB
/
test_combinations.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
# ruff: noqa: D100, D101, D102, D103, D104, D107
from __future__ import annotations
from dataclasses import replace
from typing import Literal
import pytest
from immutable import Immutable
from redux.basic_types import (
BaseAction,
CompleteReducerResult,
FinishAction,
FinishEvent,
InitAction,
InitializationActionError,
StoreOptions,
)
from redux.main import Store
class StateType(Immutable):
value1: int
value2: int
class IncrementAction(BaseAction):
which: Literal[1, 2]
Action = IncrementAction | InitAction | FinishAction
def reducer(
state: StateType | None,
action: Action,
) -> StateType | CompleteReducerResult[StateType, Action, FinishEvent]:
if state is None:
if isinstance(action, InitAction):
return StateType(value1=0, value2=0)
raise InitializationActionError(action)
if isinstance(action, IncrementAction):
field_name = f'value{action.which}'
return replace(
state,
**{field_name: getattr(state, field_name) + 1},
)
return state
StoreType = Store[StateType, Action, FinishEvent]
@pytest.fixture
def store() -> StoreType:
return Store(reducer, options=StoreOptions(auto_init=True))
def test_autorun_of_view(store: StoreType) -> None:
@store.autorun(
lambda state: state.value2,
lambda state: (state.value1, state.value2),
)
@store.view(lambda state: state.value1)
def view(value1: int, value2: int) -> tuple[int, int]:
return (value1, value2)
assert view() == (0, 0)
store.dispatch(IncrementAction(which=1))
assert view() == (1, 0)
store.dispatch(IncrementAction(which=2))
assert view() == (1, 1)
store.dispatch(FinishAction())