|
| 1 | +from __future__ import absolute_import |
| 2 | +from unittest import TestCase |
| 3 | +from nose.tools import raises |
| 4 | + |
| 5 | +import plotly.graph_objs as go |
| 6 | + |
| 7 | + |
| 8 | +class TemplateTest(TestCase): |
| 9 | + |
| 10 | + def test_starts_as_none(self): |
| 11 | + fig = go.Figure() |
| 12 | + self.assertIsNone(fig.layout.template) |
| 13 | + |
| 14 | + def test_init_in_figure_constructor(self): |
| 15 | + fig = go.Figure(layout={ |
| 16 | + 'template': {'layout': {'title': 'Hello, world'}}}) |
| 17 | + |
| 18 | + self.assertEqual(fig.layout.template, |
| 19 | + go.layout.Template(layout={'title': 'Hello, world'})) |
| 20 | + |
| 21 | + self.assertEqual(fig.to_dict(), |
| 22 | + {'data': [], |
| 23 | + 'layout': { |
| 24 | + 'template': { |
| 25 | + 'layout': {'title': 'Hello, world'}}}}) |
| 26 | + |
| 27 | + def test_init_in_property_assignment(self): |
| 28 | + fig = go.Figure() |
| 29 | + |
| 30 | + fig.layout.template = {} |
| 31 | + fig.layout.template = go.layout.Template( |
| 32 | + layout={'title': 'Hello, world'}) |
| 33 | + |
| 34 | + self.assertEqual(fig.layout.template, |
| 35 | + go.layout.Template(layout={'title': 'Hello, world'})) |
| 36 | + |
| 37 | + self.assertEqual(fig.to_dict(), |
| 38 | + {'data': [], |
| 39 | + 'layout': { |
| 40 | + 'template': { |
| 41 | + 'layout': {'title': 'Hello, world'}}}}) |
| 42 | + |
| 43 | + def test_defaults_in_constructor(self): |
| 44 | + fig = go.Figure(layout={ |
| 45 | + 'template': { |
| 46 | + 'layout': { |
| 47 | + 'imagedefaults': {'sizex': 500}}}}) |
| 48 | + |
| 49 | + self.assertEqual(fig.layout.template.layout.imagedefaults, |
| 50 | + go.layout.Image(sizex=500)) |
| 51 | + |
| 52 | + self.assertEqual(fig.to_dict(), |
| 53 | + {'data': [], |
| 54 | + 'layout': { |
| 55 | + 'template': { |
| 56 | + 'layout': { |
| 57 | + 'imagedefaults': {'sizex': 500}}}}}) |
| 58 | + |
| 59 | + def test_defaults_in_property_assignment(self): |
| 60 | + fig = go.Figure() |
| 61 | + |
| 62 | + fig.layout.template = {} |
| 63 | + fig.layout.template.layout.sliderdefaults = \ |
| 64 | + go.layout.Slider(bgcolor='green') |
| 65 | + |
| 66 | + self.assertEqual(fig.layout.template.layout.sliderdefaults, |
| 67 | + go.layout.Slider(bgcolor='green')) |
| 68 | + |
| 69 | + self.assertEqual(fig.to_dict(), |
| 70 | + {'data': [], |
| 71 | + 'layout': { |
| 72 | + 'template': { |
| 73 | + 'layout': { |
| 74 | + 'sliderdefaults': { |
| 75 | + 'bgcolor': 'green'}}}}}) |
| 76 | + |
| 77 | + @raises(ValueError) |
| 78 | + def test_invalid_defaults_property_name_constructor(self): |
| 79 | + go.Figure(layout={ |
| 80 | + 'template': { |
| 81 | + 'layout': { |
| 82 | + 'imagedefaults': {'bogus': 500}}}}) |
| 83 | + |
| 84 | + @raises(ValueError) |
| 85 | + def test_invalid_defaults_property_value_constructor(self): |
| 86 | + go.Figure(layout={ |
| 87 | + 'template': { |
| 88 | + 'layout': { |
| 89 | + 'imagedefaults': {'sizex': 'str not number'}}}}) |
| 90 | + |
| 91 | + @raises(ValueError) |
| 92 | + def test_invalid_defaults_property_name_constructor(self): |
| 93 | + go.Figure(layout={ |
| 94 | + 'template': { |
| 95 | + 'layout': { |
| 96 | + 'xaxis': {'bogus': 500}}}}) |
| 97 | + |
| 98 | + @raises(ValueError) |
| 99 | + def test_invalid_defaults_property_value_constructor(self): |
| 100 | + go.Figure(layout={ |
| 101 | + 'template': { |
| 102 | + 'layout': { |
| 103 | + 'xaxis': {'range': 'str not tuple'}}}}) |
0 commit comments