Skip to content

Commit 64a7142

Browse files
committed
Added validate false test
1 parent cd1ef15 commit 64a7142

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

Diff for: packages/python/plotly/plotly/basedatatypes.py

-2
Original file line numberDiff line numberDiff line change
@@ -1944,8 +1944,6 @@ def _initialize_layout_template(self):
19441944
# Assume default template is already validated
19451945
template_dict = pio.templates[pio.templates.default]
19461946
self._layout_obj.template = template_dict
1947-
else:
1948-
self._layout_obj.template = None
19491947

19501948
@property
19511949
def layout(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import plotly.graph_objs as go
2+
import json
3+
import pytest
4+
import plotly.io as pio
5+
6+
7+
def build_invalid_fig():
8+
return go.Figure(
9+
data=[{'type': 'bar', 'y': 'not_a_list', 'bogus': 23}],
10+
layout_title_text='valid title',
11+
layout_colorway='not a dict',
12+
)
13+
14+
15+
expected_invalid_dict = dict(
16+
data=[{'type': 'bar', 'y': 'not_a_list', 'bogus': 23}],
17+
layout={'title': {'text': 'valid title'}, 'colorway': 'not a dict'}
18+
)
19+
20+
21+
def test_validate_false():
22+
template = pio.templates.default
23+
should_validate = go.validate._should_validate
24+
try:
25+
pio.templates.default = None
26+
27+
# Build figure with variety of invalid properties (both name and value),
28+
# make sure underscore notation is still applied properly
29+
with go.validate(False):
30+
fig = build_invalid_fig()
31+
assert json.loads(fig.to_json()) == expected_invalid_dict
32+
33+
with go.validate(True):
34+
with pytest.raises(ValueError):
35+
build_invalid_fig()
36+
37+
# Use validate as callable
38+
go.validate(False)
39+
fig = build_invalid_fig()
40+
assert json.loads(fig.to_json()) == expected_invalid_dict
41+
42+
go.validate(True)
43+
with pytest.raises(ValueError):
44+
build_invalid_fig()
45+
46+
finally:
47+
pio.templates.default = template
48+
go.validate(should_validate)

0 commit comments

Comments
 (0)