44
55from plotly import graph_reference as gr
66from plotly .graph_objs import graph_objs_tools as got
7+ from plotly import graph_objs as go
78
89
910class TestGetHelp (TestCase ):
@@ -30,3 +31,183 @@ def test_get_help_does_not_raise(self):
3031 got .get_help (object_name , attribute = fake_attribute )
3132 except :
3233 self .fail (msg = msg )
34+
35+
36+ class TestKeyParts (TestCase ):
37+ def test_without_underscore_attr (self ):
38+ assert got ._key_parts ("foo" ) == ["foo" ]
39+ assert got ._key_parts ("foo_bar" ) == ["foo" , "bar" ]
40+ assert got ._key_parts ("foo_bar_baz" ) == ["foo" , "bar" , "baz" ]
41+
42+ def test_traililng_underscore_attr (self ):
43+ assert got ._key_parts ("foo_error_x" ) == ["foo" , "error_x" ]
44+ assert got ._key_parts ("foo_bar_error_x" ) == ["foo" , "bar" , "error_x" ]
45+ assert got ._key_parts ("foo_bar_baz_error_x" ) == ["foo" , "bar" , "baz" , "error_x" ]
46+
47+ def test_leading_underscore_attr (self ):
48+ assert got ._key_parts ("error_x_foo" ) == ["error_x" , "foo" ]
49+ assert got ._key_parts ("error_x_foo_bar" ) == ["error_x" , "foo" , "bar" ]
50+ assert got ._key_parts ("error_x_foo_bar_baz" ) == ["error_x" , "foo" , "bar" , "baz" ]
51+
52+
53+ class TestUnderscoreMagicDictObj (TestCase ):
54+
55+ def test_can_split_string_key_into_parts (self ):
56+ obj1 = {}
57+ obj2 = {}
58+ got ._underscore_magic ("marker_line_width" , 42 , obj1 )
59+ got ._underscore_magic (["marker" , "line" , "width" ], 42 , obj2 )
60+ want = {"marker" : {"line" : {"width" : 42 }}}
61+ assert obj1 == obj2 == want
62+
63+ def test_will_make_tree_with_empty_dict_val (self ):
64+ obj = {}
65+ got ._underscore_magic ("marker_colorbar_tickfont" , {}, obj )
66+ assert obj == {"marker" : {"colorbar" : {"tickfont" : {}}}}
67+
68+ def test_can_set_at_depths_1to4 (self ):
69+ # 1 level
70+ obj = {}
71+ got ._underscore_magic ("opacity" , 0.9 , obj )
72+ assert obj == {"opacity" : 0.9 }
73+
74+ # 2 levels
75+ got ._underscore_magic ("line_width" , 10 , obj )
76+ assert obj == {"opacity" : 0.9 , "line" : {"width" : 10 }}
77+
78+ # 3 levels
79+ got ._underscore_magic ("hoverinfo_font_family" , "Times" , obj )
80+ want = {
81+ "opacity" : 0.9 ,
82+ "line" : {"width" : 10 },
83+ "hoverinfo" : {"font" : {"family" : "Times" }}
84+ }
85+ assert obj == want
86+
87+ # 4 levels
88+ got ._underscore_magic ("marker_colorbar_tickfont_family" , "Times" , obj )
89+ want = {
90+ "opacity" : 0.9 ,
91+ "line" : {"width" : 10 },
92+ "hoverinfo" : {"font" : {"family" : "Times" }},
93+ "marker" : {"colorbar" : {"tickfont" : {"family" : "Times" }}},
94+ }
95+ assert obj == want
96+
97+ def test_does_not_displace_existing_fields (self ):
98+ obj = {}
99+ got ._underscore_magic ("marker_size" , 10 , obj )
100+ got ._underscore_magic ("marker_line_width" , 0.4 , obj )
101+ assert obj == {"marker" : {"size" : 10 , "line" : {"width" : 0.4 }}}
102+
103+ def test_doesnt_mess_up_underscore_attrs (self ):
104+ obj = {}
105+ got ._underscore_magic ("error_x_color" , "red" , obj )
106+ got ._underscore_magic ("error_x_width" , 4 , obj )
107+ assert obj == {"error_x" : {"color" : "red" , "width" : 4 }}
108+
109+
110+ class TestUnderscoreMagicPlotlyDictObj (TestCase ):
111+
112+ def test_can_split_string_key_into_parts (self ):
113+ obj1 = go .Scatter ()
114+ obj2 = go .Scatter ()
115+ got ._underscore_magic ("marker_line_width" , 42 , obj1 )
116+ got ._underscore_magic (["marker" , "line" , "width" ], 42 , obj2 )
117+ want = go .Scatter ({"marker" : {"line" : {"width" : 42 }}})
118+ assert obj1 == obj2 == want
119+
120+ def test_will_make_tree_with_empty_dict_val (self ):
121+ obj = go .Scatter ()
122+ got ._underscore_magic ("marker_colorbar_tickfont" , {}, obj )
123+ want = go .Scatter ({"marker" : {"colorbar" : {"tickfont" : {}}}})
124+ assert obj == want
125+
126+ def test_can_set_at_depths_1to4 (self ):
127+ # 1 level
128+ obj = go .Scatter ()
129+ got ._underscore_magic ("opacity" , 0.9 , obj )
130+ assert obj == go .Scatter ({"type" : "scatter" , "opacity" : 0.9 })
131+
132+ # 2 levels
133+ got ._underscore_magic ("line_width" , 10 , obj )
134+ assert obj == go .Scatter ({"opacity" : 0.9 , "line" : {"width" : 10 }})
135+
136+ # 3 levels
137+ got ._underscore_magic ("hoverinfo_font_family" , "Times" , obj )
138+ want = go .Scatter ({
139+ "opacity" : 0.9 ,
140+ "line" : {"width" : 10 },
141+ "hoverinfo" : {"font" : {"family" : "Times" }}
142+ })
143+ assert obj == want
144+
145+ # 4 levels
146+ got ._underscore_magic ("marker_colorbar_tickfont_family" , "Times" , obj )
147+ want = go .Scatter ({
148+ "opacity" : 0.9 ,
149+ "line" : {"width" : 10 },
150+ "hoverinfo" : {"font" : {"family" : "Times" }},
151+ "marker" : {"colorbar" : {"tickfont" : {"family" : "Times" }}},
152+ })
153+ assert obj == want
154+
155+ def test_does_not_displace_existing_fields (self ):
156+ obj = go .Scatter ()
157+ got ._underscore_magic ("marker_size" , 10 , obj )
158+ got ._underscore_magic ("marker_line_width" , 0.4 , obj )
159+ assert obj == go .Scatter ({"marker" : {"size" : 10 , "line" : {"width" : 0.4 }}})
160+
161+ def test_doesnt_mess_up_underscore_attrs (self ):
162+ obj = go .Scatter ()
163+ got ._underscore_magic ("error_x_color" , "red" , obj )
164+ got ._underscore_magic ("error_x_width" , 4 , obj )
165+ assert obj == go .Scatter ({"error_x" : {"color" : "red" , "width" : 4 }})
166+
167+
168+ class TestAttr (TestCase ):
169+ def test_with_no_positional_argument (self ):
170+ have = got .attr (
171+ opacity = 0.9 , line_width = 10 ,
172+ hoverinfo_font_family = "Times" ,
173+ marker_colorbar_tickfont_size = 10
174+ )
175+ want = {
176+ "opacity" : 0.9 ,
177+ "line" : {"width" : 10 },
178+ "hoverinfo" : {"font" : {"family" : "Times" }},
179+ "marker" : {"colorbar" : {"tickfont" : {"size" : 10 }}},
180+ }
181+ assert have == want
182+
183+ def test_with_dict_positional_argument (self ):
184+ have = {"x" : [1 , 2 , 3 , 4 , 5 ]}
185+ got .attr (have ,
186+ opacity = 0.9 , line_width = 10 ,
187+ hoverinfo_font_family = "Times" ,
188+ marker_colorbar_tickfont_size = 10
189+ )
190+ want = {
191+ "x" : [1 , 2 , 3 , 4 , 5 ],
192+ "opacity" : 0.9 ,
193+ "line" : {"width" : 10 },
194+ "hoverinfo" : {"font" : {"family" : "Times" }},
195+ "marker" : {"colorbar" : {"tickfont" : {"size" : 10 }}},
196+ }
197+ assert have == want
198+
199+ def test_with_PlotlyDict_positional_argument (self ):
200+ have = go .Scatter ({"x" : [1 , 2 , 3 , 4 , 5 ]})
201+ got .attr (have ,
202+ opacity = 0.9 , line_width = 10 ,
203+ hoverinfo_font_family = "Times" ,
204+ marker_colorbar_tickfont_size = 10
205+ )
206+ want = go .Scatter ({
207+ "x" : [1 , 2 , 3 , 4 , 5 ],
208+ "opacity" : 0.9 ,
209+ "line" : {"width" : 10 },
210+ "hoverinfo" : {"font" : {"family" : "Times" }},
211+ "marker" : {"colorbar" : {"tickfont" : {"size" : 10 }}},
212+ })
213+ assert have == want
0 commit comments