def setUp(self): self.test_file_formatter = "csv_multiple__%s__%s." + self.file_type self.merged_book_file = "csv_multiple." + self.file_type self.data = [ ["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"] ] self.expected_data = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] self.sheets = OrderedDict() self.sheets.update({"sheet1": self.data}) self.sheets.update({"sheet2": self.data}) self.sheets.update({"sheet3": self.data}) self.expected_sheets = OrderedDict() self.expected_sheets.update({"sheet1": self.expected_data}) self.expected_sheets.update({"sheet2": self.expected_data}) self.expected_sheets.update({"sheet3": self.expected_data}) index = 0 for key, value in self.sheets.items(): file_name = self.test_file_formatter % (key, index) with open(file_name, 'w') as f: for row in value: f.write(self.delimiter.join(row) + "\n") index = index + 1
def setUp(self): self.content = OrderedDict() self.content.update( {"Sheet1": [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}) self.content.update( {"Sheet2": [[u'A', u'B', u'C'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}) self.result1 = [{ 'Y': 4, 'X': 1, 'Z': 7 }, { 'Y': 5, 'X': 2, 'Z': 8 }, { 'Y': 6, 'X': 3, 'Z': 9 }] self.result2 = [{ 'B': 4, 'A': 1, 'C': 7 }, { 'B': 5, 'A': 2, 'C': 8 }, { 'B': 6, 'A': 3, 'C': 9 }]
def read_all(self): """read all available sheets""" result = OrderedDict() for sheet in self._native_book.sheets: data_dict = self.read_sheet(sheet) result.update(data_dict) return result
def read_all(self): result = OrderedDict() for sheet in self._native_book: if self.skip_hidden_sheets and sheet.sheet_state == "hidden": continue data_dict = self.read_sheet(sheet) result.update(data_dict) return result
def setUp(self): self.content = OrderedDict() self.content.update( {'Sheet 1': [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]}) self.content.update( {'Sheet 2': [['X', 'Y', 'Z'], [1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]}) self.content.update( {'Sheet 3': [['O', 'P', 'Q'], [3.0, 2.0, 1.0], [4.0, 3.0, 2.0]]}) save_data(self.file_name, self.content)
def read_all(self): """ read everything from a excel data book """ result = OrderedDict() for sheet_index in range(len(self.reader)): content_dict = self.read_sheet_by_index(sheet_index) result.update(content_dict) return result
def read_all(self): result = OrderedDict() self._native_book = self._get_book() for sheet in self._native_book.sheets(): if self.__skip_hidden_sheets and sheet.visibility != 0: continue data_dict = self.read_sheet(sheet) result.update(data_dict) return result
def test_unknown_sheet(self): importer = SQLTableImporter(None) category_adapter = SQLTableImportAdapter(Category) category_adapter.column_names = [""] importer.append(category_adapter) writer = SQLBookWriter() writer.open_content(importer) to_store = OrderedDict() to_store.update({"you do not see me": [[]]}) writer.write(to_store)
def setUp(self): Base.metadata.drop_all(engine) Base.metadata.create_all(engine) self.session = Session() data = { "Category": [["id", "name"], [1, "News"], [2, "Sports"]], "Post": [ ["id", "title", "body", "pub_date", "category"], [ 1, "Title A", "formal", datetime.datetime(2015, 1, 20, 23, 28, 29), "News", ], [ 2, "Title B", "informal", datetime.datetime(2015, 1, 20, 23, 28, 30), "Sports", ], ], } def category_init_func(row): c = Category(row["name"]) c.id = row["id"] return c def post_init_func(row): c = ( self.session.query(Category) .filter_by(name=row["category"]) .first() ) p = Post(row["title"], row["body"], c, row["pub_date"]) return p importer = SQLTableImporter(self.session) category_adapter = SQLTableImportAdapter(Category) category_adapter.column_names = data["Category"][0] category_adapter.row_initializer = category_init_func importer.append(category_adapter) post_adapter = SQLTableImportAdapter(Post) post_adapter.column_names = data["Post"][0] post_adapter.row_initializer = post_init_func importer.append(post_adapter) writer = SQLBookWriter() writer.open_content(importer) to_store = OrderedDict() to_store.update({category_adapter.get_name(): data["Category"][1:]}) to_store.update({post_adapter.get_name(): data["Post"][1:]}) writer.write(to_store) writer.close()
def read_many(self, sheets): """ read everything from a excel data book """ result = OrderedDict() for sheet in sheets: if isinstance(sheet, int): result.update(self.read_sheet_by_index(sheet)) else: result.update(self.read_sheet_by_name(sheet)) return result
def setUp(self): self.content = OrderedDict() self.content.update( {"Sheet 1": [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]} ) self.content.update( {"Sheet 2": [["X", "Y", "Z"], [1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]} ) self.content.update( {"Sheet 3": [["O", "P", "Q"], [3.0, 2.0, 1.0], [4.0, 3.0, 2.0]]} ) save_data(self.file_name, self.content)
def parse_styles(style_content): styles = OrderedDict() formats = NUMBER_FMT_MATCHER.findall(style_content) for aformat in formats: partial = io.BytesIO(aformat) for action, element in etree.iterparse(partial, ("end", )): if element.tag != "numFmt": continue numFmtId = element.attrib.get("numFmtId") formatCode = element.attrib.get("formatCode") formatCode = formatCode.lower().replace("\\", "") styles.update({numFmtId: formatCode}) return styles
def setUp(self): self.content = OrderedDict() self.content.update({ 'Sheet 1': [ [1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0] ] }) self.content.update({ 'Sheet 2': [ ['X', 'Y', 'Z'], [1.0, 2.0, 3.0], [4.0, 5.0, 6.0] ] }) self.content.update({ 'Sheet 3': [ ['O', 'P', 'Q'], [3.0, 2.0, 1.0], [4.0, 3.0, 2.0] ] }) self.file="mybook.csvz" save_data(self.file, self.content)
class TestReadMultipleSheets(TestCase): file_type = "csv" delimiter = ',' def setUp(self): self.test_file_formatter = "csv_multiple__%s__%s." + self.file_type self.merged_book_file = "csv_multiple." + self.file_type self.data = [ ["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"] ] self.expected_data = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] self.sheets = OrderedDict() self.sheets.update({"sheet1": self.data}) self.sheets.update({"sheet2": self.data}) self.sheets.update({"sheet3": self.data}) self.expected_sheets = OrderedDict() self.expected_sheets.update({"sheet1": self.expected_data}) self.expected_sheets.update({"sheet2": self.expected_data}) self.expected_sheets.update({"sheet3": self.expected_data}) index = 0 for key, value in self.sheets.items(): file_name = self.test_file_formatter % (key, index) with open(file_name, 'w') as f: for row in value: f.write(self.delimiter.join(row) + "\n") index = index + 1 def test_sheet_name(self): sheets = get_data(self.merged_book_file, sheet_name="sheet1") self.assertEqual(sheets['sheet1'], self.expected_sheets['sheet1']) def test_sheet_index(self): sheets = get_data(self.merged_book_file, sheet_index=1) self.assertEqual(sheets['sheet2'], self.expected_sheets['sheet2']) def tearDown(self): index = 0 for key, value in self.sheets.items(): file_name = self.test_file_formatter % (key, index) os.unlink(file_name) index = index + 1
def read_all(self): """ read everything from a excel data book """ result = OrderedDict() for sheet in self._native_book: result[sheet.name] = self.read_sheet(sheet) return result
def read_all(self): """read all sheets""" result = OrderedDict() for sheet in self._native_book.spreadsheet.getElementsByType(Table): ods_sheet = ODSSheet(sheet, **self._keywords) result[ods_sheet.name] = ods_sheet.to_array() return result
def setUp(self): self.test_file_formatter = "csv_multiple__%s__%s." + self.file_type self.merged_book_file = "csv_multiple." + self.file_type self.data1 = [ ["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"] ] self.data2 = [ ["1", "2", "3"], ["4", "5", "6"], ["7", "8", "1000"] ] self.data3 = [ ["1", "2", "3"], ["4", "5", "6888"], ["7", "8", "9"] ] self.expected_data1 = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] self.expected_data2 = [ [1, 2, 3], [4, 5, 6], [7, 8, 1000] ] self.expected_data3 = [ [1, 2, 3], [4, 5, 6888], [7, 8, 9] ] self.sheets = OrderedDict() self.sheets.update({"sheet1": self.data1}) self.sheets.update({"sheet2": self.data2}) self.sheets.update({"sheet3": self.data3}) self.expected_sheets = OrderedDict() self.expected_sheets.update({"sheet1": self.expected_data1}) self.expected_sheets.update({"sheet2": self.expected_data2}) self.expected_sheets.update({"sheet3": self.expected_data3}) self.result_dict = OrderedDict() self.result_dict.update({"sheet1": self.result1}) self.result_dict.update({"sheet2": self.result2}) self.result_dict.update({"sheet3": self.result3})
def setUp(self): self.content = OrderedDict() self.content.update({ "Sheet1": [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}) self.content.update({ "Sheet2": [[u'A', u'B', u'C'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}) self.result1 = [{'Y': 4, 'X': 1, 'Z': 7}, {'Y': 5, 'X': 2, 'Z': 8}, {'Y': 6, 'X': 3, 'Z': 9}] self.result2 = [{'B': 4, 'A': 1, 'C': 7}, {'B': 5, 'A': 2, 'C': 8}, {'B': 6, 'A': 3, 'C': 9}]
def setUp(self): self.content = OrderedDict() self.content.update( {"Sheet1": [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}) self.content.update( {"Sheet2": [[u"A", u"B", u"C"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}) self.result1 = [ { "Y": 4, "X": 1, "Z": 7 }, { "Y": 5, "X": 2, "Z": 8 }, { "Y": 6, "X": 3, "Z": 9 }, ] self.result2 = [ { "B": 4, "A": 1, "C": 7 }, { "B": 5, "A": 2, "C": 8 }, { "B": 6, "A": 3, "C": 9 }, ]
def test_reading(): data = get_data( os.path.join("tests", "fixtures", "date_field.xlsb"), library="pyexcel-xlsbr", ) expected = OrderedDict() # expected.update({ # "Sheet1": [ # ['Date', 'Time'], # [datetime(year=2014, month=12, day=25), # time(hour=11, minute=11, second=11)], # [datetime(2014, 12, 26, 0, 0), # time(hour=12, minute=12, second=12)], # [datetime(2015, 1, 1, 0, 0), # time(hour=13, minute=13, second=13)], # [datetime(year=1899, month=12, day=30), # time(hour=0, minute=0)] # ]}) expected.update({ "Sheet1": [ ["Date", "Time"], [41998.0, 0.466099537037037], [41999.0, 0.508472222222222], [42005.0, 0.550844907407407], [0.0, 0.0], ] }) expected.update({"Sheet2": []}) expected.update({"Sheet3": []}) eq_(data, expected)
def test_reading(): data = get_data(os.path.join("tests", "fixtures", "date_field.xlsx"), library="pyexcel-xlsx", skip_hidden_row_and_column=False) expected = OrderedDict() expected.update({ "Sheet1": [ ["Date", "Time"], [ datetime(year=2014, month=12, day=25), time(hour=11, minute=11, second=11), ], [ datetime(2014, 12, 26, 0, 0), time(hour=12, minute=12, second=12), ], [ datetime(2015, 1, 1, 0, 0), time(hour=13, minute=13, second=13), ], [ datetime(year=1899, month=12, day=30), datetime(1899, 12, 30, 0, 0), ], ] }) expected.update({"Sheet2": []}) expected.update({"Sheet3": []}) eq_(data, expected)
def setUp(self): self.content = OrderedDict() self.content.update( {"Sheet1": [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]} ) self.content.update( {"Sheet2": [[u"A", u"B", u"C"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]} ) self.result1 = [ {"Y": 4, "X": 1, "Z": 7}, {"Y": 5, "X": 2, "Z": 8}, {"Y": 6, "X": 3, "Z": 9}, ] self.result2 = [ {"B": 4, "A": 1, "C": 7}, {"B": 5, "A": 2, "C": 8}, {"B": 6, "A": 3, "C": 9}, ]
def setUp(self): Base.metadata.drop_all(engine) Base.metadata.create_all(engine) self.session = Session() data = { "Category": [["id", "name"], [1, "News"], [2, "Sports"]], "Post": [ ["id", "title", "body", "pub_date", "category"], [ 1, "Title A", "formal", datetime.datetime(2015, 1, 20, 23, 28, 29), "News", ], [ 2, "Title B", "informal", datetime.datetime(2015, 1, 20, 23, 28, 30), "Sports", ], ], } def category_init_func(row): c = Category(row["name"]) c.id = row["id"] return c def post_init_func(row): c = (self.session.query(Category).filter_by( name=row["category"]).first()) p = Post(row["title"], row["body"], c, row["pub_date"]) return p importer = SQLTableImporter(self.session) category_adapter = SQLTableImportAdapter(Category) category_adapter.column_names = data["Category"][0] category_adapter.row_initializer = category_init_func importer.append(category_adapter) post_adapter = SQLTableImportAdapter(Post) post_adapter.column_names = data["Post"][0] post_adapter.row_initializer = post_init_func importer.append(post_adapter) writer = SQLBookWriter() writer.open_content(importer) to_store = OrderedDict() to_store.update({category_adapter.get_name(): data["Category"][1:]}) to_store.update({post_adapter.get_name(): data["Post"][1:]}) writer.write(to_store) writer.close()
class TestMultipleSheet(TestCase): file_name = "mybook.csvz" reader_class = CSVZipBookReader def setUp(self): self.content = OrderedDict() self.content.update( {"Sheet 1": [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]} ) self.content.update( {"Sheet 2": [["X", "Y", "Z"], [1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]} ) self.content.update( {"Sheet 3": [["O", "P", "Q"], [3.0, 2.0, 1.0], [4.0, 3.0, 2.0]]} ) save_data(self.file_name, self.content) def test_read_one_from_many_by_name(self): reader = self.reader_class() reader.open(self.file_name) sheets = reader.read_sheet_by_name("Sheet 1") self.assertEqual( list(sheets["Sheet 1"]), [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ) @raises(ValueError) def test_read_one_from_many_by_unknown_name(self): reader = self.reader_class() reader.open(self.file_name) reader.read_sheet_by_name("Sheet X") def test_read_one_from_many_by_index(self): reader = self.reader_class() reader.open(self.file_name) sheets = reader.read_sheet_by_index(0) self.assertEqual( list(sheets["Sheet 1"]), [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ) @raises(IndexError) def test_read_one_from_many_by_unknown_index(self): reader = self.reader_class() reader.open(self.file_name) reader.read_sheet_by_index(9999) def tearDown(self): os.unlink(self.file_name)
def setUp(self): self.test_file_formatter = "csv_multiple__%s__%s." + self.file_type self.merged_book_file = "csv_multiple." + self.file_type self.data1 = [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]] self.data2 = [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "1000"]] self.data3 = [["1", "2", "3"], ["4", "5", "6888"], ["7", "8", "9"]] self.expected_data1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] self.expected_data2 = [[1, 2, 3], [4, 5, 6], [7, 8, 1000]] self.expected_data3 = [[1, 2, 3], [4, 5, 6888], [7, 8, 9]] self.sheets = OrderedDict() self.sheets.update({"sheet1": self.data1}) self.sheets.update({"sheet2": self.data2}) self.sheets.update({"sheet3": self.data3}) self.expected_sheets = OrderedDict() self.expected_sheets.update({"sheet1": self.expected_data1}) self.expected_sheets.update({"sheet2": self.expected_data2}) self.expected_sheets.update({"sheet3": self.expected_data3}) self.result_dict = OrderedDict() self.result_dict.update({"sheet1": self.result1}) self.result_dict.update({"sheet2": self.result2}) self.result_dict.update({"sheet3": self.result3})
class TestReadMultipleSheets(TestCase): file_type = "csv" reader_class = CSVBookReader delimiter = "," def setUp(self): self.test_file_formatter = "csv_multiple__%s__%s." + self.file_type self.merged_book_file = "csv_multiple." + self.file_type self.data = [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]] self.expected_data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] self.sheets = OrderedDict() self.sheets.update({"sheet1": self.data}) self.sheets.update({"sheet2": self.data}) self.sheets.update({"sheet3": self.data}) self.expected_sheets = OrderedDict() self.expected_sheets.update({"sheet1": self.expected_data}) self.expected_sheets.update({"sheet2": self.expected_data}) self.expected_sheets.update({"sheet3": self.expected_data}) index = 0 for key, value in self.sheets.items(): file_name = self.test_file_formatter % (key, index) with open(file_name, "w") as f: for row in value: f.write(self.delimiter.join(row) + "\n") index = index + 1 def test_multiple_sheet(self): b = self.reader_class() b.open(self.merged_book_file) sheets = b.read_all() for key in sheets: sheets[key] = list(sheets[key]) self.assertEqual(sheets, self.expected_sheets) def test_read_one_from_many_by_name(self): b = self.reader_class() b.open(self.merged_book_file) sheets = b.read_sheet_by_name("sheet1") self.assertEqual( list(sheets["sheet1"]), self.expected_sheets["sheet1"] ) @raises(ValueError) def test_read_one_from_many_by_non_existent_name(self): b = self.reader_class() b.open(self.merged_book_file) b.read_sheet_by_name("notknown") def test_read_one_from_many_by_index(self): b = self.reader_class() b.open(self.merged_book_file) sheets = b.read_sheet_by_index(1) self.assertEqual( list(sheets["sheet2"]), self.expected_sheets["sheet2"] ) @raises(IndexError) def test_read_one_from_many_by_wrong_index(self): b = self.reader_class() b.open(self.merged_book_file) b.read_sheet_by_index(90) def tearDown(self): index = 0 for key, value in self.sheets.items(): file_name = self.test_file_formatter % (key, index) os.unlink(file_name) index = index + 1
class TestMultipleModels: def setUp(self): self.content = OrderedDict() self.content.update({ "Sheet1": [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}) self.content.update({ "Sheet2": [[u'A', u'B', u'C'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}) self.result1 = [{'Y': 4, 'X': 1, 'Z': 7}, {'Y': 5, 'X': 2, 'Z': 8}, {'Y': 6, 'X': 3, 'Z': 9}] self.result2 = [{'B': 4, 'A': 1, 'C': 7}, {'B': 5, 'A': 2, 'C': 8}, {'B': 6, 'A': 3, 'C': 9}] def test_save_to_more_models(self): sample_size = 10 model1 = FakeDjangoModel() model2 = FakeDjangoModel() importer = DjangoModelImporter() adapter1 = DjangoModelImportAdapter(model1) adapter1.column_names = self.content['Sheet1'][0] adapter2 = DjangoModelImportAdapter(model2) adapter2.column_names = self.content['Sheet2'][0] importer.append(adapter1) importer.append(adapter2) to_store = { adapter1.get_name(): self.content['Sheet1'][1:], adapter2.get_name(): self.content['Sheet2'][1:] } writer = DjangoBookWriter() writer.open_content(importer, batch_size=sample_size) writer.write(to_store) writer.close() assert model1.objects.objs == self.result1 assert model2.objects.objs == self.result2 assert model1.objects.batch_size == sample_size def test_reading_from_more_models(self): model1 = FakeDjangoModel() model2 = FakeDjangoModel() importer = DjangoModelImporter() adapter1 = DjangoModelImportAdapter(model1) adapter1.column_names = self.content['Sheet1'][0] adapter2 = DjangoModelImportAdapter(model2) adapter2.column_names = self.content['Sheet2'][0] importer.append(adapter1) importer.append(adapter2) to_store = { adapter1.get_name(): self.content['Sheet1'][1:], adapter2.get_name(): self.content['Sheet2'][1:] } save_data(importer, to_store, file_type=DB_DJANGO) assert model1.objects.objs == self.result1 assert model2.objects.objs == self.result2 model1._meta.model_name = "Sheet1" model2._meta.model_name = "Sheet2" model1._meta.update(["X", "Y", "Z"]) model2._meta.update(["A", "B", "C"]) exporter = DjangoModelExporter() adapter1 = DjangoModelExportAdapter(model1) adapter2 = DjangoModelExportAdapter(model2) exporter.append(adapter1) exporter.append(adapter2) reader = DjangoBookReader() reader.open_content(exporter) data = reader.read_all() for key in data.keys(): data[key] = list(data[key]) assert data == self.content def test_special_case_where_only_one_model_used(self): model1 = FakeDjangoModel() importer = DjangoModelImporter() adapter = DjangoModelImportAdapter(model1) adapter.column_names = self.content['Sheet1'][0] importer.append(adapter) to_store = { adapter.get_name(): self.content['Sheet1'][1:], "Sheet2": self.content['Sheet2'][1:] } save_data(importer, to_store, file_type=DB_DJANGO) assert model1.objects.objs == self.result1 model1._meta.model_name = "Sheet1" model1._meta.update(["X", "Y", "Z"]) exporter = DjangoModelExporter() adapter = DjangoModelExportAdapter(model1) exporter.append(adapter) reader = DjangoBookReader() reader.open_content(exporter) data = reader.read_all() assert list(data['Sheet1']) == self.content['Sheet1']
class TestWriteMultipleSheets(TestCase): file_type = "csv" writer_class = CSVBookWriter reader_class = CSVBookReader result1 = dedent( """ 1,2,3 4,5,6 7,8,9 """ ).strip("\n") result2 = dedent( """ 1,2,3 4,5,6 7,8,1000 """ ).strip("\n") result3 = dedent( """ 1,2,3 4,5,6888 7,8,9 """ ).strip("\n") merged = dedent( """\ ---pyexcel:sheet1--- 1,2,3 4,5,6 7,8,9 ---pyexcel--- ---pyexcel:sheet2--- 1,2,3 4,5,6 7,8,1000 ---pyexcel--- ---pyexcel:sheet3--- 1,2,3 4,5,6888 7,8,9 ---pyexcel--- """ ) def setUp(self): self.test_file_formatter = "csv_multiple__%s__%s." + self.file_type self.merged_book_file = "csv_multiple." + self.file_type self.data1 = [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]] self.data2 = [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "1000"]] self.data3 = [["1", "2", "3"], ["4", "5", "6888"], ["7", "8", "9"]] self.expected_data1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] self.expected_data2 = [[1, 2, 3], [4, 5, 6], [7, 8, 1000]] self.expected_data3 = [[1, 2, 3], [4, 5, 6888], [7, 8, 9]] self.sheets = OrderedDict() self.sheets.update({"sheet1": self.data1}) self.sheets.update({"sheet2": self.data2}) self.sheets.update({"sheet3": self.data3}) self.expected_sheets = OrderedDict() self.expected_sheets.update({"sheet1": self.expected_data1}) self.expected_sheets.update({"sheet2": self.expected_data2}) self.expected_sheets.update({"sheet3": self.expected_data3}) self.result_dict = OrderedDict() self.result_dict.update({"sheet1": self.result1}) self.result_dict.update({"sheet2": self.result2}) self.result_dict.update({"sheet3": self.result3}) def test_multiple_sheet(self): w = self.writer_class() w.open(self.merged_book_file) w.write(self.sheets) w.close() index = 0 for key, value in self.sheets.items(): file_name = self.test_file_formatter % (key, index) with open(file_name, "r") as f: content = f.read().replace("\r", "") assert content.strip("\n") == self.result_dict[key] index = index + 1 self.delete_files() 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_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 delete_files(self): index = 0 for key, value in self.sheets.items(): file_name = self.test_file_formatter % (key, index) os.unlink(file_name) index = index + 1
class TestWriteMultipleSheets(TestCase): file_type = "csv" writer_class = CSVBookWriter reader_class = CSVBookReader result1 = dedent(""" 1,2,3 4,5,6 7,8,9 """).strip('\n') result2 = dedent(""" 1,2,3 4,5,6 7,8,1000 """).strip('\n') result3 = dedent(""" 1,2,3 4,5,6888 7,8,9 """).strip('\n') merged = dedent("""\ ---pyexcel:sheet1--- 1,2,3 4,5,6 7,8,9 ---pyexcel--- ---pyexcel:sheet2--- 1,2,3 4,5,6 7,8,1000 ---pyexcel--- ---pyexcel:sheet3--- 1,2,3 4,5,6888 7,8,9 ---pyexcel--- """) def setUp(self): self.test_file_formatter = "csv_multiple__%s__%s." + self.file_type self.merged_book_file = "csv_multiple." + self.file_type self.data1 = [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]] self.data2 = [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "1000"]] self.data3 = [["1", "2", "3"], ["4", "5", "6888"], ["7", "8", "9"]] self.expected_data1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] self.expected_data2 = [[1, 2, 3], [4, 5, 6], [7, 8, 1000]] self.expected_data3 = [[1, 2, 3], [4, 5, 6888], [7, 8, 9]] self.sheets = OrderedDict() self.sheets.update({"sheet1": self.data1}) self.sheets.update({"sheet2": self.data2}) self.sheets.update({"sheet3": self.data3}) self.expected_sheets = OrderedDict() self.expected_sheets.update({"sheet1": self.expected_data1}) self.expected_sheets.update({"sheet2": self.expected_data2}) self.expected_sheets.update({"sheet3": self.expected_data3}) self.result_dict = OrderedDict() self.result_dict.update({"sheet1": self.result1}) self.result_dict.update({"sheet2": self.result2}) self.result_dict.update({"sheet3": self.result3}) def test_multiple_sheet(self): w = self.writer_class() w.open(self.merged_book_file) w.write(self.sheets) w.close() index = 0 for key, value in self.sheets.items(): file_name = self.test_file_formatter % (key, index) with open(file_name, 'r') as f: content = f.read().replace('\r', '') assert content.strip('\n') == self.result_dict[key] index = index + 1 self.delete_files() 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_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 delete_files(self): index = 0 for key, value in self.sheets.items(): file_name = self.test_file_formatter % (key, index) os.unlink(file_name) index = index + 1
class TestMultipleModels: def setUp(self): self.content = OrderedDict() self.content.update( {"Sheet1": [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}) self.content.update( {"Sheet2": [[u'A', u'B', u'C'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}) self.result1 = [{ 'Y': 4, 'X': 1, 'Z': 7 }, { 'Y': 5, 'X': 2, 'Z': 8 }, { 'Y': 6, 'X': 3, 'Z': 9 }] self.result2 = [{ 'B': 4, 'A': 1, 'C': 7 }, { 'B': 5, 'A': 2, 'C': 8 }, { 'B': 6, 'A': 3, 'C': 9 }] def test_save_to_more_models(self): sample_size = 10 model1 = FakeDjangoModel() model2 = FakeDjangoModel() importer = DjangoModelImporter() adapter1 = DjangoModelImportAdapter(model1) adapter1.column_names = self.content['Sheet1'][0] adapter2 = DjangoModelImportAdapter(model2) adapter2.column_names = self.content['Sheet2'][0] importer.append(adapter1) importer.append(adapter2) to_store = { adapter1.get_name(): self.content['Sheet1'][1:], adapter2.get_name(): self.content['Sheet2'][1:] } writer = DjangoBookWriter() writer.open_content(importer, batch_size=sample_size) writer.write(to_store) writer.close() assert model1.objects.objs == self.result1 assert model2.objects.objs == self.result2 assert model1.objects.batch_size == sample_size def test_reading_from_more_models(self): model1 = FakeDjangoModel() model2 = FakeDjangoModel() importer = DjangoModelImporter() adapter1 = DjangoModelImportAdapter(model1) adapter1.column_names = self.content['Sheet1'][0] adapter2 = DjangoModelImportAdapter(model2) adapter2.column_names = self.content['Sheet2'][0] importer.append(adapter1) importer.append(adapter2) to_store = { adapter1.get_name(): self.content['Sheet1'][1:], adapter2.get_name(): self.content['Sheet2'][1:] } save_data(importer, to_store, file_type=DB_DJANGO) assert model1.objects.objs == self.result1 assert model2.objects.objs == self.result2 model1._meta.model_name = "Sheet1" model2._meta.model_name = "Sheet2" model1._meta.update(["X", "Y", "Z"]) model2._meta.update(["A", "B", "C"]) exporter = DjangoModelExporter() adapter1 = DjangoModelExportAdapter(model1) adapter2 = DjangoModelExportAdapter(model2) exporter.append(adapter1) exporter.append(adapter2) reader = DjangoBookReader() reader.open_content(exporter) data = reader.read_all() for key in data.keys(): data[key] = list(data[key]) assert data == self.content def test_special_case_where_only_one_model_used(self): model1 = FakeDjangoModel() importer = DjangoModelImporter() adapter = DjangoModelImportAdapter(model1) adapter.column_names = self.content['Sheet1'][0] importer.append(adapter) to_store = { adapter.get_name(): self.content['Sheet1'][1:], "Sheet2": self.content['Sheet2'][1:] } save_data(importer, to_store, file_type=DB_DJANGO) assert model1.objects.objs == self.result1 model1._meta.model_name = "Sheet1" model1._meta.update(["X", "Y", "Z"]) exporter = DjangoModelExporter() adapter = DjangoModelExportAdapter(model1) exporter.append(adapter) reader = DjangoBookReader() reader.open_content(exporter) data = reader.read_all() assert list(data['Sheet1']) == self.content['Sheet1']
class TestMultipleModels: def setUp(self): self.content = OrderedDict() self.content.update( {"Sheet1": [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}) self.content.update( {"Sheet2": [[u"A", u"B", u"C"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}) self.result1 = [ { "Y": 4, "X": 1, "Z": 7 }, { "Y": 5, "X": 2, "Z": 8 }, { "Y": 6, "X": 3, "Z": 9 }, ] self.result2 = [ { "B": 4, "A": 1, "C": 7 }, { "B": 5, "A": 2, "C": 8 }, { "B": 6, "A": 3, "C": 9 }, ] def test_save_to_more_models(self): sample_size = 10 model1 = FakeDjangoModel() model2 = FakeDjangoModel() importer = DjangoModelImporter() adapter1 = DjangoModelImportAdapter(model1) adapter1.column_names = self.content["Sheet1"][0] adapter2 = DjangoModelImportAdapter(model2) adapter2.column_names = self.content["Sheet2"][0] importer.append(adapter1) importer.append(adapter2) to_store = { adapter1.get_name(): self.content["Sheet1"][1:], adapter2.get_name(): self.content["Sheet2"][1:], } writer = DjangoBookWriter() writer.open_content(importer, batch_size=sample_size) writer.write(to_store) writer.close() assert model1.objects.objs == self.result1 assert model2.objects.objs == self.result2 assert model1.objects.batch_size == sample_size def test_bulk_save_flag(self): sample_size = 10 model1 = FakeDjangoModel() model2 = FakeDjangoModel() importer = DjangoModelImporter() adapter1 = DjangoModelImportAdapter(model1) adapter1.column_names = self.content["Sheet1"][0] adapter2 = DjangoModelImportAdapter(model2) adapter2.column_names = self.content["Sheet2"][0] importer.append(adapter1) importer.append(adapter2) to_store = { adapter1.get_name(): self.content["Sheet1"][1:], adapter2.get_name(): self.content["Sheet2"][1:], } writer = DjangoBookWriter() writer.open_content(importer, batch_size=sample_size, bulk_save=False) writer.write(to_store) writer.close() assert model1.objects.objs == [] def test_reading_from_more_models(self): model1 = FakeDjangoModel() model2 = FakeDjangoModel() importer = DjangoModelImporter() adapter1 = DjangoModelImportAdapter(model1) adapter1.column_names = self.content["Sheet1"][0] adapter2 = DjangoModelImportAdapter(model2) adapter2.column_names = self.content["Sheet2"][0] importer.append(adapter1) importer.append(adapter2) to_store = { adapter1.get_name(): self.content["Sheet1"][1:], adapter2.get_name(): self.content["Sheet2"][1:], } save_data(importer, to_store, file_type=DB_DJANGO) assert model1.objects.objs == self.result1 assert model2.objects.objs == self.result2 model1._meta.model_name = "Sheet1" model2._meta.model_name = "Sheet2" model1._meta.update(["X", "Y", "Z"]) model2._meta.update(["A", "B", "C"]) exporter = DjangoModelExporter() adapter1 = DjangoModelExportAdapter(model1) adapter2 = DjangoModelExportAdapter(model2) exporter.append(adapter1) exporter.append(adapter2) reader = DjangoBookReader() reader.open_content(exporter) data = reader.read_all() for key in data.keys(): data[key] = list(data[key]) assert data == self.content @raises(Exception) def test_special_case_where_only_one_model_used(self): model1 = FakeDjangoModel() importer = DjangoModelImporter() adapter = DjangoModelImportAdapter(model1) adapter.column_names = self.content["Sheet1"][0] importer.append(adapter) to_store = { adapter.get_name(): self.content["Sheet1"][1:], "Sheet2": self.content["Sheet2"][1:], } save_data(importer, to_store, file_type=DB_DJANGO) assert model1.objects.objs == self.result1 model1._meta.model_name = "Sheet1" model1._meta.update(["X", "Y", "Z"]) exporter = DjangoModelExporter() adapter = DjangoModelExportAdapter(model1) exporter.append(adapter) reader = DjangoBookReader() reader.open_content(exporter) data = reader.read_all() assert list(data["Sheet1"]) == self.content["Sheet1"]
class TestMultipleModels: def setUp(self): self.content = OrderedDict() self.content.update( {"Sheet1": [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]} ) self.content.update( {"Sheet2": [[u"A", u"B", u"C"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]} ) self.result1 = [ {"Y": 4, "X": 1, "Z": 7}, {"Y": 5, "X": 2, "Z": 8}, {"Y": 6, "X": 3, "Z": 9}, ] self.result2 = [ {"B": 4, "A": 1, "C": 7}, {"B": 5, "A": 2, "C": 8}, {"B": 6, "A": 3, "C": 9}, ] def test_save_to_more_models(self): sample_size = 10 model1 = FakeDjangoModel() model2 = FakeDjangoModel() importer = DjangoModelImporter() adapter1 = DjangoModelImportAdapter(model1) adapter1.column_names = self.content["Sheet1"][0] adapter2 = DjangoModelImportAdapter(model2) adapter2.column_names = self.content["Sheet2"][0] importer.append(adapter1) importer.append(adapter2) to_store = { adapter1.get_name(): self.content["Sheet1"][1:], adapter2.get_name(): self.content["Sheet2"][1:], } writer = DjangoBookWriter() writer.open_content(importer, batch_size=sample_size) writer.write(to_store) writer.close() assert model1.objects.objs == self.result1 assert model2.objects.objs == self.result2 assert model1.objects.batch_size == sample_size def test_bulk_save_flag(self): sample_size = 10 model1 = FakeDjangoModel() model2 = FakeDjangoModel() importer = DjangoModelImporter() adapter1 = DjangoModelImportAdapter(model1) adapter1.column_names = self.content["Sheet1"][0] adapter2 = DjangoModelImportAdapter(model2) adapter2.column_names = self.content["Sheet2"][0] importer.append(adapter1) importer.append(adapter2) to_store = { adapter1.get_name(): self.content["Sheet1"][1:], adapter2.get_name(): self.content["Sheet2"][1:], } writer = DjangoBookWriter() writer.open_content(importer, batch_size=sample_size, bulk_save=False) writer.write(to_store) writer.close() assert model1.objects.objs == [] def test_reading_from_more_models(self): model1 = FakeDjangoModel() model2 = FakeDjangoModel() importer = DjangoModelImporter() adapter1 = DjangoModelImportAdapter(model1) adapter1.column_names = self.content["Sheet1"][0] adapter2 = DjangoModelImportAdapter(model2) adapter2.column_names = self.content["Sheet2"][0] importer.append(adapter1) importer.append(adapter2) to_store = { adapter1.get_name(): self.content["Sheet1"][1:], adapter2.get_name(): self.content["Sheet2"][1:], } save_data(importer, to_store, file_type=DB_DJANGO) assert model1.objects.objs == self.result1 assert model2.objects.objs == self.result2 model1._meta.model_name = "Sheet1" model2._meta.model_name = "Sheet2" model1._meta.update(["X", "Y", "Z"]) model2._meta.update(["A", "B", "C"]) exporter = DjangoModelExporter() adapter1 = DjangoModelExportAdapter(model1) adapter2 = DjangoModelExportAdapter(model2) exporter.append(adapter1) exporter.append(adapter2) reader = DjangoBookReader() reader.open_content(exporter) data = reader.read_all() for key in data.keys(): data[key] = list(data[key]) assert data == self.content @raises(Exception) def test_special_case_where_only_one_model_used(self): model1 = FakeDjangoModel() importer = DjangoModelImporter() adapter = DjangoModelImportAdapter(model1) adapter.column_names = self.content["Sheet1"][0] importer.append(adapter) to_store = { adapter.get_name(): self.content["Sheet1"][1:], "Sheet2": self.content["Sheet2"][1:], } save_data(importer, to_store, file_type=DB_DJANGO) assert model1.objects.objs == self.result1 model1._meta.model_name = "Sheet1" model1._meta.update(["X", "Y", "Z"]) exporter = DjangoModelExporter() adapter = DjangoModelExportAdapter(model1) exporter.append(adapter) reader = DjangoBookReader() reader.open_content(exporter) data = reader.read_all() assert list(data["Sheet1"]) == self.content["Sheet1"]
class TestMultipleSheet(TestCase): def setUp(self): self.content = OrderedDict() self.content.update({ 'Sheet 1': [ [1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0] ] }) self.content.update({ 'Sheet 2': [ ['X', 'Y', 'Z'], [1.0, 2.0, 3.0], [4.0, 5.0, 6.0] ] }) self.content.update({ 'Sheet 3': [ ['O', 'P', 'Q'], [3.0, 2.0, 1.0], [4.0, 3.0, 2.0] ] }) self.file="mybook.csvz" save_data(self.file, self.content) def test_read_one_from_many_by_name(self): reader = CSVZipBookReader() reader.open(self.file) sheets = reader.read_sheet_by_name("Sheet 1") self.assertEqual(list(sheets['Sheet 1']), [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) @raises(ValueError) def test_read_one_from_many_by_unknown_name(self): reader = CSVZipBookReader() reader.open(self.file) reader.read_sheet_by_name("Sheet X") def test_read_one_from_many_by_index(self): reader = CSVZipBookReader() reader.open(self.file) sheets = reader.read_sheet_by_index(0) self.assertEqual(list(sheets['Sheet 1']), [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) @raises(IndexError) def test_read_one_from_many_by_unknown_index(self): reader = CSVZipBookReader() reader.open(self.file) reader.read_sheet_by_index(9999) def tearDown(self): os.unlink(self.file)
class TestReadMultipleSheets(TestCase): file_type = "csv" reader_class = CSVBookReader delimiter = ',' def setUp(self): self.test_file_formatter = "csv_multiple__%s__%s." + self.file_type self.merged_book_file = "csv_multiple." + self.file_type self.data = [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]] self.expected_data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] self.sheets = OrderedDict() self.sheets.update({"sheet1": self.data}) self.sheets.update({"sheet2": self.data}) self.sheets.update({"sheet3": self.data}) self.expected_sheets = OrderedDict() self.expected_sheets.update({"sheet1": self.expected_data}) self.expected_sheets.update({"sheet2": self.expected_data}) self.expected_sheets.update({"sheet3": self.expected_data}) index = 0 for key, value in self.sheets.items(): file_name = self.test_file_formatter % (key, index) with open(file_name, 'w') as f: for row in value: f.write(self.delimiter.join(row) + "\n") index = index + 1 def test_multiple_sheet(self): b = self.reader_class() b.open(self.merged_book_file) sheets = b.read_all() for key in sheets: sheets[key] = list(sheets[key]) self.assertEqual(sheets, self.expected_sheets) def test_read_one_from_many_by_name(self): b = self.reader_class() b.open(self.merged_book_file) sheets = b.read_sheet_by_name("sheet1") self.assertEqual(list(sheets["sheet1"]), self.expected_sheets["sheet1"]) @raises(ValueError) def test_read_one_from_many_by_non_existent_name(self): b = self.reader_class() b.open(self.merged_book_file) b.read_sheet_by_name("notknown") def test_read_one_from_many_by_index(self): b = self.reader_class() b.open(self.merged_book_file) sheets = b.read_sheet_by_index(1) self.assertEqual(list(sheets["sheet2"]), self.expected_sheets["sheet2"]) @raises(IndexError) def test_read_one_from_many_by_wrong_index(self): b = self.reader_class() b.open(self.merged_book_file) b.read_sheet_by_index(90) def tearDown(self): index = 0 for key, value in self.sheets.items(): file_name = self.test_file_formatter % (key, index) os.unlink(file_name) index = index + 1
def read_all(self): result = OrderedDict() for sheet in self._native_book: data_dict = self.read_sheet(sheet) result.update(data_dict) return result