-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
testResponse.py
419 lines (388 loc) · 18.1 KB
/
testResponse.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
# -*- coding: utf-8 -*-
import os
import sys
import json
from textwrap import dedent
from polls.models import Question, Choice
from django.test import Client, TestCase
from django.test.utils import override_settings
import pyexcel as pe
from django_excel._compact import urllib_quote
from django_excel import ExcelInMemoryUploadedFile
from nose.tools import eq_
PY2 = sys.version_info[0] == 2
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
from ordereddict import OrderedDict
else:
from collections import OrderedDict
if PY2:
from StringIO import StringIO
else:
from io import StringIO
_XLSX_MIME = "application/" + "vnd.openxmlformats-officedocument.spreadsheetml.sheet"
FILE_TYPE_MIME_TABLE = {
"csv": "text/csv",
"tsv": "text/tab-separated-values",
"csvz": "application/zip",
"tsvz": "application/zip",
"ods": "application/vnd.oasis.opendocument.spreadsheet",
"xls": "application/vnd.ms-excel",
"xlsx": _XLSX_MIME,
"xlsm": "application/vnd.ms-excel.sheet.macroenabled.12",
}
class ExcelResponseTestCase(TestCase):
def setUp(self):
self.client = Client()
self.data = [[1, 2, 3], [4, 5, 6]]
self.single_sheet = [["X", "Y", "Z"], [1, 2, 3], [4, 5, 6]]
self.book_content = OrderedDict()
self.book_content.update({"Sheet1": [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]})
self.book_content.update({"Sheet2": [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]]})
self.book_content.update(
{"Sheet3": [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}
)
def test_download(self):
for file_type in FILE_TYPE_MIME_TABLE.keys():
response = self.client.get("/polls/download/" + file_type)
assert response["Content-Type"] == FILE_TYPE_MIME_TABLE[file_type]
sheet = pe.get_sheet(file_type=file_type, file_content=response.content)
sheet.format(int)
array = sheet.to_array()
assert array == self.data
def test_download_attachment_with_ascii_name(self):
test_file_name = "test"
self._download_and_verify_file_name(test_file_name)
def test_download_attachment_with_unicode_name(self):
test_file_name = u"中文文件名"
self._download_and_verify_file_name(test_file_name.encode("utf-8"))
def test_download_attachment_with_unicode_name_as_string(self):
test_file_name = "中文文件名"
self._download_and_verify_file_name(test_file_name)
def _download_and_verify_file_name(self, file_name):
for file_type in FILE_TYPE_MIME_TABLE.keys():
url_encoded_file_name = urllib_quote(file_name)
response = self.client.get(
(
"/polls/download_attachment/%s/%s"
% (file_type, url_encoded_file_name)
)
)
assert response["Content-Type"] == FILE_TYPE_MIME_TABLE[file_type]
assert response["Content-Disposition"] == (
"attachment; filename=%s.%s;filename*=utf-8''%s.%s"
% (url_encoded_file_name, file_type, url_encoded_file_name, file_type)
)
sheet = pe.get_sheet(file_type=file_type, file_content=response.content)
sheet.format(int)
array = sheet.to_array()
assert array == self.data
def test_parse_single_sheet(self):
test_sample = {
"array": {
u"result": [[u"X", u"Y", u"Z"], [1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
},
"dict": {u"Y": [2.0, 5.0], u"X": [1.0, 4.0], u"Z": [3.0, 6.0]},
"records": {
u"result": [
{u"Y": 2.0, u"X": 1.0, u"Z": 3.0},
{u"Y": 5.0, u"X": 4.0, u"Z": 6.0},
]
},
}
for data_struct_type in test_sample.keys():
sheet = pe.Sheet(self.single_sheet)
tmp_filename = "test.file.xls"
sheet.save_as(tmp_filename)
with open(tmp_filename, "rb") as fp:
response = self.client.post(
"/polls/parse/" + data_struct_type, data={"file": fp}
)
self.assertEqual(
json.loads(response.content.decode("utf-8")),
test_sample[data_struct_type],
)
os.unlink(tmp_filename)
def test_parse_book(self):
test_sample = ["book", "book_dict"]
expected_dict = {
u"Sheet1": [
[1.0, 1.0, 1.0, 1.0],
[2.0, 2.0, 2.0, 2.0],
[3.0, 3.0, 3.0, 3.0],
],
u"Sheet3": [
[u"X", u"Y", u"Z"],
[1.0, 4.0, 7.0],
[2.0, 5.0, 8.0],
[3.0, 6.0, 9.0],
],
u"Sheet2": [
[4.0, 4.0, 4.0, 4.0],
[5.0, 5.0, 5.0, 5.0],
[6.0, 6.0, 6.0, 6.0],
],
}
for data_struct_type in test_sample:
sheet = pe.Book(self.book_content)
tmp_filename = "test.xls"
sheet.save_as(tmp_filename)
with open(tmp_filename, "rb") as fp:
response = self.client.post(
"/polls/parse/" + data_struct_type, data={"file": fp}
)
self.assertEqual(
json.loads(response.content.decode("utf-8")), expected_dict
)
os.unlink(tmp_filename)
def test_exchange(self):
for in_file_type in FILE_TYPE_MIME_TABLE.keys():
sheet = pe.Sheet(self.data)
tmp_filename = "test.%s" % in_file_type
sheet.save_as(tmp_filename)
for file_type in FILE_TYPE_MIME_TABLE.keys():
with open(tmp_filename, "rb") as fp:
response = self.client.post(
"/polls/exchange/" + file_type, data={"file": fp}
)
self.assertEqual(
response["Content-Type"], FILE_TYPE_MIME_TABLE[file_type]
)
sheet = pe.get_sheet(
file_type=file_type, file_content=response.content
)
sheet.format(int)
array = sheet.to_array()
assert array == self.data
os.unlink(tmp_filename)
class DatabaseOperationsTestCase(TestCase):
def setUp(self):
self.testfile = "sample-data.xls"
self.maxDiff = None
Question.objects.all().delete()
Choice.objects.all().delete()
def testBook(self):
fp = open(self.testfile, "rb")
response = self.client.post("/polls/import/", data={"file": fp})
eq_(response.status_code, 302)
response2 = self.client.get("/polls/export/book")
assert response2.status_code == 200
book = pe.get_book(file_type="xls", file_content=response2.content)
content = dedent(
"""
question:
+----+---------------------------+----------------------------------------------+----------+
| id | pub_date | question_text | slug |
+----+---------------------------+----------------------------------------------+----------+
| 1 | 2015-01-28T00:00:00+00:00 | What is your favourite programming language? | language |
+----+---------------------------+----------------------------------------------+----------+
| 2 | 2015-01-29T00:00:00+00:00 | What is your favourite IDE? | ide |
+----+---------------------------+----------------------------------------------+----------+
choice:
+---------------+----+-------------+-------+
| choice_text | id | question_id | votes |
+---------------+----+-------------+-------+
| Java | 1 | 1 | 0 |
+---------------+----+-------------+-------+
| C++ | 2 | 1 | 0 |
+---------------+----+-------------+-------+
| C | 3 | 1 | 0 |
+---------------+----+-------------+-------+
| Eclipse | 4 | 2 | 0 |
+---------------+----+-------------+-------+
| Visual Studio | 5 | 2 | 0 |
+---------------+----+-------------+-------+
| PyCharm | 6 | 2 | 0 |
+---------------+----+-------------+-------+
| IntelliJ | 7 | 2 | 0 |
+---------------+----+-------------+-------+"""
).strip(
"\n"
) # noqa
self.assertEqual(str(book), content)
def testBookWithoutBulkSave(self):
fp = open(self.testfile, "rb")
response = self.client.post("/polls/import/", data={"file": fp})
eq_(response.status_code, 302)
response2 = self.client.get("/polls/export/book")
assert response2.status_code == 200
book = pe.get_book(file_type="xls", file_content=response2.content)
content = dedent(
"""
question:
+----+---------------------------+----------------------------------------------+----------+
| id | pub_date | question_text | slug |
+----+---------------------------+----------------------------------------------+----------+
| 1 | 2015-01-28T00:00:00+00:00 | What is your favourite programming language? | language |
+----+---------------------------+----------------------------------------------+----------+
| 2 | 2015-01-29T00:00:00+00:00 | What is your favourite IDE? | ide |
+----+---------------------------+----------------------------------------------+----------+
choice:
+---------------+----+-------------+-------+
| choice_text | id | question_id | votes |
+---------------+----+-------------+-------+
| Java | 1 | 1 | 0 |
+---------------+----+-------------+-------+
| C++ | 2 | 1 | 0 |
+---------------+----+-------------+-------+
| C | 3 | 1 | 0 |
+---------------+----+-------------+-------+
| Eclipse | 4 | 2 | 0 |
+---------------+----+-------------+-------+
| Visual Studio | 5 | 2 | 0 |
+---------------+----+-------------+-------+
| PyCharm | 6 | 2 | 0 |
+---------------+----+-------------+-------+
| IntelliJ | 7 | 2 | 0 |
+---------------+----+-------------+-------+"""
).strip(
"\n"
) # noqa
self.assertEqual(str(book), content)
def testBookUsingIsave(self):
fp = open(self.testfile, "rb")
response = self.client.post("/polls/import_using_isave/", data={"file": fp})
eq_(response.status_code, 302)
response2 = self.client.get("/polls/export/book")
assert response2.status_code == 200
book = pe.get_book(file_type="xls", file_content=response2.content)
content = dedent(
"""
question:
+----+---------------------------+----------------------------------------------+----------+
| id | pub_date | question_text | slug |
+----+---------------------------+----------------------------------------------+----------+
| 1 | 2015-01-28T00:00:00+00:00 | What is your favourite programming language? | language |
+----+---------------------------+----------------------------------------------+----------+
| 2 | 2015-01-29T00:00:00+00:00 | What is your favourite IDE? | ide |
+----+---------------------------+----------------------------------------------+----------+
choice:
+---------------+----+-------------+-------+
| choice_text | id | question_id | votes |
+---------------+----+-------------+-------+
| Java | 1 | 1 | 0 |
+---------------+----+-------------+-------+
| C++ | 2 | 1 | 0 |
+---------------+----+-------------+-------+
| C | 3 | 1 | 0 |
+---------------+----+-------------+-------+
| Eclipse | 4 | 2 | 0 |
+---------------+----+-------------+-------+
| Visual Studio | 5 | 2 | 0 |
+---------------+----+-------------+-------+
| PyCharm | 6 | 2 | 0 |
+---------------+----+-------------+-------+
| IntelliJ | 7 | 2 | 0 |
+---------------+----+-------------+-------+"""
).strip(
"\n"
) # noqa
self.assertEqual(str(book), content)
def testSheet(self):
fp = open(self.testfile, "rb")
response = self.client.post("/polls/import/", data={"file": fp})
eq_(response.status_code, 302)
response2 = self.client.get("/polls/export/sheet")
assert response2.status_code == 200
sheet = pe.get_sheet(file_type="xls", file_content=response2.content)
content = dedent(
"""
question:
+----+---------------------------+----------------------------------------------+----------+
| id | pub_date | question_text | slug |
+----+---------------------------+----------------------------------------------+----------+
| 1 | 2015-01-28T00:00:00+00:00 | What is your favourite programming language? | language |
+----+---------------------------+----------------------------------------------+----------+
| 2 | 2015-01-29T00:00:00+00:00 | What is your favourite IDE? | ide |
+----+---------------------------+----------------------------------------------+----------+"""
).strip(
"\n"
) # noqa
assert str(sheet) == content
def testImportSheet(self):
fp = open("sample-sheet.xls", "rb")
response = self.client.post("/polls/import_sheet/", data={"file": fp})
eq_(response.status_code, 200)
response2 = self.client.get("/polls/export/sheet")
eq_(response2.status_code, 200)
sheet = pe.get_sheet(file_type="xls", file_content=response2.content)
content = dedent(
"""
question:
+----+---------------------------+----------------------------------------------+----------+
| id | pub_date | question_text | slug |
+----+---------------------------+----------------------------------------------+----------+
| 1 | 2015-01-28T00:00:00+00:00 | What is your favourite programming language? | language |
+----+---------------------------+----------------------------------------------+----------+
| 2 | 2015-01-29T00:00:00+00:00 | What is your favourite IDE? | ide |
+----+---------------------------+----------------------------------------------+----------+"""
).strip(
"\n"
) # noqa
self.assertEqual(str(sheet), content)
def testImportSheetUsingISave(self):
fp = open("sample-sheet-for-isave.xls", "rb")
response = self.client.post(
"/polls/import_sheet_using_isave/", data={"file": fp}
)
eq_(response.status_code, 200)
response2 = self.client.get("/polls/export/sheet")
eq_(response2.status_code, 200)
sheet = pe.get_sheet(file_type="xls", file_content=response2.content)
content = dedent(
"""
question:
+----+---------------------------+----------------------------------------------+----------+
| id | pub_date | question_text | slug |
+----+---------------------------+----------------------------------------------+----------+
| 1 | 2015-01-28T00:00:00+00:00 | What is your favourite programming language? | language |
+----+---------------------------+----------------------------------------------+----------+
| 2 | 2015-01-29T00:00:00+00:00 | What is your favourite IDE? | ide |
+----+---------------------------+----------------------------------------------+----------+"""
).strip(
"\n"
) # noqa
self.assertEqual(str(sheet), content)
def testCustomExport(self):
fp = open(self.testfile, "rb")
response = self.client.post("/polls/import/", data={"file": fp})
eq_(response.status_code, 302)
response2 = self.client.get("/polls/export/custom")
eq_(response2.status_code, 200)
sheet = pe.get_sheet(file_type="xls", file_content=response2.content)
content = dedent(
"""
pyexcel_sheet1:
+---------------+----+-------+
| choice_text | id | votes |
+---------------+----+-------+
| Eclipse | 4 | 0 |
+---------------+----+-------+
| Visual Studio | 5 | 0 |
+---------------+----+-------+
| PyCharm | 6 | 0 |
+---------------+----+-------+
| IntelliJ | 7 | 0 |
+---------------+----+-------+"""
).strip("\n")
self.assertEqual(str(sheet), content)
@override_settings(FILE_UPLOAD_MAX_MEMORY_SIZE=1)
class ExcelResponseUsingFileTestCase(ExcelResponseTestCase):
pass
@override_settings(FILE_UPLOAD_MAX_MEMORY_SIZE=1)
class DatabaseOperationsUsingFileTestCase(DatabaseOperationsTestCase):
pass
class TestUploadedFile(TestCase):
def test_in_memory_file(self):
test_content = "a,b,c"
strio = StringIO(test_content)
strio.read()
in_memory_file = ExcelInMemoryUploadedFile(
file=strio,
field_name="test",
name="test_file",
content_type="text",
size=3,
charset=None,
)
params = in_memory_file.get_params()
eq_(params["file_content"], test_content)