-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bubble_plot.py
More file actions
324 lines (286 loc) · 10.6 KB
/
test_bubble_plot.py
File metadata and controls
324 lines (286 loc) · 10.6 KB
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
from secrets import token_urlsafe
from unittest import TestCase
from unittest.mock import mock_open, patch
from bubble_plot import (
Bubble,
BubblePlot,
Config,
CSVWriter,
Facets,
LatexBubblePlotWriter,
Occurrence,
SplitXAxis,
build_and_save_plots,
compute_occurences_from,
compute_color_map,
)
class TestOccurence(TestCase):
def setUp(self):
self.year = "2020"
self.occu = Occurrence(self.year)
def test_init(self):
self.assertEqual(self.year, self.occu.year)
self.assertEqual(1, self.occu.occurrence)
def test_update(self):
self.occu.update("2019")
self.assertEqual("2019", self.occu.year)
self.assertEqual(2, self.occu.occurrence)
self.occu.update("2021")
self.assertNotEqual("2021", self.occu.year)
self.assertEqual(3, self.occu.occurrence)
class TestSplitXAxis(TestCase):
def setUp(self):
self.x_axis = SplitXAxis("X", {})
def test_update(self):
entry = {"X": "pouet", "Y": "hoho"}
bubble = Bubble(label_x=entry["X"], label_y=entry["Y"])
self.x_axis.update(entry, "2020", "Y")
self.assertTrue(bubble in self.x_axis.bubbles)
def test_update_unkown_facet(self):
entry = {"Z": "pouet", "Y": "hoho"}
regex = "Unknown facet named: 'X' on the x axis"
self.assertRaisesRegex(
KeyError, regex, self.x_axis.update, entry, "2020", "Y"
)
entry = {"X": "pouet", "Z": "hoho"}
regex = "Unknown facet named: 'Y' on the y axis"
self.assertRaisesRegex(
KeyError, regex, self.x_axis.update, entry, "2020", "Y"
)
class TestBubblePlot(TestCase):
def setUp(self):
self.bubble_plot = BubblePlot(Facets("Y", "X_left", "X_right"))
def test_init(self):
self.assertEqual("Y", self.bubble_plot.y_axis)
self.assertEqual("X_left", self.bubble_plot.x_axis.left.facet)
self.assertEqual("X_right", self.bubble_plot.x_axis.right.facet)
def test_update(self):
entry = {"X_left": "pouet", "X_right": "teuop", "Y": "hoho"}
bubble_left = Bubble(label_x=entry["X_left"], label_y=entry["Y"])
bubble_right = Bubble(label_x=entry["X_right"], label_y=entry["Y"])
self.bubble_plot.update(entry, "2020")
self.assertTrue(bubble_left in self.bubble_plot.x_axis.left.bubbles)
self.assertTrue(bubble_right in self.bubble_plot.x_axis.right.bubbles)
class TestCSVWriter(TestCase):
def setUp(self):
self.output_dir = "output_dir"
self.conf = Config(
1,
2,
"year",
["iy", "ix", "nbr", "year", "y", "x"],
"template.tex",
self.output_dir,
[],
)
self.years = {"2018", "2019", "2020"}
self.entries = [
{"Y": "0", "X_left": "1", "X_right": "2", "year": "2018"},
{"Y": "3", "X_left": "4", "X_right": "4", "year": "2020"},
{"Y": "6", "X_left": "7", "X_right": "7", "year": "2019"},
]
self.year_mapping = {"2018": 0, "2019": 500, "2020": 1000}
self.x_left_mapping = {"1": -4, "4": -3, "7": -2}
self.x_right_mapping = {"2": 2, "4": 3, "7": 4}
self.y_mapping = {"0": 0, "3": 1, "6": 2}
self.data = [
(0, -4, 1, 0),
(1, -3, 1, 1000),
(2, -2, 1, 500),
(0, 2, 1, 0),
(1, 3, 1, 1000),
(2, 4, 1, 500),
]
self.facets = Facets("Y", "X_left", "X_right")
self.bubble_plot = BubblePlot(self.facets)
for entry in self.entries:
self.bubble_plot.update(entry, entry[self.conf.class_year])
self.writer = CSVWriter(self.bubble_plot, self.years, self.conf)
def test_compute_year_score_mapping(self):
mapping = self.writer.compute_year_score_mapping()
self.assertEqual(self.year_mapping, mapping)
def test_compute_labels_indices_mapping(self):
(
x_left_mapping,
x_right_mapping,
y_mapping,
) = self.writer.compute_labels_indices_mapping()
self.assertEqual(self.y_mapping, y_mapping)
self.assertEqual(self.x_left_mapping, x_left_mapping)
self.assertEqual(self.x_right_mapping, x_right_mapping)
def test_prepared_bubbles_data(self):
prepared_data = self.writer.prepared_bubbles_data(
self.x_left_mapping,
self.x_right_mapping,
self.y_mapping,
self.year_mapping,
)
self.assertEqual(self.data, prepared_data)
def test_write(self):
mock = mock_open()
with patch("bubble_plot.open", mock):
self.writer.write(
self.data,
list(self.x_left_mapping.keys())
+ list(self.x_right_mapping.keys()),
list(self.y_mapping.keys()),
)
mock.assert_called_once_with(
f"{self.output_dir}/{self.bubble_plot}.csv", "w"
)
exps = [
("iy,ix,nbr,year,y,x\r\n"),
("0,-4,1,0,1,0\r\n"),
("1,-3,1,1000,4,3\r\n"),
("2,-2,1,500,7,6\r\n"),
("0,2,1,0,2,\r\n"),
("1,3,1,1000,4,\r\n"),
("2,4,1,500,7,\r\n"),
]
handle = mock()
for expected, (args, _) in zip(exps, handle.write.call_args_list):
self.assertEqual(expected, args[0])
class TestLatexBubblePlotWriter(TestCase):
def setUp(self):
self.output_dir = "output_dir"
self.conf = Config(
1,
2,
"year",
["iy", "ix", "nbr", "year", "y", "x"],
"template.tex",
self.output_dir,
[],
)
self.years = {"2018", "2019", "2020"}
self.entries = [
{"Y": "0", "X_left": "1", "X_right": "2", "year": "2018"},
{"Y": "3", "X_left": "4", "X_right": "5", "year": "2020"},
{"Y": "6", "X_left": "7", "X_right": "8", "year": "2019"},
]
self.year_mapping = {"2018": 0, "2019": 500, "2020": 1000}
self.x_mapping = {"1": -4, "4": -3, "7": -2, "2": 2, "5": 3, "8": 4}
self.y_mapping = {"0": 0, "3": 1, "6": 2}
self.data = (
(0, -4, 1, 0),
(1, -3, 1, 1000),
(2, -2, 1, 500),
(0, 2, 1, 0),
(1, 3, 1, 1000),
(2, 4, 1, 500),
)
self.facets = Facets("Y", "X_left", "X_right")
self.bubble_plot = BubblePlot(self.facets)
for entry in self.entries:
self.bubble_plot.update(entry, entry[self.conf.class_year])
mock = mock_open(
read_data="ix\n" + "\n".join(map(str, self.x_mapping.values()))
)
with patch("bubble_plot.open", mock):
self.writer = LatexBubblePlotWriter(
self.bubble_plot, self.years, self.conf
)
self.writer.x_indices = tuple(x for (_, x, _, _) in self.data)
def test_init(self):
mock = mock_open(
read_data="ix\n" + "\n".join(map(str, self.x_mapping.values()))
)
with patch("bubble_plot.open", mock):
LatexBubblePlotWriter(self.bubble_plot, self.years, self.conf)
self.assertEqual(
(f"{self.output_dir}/{self.bubble_plot}.csv", "r"),
mock.call_args_list[0][0],
)
def test_year_color(self):
years_len = len(self.years)
year_color = compute_color_map(len(list(self.years)))
self.assertEqual(years_len, len(set(year_color)))
def test_prepare_values(self):
expect = (
"defineColorsYear",
"setColorsYear",
"xMin",
"xMax",
"yLabel",
"meta",
"xField",
"xIndexField",
"yField",
"yIndexField",
"yearField",
"xLeftLabel",
"xRightLabel",
"CSVDataFile",
"colorsYear",
)
year_color = {
"2017": (0, 0, 1),
"2018": (0, 1, 0),
"2019": (1, 0, 0),
"2020": (1, 1, 1),
}
self.assertEqual(
expect, tuple(self.writer.prepare_values(year_color).keys())
)
def test_write(self):
template_values = {
"defineColorsYear": [token_urlsafe(5) for _ in self.years],
"setColorsYear": [token_urlsafe(5) for _ in self.years],
"xMin": token_urlsafe(5),
"xMax": token_urlsafe(5),
"yLabel": token_urlsafe(5),
"meta": token_urlsafe(5),
"xField": token_urlsafe(5),
"xIndexField": token_urlsafe(5),
"yField": token_urlsafe(5),
"yIndexField": token_urlsafe(5),
"yearField": token_urlsafe(5),
"xLeftLabel": token_urlsafe(5),
"xRightLabel": token_urlsafe(5),
"CSVDataFile": token_urlsafe(5),
"colorsYear": [token_urlsafe(5) for _ in self.years],
}
mock = mock_open()
with patch("bubble_plot.open", mock):
self.writer.write(template_values)
self.assertEqual(
(f"{self.conf.latex_template}", "r"), mock.call_args_list[0][0]
)
self.assertEqual(
(f"{self.conf.output_dir}/{self.writer.plot}.tex", "w"),
mock.call_args_list[1][0],
)
class TestAPI(TestCase):
def setUp(self):
self.entries = [
{"Y": "0", "X_left": "1", "X_right": "2", "year": "2018"},
{"Y": "3", "X_left": "4", "X_right": "5", "year": "2020"},
{"Y": "6", "X_left": "7", "X_right": "8", "year": "2019"},
]
self.output_dir = "output_dir"
self.conf = Config(
1,
2,
"year",
["iy", "ix", "nbr", "year", "y", "x"],
"template.tex",
self.output_dir,
[],
)
self.plot_plan = Facets("Y", "X_left", "X_right")
def test_compute_occurences_from(self):
plot, years = compute_occurences_from(
self.entries, self.plot_plan, self.conf
)
entries_len = len(self.entries)
self.assertEqual(entries_len, len(plot.x_axis.left.bubbles))
self.assertEqual(entries_len, len(plot.x_axis.right.bubbles))
self.assertEqual({"2018", "2019", "2020"}, years)
def test_build_and_save_plots(self):
number_plot = 3
mock = mock_open(read_data="ix\n-1\n0\n1")
with patch("bubble_plot.open", mock):
build_and_save_plots(
self.entries, [self.plot_plan] * number_plot, self.conf
)
self.assertEqual(number_plot * 4, len(mock.call_args_list))