Skip to content

Commit b894b89

Browse files
Assert that errors are raised
1 parent afd5b7a commit b894b89

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

packages/python/plotly/plotly/tests/test_core/test_errors/test_dict_path_errors.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ def some_fig():
2020

2121
def test_raises_on_bad_index(some_fig):
2222
# Check indexing errors can be detected when path used as key to go.Figure
23+
raised = False
2324
try:
2425
x0 = some_fig["layout.shapes[2].x0"]
2526
except IndexError as e:
27+
raised = True
2628
assert (
2729
e.args[0].find(
2830
"""Bad property path:
@@ -31,15 +33,18 @@ def test_raises_on_bad_index(some_fig):
3133
)
3234
>= 0
3335
)
36+
assert raised
3437

3538

3639
def test_raises_on_bad_dot_property(some_fig):
3740

3841
# Check . property lookup errors can be detected when path used as key to
3942
# go.Figure
43+
raised = False
4044
try:
4145
x2000 = some_fig["layout.shapes[1].x2000"]
4246
except ValueError as e:
47+
raised = True
4348
assert (
4449
e.args[0].find(
4550
"""Bad property path:
@@ -48,14 +53,17 @@ def test_raises_on_bad_dot_property(some_fig):
4853
)
4954
>= 0
5055
)
56+
assert raised
5157

5258

5359
def test_raises_on_bad_ancestor_dot_property(some_fig):
5460

5561
# Check . property lookup errors but not on the last part of the path
62+
raised = False
5663
try:
5764
x2000 = some_fig["layout.shapa[1].x2000"]
5865
except ValueError as e:
66+
raised = True
5967
assert (
6068
e.args[0].find(
6169
"""Bad property path:
@@ -64,16 +72,19 @@ def test_raises_on_bad_ancestor_dot_property(some_fig):
6472
)
6573
>= 0
6674
)
75+
assert raised
6776

6877

6978
def test_raises_on_bad_indexed_underscore_property(some_fig):
7079

7180
# finds bad part when using the path as a key to figure and throws the error
7281
# for the last good property it found in the path
82+
raised = False
7383
try:
7484
# get the error without using a path-like key, we compare with this error
7585
some_fig.data[0].line["colr"] = "blue"
7686
except ValueError as e_correct:
87+
raised = True
7788
# remove "Bad property path:
7889
e_correct_substr = error_substr(
7990
e_correct.args[0],
@@ -85,9 +96,13 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
8596
# if the string starts with "Bad property path:" then this test cannot work
8697
# this way.
8798
assert len(e_correct_substr) > 0
99+
assert raised
100+
101+
raised = False
88102
try:
89103
some_fig["data[0].line_colr"] = "blue"
90104
except ValueError as e:
105+
raised = True
91106
e_substr = error_substr(
92107
e.args[0],
93108
"""
@@ -106,24 +121,31 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
106121
)
107122
and (e_substr == e_correct_substr)
108123
)
124+
assert raised
109125

126+
raised = False
110127
try:
111128
# get the error without using a path-like key
112129
some_fig.add_trace(go.Scatter(x=[1, 2], y=[3, 4], line=dict(colr="blue")))
113130
except ValueError as e_correct:
131+
raised = True
114132
e_correct_substr = error_substr(
115133
e_correct.args[0],
116134
"""
117135
Bad property path:
118136
colr
119137
^""",
120138
)
139+
assert raised
140+
141+
raised = False
121142
# finds bad part when using the path as a keyword argument to a subclass of
122143
# BasePlotlyType and throws the error for the last good property found in
123144
# the path
124145
try:
125146
some_fig.add_trace(go.Scatter(x=[1, 2], y=[3, 4], line_colr="blue"))
126147
except ValueError as e:
148+
raised = True
127149
e_substr = error_substr(
128150
e.args[0],
129151
"""
@@ -142,24 +164,30 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
142164
)
143165
and (e_substr == e_correct_substr)
144166
)
167+
assert raised
145168

169+
raised = False
146170
# finds bad part when using the path as a keyword argument to a subclass of
147171
# BaseFigure and throws the error for the last good property found in
148172
# the path
149173
try:
150174
fig2 = go.Figure(layout=dict(title=dict(txt="two")))
151175
except ValueError as e_correct:
176+
raised = True
152177
e_correct_substr = error_substr(
153178
e_correct.args[0],
154179
"""
155180
Bad property path:
156181
txt
157182
^""",
158183
)
184+
assert raised
159185

186+
raised = False
160187
try:
161188
fig2 = go.Figure(layout_title_txt="two")
162189
except TypeError as e:
190+
raised = True
163191
# when the Figure constructor sees the same ValueError above, a
164192
# TypeError is raised and adds an error message in front of the same
165193
# ValueError thrown above
@@ -187,22 +215,29 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
187215
)
188216
and (e_substr == e_correct_substr)
189217
)
218+
assert raised
190219

220+
raised = False
191221
# this is like the above test for subclasses of BasePlotlyType but makes sure it
192222
# works when the bad part is not the last part in the path
193223
try:
194224
some_fig.update_layout(geo=dict(ltaxis=dict(showgrid=True)))
195225
except ValueError as e_correct:
226+
raised = True
196227
e_correct_substr = error_substr(
197228
e_correct.args[0],
198229
"""
199230
Bad property path:
200231
ltaxis
201232
^""",
202233
)
234+
assert raised
235+
236+
raised = False
203237
try:
204238
some_fig.update_layout(geo_ltaxis_showgrid=True)
205239
except ValueError as e:
240+
raised = True
206241
e_substr = error_substr(
207242
e.args[0],
208243
"""
@@ -221,3 +256,4 @@ def test_raises_on_bad_indexed_underscore_property(some_fig):
221256
)
222257
and (e_substr == e_correct_substr)
223258
)
259+
assert raised

0 commit comments

Comments
 (0)