-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_batch_timeframe.py
590 lines (397 loc) · 19.6 KB
/
test_batch_timeframe.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
from datetime import datetime, timedelta
from functools import reduce
import pytest
from timeframe import BatchTimeFrame, TimeFrame
# ======================= Initialization ============================
def test_batch_timeframe_initialize_successfully():
tf1 = TimeFrame(datetime(2021, 1, 17, 10), datetime(2021, 1, 17, 11))
tf2 = TimeFrame(datetime(2021, 1, 17, 12), datetime(2021, 1, 17, 14))
tf3 = TimeFrame(datetime(2021, 1, 17, 18), datetime(2021, 1, 17, 20))
tf_list = [tf1, tf2, tf3]
btf = BatchTimeFrame(tf_list)
assert btf.duration == sum(tf.duration for tf in tf_list)
assert btf.len_timeframes == len(tf_list)
def test_batch_timeframe_initialize_with_negligible_difference_combines_them():
tf1 = TimeFrame(datetime(2021, 1, 17, 10), datetime(2021, 1, 17, 11))
tf2 = TimeFrame(datetime(2021, 1, 17, 11), datetime(2021, 1, 17, 12))
tf3 = TimeFrame(datetime(2021, 1, 17, 13), datetime(2021, 1, 17, 20))
tf_list = [tf1, tf2, tf3]
btf = BatchTimeFrame(tf_list)
assert btf.duration == sum(tf.duration for tf in tf_list)
assert btf.len_timeframes == 2
def test_batch_timeframe_initialize_with_duplicate_timeframes_successfully():
tf1 = TimeFrame(datetime(2021, 1, 17, 10), datetime(2021, 1, 17, 11))
tf2 = TimeFrame(datetime(2021, 1, 17, 12), datetime(2021, 1, 17, 14))
tf3 = TimeFrame(datetime(2021, 1, 17, 18), datetime(2021, 1, 17, 20))
tf4 = TimeFrame(datetime(2021, 1, 17, 18), datetime(2021, 1, 17, 20))
tf5 = TimeFrame(datetime(2021, 1, 17, 10), datetime(2021, 1, 17, 11))
tf_list = [tf1, tf2, tf3, tf4, tf5]
unique_tfs = tf_list[:3]
btf = BatchTimeFrame(tf_list)
assert btf.duration == sum(tf.duration for tf in unique_tfs)
assert btf.len_timeframes == len(unique_tfs)
def test_batch_timeframe_initialize_wrong_type_raises_type_error():
with pytest.raises(TypeError):
BatchTimeFrame(1)
with pytest.raises(TypeError):
BatchTimeFrame(1.0)
with pytest.raises(TypeError):
BatchTimeFrame("dummy")
with pytest.raises(TypeError):
BatchTimeFrame([1, 1.0, "dummy"])
with pytest.raises(TypeError):
BatchTimeFrame([TimeFrame(datetime(2021, 1, 17), datetime(2021, 1, 18)), 1])
def test_batch_timeframe_initialize_empty_list_is_find_as_well():
btf = BatchTimeFrame([])
assert btf.duration == 0
def test_batch_timeframe_initialize_with_overlapped_elements_prunes_extras():
tf1 = TimeFrame(datetime(2021, 1, 17, 10), datetime(2021, 1, 17, 11))
tf2 = TimeFrame(datetime(2021, 1, 17, 12), datetime(2021, 1, 17, 14))
tf3 = TimeFrame(datetime(2021, 1, 17, 18), datetime(2021, 1, 17, 20))
tf4 = TimeFrame(datetime(2021, 1, 17, 10, 30), datetime(2021, 1, 17, 13))
tf5 = TimeFrame(datetime(2021, 1, 17, 14, 30), datetime(2021, 1, 17, 14, 40))
tf6 = TimeFrame(datetime(2021, 1, 17, 14, 50), datetime(2021, 1, 17, 17))
tf7 = TimeFrame(datetime(2021, 1, 17, 14, 35), datetime(2021, 1, 17, 18, 30))
tf_list = [tf1, tf2, tf3, tf4, tf5, tf5, tf6, tf7]
union = reduce(lambda x, y: x + y, tf_list)
expected_len = 2 # calculated manually
btf = BatchTimeFrame(tf_list)
assert btf.duration == union.duration
assert btf.len_timeframes == expected_len
# ======================= Inclusion ============================
def test_batch_timeframe_includes_another_batch_timeframe_with_overlap():
tf1 = TimeFrame(datetime(2021, 1, 17, 10), datetime(2021, 1, 17, 11))
tf2 = TimeFrame(datetime(2021, 1, 17, 12), datetime(2021, 1, 17, 14))
tf3 = TimeFrame(datetime(2021, 1, 17, 18), datetime(2021, 1, 17, 20))
tf4 = TimeFrame(datetime(2021, 1, 17, 19), datetime(2021, 1, 17, 19, 30))
tf5 = TimeFrame(datetime(2021, 1, 17, 10, 30), datetime(2021, 1, 17, 10, 40))
tf_list1 = [tf1, tf2, tf3]
tf_list2 = [tf4, tf5]
btf1 = BatchTimeFrame(tf_list1)
btf2 = BatchTimeFrame(tf_list2)
assert btf1.includes(btf2)
def test_batch_timeframe_doesnt_include_another_batch_timeframe_with_minor_overlap():
tf1 = TimeFrame(datetime(2021, 1, 17, 10), datetime(2021, 1, 17, 11))
tf2 = TimeFrame(datetime(2021, 1, 17, 12), datetime(2021, 1, 17, 14))
tf3 = TimeFrame(datetime(2021, 1, 17, 18), datetime(2021, 1, 17, 20))
tf4 = TimeFrame(datetime(2021, 1, 17, 13), datetime(2021, 1, 17, 23))
tf5 = TimeFrame(datetime(2021, 1, 17, 10, 30), datetime(2021, 1, 17, 10, 40))
tf_list1 = [tf1, tf2, tf3]
tf_list2 = [tf4, tf5]
btf1 = BatchTimeFrame(tf_list1)
btf2 = BatchTimeFrame(tf_list2)
assert not btf1.includes(btf2)
def test_batch_timeframe_doesnt_include_another_batch_timeframe_without_overlap():
tf1 = TimeFrame(datetime(2021, 1, 17, 10), datetime(2021, 1, 17, 11))
tf2 = TimeFrame(datetime(2021, 1, 17, 12), datetime(2021, 1, 17, 14))
tf3 = TimeFrame(datetime(2021, 1, 17, 18), datetime(2021, 1, 17, 20))
tf4 = TimeFrame(datetime(2021, 1, 18), datetime(2021, 1, 19))
tf5 = TimeFrame(datetime(2021, 1, 20, 10, 30), datetime(2021, 1, 20, 10, 40))
tf_list1 = [tf1, tf2, tf3]
tf_list2 = [tf4, tf5]
btf1 = BatchTimeFrame(tf_list1)
btf2 = BatchTimeFrame(tf_list2)
assert not btf1.includes(btf2)
def test_batch_timeframe_includes_timeframe_accurately():
tf1 = TimeFrame(datetime(2021, 1, 17, 10), datetime(2021, 1, 17, 11))
tf2 = TimeFrame(datetime(2021, 1, 17, 12), datetime(2021, 1, 17, 14))
tf3 = TimeFrame(datetime(2021, 1, 17, 18), datetime(2021, 1, 17, 20))
tf4 = TimeFrame(datetime(2021, 1, 17, 9), datetime(2021, 1, 17, 12))
tf5 = TimeFrame(datetime(2021, 1, 17, 10, 30), datetime(2021, 1, 17, 10, 55))
tf6 = TimeFrame(datetime(2021, 1, 17, 18, 30), datetime(2021, 1, 17, 19, 30))
tf_list1 = [tf1, tf2, tf3]
btf = BatchTimeFrame(tf_list1)
assert not btf.includes(tf4)
assert btf.includes(tf5)
assert btf.includes(tf6)
def test_batch_timeframe_includes_with_wrong_type_raises_error():
tf1 = TimeFrame(datetime(2021, 1, 17, 10), datetime(2021, 1, 17, 11))
tf2 = TimeFrame(datetime(2021, 1, 17, 12), datetime(2021, 1, 17, 14))
tf3 = TimeFrame(datetime(2021, 1, 17, 18), datetime(2021, 1, 17, 20))
tf_list1 = [tf1, tf2, tf3]
btf = BatchTimeFrame(tf_list1)
with pytest.raises(TypeError):
btf.includes(1)
with pytest.raises(TypeError):
btf.includes(1.0)
with pytest.raises(TypeError):
btf.includes("dummy")
with pytest.raises(TypeError):
btf.includes([])
with pytest.raises(TypeError):
btf.includes([1, 1.0, "dummy"])
def test_batch_timeframe_inclusion_check_with_in_keyword(faker, empty_timeframe):
dt1 = faker.date_time_between(start_date="-1y", end_date="-6M")
dt2 = faker.date_time_between(start_date="-5M", end_date="now")
dt12 = faker.date_time_between(start_date=dt1, end_date=dt2)
dt3 = dt1 + timedelta(days=1)
dt4 = dt2 - timedelta(days=1)
dt5 = faker.date_time_between(start_date="now", end_date="+1y")
dt6 = faker.date_time_between(start_date="+2y")
tf1 = TimeFrame(dt1, dt2)
tf2 = TimeFrame(dt3, dt4)
tf3 = TimeFrame(dt5, dt6)
assert tf2 in BatchTimeFrame([tf1])
assert tf1 in BatchTimeFrame([tf1])
assert BatchTimeFrame([tf1]) in BatchTimeFrame([tf1])
assert tf3 not in BatchTimeFrame([tf1])
assert dt12 in BatchTimeFrame([tf1])
assert dt5 not in BatchTimeFrame([tf1])
assert tf1.start in BatchTimeFrame([tf1])
assert tf1.end in BatchTimeFrame([tf1])
assert empty_timeframe in BatchTimeFrame([tf1])
def test_batch_timeframe_inclusion_check_with_other_types_raise_value_error(faker):
dt1 = faker.date_time_between(start_date="-1y", end_date="-6M")
dt2 = faker.date_time_between(start_date=dt1, end_date="now")
with pytest.raises(TypeError):
faker.pyint() in BatchTimeFrame([TimeFrame(dt1, dt2)])
with pytest.raises(TypeError):
faker.pyfloat() in BatchTimeFrame([TimeFrame(dt1, dt2)])
with pytest.raises(TypeError):
faker.text() in BatchTimeFrame([TimeFrame(dt1, dt2)])
with pytest.raises(TypeError):
[] in BatchTimeFrame([TimeFrame(dt1, dt2)])
with pytest.raises(TypeError):
[faker.pyint(), faker.pyfloat(), faker.text()] in BatchTimeFrame(
[TimeFrame(dt1, dt2)]
)
with pytest.raises(TypeError):
faker.pybool() in BatchTimeFrame([TimeFrame(dt1, dt2)])
def test_batch_timeframe_inclusion_check_with_include_warns_deprecation(
random_timeframe, random_batch_timeframes
):
with pytest.warns(DeprecationWarning):
random_batch_timeframes.includes(random_timeframe)
# ======================= Summation ============================
def test_batch_timeframe_add_two_instances_successfully():
tf1 = TimeFrame(datetime(2021, 1, 18, 10), datetime(2021, 1, 18, 11))
tf2 = TimeFrame(datetime(2021, 1, 18, 12), datetime(2021, 1, 18, 14))
tf3 = TimeFrame(datetime(2021, 1, 18, 18), datetime(2021, 1, 18, 20))
tf4 = TimeFrame(datetime(2021, 1, 18), datetime(2021, 1, 19))
tf5 = TimeFrame(datetime(2021, 1, 20, 10, 30), datetime(2021, 1, 20, 10, 40))
tf_list1 = [tf1, tf2, tf3]
tf_list2 = [tf4, tf5]
btf1 = BatchTimeFrame(tf_list1)
btf2 = BatchTimeFrame(tf_list2)
btf3 = btf1 + btf2
assert btf3.len_timeframes == 2 # calculated manually
assert btf3.duration == btf2.duration # again, calculated manually
def test_batch_timeframe_add_with_timeframe_successfully():
tf1 = TimeFrame(datetime(2021, 1, 18, 10), datetime(2021, 1, 18, 11))
tf2 = TimeFrame(datetime(2021, 1, 18, 12), datetime(2021, 1, 18, 14))
tf3 = TimeFrame(datetime(2021, 1, 18, 18), datetime(2021, 1, 18, 20))
tf4 = TimeFrame(datetime(2021, 1, 18), datetime(2021, 1, 19))
tf5 = TimeFrame(datetime(2021, 1, 20, 10, 30), datetime(2021, 1, 20, 10, 40))
tf_list1 = [tf1, tf2, tf3]
btf1 = BatchTimeFrame(tf_list1)
btf2 = btf1 + tf4
assert btf2.len_timeframes == 1 # calculated manually
assert btf2.duration == tf4.duration # again, calculated manually
btf2 += tf5
assert btf2.len_timeframes == 2 # dah! calculated manually
assert btf2.duration == (tf4 + tf5).duration # seriously dude?
def test_batch_timeframe_add_empty_timeframe_results_in_the_same_object():
tf1 = TimeFrame(datetime(2021, 1, 18, 10), datetime(2021, 1, 18, 11))
tf2 = TimeFrame(datetime(2021, 1, 18, 12), datetime(2021, 1, 18, 14))
tf3 = TimeFrame(datetime(2021, 1, 18, 18), datetime(2021, 1, 18, 20))
tf4 = TimeFrame(datetime(2021, 1, 18), datetime(2021, 1, 19))
tf5 = TimeFrame(datetime(2021, 1, 20, 10, 30), datetime(2021, 1, 20, 10, 40))
tf_list1 = [tf1, tf2, tf3]
btf1 = BatchTimeFrame(tf_list1)
btf2 = btf1 + (tf4 * tf5) # the multiplication is an empty timeframe
assert btf2.len_timeframes == btf1.len_timeframes
assert btf2.duration == btf1.duration
btf2 += tf1 * tf5 # another empty timeframe
assert btf2.len_timeframes == btf1.len_timeframes
assert btf2.duration == btf1.duration
def test_batch_timeframe_add_with_non_base_timeframe_raises_type_error():
tf1 = TimeFrame(datetime(2021, 1, 17, 10), datetime(2021, 1, 17, 11))
tf2 = TimeFrame(datetime(2021, 1, 17, 12), datetime(2021, 1, 17, 14))
tf3 = TimeFrame(datetime(2021, 1, 17, 18), datetime(2021, 1, 17, 20))
tf_list1 = [tf1, tf2, tf3]
btf = BatchTimeFrame(tf_list1)
with pytest.raises(TypeError):
btf + 1
with pytest.raises(TypeError):
btf + 1.0
with pytest.raises(TypeError):
btf + "dummy"
with pytest.raises(TypeError):
btf + []
with pytest.raises(TypeError):
btf + True
with pytest.raises(TypeError):
btf + [1, 1.0, "dummy", True]
# ======================= Multiplication ============================
def test_batch_timeframe_muliply_two_instances_successfully():
tf1 = TimeFrame(datetime(2021, 1, 18, 10), datetime(2021, 1, 18, 11))
tf2 = TimeFrame(datetime(2021, 1, 18, 12), datetime(2021, 1, 18, 14))
tf3 = TimeFrame(datetime(2021, 1, 18, 18), datetime(2021, 1, 18, 20))
tf4 = TimeFrame(datetime(2021, 1, 18), datetime(2021, 1, 19))
tf5 = TimeFrame(datetime(2021, 1, 20, 10, 30), datetime(2021, 1, 20, 10, 40))
tf_list1 = [tf1, tf2, tf3]
tf_list2 = [tf4, tf5]
btf1 = BatchTimeFrame(tf_list1)
btf2 = BatchTimeFrame(tf_list2)
btf3 = btf1 * btf2
assert btf3.len_timeframes == 3
assert btf3.duration == (tf1 + tf2 + tf3).duration
def test_batch_timeframe_multiply_with_timeframe_successfully():
tf1 = TimeFrame(datetime(2021, 1, 18, 10), datetime(2021, 1, 18, 11))
tf2 = TimeFrame(datetime(2021, 1, 18, 12), datetime(2021, 1, 18, 14))
tf3 = TimeFrame(datetime(2021, 1, 18, 18), datetime(2021, 1, 18, 20))
tf4 = TimeFrame(datetime(2021, 1, 18), datetime(2021, 1, 19))
tf5 = TimeFrame(datetime(2021, 1, 20, 10, 30), datetime(2021, 1, 20, 10, 40))
tf_list1 = [tf1, tf2, tf3]
btf1 = BatchTimeFrame(tf_list1)
btf2 = btf1 * tf4
assert btf2.len_timeframes == 3
assert btf2.duration == (tf1 + tf2 + tf3).duration
btf2 *= tf5
assert btf2.len_timeframes == 0
assert btf2.duration == 0
def test_batch_timeframe_multiply_with_empty_timeframe_results_in_empty_timeframe():
tf1 = TimeFrame(datetime(2021, 1, 18, 10), datetime(2021, 1, 18, 11))
tf2 = TimeFrame(datetime(2021, 1, 18, 12), datetime(2021, 1, 18, 14))
tf3 = TimeFrame(datetime(2021, 1, 18, 18), datetime(2021, 1, 18, 20))
tf4 = TimeFrame(datetime(2021, 1, 18), datetime(2021, 1, 19))
tf5 = TimeFrame(datetime(2021, 1, 20, 10, 30), datetime(2021, 1, 20, 10, 40))
tf_list1 = [tf1, tf2, tf3]
btf1 = BatchTimeFrame(tf_list1)
btf2 = btf1 * (tf4 * tf5) # the multiplication is an empty timeframe
assert btf2.len_timeframes == 0
assert btf2.duration == 0
btf2 += tf1 * tf5 # another empty timeframe
assert btf2.len_timeframes == 0
assert btf2.duration == 0
def test_batch_timeframe_multiply_with_non_base_timeframe_raises_type_error():
tf1 = TimeFrame(datetime(2021, 1, 17, 10), datetime(2021, 1, 17, 11))
tf2 = TimeFrame(datetime(2021, 1, 17, 12), datetime(2021, 1, 17, 14))
tf3 = TimeFrame(datetime(2021, 1, 17, 18), datetime(2021, 1, 17, 20))
tf_list1 = [tf1, tf2, tf3]
btf = BatchTimeFrame(tf_list1)
with pytest.raises(TypeError):
btf * 1
with pytest.raises(TypeError):
btf * 1.0
with pytest.raises(TypeError):
btf * "dummy"
with pytest.raises(TypeError):
btf * []
with pytest.raises(TypeError):
btf * True
with pytest.raises(TypeError):
btf * [1, 1.0, "dummy", True]
# ======================= Subtraction ============================
def test_batch_timeframe_subtract_two_instances_successfully():
tf1 = TimeFrame(datetime(2021, 1, 18, 10), datetime(2021, 1, 18, 11))
tf2 = TimeFrame(datetime(2021, 1, 18, 12), datetime(2021, 1, 18, 14))
tf3 = TimeFrame(datetime(2021, 1, 18, 18), datetime(2021, 1, 18, 20))
tf4 = TimeFrame(datetime(2021, 1, 18), datetime(2021, 1, 19))
tf5 = TimeFrame(datetime(2021, 1, 20, 10, 30), datetime(2021, 1, 20, 10, 40))
tf_list1 = [tf1, tf2, tf3]
btf1 = BatchTimeFrame(tf_list1)
btf2 = btf1 - BatchTimeFrame([tf4, tf5])
assert btf2.len_timeframes == 0
assert btf2.duration == 0
btf2 = btf1 - BatchTimeFrame([tf5])
assert btf2.len_timeframes == 3
assert btf2.duration == btf1.duration
def test_batch_timeframe_subtract_with_timeframe_successfully():
tf1 = TimeFrame(datetime(2021, 1, 18, 10), datetime(2021, 1, 18, 11))
tf2 = TimeFrame(datetime(2021, 1, 18, 12), datetime(2021, 1, 18, 14))
tf3 = TimeFrame(datetime(2021, 1, 18, 18), datetime(2021, 1, 18, 20))
tf4 = TimeFrame(datetime(2021, 1, 18), datetime(2021, 1, 19))
tf5 = TimeFrame(datetime(2021, 1, 18, 10, 30), datetime(2021, 1, 18, 12, 30))
tf_list1 = [tf1, tf2, tf3]
btf1 = BatchTimeFrame(tf_list1)
btf2 = btf1 - tf4
assert btf2.len_timeframes == 0
assert btf2.duration == 0
btf2 = btf1 - tf5
assert btf2.len_timeframes == 3
assert btf2.duration == (tf1 + tf2 + tf3 - tf5).duration
def test_batch_timeframe_subtract_from_empty_timeframe_results_in_the_same_object():
tf1 = TimeFrame(datetime(2021, 1, 18, 10), datetime(2021, 1, 18, 11))
tf2 = TimeFrame(datetime(2021, 1, 18, 12), datetime(2021, 1, 18, 14))
tf3 = TimeFrame(datetime(2021, 1, 18, 18), datetime(2021, 1, 18, 20))
tf4 = TimeFrame(datetime(2021, 1, 18), datetime(2021, 1, 19))
tf5 = TimeFrame(datetime(2021, 1, 20, 10, 30), datetime(2021, 1, 20, 10, 40))
tf_list1 = [tf1, tf2, tf3]
btf1 = BatchTimeFrame(tf_list1)
btf2 = btf1 - (tf4 * tf5)
assert btf2.len_timeframes == btf1.len_timeframes
assert btf2.duration == btf1.duration
btf2 += tf1 * tf5 # another empty timeframe
assert btf2.len_timeframes == btf1.len_timeframes
assert btf2.duration == btf1.duration
def test_batch_timeframe_subtract_from_non_base_timeframe_raises_type_error():
tf1 = TimeFrame(datetime(2021, 1, 17, 10), datetime(2021, 1, 17, 11))
tf2 = TimeFrame(datetime(2021, 1, 17, 12), datetime(2021, 1, 17, 14))
tf3 = TimeFrame(datetime(2021, 1, 17, 18), datetime(2021, 1, 17, 20))
tf_list1 = [tf1, tf2, tf3]
btf = BatchTimeFrame(tf_list1)
with pytest.raises(TypeError):
btf - 1
with pytest.raises(TypeError):
btf - 1.0
with pytest.raises(TypeError):
btf - "dummy"
with pytest.raises(TypeError):
btf - []
with pytest.raises(TypeError):
btf - True
with pytest.raises(TypeError):
btf - [1, 1.0, "dummy", True]
# ======================= Equality ============================
def test_batch_timeframe_equals_another_identical_batch_timeframe():
tf1 = TimeFrame(datetime(2021, 1, 18, 10), datetime(2021, 1, 18, 11))
tf2 = TimeFrame(datetime(2021, 1, 18, 12), datetime(2021, 1, 18, 14))
tf3 = TimeFrame(datetime(2021, 1, 18, 18), datetime(2021, 1, 18, 20))
tf4 = TimeFrame(datetime(2021, 1, 18), datetime(2021, 1, 19))
tf5 = TimeFrame(datetime(2021, 1, 20, 10, 30), datetime(2021, 1, 20, 10, 40))
tf_list1 = [tf1, tf2, tf3]
tf_list2 = [tf4, tf5]
btf1 = BatchTimeFrame(tf_list1)
btf2 = BatchTimeFrame(tf_list1)
btf3 = BatchTimeFrame(tf_list2)
assert btf1 == btf2 and btf2 == btf1
assert btf1 != btf3 and btf2 != btf3
def test_batch_timeframe_equality_with_non_batch_timeframe_raises_type_error():
tf1 = TimeFrame(datetime(2021, 1, 17, 10), datetime(2021, 1, 17, 11))
tf2 = TimeFrame(datetime(2021, 1, 17, 12), datetime(2021, 1, 17, 14))
tf3 = TimeFrame(datetime(2021, 1, 17, 18), datetime(2021, 1, 17, 20))
tf_list1 = [tf1, tf2, tf3]
btf = BatchTimeFrame(tf_list1)
with pytest.raises(TypeError):
btf == 1
with pytest.raises(TypeError):
btf == 1.0
with pytest.raises(TypeError):
btf == "dummy"
with pytest.raises(TypeError):
btf == []
with pytest.raises(TypeError):
btf == True # noqa: E712
with pytest.raises(TypeError):
btf == [1, 1.0, "dummy", True]
# ======================= Repr ============================
def test_batch_timeframe_single_timeframe_repr():
btf = BatchTimeFrame(
[TimeFrame(datetime(2022, 10, 18, 10), datetime(2022, 10, 18, 11))]
)
assert repr(btf) == "2022-10-18T10:00:00#2022-10-18T11:00:00"
def test_batch_timeframe_multiple_timeframes_repr():
btf = BatchTimeFrame(
[
TimeFrame(datetime(2022, 10, 18, 10), datetime(2022, 10, 18, 11)),
TimeFrame(datetime(2022, 10, 18, 12), datetime(2022, 10, 18, 14)),
TimeFrame(datetime(2022, 10, 18, 18), datetime(2022, 10, 18, 20)),
]
)
assert (
repr(btf)
== "2022-10-18T10:00:00#2022-10-18T11:00:00,2022-10-18T12:00:00#2022-10-18T14:00:00,2022-10-18T18:00:00#2022-10-18T20:00:00".replace(
",", "\n"
)
)