def test_book_writer_to_memroy(self): io = manager.get_io(self.file_type) w = self.writer_class() w.open(io, single_sheet_in_book=True) w.write({self.file_type: self.data}) w.close() content = io.getvalue().replace('\r', '') assert content.strip('\n') == self.result
def test_sheet_memory_reader(self): io = manager.get_io(self.file_type) with open(self.test_file, "r") as f: io.write(f.read()) io.seek(0) r = CSVinMemoryReader(NamedContent(self.file_type, io)) result = list(r.to_array()) self.assertEqual(result, self.expected_data)
def test_sheet_writer_to_memory(self): io = manager.get_io(self.file_type) w = CSVMemoryWriter(io, None, single_sheet_in_book=True) for row in self.data: w.write_row(row) w.close() content = io.getvalue().replace("\r", "") self.assertEqual(content.strip("\n"), self.result)
def test_sheet_memory_reader_delimiters(self): io = manager.get_io(self.file_type) with open(self.test_file, "r") as f: io.write(f.read()) io.seek(0) r = CSVinMemoryReader(NamedContent(self.file_type, io), delimiters=None) result = list(r.to_array()) self.assertEqual(result, [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
def test_multiple_sheet_into_memory(self): io = manager.get_io(self.file_type) w = self.writer_class() w.open(io, lineterminator="\n") w.write(self.sheets) w.close() content = io.getvalue() self.assertEqual(content, self.merged)
def test_multiple_sheet_into_memory(self): io = manager.get_io(self.file_type) w = self.writer_class() w.open(io, lineterminator='\n') w.write(self.sheets) w.close() content = io.getvalue() self.assertEqual(content, self.merged)
def test_book_writer_to_memroy(self): io = manager.get_io(self.file_type) w = self.writer_class() w.open(io, single_sheet_in_book=True) w.write({self.file_type: self.data}) w.close() content = io.getvalue().replace("\r", "") assert content.strip("\n") == self.result
def test_case_insentivity(): data = [["1", "2", "3"]] io = manager.get_io("CSV") # test default format for saving is 'csv' save_data(io, data) io.seek(0) # test default format for reading is 'csv' result = get_data(io) assert result["csv"] == [[1, 2, 3]]
def test_case_insentivity(): data = [['1', '2', '3']] io = manager.get_io("CSV") # test default format for saving is 'csv' save_data(io, data) io.seek(0) # test default format for reading is 'csv' result = get_data(io) assert result['csv'] == [[1, 2, 3]]
def test_default_csv_format(): data = [['1', '2', '3']] io = manager.get_io("csv") # test default format for saving is 'csv' save_data(io, data) io.seek(0) # test default format for reading is 'csv' result = get_data(io) assert result['csv'] == [[1, 2, 3]]
def test_book_reader_from_memory_source(self): io = manager.get_io(self.file_type) with open(self.test_file, 'r') as f: io.write(f.read()) io.seek(0) b = self.reader_class() b.open_stream(io) sheets = b.read_all() self.assertEqual(list(sheets[self.file_type]), self.expected_data)
def test_book_reader_from_memory_source(self): io = manager.get_io(self.file_type) with open(self.test_file, "r") as f: io.write(f.read()) io.seek(0) b = self.reader_class() b.open_stream(io) sheets = b.read_all() self.assertEqual(list(sheets[self.file_type]), self.expected_data)
def test_reading_from_memory_tsvz(): data = [[1, 2, 3]] io = manager.get_io("tsvz") zipbook = TSVZipBookWriter() zipbook.open_stream(io) zipbook.write({None: data}) zipbook.close() zipreader = TSVZipBookReader() zipreader.open_stream(io) data = zipreader.read_all() assert list(data['pyexcel_sheet1']) == [[1, 2, 3]]
def _convert_content_to_stream(file_content, file_type): io = manager.get_io(file_type) if PY2: io.write(file_content) else: if (isinstance(io, StringIO) and isinstance(file_content, bytes)): content = file_content.decode('utf-8') else: content = file_content io.write(content) io.seek(0) return io
def test_multiple_sheet_into_memory_2(self): """Write csv book into a single stream""" io = manager.get_io(self.file_type) w = self.writer_class() w.open(io, lineterminator="\n") w.write(self.sheets) w.close() reader = self.reader_class() reader.open_stream(io, lineterminator="\n", multiple_sheets=True) sheets = reader.read_all() for sheet in sheets: sheets[sheet] = list(sheets[sheet]) self.assertEqual(sheets, self.expected_sheets)
def test_multiple_sheet_into_memory_2(self): """Write csv book into a single stream""" io = manager.get_io(self.file_type) w = self.writer_class() w.open(io, lineterminator='\n') w.write(self.sheets) w.close() reader = self.reader_class() reader.open_stream(io, lineterminator='\n', multiple_sheets=True) sheets = reader.read_all() for sheet in sheets: sheets[sheet] = list(sheets[sheet]) self.assertEqual(sheets, self.expected_sheets)
def test_save_to(): file_type = 'csv' io = manager.get_io(file_type) g = (i for i in [[1, 2], [3, 4]]) ss = WriteSheetToMemory(file_type=file_type, file_stream=io, lineterminator='\n') sheet_stream = SheetStream("test", g) ss.write_data(sheet_stream) content = io.getvalue() expected = dedent("""\ 1,2 3,4 """) assert content == expected
def _convert_content_to_stream(file_content, file_type): stream = manager.get_io(file_type) target_content_type = manager.get_io_type(file_type) needs_encode = target_content_type == "bytes" and not isinstance( file_content, bytes ) needs_decode = target_content_type == "string" and isinstance( file_content, bytes ) if needs_encode: file_content = file_content.encode("utf-8") elif needs_decode: file_content = file_content.decode("utf-8") stream.write(file_content) stream.seek(0) return stream
def test_binary_file_content(): data = [['1', '2', '3']] io = manager.get_io("csvz") save_data(io, data, 'csvz') result = get_data(io.getvalue(), 'csvz') assert result['pyexcel_sheet1'] == [[1, 2, 3]]
def test_text_file_content(): data = [["1", "2", "3"]] io = manager.get_io("csv") save_data(io, data, "csv") result = get_data(io.getvalue(), "csv") eq_(result["csv"], [[1, 2, 3]])
def _convert_content_to_stream(file_content, file_type): stream = manager.get_io(file_type) stream.write(file_content) stream.seek(0) return stream
def test_text_file_content(): data = [['1', '2', '3']] io = manager.get_io("csv") save_data(io, data, 'csv') result = get_data(io.getvalue(), 'csv') assert result['csv'] == [[1, 2, 3]]
def test_library_parameter(): data = [['1', '2', '3']] io = manager.get_io("csv") save_data(io, data, 'csv', library="built-in") result = get_data(io.getvalue(), 'csv', library="built-in") assert result['csv'] == [[1, 2, 3]]
def test_library_parameter_error_situation(): data = [['1', '2', '3']] io = manager.get_io("csv") save_data(io, data, 'csv', library="doesnot-exist")
def test_library_parameter_error_situation(): data = [["1", "2", "3"]] io = manager.get_io("csv") save_data(io, data, "csv", library="doesnot-exist")
def test_get_io(): io = manager.get_io("hello") assert io is None
def test_binary_file_content(): data = [["1", "2", "3"]] io = manager.get_io("csvz") save_data(io, data, "csvz") result = get_data(io.getvalue(), "csvz") eq_(result["pyexcel_sheet1"], [[1, 2, 3]])
def get_io(self): return manager.get_io(self._file_type)
def test_library_parameter(): data = [["1", "2", "3"]] io = manager.get_io("csv") save_data(io, data, "csv", library="pyexcel-io") result = get_data(io.getvalue(), "csv", library="pyexcel-io") eq_(result["csv"], [[1, 2, 3]])
def test_library_parameter(): data = [['1', '2', '3']] io = manager.get_io("csv") save_data(io, data, 'csv', library="pyexcel-io") result = get_data(io.getvalue(), 'csv', library="pyexcel-io") eq_(result['csv'], [[1, 2, 3]])