-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimerange_spec.py
342 lines (283 loc) · 15.6 KB
/
timerange_spec.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
from mamba import description, context, it
from expects import *
from ooui.graph.timerange import (
get_unique_values_grouped_by, get_format_for_units, check_dates_consecutive,
get_date_format, convert_date_to_time_range_adjusted,
adjust_x_values_for_time_range, combine_values_for_timerange,
get_missing_consecutive_dates, fill_gaps_in_timerange_data,
process_timerange_data
)
with description('Testing get_unique_values_grouped_by') as self:
with context('when grouping values by "all"'):
with it('should return unique values grouped by x, type, and stacked'):
values_data = [
{'x': 'Jan', 'type': 'Revenue', 'stacked': 'A'},
{'x': 'Jan', 'type': 'Revenue', 'stacked': 'A'},
{'x': 'Feb', 'type': 'Revenue', 'stacked': 'B'},
{'x': 'Mar', 'type': 'Profit', 'stacked': 'A'}
]
grouped_values = get_unique_values_grouped_by(values_data, 'all')
expect(grouped_values).to(equal({
'Jan-Revenue-A': [
{'x': 'Jan', 'type': 'Revenue', 'stacked': 'A'},
{'x': 'Jan', 'type': 'Revenue', 'stacked': 'A'}
],
'Feb-Revenue-B': [
{'x': 'Feb', 'type': 'Revenue', 'stacked': 'B'}
],
'Mar-Profit-A': [
{'x': 'Mar', 'type': 'Profit', 'stacked': 'A'}
]
}))
with context('when grouping values by "type-stacked"'):
with it('should return unique values grouped by type and stacked only'):
values_data = [
{'x': 'Jan', 'type': 'Revenue', 'stacked': 'A'},
{'x': 'Jan', 'type': 'Revenue', 'stacked': 'A'},
{'x': 'Feb', 'type': 'Revenue', 'stacked': 'B'},
{'x': 'Mar', 'type': 'Profit', 'stacked': 'A'}
]
grouped_values = get_unique_values_grouped_by(values_data, 'type-stacked')
expect(grouped_values).to(equal({
'Revenue-A': [{'x': 'Jan', 'type': 'Revenue', 'stacked': 'A'}, {'x': 'Jan', 'type': 'Revenue', 'stacked': 'A'}],
'Revenue-B': [{'x': 'Feb', 'type': 'Revenue', 'stacked': 'B'}],
'Profit-A': [{'x': 'Mar', 'type': 'Profit', 'stacked': 'A'}]
}))
with description('Testing get_format_for_units') as self:
with context('when units is "days"'):
with it('should return the date format "YYYY-MM-DD"'):
result = get_format_for_units('days')
expect(result).to(equal('%Y-%m-%d'))
with context('when units is "weeks"'):
with it('should return the date format "YYYY-WW"'):
result = get_format_for_units('weeks')
expect(result).to(equal('%Y-%W'))
with context('when units is "months"'):
with it('should return the date format "YYYY-MM"'):
result = get_format_for_units('months')
expect(result).to(equal('%Y-%m'))
with context('when units is "years"'):
with it('should return the date format "YYYY"'):
result = get_format_for_units('years')
expect(result).to(equal('%Y'))
with context('when units is "hours" or default'):
with it('should return the default date format "YYYY-MM-DD HH:mm"'):
result = get_format_for_units('hours')
expect(result).to(equal('%Y-%m-%d %H:%M'))
with it('should return the default format for an unknown unit'):
result = get_format_for_units('minutes')
expect(result).to(equal('%Y-%m-%d %H:%M'))
with description('Testing check_dates_consecutive') as self:
with context('when the dates list is empty'):
with it('should return False'):
result = check_dates_consecutive([], 'days')
expect(result).to(equal(False))
with context('when the dates list has a single date'):
with it('should return True'):
result = check_dates_consecutive(['2024-05-01'], 'days')
expect(result).to(equal(True))
with context('when the dates are consecutive by days'):
with it('should return True'):
result = check_dates_consecutive(['2024-05-01', '2024-05-02', '2024-05-03'], 'days')
expect(result).to(equal(True))
with context('when the dates are not consecutive by days'):
with it('should return False'):
result = check_dates_consecutive(['2024-05-01', '2024-05-03'], 'days')
expect(result).to(equal(False))
with context('when the dates are consecutive by months'):
with it('should return True'):
result = check_dates_consecutive(['2024-01', '2024-02', '2024-03'], 'months')
expect(result).to(equal(True))
with context('when the dates are not consecutive by months'):
with it('should return False'):
result = check_dates_consecutive(['2024-01', '2024-03'], 'months')
expect(result).to(equal(False))
with description('Testing get_date_format') as self:
with context('when the date string contains a colon'):
with it('should return the format "YYYY-MM-DD HH:mm:ss"'):
result = get_date_format('2024-05-01 12:34:56')
expect(result).to(equal('%Y-%m-%d %H:%M:%S'))
with context('when the date string does not contain a colon'):
with it('should return the format "YYYY-MM-DD"'):
result = get_date_format('2024-05-01')
expect(result).to(equal('%Y-%m-%d'))
with description('Testing convert_date_to_time_range_adjusted') as self:
with context('when the timerange is "hour"'):
with it('should return the adjusted date with hour precision'):
result = convert_date_to_time_range_adjusted('2024-05-01 14:35:00', 'hour')
expect(result).to(equal('2024-05-01 14:00'))
with context('when the timerange is "day"'):
with it('should return the adjusted date with day precision'):
result = convert_date_to_time_range_adjusted('2024-05-01 14:35:00', 'day')
expect(result).to(equal('2024-05-01'))
with context('when the timerange is "week"'):
with it('should return the adjusted date with week precision'):
result = convert_date_to_time_range_adjusted('2024-05-01', 'week')
expect(result).to(equal('2024-18'))
with context('when the timerange is "month"'):
with it('should return the adjusted date with month precision'):
result = convert_date_to_time_range_adjusted('2024-05-01', 'month')
expect(result).to(equal('2024-05'))
with context('when the timerange is "year"'):
with it('should return the adjusted date with year precision'):
result = convert_date_to_time_range_adjusted('2024-05-01', 'year')
expect(result).to(equal('2024'))
with context('when an unsupported timerange is provided'):
with it('should raise a ValueError'):
expect(lambda: convert_date_to_time_range_adjusted('2024-05-01', 'decade')).to(
raise_error(ValueError, 'Unsupported timerange: decade')
)
with description('Testing adjust_x_values_for_time_range') as self:
with context('when adjusting "x" values to the "hour" timerange'):
with it('should correctly adjust the "x" values to hour precision'):
values_data = [
{'id': 1, 'x': '2024-05-01 12:45:00', 'y': 100},
{'id': 2, 'x': '2024-05-02 08:30:00', 'y': 200}
]
result = adjust_x_values_for_time_range(
values_data, 'hour',
)
expect(result).to(equal([
{'id': 1, 'x': '2024-05-01 12:00', 'y': 100},
{'id': 2, 'x': '2024-05-02 08:00', 'y': 200}
]))
with context('when adjusting "x" values to the "day" timerange'):
with it('should correctly adjust the "x" values to day precision'):
values_data = [
{'id': 1, 'x': '2024-05-01 12:45:00', 'y': 100},
{'id': 2, 'x': '2024-05-02 08:30:00', 'y': 200}
]
result = adjust_x_values_for_time_range(values_data, 'day')
expect(result).to(equal([
{'id': 1, 'x': '2024-05-01', 'y': 100},
{'id': 2, 'x': '2024-05-02', 'y': 200}
]))
with description('Testing combine_values_for_timerange') as self:
with context('when combining values for a specific timerange'):
with it('should return the final combined values'):
values_data = [
{'x': '2024-01-01', 'type': 'Revenue', 'stacked': 'A', 'value': 100,
'operator': '+'},
{'x': '2024-01-02', 'type': 'Revenue', 'stacked': 'A', 'value': 150,
'operator': '+'},
{'x': '2024-02-01', 'type': 'Revenue', 'stacked': 'A', 'value': 200,
'operator': '+'},
{'x': '2024-02-02', 'type': 'Revenue', 'stacked': 'A', 'value': 250,
'operator': '+'},
{'x': '2024-03-01', 'type': 'Profit', 'stacked': 'A', 'value': 300,
'operator': '-'}
]
result = combine_values_for_timerange(values_data, 'month')
expect(result).to(contain_only(
{'x': '2024-01', 'type': 'Revenue', 'stacked': 'A', 'value': 250,
'operator': '+'},
{'x': '2024-02', 'type': 'Revenue', 'stacked': 'A', 'value': 450,
'operator': '+'},
{'x': '2024-03', 'type': 'Profit', 'stacked': 'A', 'value': 300,
'operator': '-'})
)
with description('Testing get_missing_consecutive_dates') as self:
with context('when no dates are missing'):
with it('should return an empty list'):
dates_data = ['2024-05-01', '2024-05-02', '2024-05-03']
result = get_missing_consecutive_dates(dates_data, 'day')
expect(result).to(equal([]))
with context('when some dates are missing'):
with it('should return a list of missing dates'):
dates_data = ['2024-05-01', '2024-05-03', '2024-05-06']
result = get_missing_consecutive_dates(dates_data, 'day')
expect(result).to(equal(['2024-05-02', '2024-05-04', '2024-05-05']))
with context('when checking for missing months'):
with it('should return a list of missing months'):
dates_data = ['2024-01', '2024-03', '2024-05']
result = get_missing_consecutive_dates(dates_data, 'month')
expect(result).to(equal(['2024-02', '2024-04']))
with description('Testing fill_gaps_in_timerange_data') as self:
with context('when filling gaps in time range data'):
with it('should return the final values with gaps filled'):
values_data = [
{'x': '2024-05-01', 'type': 'Revenue', 'stacked': 'A', 'value': 100},
{'x': '2024-05-05', 'type': 'Revenue', 'stacked': 'A', 'value': 200},
{'x': '2024-06-01', 'type': 'Profit', 'stacked': 'B', 'value': 300}
]
result = fill_gaps_in_timerange_data(values_data, 'day', 1)
expect(result).to(contain_only(
{'x': '2024-05-01', 'type': 'Revenue', 'stacked': 'A', 'value': 100},
{'x': '2024-05-02', 'type': 'Revenue', 'stacked': 'A', 'value': 0},
{'x': '2024-05-03', 'type': 'Revenue', 'stacked': 'A', 'value': 0},
{'x': '2024-05-04', 'type': 'Revenue', 'stacked': 'A', 'value': 0},
{'x': '2024-05-05', 'type': 'Revenue', 'stacked': 'A', 'value': 200},
{'x': '2024-06-01', 'type': 'Profit', 'stacked': 'B', 'value': 300}
))
with description('Testing process_timerange_data') as self:
with context('when processing time range data'):
with it('should return the final combined and filled values by day'):
values_data = [
{'x': '2024-05-01', 'type': 'Revenue', 'stacked': 'A',
'value': 100, 'operator': '+'},
{'x': '2024-05-01', 'type': 'Revenue', 'stacked': 'A',
'value': 200, 'operator': '+'},
{'x': '2024-05-05', 'type': 'Revenue', 'stacked': 'A',
'value': 200, 'operator': '+'},
{'x': '2024-06-01', 'type': 'Profit', 'stacked': 'B',
'value': 300, 'operator': '+'}
]
result = process_timerange_data(
values_data, 'day', 1,
)
expect(result).to(contain_only(
{'x': '2024-05-01', 'type': 'Revenue', 'stacked': 'A', 'value': 300, 'operator': '+'},
{'x': '2024-05-02', 'type': 'Revenue', 'stacked': 'A', 'value': 0},
{'x': '2024-05-03', 'type': 'Revenue', 'stacked': 'A', 'value': 0},
{'x': '2024-05-04', 'type': 'Revenue', 'stacked': 'A', 'value': 0},
{'x': '2024-05-05', 'type': 'Revenue', 'stacked': 'A', 'value': 200, 'operator': '+'},
{'x': '2024-06-01', 'type': 'Profit', 'stacked': 'B', 'value': 300, 'operator': '+'}
))
with it('should return the final combined and filled values by month'):
values_data = [
{'x': '2024-01', 'type': 'Revenue', 'stacked': None,
'value': 100, 'operator': '+'},
{'x': '2024-01', 'type': 'Profit', 'stacked': None,
'value': 50, 'operator': '+'},
{'x': '2024-02', 'type': 'Revenue', 'stacked': None,
'value': 200, 'operator': '+'},
{'x': '2024-02', 'type': 'Profit', 'stacked': None,
'value': 100, 'operator': '+'},
{'x': '2024-03', 'type': 'Revenue', 'stacked': None,
'value': 300, 'operator': '+'},
{'x': '2024-03', 'type': 'Profit', 'stacked': None,
'value': 150, 'operator': '+'},
{'x': '2024-04', 'type': 'Profit', 'stacked': None,
'value': 400, 'operator': '+'},
{'x': '2024-05', 'type': 'Profit', 'stacked': None,
'value': 500, 'operator': '+'},
{'x': '2024-06', 'type': 'Profit', 'stacked': None,
'value': 600, 'operator': '+'},
{'x': '2024-06', 'type': 'Revenue', 'stacked': None,
'value': 300, 'operator': '+'},
]
result = process_timerange_data(values_data, 'month')
expect(result).to(contain_only(
{'stacked': None, 'operator': '+', 'x': '2024-01',
'type': 'Profit', 'value': 50.0},
{'stacked': None, 'operator': '+', 'x': '2024-01',
'type': 'Revenue', 'value': 100.0},
{'stacked': None, 'operator': '+', 'x': '2024-02', 'type': 'Profit',
'value': 100.0},
{'stacked': None, 'operator': '+', 'x': '2024-02',
'type': 'Revenue', 'value': 200.0},
{'stacked': None, 'operator': '+', 'x': '2024-03', 'type': 'Profit',
'value': 150.0},
{'stacked': None, 'operator': '+', 'x': '2024-03',
'type': 'Revenue', 'value': 300.0},
{'stacked': None, 'operator': '+', 'x': '2024-04', 'type': 'Profit',
'value': 400.0},
{'x': '2024-04', 'type': 'Revenue', 'stacked': None, 'value': 0},
{'stacked': None, 'operator': '+', 'x': '2024-05', 'type': 'Profit',
'value': 500.0},
{'x': '2024-05', 'type': 'Revenue', 'stacked': None, 'value': 0},
{'stacked': None, 'operator': '+', 'x': '2024-06', 'type': 'Profit',
'value': 600.0},
{'stacked': None, 'operator': '+', 'x': '2024-06',
'type': 'Revenue', 'value': 300.0},
))