-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest_table_helper.py
195 lines (160 loc) · 9.57 KB
/
test_table_helper.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
import unittest
from table_helper import TableHelper
from table import Table
class TestTableHelper(unittest.TestCase):
def setUp(self):
self.table_helper = TableHelper()
def test_is_table_top_left(self):
sheet = self.table_helper.open_file('tests/example_0.xlsx')
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 1-1, 3-1))
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 19-1, 3-1))
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 39-1, 3-1))
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 51-1, 3-1))
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 62-1, 3-1))
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 67-1, 3-1))
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 73-1, 3-1))
self.assertEqual(False, self.table_helper.is_table_top_left(sheet, 74-1, 3-1))
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 84-1, 3-1))
self.assertEqual(False, self.table_helper.is_table_top_left(sheet, 50, 6))
self.assertEqual(False, self.table_helper.is_table_top_left(sheet, 50, 7))
sheet = self.table_helper.open_file('tests/example_1.xlsx')
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 2, 4))
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 21, 2))
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 42, 4))
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 44, 8))
sheet = self.table_helper.open_file('tests/example_2.xlsx')
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 2, 4))
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 20, 2))
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 40, 4))
self.assertEqual(True, self.table_helper.is_table_top_left(sheet, 42, 8))
def test_is_table_top_right(self):
sheet = self.table_helper.open_file('tests/example_0.xlsx')
self.assertEqual(False, self.table_helper.is_table_top_right(sheet, 1-1, 6 ))
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 1-1, 7 ))
self.assertEqual(False, self.table_helper.is_table_top_right(sheet, 1-1, 8 ))
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 19-1, 7 ))
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 39-1, 4 ))
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 51-1, 7 ))
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 62-1, 7 ))
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 67-1, 7 ))
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 73-1, 7 ))
self.assertEqual(False, self.table_helper.is_table_top_right(sheet, 74-1, 7 ))
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 84-1, 5 ))
sheet = self.table_helper.open_file('tests/example_1.xlsx')
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 2, 9 ))
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 21, 7 ))
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 42, 6 ))
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 44, 11 ))
sheet = self.table_helper.open_file('tests/example_2.xlsx')
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 2, 9 ))
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 20, 7 ))
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 40, 6 ))
self.assertEqual(True, self.table_helper.is_table_top_right(sheet, 42, 11 ))
def test_is_table_bottom_left(self):
sheet = self.table_helper.open_file('tests/example_0.xlsx')
self.assertEqual(False, self.table_helper.is_table_bottom_left(sheet, 15, 2 ))
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 16, 2 ))
self.assertEqual(False, self.table_helper.is_table_bottom_left(sheet, 17, 2 ))
self.assertEqual(False, self.table_helper.is_table_bottom_left(sheet, 21, 2 ))
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 36, 2 ))
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 48, 2 ))
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 59, 2 ))
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 64, 2 ))
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 70, 2 ))
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 93, 2 ))
sheet = self.table_helper.open_file('tests/example_1.xlsx')
self.assertEqual(False, self.table_helper.is_table_bottom_left(sheet, 2, 4 ))
self.assertEqual(False, self.table_helper.is_table_bottom_left(sheet, 17, 4 ))
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 18, 4 ))
self.assertEqual(False, self.table_helper.is_table_bottom_left(sheet, 19, 4 ))
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 39, 2 ))
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 52, 4 ))
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 54, 8 ))
sheet = self.table_helper.open_file('tests/example_2.xlsx')
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 18, 4 ))
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 38, 2 ))
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 50, 4 ))
self.assertEqual(True, self.table_helper.is_table_bottom_left(sheet, 52, 8 ))
def test_get_tables_for_example0(self):
## 对于example0, 只看第一个和最后一个table就可以了。
sheet = self.table_helper.open_file('tests/example_0.xlsx')
self.table_helper.get_tables(sheet)
print(f"== tables: {self.table_helper.tables}")
self.assertEqual(8, len(self.table_helper.tables))
table = self.table_helper.tables[0]
self.assertEqual('BALANCE SHEET', table.name)
self.assertEqual(0, table.top)
self.assertEqual(2, table.left)
self.assertEqual(7, table.right)
self.assertEqual(16, table.bottom)
table = self.table_helper.tables[-1]
self.assertEqual(None, table.name)
self.assertEqual(83, table.top)
self.assertEqual(2, table.left)
self.assertEqual(5, table.right)
self.assertEqual(93, table.bottom)
def test_get_tables_for_example1(self):
# 对于example1, 只看第一个和最后一个table就可以了。
sheet = self.table_helper.open_file('tests/example_1.xlsx')
self.table_helper.get_tables(sheet)
self.assertEqual(4, len(self.table_helper.tables))
table = self.table_helper.tables[0]
self.assertEqual('BALANCE SHEET', table.name)
self.assertEqual(2, table.top)
self.assertEqual(4, table.left)
self.assertEqual(9, table.right)
self.assertEqual(18, table.bottom)
table = self.table_helper.tables[-1]
self.assertEqual(None, table.name)
self.assertEqual(44, table.top)
self.assertEqual(8, table.left)
self.assertEqual(11, table.right)
self.assertEqual(54, table.bottom)
def test_get_tables_for_example2(self):
# 对于example2, 查看所有
sheet = self.table_helper.open_file('tests/example_2.xlsx')
self.table_helper.get_tables(sheet)
self.assertEqual(4, len(self.table_helper.tables))
table = self.table_helper.tables[0]
self.assertEqual('BALANCE SHEET', table.name)
self.assertEqual(2, table.top)
self.assertEqual(4, table.left)
self.assertEqual(9, table.right)
self.assertEqual(18, table.bottom)
table = self.table_helper.tables[1]
self.assertEqual('INCOME STATEMENT', table.name)
self.assertEqual(20, table.top)
self.assertEqual(2, table.left)
self.assertEqual(7, table.right)
self.assertEqual(38, table.bottom)
table = self.table_helper.tables[2]
self.assertEqual('SHAREHOLDERS (Fully Diluted)', table.name)
self.assertEqual(40, table.top)
self.assertEqual(4, table.left)
self.assertEqual(6, table.right)
self.assertEqual(50, table.bottom)
table = self.table_helper.tables[3]
self.assertEqual(None, table.name)
self.assertEqual(42, table.top)
self.assertEqual(8, table.left)
self.assertEqual(11, table.right)
self.assertEqual(52, table.bottom)
def test_get_content_from_sheet(self):
sheet = self.table_helper.open_file('tests/example_2.xlsx')
table = Table()
table.top = 2
table.left = 4
table.right = 9
table.bottom = 18
content = self.table_helper.get_content_from_sheet(sheet, table)
self.assertEqual('BALANCE SHEET', content[0][0])
self.assertEqual('Cash & Due from Banks', content[1][0])
self.assertEqual('Liabilities + Equity', content[-2][0])
#print(f"=== content: {content}")
def test_get_csv_for_table_content(self):
result = self.table_helper.get_csv_for_table_content([[1,2], [3,4]])
print(f"== result: {result}")
self.assertEqual('1,2', result.splitlines()[0])
self.assertEqual('3,4', result.splitlines()[1])
if __name__ == '__main__':
unittest.main()