/
test_config.py
150 lines (114 loc) · 4.72 KB
/
test_config.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
"""
Tests pn.config variables
"""
import pytest
from panel import config, state
from panel.pane import HTML
from panel.tests.conftest import set_env_var
def test_env_var_console_output():
with set_env_var('PANEL_CONSOLE_OUTPUT', 'disable'):
assert config.console_output == 'disable'
with set_env_var('PANEL_CONSOLE_OUTPUT', 'replace'):
assert config.console_output == 'replace'
with config.set(console_output='disable'):
with set_env_var('PANEL_DOC_BUILD', 'accumulate'):
assert config.console_output == 'disable'
def test_config_set_console_output():
with config.set(console_output=False):
assert config.console_output == 'disable'
with config.set(console_output='disable'):
assert config.console_output == 'disable'
with config.set(console_output='replace'):
assert config.console_output == 'replace'
with config.set(console_output='disable'):
with config.set(console_output='accumulate'):
assert config.console_output == 'accumulate'
@pytest.mark.usefixtures("with_curdoc")
def test_session_override():
config.sizing_mode = 'stretch_width'
assert config.sizing_mode == 'stretch_width'
assert state.curdoc in config._session_config
assert config._session_config[state.curdoc] == {'sizing_mode': 'stretch_width'}
state.curdoc = None
assert config.sizing_mode is None
def test_console_output_replace_stdout(document, comm, get_display_handle):
pane = HTML()
with set_env_var('PANEL_CONSOLE_OUTPUT', 'replace'):
model = pane.get_root(document, comm)
handle = get_display_handle(model)
pane._on_stdout(model.ref['id'], ['print output'])
assert handle == {'text/html': 'print output</br>', 'raw': True}
pane._on_stdout(model.ref['id'], ['new output'])
assert handle == {'text/html': 'new output</br>', 'raw': True}
pane._cleanup(model)
assert model.ref['id'] not in state._handles
def test_console_output_accumulate_stdout(document, comm, get_display_handle):
pane = HTML()
model = pane.get_root(document, comm)
handle = get_display_handle(model)
pane._on_stdout(model.ref['id'], ['print output'])
assert handle == {'text/html': 'print output</br>', 'raw': True}
pane._on_stdout(model.ref['id'], ['new output'])
assert handle == {'text/html': 'print output</br>\nnew output</br>', 'raw': True}
pane._cleanup(model)
assert model.ref['id'] not in state._handles
def test_console_output_disable_stdout(document, comm, get_display_handle):
pane = HTML()
with set_env_var('PANEL_CONSOLE_OUTPUT', 'disable'):
model = pane.get_root(document, comm)
handle = get_display_handle(model)
pane._on_stdout(model.ref['id'], ['print output'])
assert handle == {}
pane._cleanup(model)
assert model.ref['id'] not in state._handles
def test_console_output_replace_error(document, comm, get_display_handle):
pane = HTML()
with set_env_var('PANEL_CONSOLE_OUTPUT', 'replace'):
model = pane.get_root(document, comm)
handle = get_display_handle(model)
try:
1/0
except Exception as e:
pane._on_error(model.ref['id'], e)
assert 'text/html' in handle
assert 'ZeroDivisionError' in handle['text/html']
try:
1 + '2'
except Exception as e:
pane._on_error(model.ref['id'], e)
assert 'text/html' in handle
assert 'ZeroDivisionError' not in handle['text/html']
assert 'TypeError' in handle['text/html']
pane._cleanup(model)
assert model.ref['id'] not in state._handles
def test_console_output_accumulate_error(document, comm, get_display_handle):
pane = HTML()
model = pane.get_root(document, comm)
handle = get_display_handle(model)
try:
1/0
except Exception as e:
pane._on_error(model.ref['id'], e)
assert 'text/html' in handle
assert 'ZeroDivisionError' in handle['text/html']
try:
1 + '2'
except Exception as e:
pane._on_error(model.ref['id'], e)
assert 'text/html' in handle
assert 'ZeroDivisionError' in handle['text/html']
assert 'TypeError' in handle['text/html']
pane._cleanup(model)
assert model.ref['id'] not in state._handles
def test_console_output_disable_error(document, comm, get_display_handle):
pane = HTML()
with set_env_var('PANEL_CONSOLE_OUTPUT', 'disable'):
model = pane.get_root(document, comm)
handle = get_display_handle(model)
try:
1/0
except Exception as e:
pane._on_error(model.ref['id'], e)
assert handle == {}
pane._cleanup(model)
assert model.ref['id'] not in state._handles