-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
325 lines (299 loc) · 12 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from baciphacs import DataSample
from baciphacs import GenerateHTMLHorizontalBar
from baciphacs import GenerateHTMLLabelledRow
from baciphacs import GenerateHTMLHorizontalBarChart
class TestGenerateHTMLHorizontalBar(unittest.TestCase):
def test_generate_bar_with_values_is_ok(self):
htmlSnippet = GenerateHTMLHorizontalBar(.6,.1,"mycolor")
self.assertEqual(
"""\
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:50%;height:1ex;background-color:mycolor;"></td>
<td style="width:10%;height:1ex;background-color:mycolor;text-align:left">|</td>
<td style="width:10%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>""",
htmlSnippet)
def test_generate_bar_with_stdev_bigger_than_value_first_part_is_null(self):
htmlSnippet = GenerateHTMLHorizontalBar(.1,.3,"mycolor")
self.assertEqual(
"""\
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:0%;height:1ex;background-color:mycolor;"></td>
<td style="width:10%;height:1ex;background-color:mycolor;text-align:left">|</td>
<td style="width:30%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>""",
htmlSnippet)
def test_generate_bar_with_negative_relWidth_raises_exception(self):
self.assertRaises(ValueError,GenerateHTMLHorizontalBar,-.6,.3,"mycolor")
def test_generate_bar_with_negative_relErrorWidth_raises_exception(self):
self.assertRaises(ValueError,GenerateHTMLHorizontalBar,.6,-.3,"mycolor")
def test_generate_bar_with_relWidth_greater_than_one_raises_exception(self):
self.assertRaises(ValueError,GenerateHTMLHorizontalBar,6,.3,"mycolor")
def test_generate_bar_with_relErrorWidth_greater_than_one_raises_exception(self):
self.assertRaises(ValueError,GenerateHTMLHorizontalBar,.6,3,"mycolor")
def test_generate_bar_with_relErrorWidth_plus_relWidth_greater_than_one_raises_exception(self):
self.assertRaises(ValueError,GenerateHTMLHorizontalBar,.6,.5,"mycolor")
def test_generate_bar_with_relErrorWidth_0_is_ok(self):
htmlSnippet = GenerateHTMLHorizontalBar(.1,0,"mycolor")
self.assertEqual(
"""\
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:10%;height:1ex;background-color:mycolor;"></td>
<td style="width:0%;height:1ex;background-color:mycolor;text-align:left">|</td>
<td style="width:0%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>""",
htmlSnippet)
def test_generate_bar_with_relWidth_0_is_ok(self):
htmlSnippet = GenerateHTMLHorizontalBar(0,.3,"mycolor")
self.assertEqual(
"""\
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:0%;height:1ex;background-color:mycolor;"></td>
<td style="width:0%;height:1ex;background-color:mycolor;text-align:left">|</td>
<td style="width:30%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>""",
htmlSnippet)
def test_generate_bar_with_relWidth_1_is_ok(self):
htmlSnippet = GenerateHTMLHorizontalBar(1.,0,"mycolor")
self.assertEqual(
"""\
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:100%;height:1ex;background-color:mycolor;"></td>
<td style="width:0%;height:1ex;background-color:mycolor;text-align:left">|</td>
<td style="width:0%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>""",
htmlSnippet)
def test_generate_bar_with_relErrorWidth_1_is_ok(self):
htmlSnippet = GenerateHTMLHorizontalBar(0,1,"mycolor")
self.assertEqual(
"""\
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:0%;height:1ex;background-color:mycolor;"></td>
<td style="width:0%;height:1ex;background-color:mycolor;text-align:left">|</td>
<td style="width:100%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>""",
htmlSnippet)
def test_generate_bar_with_relErrorWidth_rounding_to_upper_int_increase_left_error_width(self):
htmlSnippet = GenerateHTMLHorizontalBar(.353,.1765,"mycolor")
self.assertEqual(
"""\
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:17%;height:1ex;background-color:mycolor;"></td>
<td style="width:18%;height:1ex;background-color:mycolor;text-align:left">|</td>
<td style="width:18%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>""",
htmlSnippet)
def test_generate_bar_with_relErrorWidth_rounding_to_lower_int_increase_right_error_width(self):
htmlSnippet = GenerateHTMLHorizontalBar(.226,.113,"mycolor")
self.assertEqual(
"""\
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:11%;height:1ex;background-color:mycolor;"></td>
<td style="width:12%;height:1ex;background-color:mycolor;text-align:left">|</td>
<td style="width:12%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>""",
htmlSnippet)
class TestGenerateHTMLLabelledRow(unittest.TestCase):
def test_generate_row_with_label_is_ok(self):
htmlSnippet = GenerateHTMLLabelledRow("my_label","my_title","MARKER")
self.assertEqual(
"""\
<tr title="my_title">
<th style="padding-top:.5ex;padding-right:1ex;text-align:right;">my_label</th>
<td style="padding-top:.5ex;width:100%;">
MARKER
</td>
</tr>""",
htmlSnippet)
def test_generate_row_with_label_and_multine_data_is_indented_correctly(self):
htmlSnippet = GenerateHTMLLabelledRow("my_label","my_title","MARKER\nOTHER")
self.assertEqual(
"""\
<tr title="my_title">
<th style="padding-top:.5ex;padding-right:1ex;text-align:right;">my_label</th>
<td style="padding-top:.5ex;width:100%;">
MARKER
OTHER
</td>
</tr>""",
htmlSnippet)
class TestGenerateHTMLHorizontalBarChart(unittest.TestCase):
def test_generate_bar_chart_with_negative_numStdev_raises(self):
self.assertRaises(ValueError,GenerateHTMLHorizontalBarChart,[DataSample("my_label",6,1)],-3,"mycolor")
def test_generate_bar_chart_with_single_sample(self):
htmlSnippet = GenerateHTMLHorizontalBarChart([DataSample("my_label",6,1)],3,"mycolor")
self.assertEqual("""\
<table cellspacing="0" cellpadding="0" border="0" style="width:80ex;font-family:monospace;">
<tr title="6(+/-3)">
<th style="padding-top:.5ex;padding-right:1ex;text-align:right;">my_label</th>
<td style="padding-top:.5ex;width:100%;">
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:33%;height:1ex;background-color:mycolor;"></td>
<td style="width:34%;height:1ex;background-color:mycolor;text-align:left">|</td>
<td style="width:33%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>
</td>
</tr>
</table>""",htmlSnippet)
def test_generate_bar_chart_with_more_than_one_sample(self):
htmlSnippet = GenerateHTMLHorizontalBarChart([DataSample("my_label",6,1),DataSample("other_label",5,4)],3,"mycolor")
self.assertEqual("""\
<table cellspacing="0" cellpadding="0" border="0" style="width:80ex;font-family:monospace;">
<tr title="6(+/-3)">
<th style="padding-top:.5ex;padding-right:1ex;text-align:right;">my_label</th>
<td style="padding-top:.5ex;width:100%;">
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:17%;height:1ex;background-color:mycolor;"></td>
<td style="width:18%;height:1ex;background-color:mycolor;text-align:left">|</td>
<td style="width:18%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>
</td>
</tr>
<tr title="5(+/-12)">
<th style="padding-top:.5ex;padding-right:1ex;text-align:right;">other_label</th>
<td style="padding-top:.5ex;width:100%;">
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:0%;height:1ex;background-color:mycolor;"></td>
<td style="width:29%;height:1ex;background-color:mycolor;text-align:left">|</td>
<td style="width:71%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>
</td>
</tr>
</table>""",htmlSnippet)
def test_generate_bar_chart_for_documentation(self):
"""Test that the use case shown in documentation is valid."""
data = [("ba",3,2),("c",6,1),("i",2,1),("p",4,1),("h",4,1),("a",3,1),("cs",3,2)]
dataSamples = [DataSample._make(t) for t in data]
htmlChart = GenerateHTMLHorizontalBarChart(dataSamples,numStdev=2,color="blue")
self.assertEqual("""\
<table cellspacing="0" cellpadding="0" border="0" style="width:80ex;font-family:monospace;">
<tr title="3(+/-4)">
<th style="padding-top:.5ex;padding-right:1ex;text-align:right;">ba</th>
<td style="padding-top:.5ex;width:100%;">
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:0%;height:1ex;background-color:blue;"></td>
<td style="width:38%;height:1ex;background-color:blue;text-align:left">|</td>
<td style="width:50%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>
</td>
</tr>
<tr title="6(+/-2)">
<th style="padding-top:.5ex;padding-right:1ex;text-align:right;">c</th>
<td style="padding-top:.5ex;width:100%;">
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:50%;height:1ex;background-color:blue;"></td>
<td style="width:25%;height:1ex;background-color:blue;text-align:left">|</td>
<td style="width:25%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>
</td>
</tr>
<tr title="2(+/-2)">
<th style="padding-top:.5ex;padding-right:1ex;text-align:right;">i</th>
<td style="padding-top:.5ex;width:100%;">
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:0%;height:1ex;background-color:blue;"></td>
<td style="width:25%;height:1ex;background-color:blue;text-align:left">|</td>
<td style="width:25%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>
</td>
</tr>
<tr title="4(+/-2)">
<th style="padding-top:.5ex;padding-right:1ex;text-align:right;">p</th>
<td style="padding-top:.5ex;width:100%;">
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:25%;height:1ex;background-color:blue;"></td>
<td style="width:25%;height:1ex;background-color:blue;text-align:left">|</td>
<td style="width:25%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>
</td>
</tr>
<tr title="4(+/-2)">
<th style="padding-top:.5ex;padding-right:1ex;text-align:right;">h</th>
<td style="padding-top:.5ex;width:100%;">
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:25%;height:1ex;background-color:blue;"></td>
<td style="width:25%;height:1ex;background-color:blue;text-align:left">|</td>
<td style="width:25%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>
</td>
</tr>
<tr title="3(+/-2)">
<th style="padding-top:.5ex;padding-right:1ex;text-align:right;">a</th>
<td style="padding-top:.5ex;width:100%;">
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:12%;height:1ex;background-color:blue;"></td>
<td style="width:26%;height:1ex;background-color:blue;text-align:left">|</td>
<td style="width:25%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>
</td>
</tr>
<tr title="3(+/-4)">
<th style="padding-top:.5ex;padding-right:1ex;text-align:right;">cs</th>
<td style="padding-top:.5ex;width:100%;">
<table cellspacing="0" cellpadding="0" border="0" style="width:100%">
<tr>
<td style="width:0%;height:1ex;background-color:blue;"></td>
<td style="width:38%;height:1ex;background-color:blue;text-align:left">|</td>
<td style="width:50%;height:1ex;text-align:right">|</td>
<td></td>
</tr>
</table>
</td>
</tr>
</table>""",htmlChart)
if __name__ == '__main__':
unittest.main()