Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_tinkoff-invest-volume-analysis-robot-master/tests/test_merge_two_frames.py
5931 views
1
import unittest
2
3
import pandas as pd
4
from pandas.util.testing import assert_frame_equal
5
6
from utils.strategy_util import merge_two_frames
7
8
9
class TestMergeTwoFrames(unittest.TestCase):
10
def setUp(self):
11
source = [
12
{"figi": "BBG0013HGFT4", "direction": 1, "price": 67.06, "quantity": 15,
13
"time": "2022-05-06 06:59:44.000144+00:00"},
14
{"figi": "BBG0013HGFT4", "direction": 2, "price": 67.06, "quantity": 1,
15
"time": "2022-05-06 06:59:44.000144+00:00"},
16
{"figi": "BBG0013HGFT4", "direction": 2, "price": 67.06, "quantity": 4,
17
"time": "2022-05-06 06:59:44.000144+00:00"},
18
{"figi": "BBG0013HGFT4", "direction": 1, "price": 67.07, "quantity": 18,
19
"time": "2022-05-06 07:00:00.084909+00:00"},
20
{"figi": "BBG0013HGFT4", "direction": 1, "price": 67.075, "quantity": 15,
21
"time": "2022-05-06 07:00:00.084909+00:00"},
22
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.9, "quantity": 2,
23
"time": "2022-05-06 07:00:01.119835+00:00"},
24
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.81, "quantity": 15,
25
"time": "2022-05-06 07:00:05.408809+00:00"},
26
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.81, "quantity": 3,
27
"time": "2022-05-06 07:00:15.605217+00:00"},
28
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.745, "quantity": 10,
29
"time": "2022-05-06 07:00:46.862681+00:00"},
30
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.6775, "quantity": 7,
31
"time": "2022-05-06 07:00:55.531078+00:00"},
32
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.6775, "quantity": 7,
33
"time": "2022-05-06 07:01:01.531078+00:00"},
34
35
# условно потерял данные с 07:01:01 по 07:02:08
36
37
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.795, "quantity": 50,
38
"time": "2022-05-06 07:02:08.652788+00:00"},
39
{"figi": "BBG0013HGFT4", "direction": 1, "price": 66.79, "quantity": 5,
40
"time": "2022-05-06 07:02:09.338233+00:00"},
41
{"figi": "BBG0013HGFT4", "direction": 1, "price": 66.88, "quantity": 25,
42
"time": "2022-05-06 07:02:30.858362+00:00"},
43
{"figi": "BBG0013HGFT4", "direction": 1, "price": 66.88, "quantity": 1,
44
"time": "2022-05-06 07:02:31.232032+00:00"},
45
{"figi": "BBG0013HGFT4", "direction": 1, "price": 66.9525, "quantity": 1,
46
"time": "2022-05-06 07:02:58.749556+00:00"},
47
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.9, "quantity": 1,
48
"time": "2022-05-06 07:03:01.484374+00:00"},
49
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.895, "quantity": 30,
50
"time": "2022-05-06 07:03:55.090231+00:00"}
51
]
52
self.source_df = pd.DataFrame(source, columns=["figi", "direction", "price", "quantity", "time"])
53
54
# запрос данных с 07:01:01 по 07:02:08
55
# api вернет с 07:01:00 по 07:02:59
56
response = [
57
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.6775, "quantity": 7,
58
"time": "2022-05-06 07:01:01.531078+00:00"},
59
# region новые данные
60
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.6775, "quantity": 7,
61
"time": "2022-05-06 07:01:05.531078+00:00"},
62
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.6775, "quantity": 7,
63
"time": "2022-05-06 07:01:20.531078+00:00"},
64
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.6775, "quantity": 7,
65
"time": "2022-05-06 07:01:45.531078+00:00"},
66
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.6775, "quantity": 7,
67
"time": "2022-05-06 07:01:59.531078+00:00"},
68
# endregion новые данные
69
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.795, "quantity": 50,
70
"time": "2022-05-06 07:02:08.652788+00:00"},
71
{"figi": "BBG0013HGFT4", "direction": 1, "price": 66.79, "quantity": 5,
72
"time": "2022-05-06 07:02:09.338233+00:00"},
73
{"figi": "BBG0013HGFT4", "direction": 1, "price": 66.88, "quantity": 25,
74
"time": "2022-05-06 07:02:30.858362+00:00"},
75
{"figi": "BBG0013HGFT4", "direction": 1, "price": 66.88, "quantity": 1,
76
"time": "2022-05-06 07:02:31.232032+00:00"},
77
{"figi": "BBG0013HGFT4", "direction": 1, "price": 66.9525, "quantity": 1,
78
"time": "2022-05-06 07:02:58.749556+00:00"},
79
]
80
self.response_df = pd.DataFrame(response, columns=["figi", "direction", "price", "quantity", "time"])
81
82
self.empty_df = pd.DataFrame([], columns=["figi", "direction", "price", "quantity", "time"])
83
84
def test_merge_two_frames(self):
85
expected = [
86
{"figi": "BBG0013HGFT4", "direction": 1, "price": 67.06, "quantity": 15,
87
"time": "2022-05-06 06:59:44.000144+00:00"},
88
{"figi": "BBG0013HGFT4", "direction": 2, "price": 67.06, "quantity": 1,
89
"time": "2022-05-06 06:59:44.000144+00:00"},
90
{"figi": "BBG0013HGFT4", "direction": 2, "price": 67.06, "quantity": 4,
91
"time": "2022-05-06 06:59:44.000144+00:00"},
92
{"figi": "BBG0013HGFT4", "direction": 1, "price": 67.07, "quantity": 18,
93
"time": "2022-05-06 07:00:00.084909+00:00"},
94
{"figi": "BBG0013HGFT4", "direction": 1, "price": 67.075, "quantity": 15,
95
"time": "2022-05-06 07:00:00.084909+00:00"},
96
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.9, "quantity": 2,
97
"time": "2022-05-06 07:00:01.119835+00:00"},
98
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.81, "quantity": 15,
99
"time": "2022-05-06 07:00:05.408809+00:00"},
100
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.81, "quantity": 3,
101
"time": "2022-05-06 07:00:15.605217+00:00"},
102
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.745, "quantity": 10,
103
"time": "2022-05-06 07:00:46.862681+00:00"},
104
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.6775, "quantity": 7,
105
"time": "2022-05-06 07:00:55.531078+00:00"},
106
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.6775, "quantity": 7,
107
"time": "2022-05-06 07:01:01.531078+00:00"},
108
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.6775, "quantity": 7,
109
"time": "2022-05-06 07:01:05.531078+00:00"},
110
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.6775, "quantity": 7,
111
"time": "2022-05-06 07:01:20.531078+00:00"},
112
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.6775, "quantity": 7,
113
"time": "2022-05-06 07:01:45.531078+00:00"},
114
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.6775, "quantity": 7,
115
"time": "2022-05-06 07:01:59.531078+00:00"},
116
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.795, "quantity": 50,
117
"time": "2022-05-06 07:02:08.652788+00:00"},
118
{"figi": "BBG0013HGFT4", "direction": 1, "price": 66.79, "quantity": 5,
119
"time": "2022-05-06 07:02:09.338233+00:00"},
120
{"figi": "BBG0013HGFT4", "direction": 1, "price": 66.88, "quantity": 25,
121
"time": "2022-05-06 07:02:30.858362+00:00"},
122
{"figi": "BBG0013HGFT4", "direction": 1, "price": 66.88, "quantity": 1,
123
"time": "2022-05-06 07:02:31.232032+00:00"},
124
{"figi": "BBG0013HGFT4", "direction": 1, "price": 66.9525, "quantity": 1,
125
"time": "2022-05-06 07:02:58.749556+00:00"},
126
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.9, "quantity": 1,
127
"time": "2022-05-06 07:03:01.484374+00:00"},
128
{"figi": "BBG0013HGFT4", "direction": 2, "price": 66.895, "quantity": 30,
129
"time": "2022-05-06 07:03:55.090231+00:00"}
130
]
131
expected_df = pd.DataFrame(expected, columns=["figi", "direction", "price", "quantity", "time"])
132
assert_frame_equal(merge_two_frames(self.source_df, self.response_df), expected_df)
133
134
def test_boundary(self):
135
assert_frame_equal(merge_two_frames(self.source_df, self.empty_df), self.source_df)
136
assert_frame_equal(merge_two_frames(self.empty_df, self.response_df), self.response_df)
137
assert_frame_equal(merge_two_frames(self.empty_df, self.empty_df), self.empty_df)
138
139
140
if __name__ == "__main__":
141
unittest.main()
142
143