forked from reactive-python/reactpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_testing.py
223 lines (181 loc) · 6.86 KB
/
test_testing.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
import logging
import os
import pytest
from reactpy import Ref, component, html, testing
from reactpy.backend import starlette as starlette_implementation
from reactpy.logging import ROOT_LOGGER
from reactpy.testing.backend import _hotswap
from reactpy.testing.display import DisplayFixture
from tests.sample import SampleApp
def test_assert_reactpy_logged_does_not_suppress_errors():
with pytest.raises(RuntimeError, match="expected error"):
with testing.assert_reactpy_did_log():
msg = "expected error"
raise RuntimeError(msg)
def test_assert_reactpy_logged_message():
with testing.assert_reactpy_did_log(match_message="my message"):
ROOT_LOGGER.info("my message")
with testing.assert_reactpy_did_log(match_message=r".*"):
ROOT_LOGGER.info("my message")
def test_assert_reactpy_logged_error():
with testing.assert_reactpy_did_log(
match_message="log message",
error_type=ValueError,
match_error="my value error",
):
try:
msg = "my value error"
raise ValueError(msg)
except ValueError:
ROOT_LOGGER.exception("log message")
with pytest.raises(
AssertionError,
match=r"Could not find a log record matching the given",
):
with testing.assert_reactpy_did_log(
match_message="log message",
error_type=ValueError,
match_error="my value error",
):
try:
# change error type
msg = "my value error"
raise RuntimeError(msg)
except RuntimeError:
ROOT_LOGGER.exception("log message")
with pytest.raises(
AssertionError,
match=r"Could not find a log record matching the given",
):
with testing.assert_reactpy_did_log(
match_message="log message",
error_type=ValueError,
match_error="my value error",
):
try:
# change error message
msg = "something else"
raise ValueError(msg)
except ValueError:
ROOT_LOGGER.exception("log message")
with pytest.raises(
AssertionError,
match=r"Could not find a log record matching the given",
):
with testing.assert_reactpy_did_log(
match_message="log message",
error_type=ValueError,
match_error="my value error",
):
try:
# change error message
msg = "my error message"
raise ValueError(msg)
except ValueError:
ROOT_LOGGER.exception("something else")
def test_assert_reactpy_logged_assertion_error_message():
with pytest.raises(
AssertionError,
match=r"Could not find a log record matching the given",
):
with testing.assert_reactpy_did_log(
# put in all possible params full assertion error message
match_message=r".*",
error_type=Exception,
match_error=r".*",
):
pass
def test_assert_reactpy_logged_ignores_level():
original_level = ROOT_LOGGER.level
ROOT_LOGGER.setLevel(logging.INFO)
try:
with testing.assert_reactpy_did_log(match_message=r".*"):
# this log would normally be ignored
ROOT_LOGGER.debug("my message")
finally:
ROOT_LOGGER.setLevel(original_level)
def test_assert_reactpy_did_not_log():
with testing.assert_reactpy_did_not_log(match_message="my message"):
pass
with testing.assert_reactpy_did_not_log(match_message=r"something else"):
ROOT_LOGGER.info("my message")
with pytest.raises(
AssertionError,
match=r"Did find a log record matching the given",
):
with testing.assert_reactpy_did_not_log(
# put in all possible params full assertion error message
match_message=r".*",
error_type=Exception,
match_error=r".*",
):
try:
msg = "something"
raise Exception(msg)
except Exception:
ROOT_LOGGER.exception("something")
async def test_simple_display_fixture():
if os.name == "nt":
pytest.skip("Browser tests not supported on Windows")
async with testing.DisplayFixture() as display:
await display.show(SampleApp)
await display.page.wait_for_selector("#sample")
def test_if_app_is_given_implementation_must_be_too():
with pytest.raises(
ValueError,
match=r"If an application instance its corresponding server implementation must be provided too",
):
testing.BackendFixture(app=starlette_implementation.create_development_app())
testing.BackendFixture(
app=starlette_implementation.create_development_app(),
implementation=starlette_implementation,
)
def test_list_logged_excptions():
the_error = None
with testing.capture_reactpy_logs() as records:
ROOT_LOGGER.info("A non-error log message")
try:
msg = "An error for testing"
raise ValueError(msg)
except Exception as error:
ROOT_LOGGER.exception("Log the error")
the_error = error
logged_errors = testing.logs.list_logged_exceptions(records)
assert logged_errors == [the_error]
async def test_hotswap_update_on_change(display: DisplayFixture):
"""Ensure shared hotswapping works
This basically means that previously rendered views of a hotswap component get updated
when a new view is mounted, not just the next time it is re-displayed
In this test we construct a scenario where clicking a button will cause a pre-existing
hotswap component to be updated
"""
def hotswap_1():
return html.div({"id": "hotswap-1"}, 1)
def hotswap_2():
return html.div({"id": "hotswap-2"}, 2)
def hotswap_3():
return html.div({"id": "hotswap-3"}, 3)
@component
def ButtonSwapsDivs():
count = Ref(0)
mount, hostswap = _hotswap(update_on_change=True)
async def on_click(event):
count.set_current(count.current + 1)
if count.current == 1:
mount(hotswap_1)
if count.current == 2:
mount(hotswap_2)
if count.current == 3:
mount(hotswap_3)
return html.div(
html.button({"on_click": on_click, "id": "incr-button"}, "incr"),
hostswap(),
)
await display.show(ButtonSwapsDivs)
client_incr_button = await display.page.wait_for_selector("#incr-button")
await client_incr_button.click()
await display.page.wait_for_selector("#hotswap-1")
await client_incr_button.click()
await display.page.wait_for_selector("#hotswap-2")
await client_incr_button.click()
await display.page.wait_for_selector("#hotswap-3")