1
- import math
2
1
from unittest import TestCase
3
2
4
3
from nose .tools import raises
5
4
import plotly .tools as tls
6
5
from plotly .exceptions import PlotlyError
7
6
import plotly .colors as colors
8
7
9
- """
8
+
10
9
class TestColors (TestCase ):
11
10
12
11
def test_validate_colors (self ):
13
12
14
13
# test string input
15
- color = 'foo'
14
+ color_string = 'foo'
16
15
17
16
pattern = ("If your colors variable is a string, it must be a "
18
17
"Plotly scale, an rgb color or a hex color." )
19
18
20
19
self .assertRaisesRegexp (PlotlyError , pattern , colors .validate_colors ,
21
- color )
20
+ color_string )
22
21
23
22
# test rgb color
24
- color2 = 'rgb(265,0, 0)'
23
+ color_string2 = 'rgb(265, 0, 0)'
25
24
26
25
pattern2 = ("Whoops! The elements in your rgb colors tuples cannot "
27
26
"exceed 255.0." )
28
27
29
28
self .assertRaisesRegexp (PlotlyError , pattern2 , colors .validate_colors ,
30
- color2 )
29
+ color_string2 )
31
30
32
31
# test tuple color
33
-
34
- color3 = (1.1, 1, 1)
32
+ color_tuple = (1 , 1 , 2 )
35
33
36
34
pattern3 = ("Whoops! The elements in your colors tuples cannot "
37
35
"exceed 1.0." )
38
36
39
37
self .assertRaisesRegexp (PlotlyError , pattern3 , colors .validate_colors ,
40
- color3)
41
- """
38
+ color_tuple )
39
+
40
+ def test_convert_colors_to_same_type (self ):
41
+
42
+ # test string input
43
+ color_string = 'foo'
44
+
45
+ pattern = ("If your colors variable is a string, it must be a "
46
+ "Plotly scale, an rgb color or a hex color." )
47
+
48
+ self .assertRaisesRegexp (PlotlyError , pattern ,
49
+ colors .convert_colors_to_same_type ,
50
+ color_string )
51
+
52
+ # test colortype
53
+ color_tuple = (1 , 1 , 1 )
54
+ colortype = 2
55
+
56
+ pattern2 = ("You must select either rgb or tuple for your colortype "
57
+ "variable." )
58
+
59
+ self .assertRaisesRegexp (PlotlyError , pattern2 ,
60
+ colors .convert_colors_to_same_type ,
61
+ color_tuple , colortype )
62
+
63
+ def test_convert_dict_colors_to_same_type (self ):
64
+
65
+ # test colortype
66
+ color_dict = dict (apple = 'rgb(1, 1, 1)' )
67
+ colortype = 2
68
+
69
+ pattern = ("You must select either rgb or tuple for your colortype "
70
+ "variable." )
71
+
72
+ self .assertRaisesRegexp (PlotlyError , pattern ,
73
+ colors .convert_dict_colors_to_same_type ,
74
+ color_dict , colortype )
75
+
76
+ def test_make_colorscale (self ):
77
+
78
+ # test minimum colors length
79
+ color_list = [(0 , 0 , 0 )]
80
+
81
+ pattern = (
82
+ "You must input a list of colors that has at least two colors."
83
+ )
84
+
85
+ self .assertRaisesRegexp (PlotlyError , pattern , colors .make_colorscale ,
86
+ color_list )
87
+
88
+ # test length of colors and scale
89
+ color_list2 = [(0 , 0 , 0 ), (1 , 1 , 1 )]
90
+ scale = [0 ]
91
+
92
+ pattern2 = ("The length of colors and scale must be the same." )
93
+
94
+ self .assertRaisesRegexp (PlotlyError , pattern2 , colors .make_colorscale ,
95
+ color_list2 , scale )
96
+
97
+ # test first and last number of scale
98
+ scale2 = [0 , 2 ]
99
+
100
+ pattern3 = ("The first and last number in scale must be 0.0 and 1.0 "
101
+ "respectively." )
102
+
103
+ self .assertRaisesRegexp (PlotlyError , pattern3 , colors .make_colorscale ,
104
+ color_list2 , scale2 )
105
+
106
+ # test for strictly increasing scale
107
+ color_list3 = [(0 , 0 , 0 ), (0.5 , 0.5 , 0.5 ), (1 , 1 , 1 )]
108
+ scale3 = [0 , 1 , 1 ]
109
+
110
+ pattern4 = ("'scale' must be a list that contains an increasing "
111
+ "sequence of numbers where the first and last number are"
112
+ "0.0 and 1.0 respectively." )
113
+
114
+ self .assertRaisesRegexp (PlotlyError , pattern4 , colors .make_colorscale ,
115
+ color_list3 , scale3 )
0 commit comments