This repository has been archived by the owner on Nov 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_create_downloads.py
239 lines (177 loc) · 7.68 KB
/
test_create_downloads.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
import mock
from io import BytesIO
from resource import getrusage, RUSAGE_SELF, getpagesize
from textwrap import dedent
from nose.tools import assert_equal, assert_less_equal
from nose.plugins.skip import SkipTest
from create_downloads import (ExcelOutput, CsvOutput, grid_rows_from_string,
dump_grids, find_trs, ExceleratorOutput)
def test_generate_excel_colspans():
# This test doesn't actually check the output, it just exercises the code.
# :(
with open("test/fixtures/simple-table-colspans.html") as fd:
grid_rows = grid_rows_from_string(fd.read())
with ExcelOutput("test/test.xls") as excel_output:
add_row = excel_output.add_sheet("test_sheet")
for row in grid_rows:
add_row(row)
def test_generate_excel_colspans_complex():
# This test doesn't actually check the output, it just exercises the code.
# :(
with open("test/fixtures/more-complicated-colspan.html") as fd:
grid_rows = grid_rows_from_string(fd.read())
with ExcelOutput("test/test.xls") as excel_output:
add_row = excel_output.add_sheet("test_sheet")
for row in grid_rows:
add_row(row)
def test_generate_csv_rowspans():
raise SkipTest("Unskip this when rowspanning is implemented correctly")
with open("test/fixtures/simple-table-rowspans.html") as fd:
grid_rows = grid_rows_from_string(fd.read())
with mock.patch("create_downloads.CsvOutput._send_row") as _send_row:
with CsvOutput("test/test-rowspans.csv") as csv_output:
for row in grid_rows:
csv_output.write_row(row)
from mock import call
expected = [
call([u'Name: \u201cBlad1\u201d', u'Name: \u201cBlad1\u201d',
u'Name: \u201cBlad1\u201d', u'Name: \u201cBlad1\u201d']),
call(['Table: 1', 'Table: 1', 'Table: 1', 'Table: 1']),
call(['', '', '', '']),
call(['Port', '31 December 2012', '31 January 2013',
'Difference']),
call(['Port', '', '', '']),
call(['Antwerp', '4827966.66667', '4947533.33333',
'119566.666667']),
call(['Bremen', '1265600.0', '1344250.0', '78650.0']),
call(['Hamburg', '1593400.0', '1653916.66667', '60516.6666667']),
call(['Genova', '934993.0', '947459.0', '12466.0']),
call(['Le Havre', '445833.333333', '479983.333333', '34150.0']),
call(['Trieste', '695243.0', '702157.0', '6914.0']),
call(['Total Europe', '9763036.0', '10075299.3333',
'312263.333333'])]
# The intent of this test is to ensure that colspans are correctly
# copied to all of the destination cells which are overlapped by it.
assert_equal(expected, _send_row.call_args_list)
def test_generate_csv_colspans():
with open("test/fixtures/simple-table-colspans.html") as fd:
grid_rows = grid_rows_from_string(fd.read())
with mock.patch("create_downloads.CsvOutput._send_row") as _send_row:
with CsvOutput("test/test.csv") as csv_output:
for row in grid_rows:
csv_output.write_row(row)
from mock import call
expected = [
call([u'Name: \u201cBlad1\u201d', u'Name: \u201cBlad1\u201d',
u'Name: \u201cBlad1\u201d', u'Name: \u201cBlad1\u201d']),
call(['Table: 1', 'Table: 1', 'Table: 1', 'Table: 1']),
call(['', '', '', '']),
call(['Port', '31 December 2012', '31 January 2013',
'Difference']),
call(['', '', '', '']),
call(['Antwerp', '4827966.66667', '4947533.33333',
'119566.666667']),
call(['Bremen', '1265600.0', '1344250.0', '78650.0']),
call(['Hamburg', '1593400.0', '1653916.66667', '60516.6666667']),
call(['Genova', '934993.0', '947459.0', '12466.0']),
call(['Le Havre', '445833.333333', '479983.333333', '34150.0']),
call(['Trieste', '695243.0', '702157.0', '6914.0']),
call(['Total Europe', '9763036.0', '10075299.3333',
'312263.333333'])]
# The intent of this test is to ensure that colspans are correctly
# copied to all of the destination cells which are overlapped by it.
assert_equal(expected, _send_row.call_args_list)
def test_dump_grids():
with mock.patch("create_downloads.get_grid_rows") as get_grid_rows, \
mock.patch("create_downloads.CsvOutput.write_row") as write_row:
rows = [
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
]
def get_rows(*args):
return rows
get_grid_rows.side_effect = get_rows
# class MockExcel():
# def add_sheet(self, name):
# def write_row(row):
# pass
# return write_row
with ExcelOutput("test/test_all_tables.xls") as excel_output:
grids = [{'name': '<name>', 'url': '<url>'}]
dump_grids(excel_output, grids)
# assert_equal( ) write_row.call_args_list
def getmaxrss_mb():
"""
Return the maximum resident memory usage of this process
"""
ru = getrusage(RUSAGE_SELF)
def as_mb(n_pages):
return n_pages * getpagesize() / 1024. ** 2
return as_mb(ru.ru_maxrss)
def make_table(nrows, ncols):
"""
Return a simple HTML table with nrows ncols.
"""
return dedent(b"""\
<table>
{rows}
</table>
""").format(
rows="\n".join(
"<tr>{cells}</tr>".format(cells="".join("<td>{i}</td>".format(i=i)
for i in xrange(ncols)))
for _ in xrange(nrows)))
def kick_maxrss():
"""
Return an object, which if kept alive, guarantees that the maximum resident
memory of the process is roughly equal to the current usage.
"""
delta = 0
mem_before = getmaxrss_mb()
keep_alive = []
# Algorithm: whilst delta doesn't go up, more memory needs to be allocated.
MiB = (1024 * 1024)
while delta < 1:
delta = getmaxrss_mb() - mem_before
# Take another 1MB of memory..
keep_alive.append(" " * MiB)
print "Allocated {0}MiB..".format(len(keep_alive))
return keep_alive
def test_mem_parse_giant_table():
# Note: this test really wants to be run by itself in a process since it
# measures the *max* rss of the whole program. If python allocates
# a large object which goes away, the test will lie to us. Hence,
# kick_maxrss().
alive = kick_maxrss()
# Note: this has been tested with 1M row, and it works but it's slow.
# 100krow makes the point.
N_ROWS = 100000
table = make_table(N_ROWS, 4)
mem_before = getmaxrss_mb()
n = 0
for row in find_trs(BytesIO(table)):
n += 1
used = getmaxrss_mb() - mem_before
assert_equal(N_ROWS, n)
# Check that we didn't use more than 1MB to parse the table.
assert_less_equal(used, 1)
def test_mem_generate_excel():
alive = kick_maxrss()
# Note: this has been tested with 1M row, and it works but it's slow.
# 100krow makes the point.
N_ROWS = 80000
table = make_table(N_ROWS, 4)
mem_before = getmaxrss_mb()
# outputter = ExcelOutput
outputter = ExceleratorOutput
# outputter = XlsxWriterOutput
with outputter("test/test_mem_generate_excel.xlsx") as xls:
n = 0
write_row = xls.add_sheet("hi")
for row in find_trs(BytesIO(table)):
write_row([e.text for e in row])
used = getmaxrss_mb() - mem_before
print "Used MB for 65krow:", used
# measured at <42 MB
assert used < 50, "Excessive memory usage"