Skip to content

Commit 21f1e12

Browse files
committed
test: add/modify tests to bring back coverage to 100%
1 parent 9062a50 commit 21f1e12

File tree

6 files changed

+149
-19
lines changed

6 files changed

+149
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Upcoming
44

55
- fix: add `__name__` to `WithStore` instances so that they play nice when passed as a function to something assuming they are functions and have a `__name__` attribute
6+
- test: add/modify tests to bring back coverage to 100%
67

78
## Version 0.22.0
89

tests/results/test_weakref/subscription/store-000.jsonc

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/results/test_weakref/subscription/store-001.jsonc

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/test_autorun.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,41 @@ def store() -> Generator[StoreType, None, None]:
8484

8585
def test_general(store_snapshot: StoreSnapshot, store: StoreType) -> None:
8686
@store.autorun(lambda state: state.value)
87-
def decorated(value: int) -> int:
87+
def _(value: int) -> int:
8888
store_snapshot.take()
8989
return value
9090

91+
92+
def test_name(store: StoreType) -> None:
93+
"""Test `autorun` decorator name attribute."""
94+
95+
@store.autorun(lambda state: state.value)
96+
def decorated(value: int) -> int:
97+
return value
98+
9199
assert decorated.__name__ == 'Autorun:decorated'
92100

101+
inline_decorated = store.autorun(lambda state: state.value)(
102+
lambda value: value,
103+
)
104+
105+
assert inline_decorated.__name__ == 'Autorun:<lambda>'
106+
107+
class Decorated:
108+
def __call__(self, value: int) -> int:
109+
return value
110+
111+
def __repr__(self) -> str:
112+
return 'Decorated'
113+
114+
decorated_instance = store.autorun(lambda state: state.value)(Decorated())
115+
116+
assert decorated_instance.__name__ == 'Autorun:Decorated'
117+
93118

94119
def test_ignore_attribute_error_in_selector(store: StoreType) -> None:
95120
@store.autorun(lambda state: cast('Any', state).non_existing)
96-
def _(_: int) -> int:
121+
def _(_: int) -> None:
97122
pytest.fail('This should never be called')
98123

99124

@@ -102,7 +127,7 @@ def test_ignore_attribute_error_in_comparator(store: StoreType) -> None:
102127
lambda state: state.value,
103128
lambda state: cast('Any', state).non_existing,
104129
)
105-
def _(_: int) -> int:
130+
def _(_: int) -> None:
106131
pytest.fail('This should never be called')
107132

108133

tests/test_weakref.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,28 +237,36 @@ def render(value: int) -> int:
237237
store.dispatch(IncrementAction())
238238

239239

240-
def test_subscription(
241-
store_snapshot: StoreSnapshot,
242-
store: StoreType,
243-
) -> None:
240+
def test_subscription(store: StoreType) -> None:
241+
with_ref_counter = []
242+
244243
def subscription_with_keep_ref(_: StateType) -> None:
245-
store_snapshot.take()
244+
with_ref_counter.append(None)
246245

247246
store._subscribe(subscription_with_keep_ref) # noqa: SLF001
247+
248248
ref = weakref.ref(subscription_with_keep_ref)
249249
del subscription_with_keep_ref
250250
assert ref() is not None
251251

252+
without_ref_counter = []
253+
252254
def subscription_without_keep_ref(_: StateType) -> None:
253-
pytest.fail('This should never be called')
255+
without_ref_counter.append(None)
254256

255257
store._subscribe(subscription_without_keep_ref, keep_ref=False) # noqa: SLF001
258+
259+
store.dispatch(IncrementAction())
260+
256261
ref = weakref.ref(subscription_without_keep_ref)
257262
del subscription_without_keep_ref
258263
assert ref() is None
259264

260265
store.dispatch(IncrementAction())
261266

267+
assert len(with_ref_counter) == 2
268+
assert len(without_ref_counter) == 1
269+
262270

