def setUp(self): file_type = "xlsx" io = BytesIO() self.content = [["X", "Y", "Z"], [1, 11, 12], [2, 21, 22], [3, 31, 32], [4, 41, 42], [5, 51, 52]] sheet = pe.Sheet(pe.transpose(self.content), name_rows_by_column=0) sheet.save_to_memory(file_type, io) self.test_tuple = (file_type, io.getvalue())
def test_book_save_to_stringio(self): data = {"Sheet 1": [[1, 2, 3], [4, 5, 6]]} book = pe.Book(data) io = BytesIO() book.save_to_memory("xlsm", io) b = pe.load_book_from_memory("xlsm", io.getvalue()) result = [1, 2, 3, 4, 5, 6] actual = pe.utils.to_array(b[0].enumerate()) assert result == actual
def test_tsvz_output_stringio2(self): data = [[1, 2, 3], [4, 5, 6]] r = pe.Sheet(data) io = BytesIO() r.save_to_memory("tsvz", io) r = pe.load_from_memory("tsvz", io.getvalue()) result = ['1', '2', '3', '4', '5', '6'] actual = pe.utils.to_array(r.enumerate()) assert actual == result
def test_csvz_output_stringio(self): data = [[1, 2, 3], [4, 5, 6]] io = BytesIO() w = pe.Writer(("csvz", io)) w.write_rows(data) w.close() r = pe.Reader(("csvz", io.getvalue())) result = ['1', '2', '3', '4', '5', '6'] actual = pe.utils.to_array(r.enumerate()) assert actual == result
def test_book_output_stringio(self): data = {"Sheet 1": [[1, 2, 3], [4, 5, 6]]} io = BytesIO() w = pe.BookWriter(("xlsm", io)) w.write_book_from_dict(data) w.close() b = pe.load_book_from_memory("xlsm", io.getvalue()) result = [1, 2, 3, 4, 5, 6] actual = pe.utils.to_array(b[0].enumerate()) assert result == actual
def test_xlsm_output_stringio(self): data = [[1, 2, 3], [4, 5, 6]] io = BytesIO() w = pe.Writer(("xlsm", io)) w.write_rows(data) w.close() r = pe.load_from_memory("xlsm", io.getvalue()) result = [1, 2, 3, 4, 5, 6] actual = pe.utils.to_array(r.enumerate()) assert result == actual
def test_no_file_type(self): file_name = 'issue15' download_file_type = "xls" io = pe.save_as(dest_file_type="xls", array=self.data) if not PY2: if not isinstance(io, BytesIO): io = BytesIO(io.getvalue().encode('utf-8')) response = self.app.post('/switch/%s' % download_file_type, buffered=True, data={"file": (io, file_name)}, content_type="multipart/form-data") eq_(response.content_type, 'text/html') eq_(response.status_code, 400)
def setUp(self): self.data = { "1": [1, 2, 3, 4, 5, 6, 7, 8], "2": ["1.0", "2.0", "3.0", "4.0", "5.0", "6.0", "7.0", "8.0"], "3": [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8], "4": ["1.1", "2.2", "3.3", "4.4", "5.5", "6.6", "7.7", "8.8"], "5": [2, 3, 4, 5, 6, 7, 8, 9], "6": ["2", "3", "4", "5", "6", "7", "8", "9"] } self.io = BytesIO() w = pe.Writer(("xls", self.io)) w.write_dict(self.data) w.close() self.test_tuple = ("xls", self.io.getvalue())
def create_sample_file2_in_memory(file_type): """ 1,2,3,4 5,6,7,8 9,10,11,12 """ io = BytesIO() w = pe.Writer((file_type, io)) table = [] for i in [0, 4, 8]: array = [i + 1, i + 2, i + 3, i + 4] table.append(array) w.write_array(table) w.close() return io
def test_array(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"result": { 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 struct_type in test_sample.keys(): io = BytesIO() sheet = pe.Sheet(self.data) sheet.save_to_memory("xls", io) io.seek(0) response = self.app.post( "/respond/%s" % struct_type, buffered=True, data={"file": (io, "test.xls")}, content_type="multipart/form-data", ) expected = test_sample[struct_type] assert json.loads(response.data.decode("utf-8")) == expected
def test_download(self): for upload_file_type in FILE_TYPE_MIME_TABLE.keys(): file_name = 'test.%s' % upload_file_type for download_file_type in FILE_TYPE_MIME_TABLE.keys(): print("Uploading %s Downloading %s" % (upload_file_type, download_file_type)) io = pe.save_as(dest_file_type=upload_file_type, array=self.data) if not PY2: if not isinstance(io, BytesIO): io = BytesIO(io.getvalue().encode('utf-8')) response = self.app.post('/switch/%s' % download_file_type, buffered=True, data={"file": (io, file_name)}, content_type="multipart/form-data") assert response.content_type == FILE_TYPE_MIME_TABLE[download_file_type] sheet = pe.get_sheet(file_type=download_file_type, file_content=response.data) sheet.format(int) array = sheet.to_array() assert array == self.data
def test_book(self): test_sample = ["book", "book_dict"] expected = { 'result': { '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]], 'Sheet3': [['X', 'Y', 'Z'], [1.0, 4.0, 7.0], [2.0, 5.0, 8.0], [3.0, 6.0, 9.0]], '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 struct_type in test_sample: io = BytesIO() book = pe.Book(self.content) book.save_to_memory('xls', io) io.seek(0) response = self.app.post('/respond/%s' % struct_type, buffered=True, data={"file": (io, "test.xls")}, content_type="multipart/form-data") assert json.loads(response.data.decode('utf-8')) == expected
def test_array(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'result': { 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 struct_type in test_sample.keys(): io = BytesIO() sheet = pe.Sheet(self.data) sheet.save_to_memory('xls', io) io.seek(0) response = self.app.post('/respond/%s' % struct_type, buffered=True, data={"file": (io, "test.xls")}, content_type="multipart/form-data") expected = test_sample[struct_type] assert json.loads(response.data.decode('utf-8')) == expected