FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/tests/test_async_hooks.py at v0.41.0 · pythonnative/pythonnative · GitHub
pythonnative
pythonnative
Repository navigation
Code
Issues
11
(11)
Pull requests
4
(4)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
tests
/
test_async_hooks.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
343 lines (250 loc) · 8.75 KB
Breadcrumbs
pythonnative
/
tests
/
test_async_hooks.py
Copy path
File metadata and controls
343 lines (250 loc) · 8.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""Unit tests for async effects (use_effect), pn.use_query, pn.use_mutation."""
from
__future__
import
annotations
import
asyncio
from
typing
import
Any
import
pytest
from
pythonnative
.
component
import
component
from
pythonnative
.
element
import
Element
from
pythonnative
.
hooks
import
(
MutationCall
,
QueryResult
,
use_effect
,
use_mutation
,
use_query
,
use_state
,
)
from
pythonnative
.
reconciler
import
Reconciler
from
pythonnative
.
runtime
import
drain
,
run_blocking
from
pythonnative
.
testing
import
FakeBackend
as
_StubBackend
def
_settle
(
rec
:
Reconciler
,
predicate
:
Any
,
timeout
:
float
=
2.0
)
->
bool
:
"""Pump the framework loop and flush renders until ``predicate`` holds."""
import
time
deadline
=
time
.
monotonic
()
+
timeout
while
time
.
monotonic
()
<
deadline
:
drain
(
timeout
=
0.05
)
rec
.
flush_dirty
()
if
predicate
():
return
True
return
False
# ======================================================================
# Async use_effect (coroutine effects)
# ======================================================================
def
test_async_effect_runs_on_mount
()
->
None
:
fired
:
list
=
[]
@
component
def
screen
()
->
Element
:
async
def
effect
()
->
None
:
fired
.
append
(
True
)
use_effect
(
effect
, [])
return
Element
(
"View"
, {}, [])
rec
=
Reconciler
(
_StubBackend
())
rec
.
mount
(
screen
())
drain
()
assert
fired
==
[
True
]
def
test_async_effect_cancels_on_unmount
()
->
None
:
events
:
list
=
[]
@
component
def
screen
()
->
Element
:
async
def
effect
()
->
None
:
events
.
append
(
"started"
)
try
:
await
asyncio
.
sleep
(
5.0
)
except
asyncio
.
CancelledError
:
events
.
append
(
"cancelled"
)
raise
use_effect
(
effect
, [])
return
Element
(
"View"
, {}, [])
rec
=
Reconciler
(
_StubBackend
())
rec
.
mount
(
screen
())
drain
(
until
=
lambda
:
"started"
in
events
)
assert
events
==
[
"started"
]
rec
.
unmount
()
drain
(
until
=
lambda
:
"cancelled"
in
events
)
assert
events
==
[
"cancelled"
,
"started"
]
or
events
==
[
"started"
,
"cancelled"
]
def
test_async_effect_reruns_on_deps_change
()
->
None
:
fired_with
:
list
=
[]
@
component
def
screen
(
value
:
int
=
0
)
->
Element
:
async
def
effect
()
->
None
:
fired_with
.
append
(
value
)
use_effect
(
effect
, [
value
])
return
Element
(
"View"
, {}, [])
rec
=
Reconciler
(
_StubBackend
())
rec
.
mount
(
screen
(
0
))
drain
()
assert
fired_with
==
[
0
]
rec
.
reconcile
(
screen
(
1
))
drain
()
assert
fired_with
==
[
0
,
1
]
rec
.
reconcile
(
screen
(
1
))
drain
()
# Same value: no rerun.
assert
fired_with
==
[
0
,
1
]
def
test_async_effect_completed_cleanup_runs
()
->
None
:
"""An async effect that returns a callable gets that callable run
as its cleanup once the effect re-runs or the component unmounts."""
events
:
list
=
[]
@
component
def
screen
()
->
Element
:
async
def
effect
()
->
Any
:
events
.
append
(
"ran"
)
return
lambda
:
events
.
append
(
"cleaned"
)
use_effect
(
effect
, [])
return
Element
(
"View"
, {}, [])
rec
=
Reconciler
(
_StubBackend
())
rec
.
mount
(
screen
())
drain
()
assert
events
==
[
"ran"
]
rec
.
unmount
()
drain
()
assert
events
==
[
"ran"
,
"cleaned"
]
def
test_sync_effect_still_works
()
->
None
:
events
:
list
=
[]
@
component
def
screen
()
->
Element
:
def
effect
()
->
Any
:
events
.
append
(
"ran"
)
return
lambda
:
events
.
append
(
"cleaned"
)
use_effect
(
effect
, [])
return
Element
(
"View"
, {}, [])
rec
=
Reconciler
(
_StubBackend
())
rec
.
mount
(
screen
())
assert
events
==
[
"ran"
]
rec
.
unmount
()
assert
events
==
[
"ran"
,
"cleaned"
]
def
test_async_effect_state_update_triggers_local_rerender
()
->
None
:
"""The core async-first loop: an async effect awaits, sets state,
and the component re-renders locally, all on one thread."""
snapshots
:
list
=
[]
@
component
def
screen
()
->
Element
:
message
,
set_message
=
use_state
(
"loading"
)
snapshots
.
append
(
message
)
async
def
load
()
->
None
:
await
asyncio
.
sleep
(
0
)
set_message
(
"done"
)
use_effect
(
load
, [])
return
Element
(
"View"
, {}, [])
rec
=
Reconciler
(
_StubBackend
())
rec
.
mount
(
screen
())
assert
snapshots
[
-
1
]
==
"loading"
assert
_settle
(
rec
,
lambda
:
snapshots
[
-
1
]
==
"done"
)
# ======================================================================
# use_query
# ======================================================================
def
test_use_query_resolves_to_data
()
->
None
:
captured
:
list
=
[]
@
component
def
screen
()
->
Element
:
async
def
fetcher
()
->
str
:
return
"hello"
q
=
use_query
(
fetcher
, [])
captured
.
append
(
q
)
return
Element
(
"View"
, {}, [])
rec
=
Reconciler
(
_StubBackend
())
rec
.
mount
(
screen
())
# First snapshot is loading=True.
assert
captured
[
0
].
loading
is
True
assert
captured
[
0
].
data
is
None
assert
_settle
(
rec
,
lambda
:
captured
[
-
1
].
data
==
"hello"
)
assert
captured
[
-
1
].
loading
is
False
assert
captured
[
-
1
].
error
is
None
def
test_use_query_captures_error
()
->
None
:
captured
:
list
=
[]
@
component
def
screen
()
->
Element
:
async
def
fetcher
()
->
str
:
raise
RuntimeError
(
"boom"
)
q
=
use_query
(
fetcher
, [])
captured
.
append
(
q
)
return
Element
(
"View"
, {}, [])
rec
=
Reconciler
(
_StubBackend
())
rec
.
mount
(
screen
())
assert
_settle
(
rec
,
lambda
:
captured
[
-
1
].
error
is
not
None
)
assert
isinstance
(
captured
[
-
1
].
error
,
RuntimeError
)
assert
captured
[
-
1
].
loading
is
False
def
test_use_query_refetches_when_called
()
->
None
:
counter
=
{
"calls"
:
0
}
captured
:
list
=
[]
@
component
def
screen
()
->
Element
:
async
def
fetcher
()
->
int
:
counter
[
"calls"
]
+=
1
return
counter
[
"calls"
]
q
=
use_query
(
fetcher
, [])
captured
.
append
(
q
)
return
Element
(
"View"
, {}, [])
rec
=
Reconciler
(
_StubBackend
())
rec
.
mount
(
screen
())
assert
_settle
(
rec
,
lambda
:
captured
[
-
1
].
data
==
1
)
captured
[
-
1
].
refetch
()
assert
_settle
(
rec
,
lambda
:
captured
[
-
1
].
data
==
2
)
def
test_use_query_returns_query_result_dataclass
()
->
None
:
captured
:
list
=
[]
@
component
def
screen
()
->
Element
:
q
=
use_query
(
lambda
:
_resolved
(
"x"
), [])
captured
.
append
(
q
)
return
Element
(
"View"
, {}, [])
rec
=
Reconciler
(
_StubBackend
())
rec
.
mount
(
screen
())
assert
isinstance
(
captured
[
0
],
QueryResult
)
async
def
_resolved
(
value
:
Any
)
->
Any
:
return
value
# ======================================================================
# use_mutation
# ======================================================================
def
test_use_mutation_runs_and_resolves
()
->
None
:
captured_state
:
list
=
[]
triggered
:
list
=
[]
@
component
def
screen
()
->
Element
:
async
def
mutator
(
x
:
int
)
->
int
:
return
x
*
2
state
,
mutate
=
use_mutation
(
mutator
)
captured_state
.
append
(
state
)
triggered
.
append
(
mutate
)
return
Element
(
"View"
, {}, [])
rec
=
Reconciler
(
_StubBackend
())
rec
.
mount
(
screen
())
assert
captured_state
[
0
].
loading
is
False
assert
captured_state
[
0
].
data
is
None
call
=
triggered
[
-
1
](
7
)
assert
isinstance
(
call
,
MutationCall
)
assert
_settle
(
rec
,
lambda
:
captured_state
[
-
1
].
data
==
14
)
assert
captured_state
[
-
1
].
loading
is
False
def
test_use_mutation_awaitable_returns_value
()
->
None
:
triggered
:
list
=
[]
@
component
def
screen
()
->
Element
:
async
def
mutator
(
x
:
int
)
->
int
:
return
x
+
1
_state
,
mutate
=
use_mutation
(
mutator
)
triggered
.
append
(
mutate
)
return
Element
(
"View"
, {}, [])
rec
=
Reconciler
(
_StubBackend
())
rec
.
mount
(
screen
())
mutate
=
triggered
[
-
1
]
async
def
call_and_await
()
->
int
:
return
await
mutate
(
41
)
assert
run_blocking
(
call_and_await
(),
timeout
=
2.0
)
==
42
def
test_use_mutation_captures_error
()
->
None
:
captured_state
:
list
=
[]
triggered
:
list
=
[]
@
component
def
screen
()
->
Element
:
async
def
mutator
()
->
None
:
raise
ValueError
(
"fail"
)
state
,
mutate
=
use_mutation
(
mutator
)
captured_state
.
append
(
state
)
triggered
.
append
(
mutate
)
return
Element
(
"View"
, {}, [])
rec
=
Reconciler
(
_StubBackend
())
rec
.
mount
(
screen
())
call
=
triggered
[
-
1
]()
# Discarding the call is fine; the state will reflect the failure.
assert
_settle
(
rec
,
lambda
:
captured_state
[
-
1
].
error
is
not
None
)
assert
isinstance
(
captured_state
[
-
1
].
error
,
ValueError
)
assert
call
.
done
()
is
True
# Suppress unused-import lint when running just the file.
_
=
pytest
Back
|
FazBrowse Home
|
New Git URL