263271
def test_subscription_method(
264272
store_snapshot: StoreSnapshot,

tests/test_with_state.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
import inspect
6+
import re
57
from dataclasses import replace
68
from typing import TYPE_CHECKING
79

@@ -54,6 +56,110 @@ def _() -> StoreType:
5456
return Store(_reducer, options=CreateStoreOptions(auto_init=False))
5557

5658

59+
def test_name_attr(store: StoreType) -> None:
60+
"""Test `with_state` decorator name attribute."""
61+
62+
@store.with_state(lambda state: state.value)
63+
def decorated(value: int) -> int:
64+
return value
65+
66+
assert decorated.__name__ == 'WithState:decorated'
67+
68+
inline_decorated = store.with_state(lambda state: state.value)(
69+
lambda value: value,
70+
)
71+
72+
assert inline_decorated.__name__ == 'WithState:<lambda>'
73+
74+
class Decorated:
75+
def __call__(self, value: int) -> int:
76+
return value
77+
78+
def __repr__(self) -> str:
79+
return 'Decorated'
80+
81+
decorated_instance = store.with_state(lambda state: state.value)(Decorated())
82+
83+
assert decorated_instance.__name__ == 'WithState:Decorated'
84+
85+
store.dispatch(InitAction())
86+
store.dispatch(FinishAction())
87+
88+
89+
def test_repr(store: StoreType) -> None:
90+
"""Test `with_state` decorator `__repr__` method."""
91+
92+
@store.with_state(lambda state: state.value)
93+
def func(value: int) -> int:
94+
return value
95+
96+
assert re.match(
97+
r'.*\(func: <function test_repr\.<locals>\.func at .*>\)$',
98+
repr(func),
99+
)
100+
101+
store.dispatch(InitAction())
102+
store.dispatch(FinishAction())
103+
104+
105+
def test_signature(store: StoreType) -> None:
106+
"""Test `with_state` decorator `__signature__` attribute."""
107+
108+
@store.with_state(lambda state: state.value)
109+
def func(
110+
value: int,
111+
some_positional_parameter: str,
112+
some_positional_parameter_with_default: int = 0,
113+
*,
114+
some_keyword_parameter: bool,
115+
some_keyword_parameter_with_default: int = 1,
116+
) -> int:
117+
_ = (
118+
some_positional_parameter,
119+
some_positional_parameter_with_default,
120+
some_keyword_parameter,
121+
some_keyword_parameter_with_default,
122+
)
123+
return value
124+
125+
signature = inspect.signature(func)
126+
assert len(signature.parameters) == 4
127+
128+
assert 'some_positional_parameter' in signature.parameters
129+
assert (
130+
signature.parameters['some_positional_parameter'].default
131+
is inspect.Parameter.empty
132+
)
133+
assert signature.parameters['some_positional_parameter'].annotation == 'str'
134+
135+
assert 'some_positional_parameter_with_default' in signature.parameters
136+
assert signature.parameters['some_positional_parameter_with_default'].default == 0
137+
assert (
138+
signature.parameters['some_positional_parameter_with_default'].annotation
139+
== 'int'
140+
)
141+
142+
assert 'some_keyword_parameter' in signature.parameters
143+
assert (
144+
signature.parameters['some_keyword_parameter'].default
145+
is inspect.Parameter.empty
146+
)
147+
assert signature.parameters['some_keyword_parameter'].annotation == 'bool'
148+
149+
assert 'some_keyword_parameter_with_default' in signature.parameters
150+
assert signature.parameters['some_keyword_parameter_with_default'].default == 1
151+
assert (
152+
signature.parameters['some_keyword_parameter_with_default'].annotation == 'int'
153+
)
154+
155+
assert 'value' not in signature.parameters
156+
157+
assert signature.return_annotation == 'int'
158+
159+
store.dispatch(InitAction())
160+
store.dispatch(FinishAction())
161+
162+
57163
def test_with_state(store: StoreType) -> None:
58164
"""Test `with_state` decorator."""
59165
counter = 0

0 commit comments

Comments
 (0)