|
| 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