debian-python-pyexcel-ods/tests/test_multiple_sheets.py

247 lines
7.6 KiB
Python
Raw Normal View History

2014-10-13 00:35:04 +02:00
import os
import sys
import pyexcel
2016-10-24 23:26:43 +02:00
from base import PyexcelMultipleSheetBase
2019-11-10 17:30:00 +01:00
from nose.tools import raises
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
from ordereddict import OrderedDict
else:
from collections import OrderedDict
2014-10-13 00:35:04 +02:00
class TestOdsNxlsMultipleSheets(PyexcelMultipleSheetBase):
def setUp(self):
self.testfile = "multiple1.ods"
self.testfile2 = "multiple1.xls"
2016-06-09 00:43:46 +02:00
self.content = _produce_ordered_dict()
2014-10-13 00:35:04 +02:00
self._write_test_file(self.testfile)
def tearDown(self):
self._clean_up()
class TestXlsNOdsMultipleSheets(PyexcelMultipleSheetBase):
def setUp(self):
self.testfile = "multiple1.xls"
self.testfile2 = "multiple1.ods"
2016-06-09 00:43:46 +02:00
self.content = _produce_ordered_dict()
2014-10-13 00:35:04 +02:00
self._write_test_file(self.testfile)
def tearDown(self):
self._clean_up()
class TestAddBooks:
def _write_test_file(self, file):
"""
Make a test file as:
1,1,1,1
2,2,2,2
3,3,3,3
"""
self.rows = 3
pyexcel.save_book_as(bookdict=self.content, dest_file_name=file)
2014-10-13 00:35:04 +02:00
def setUp(self):
self.testfile = "multiple1.ods"
self.testfile2 = "multiple1.xls"
2016-06-09 00:43:46 +02:00
self.content = _produce_ordered_dict()
2014-10-13 00:35:04 +02:00
self._write_test_file(self.testfile)
self._write_test_file(self.testfile2)
def test_load_a_single_sheet(self):
2016-06-09 09:41:07 +02:00
b1 = pyexcel.get_book(file_name=self.testfile, sheet_name="Sheet1")
assert len(b1.sheet_names()) == 1
assert b1["Sheet1"].to_array() == self.content["Sheet1"]
def test_load_a_single_sheet2(self):
b1 = pyexcel.load_book(self.testfile, sheet_index=0)
assert len(b1.sheet_names()) == 1
assert b1["Sheet1"].to_array() == self.content["Sheet1"]
2015-02-19 10:08:47 +01:00
@raises(IndexError)
2015-02-19 09:48:37 +01:00
def test_load_a_single_sheet3(self):
2016-10-24 23:26:43 +02:00
pyexcel.get_book(file_name=self.testfile, sheet_index=10000)
2016-06-09 00:43:46 +02:00
2015-02-19 09:48:37 +01:00
@raises(ValueError)
def test_load_a_single_sheet4(self):
2016-10-24 23:26:43 +02:00
pyexcel.get_book(file_name=self.testfile, sheet_name="Not exist")
2015-02-19 09:48:37 +01:00
2014-10-13 00:35:04 +02:00
def test_delete_sheets(self):
2014-11-20 00:54:44 +01:00
b1 = pyexcel.load_book(self.testfile)
2014-10-13 00:35:04 +02:00
assert len(b1.sheet_names()) == 3
del b1["Sheet1"]
assert len(b1.sheet_names()) == 2
try:
del b1["Sheet1"]
2016-06-09 00:43:46 +02:00
assert 1 == 2
2014-10-13 00:35:04 +02:00
except KeyError:
2016-06-09 00:43:46 +02:00
assert 1 == 1
2014-10-13 00:35:04 +02:00
del b1[1]
assert len(b1.sheet_names()) == 1
try:
del b1[1]
2016-06-09 00:43:46 +02:00
assert 1 == 2
2014-10-13 00:35:04 +02:00
except IndexError:
2016-06-09 00:43:46 +02:00
assert 1 == 1
2014-10-13 00:35:04 +02:00
def test_delete_sheets2(self):
"""repetitively delete first sheet"""
2014-11-20 00:54:44 +01:00
b1 = pyexcel.load_book(self.testfile)
2014-10-13 00:35:04 +02:00
del b1[0]
assert len(b1.sheet_names()) == 2
del b1[0]
assert len(b1.sheet_names()) == 1
del b1[0]
assert len(b1.sheet_names()) == 0
2016-06-09 00:43:46 +02:00
2014-10-13 00:35:04 +02:00
def test_add_book1(self):
"""
test this scenario: book3 = book1 + book2
"""
b1 = pyexcel.get_book(file_name=self.testfile)
b2 = pyexcel.get_book(file_name=self.testfile2)
2014-10-13 00:35:04 +02:00
b3 = b1 + b2
2016-10-24 22:35:03 +02:00
content = b3.dict
2014-10-13 00:35:04 +02:00
sheet_names = content.keys()
assert len(sheet_names) == 6
for name in sheet_names:
if "Sheet3" in name:
assert content[name] == self.content["Sheet3"]
elif "Sheet2" in name:
assert content[name] == self.content["Sheet2"]
elif "Sheet1" in name:
assert content[name] == self.content["Sheet1"]
2016-06-09 00:43:46 +02:00
2014-10-13 00:35:04 +02:00
def test_add_book1_in_place(self):
"""
2016-06-09 09:41:07 +02:00
test this scenario: book1 += book2
2014-10-13 00:35:04 +02:00
"""
b1 = pyexcel.BookReader(self.testfile)
b2 = pyexcel.BookReader(self.testfile2)
b1 += b2
2016-10-24 22:35:03 +02:00
content = b1.dict
2014-10-13 00:35:04 +02:00
sheet_names = content.keys()
assert len(sheet_names) == 6
for name in sheet_names:
if "Sheet3" in name:
assert content[name] == self.content["Sheet3"]
elif "Sheet2" in name:
assert content[name] == self.content["Sheet2"]
elif "Sheet1" in name:
assert content[name] == self.content["Sheet1"]
def test_add_book2(self):
"""
2016-06-09 09:41:07 +02:00
test this scenario: book3 = book1 + sheet3
2014-10-13 00:35:04 +02:00
"""
b1 = pyexcel.BookReader(self.testfile)
b2 = pyexcel.BookReader(self.testfile2)
b3 = b1 + b2["Sheet3"]
2016-10-24 22:35:03 +02:00
content = b3.dict
2014-10-13 00:35:04 +02:00
sheet_names = content.keys()
assert len(sheet_names) == 4
for name in sheet_names:
if "Sheet3" in name:
assert content[name] == self.content["Sheet3"]
elif "Sheet2" in name:
assert content[name] == self.content["Sheet2"]
elif "Sheet1" in name:
assert content[name] == self.content["Sheet1"]
def test_add_book2_in_place(self):
"""
2016-06-09 09:41:07 +02:00
test this scenario: book3 = book1 + sheet3
2014-10-13 00:35:04 +02:00
"""
b1 = pyexcel.BookReader(self.testfile)
b2 = pyexcel.BookReader(self.testfile2)
b1 += b2["Sheet3"]
2016-10-24 22:35:03 +02:00
content = b1.dict
2014-10-13 00:35:04 +02:00
sheet_names = content.keys()
assert len(sheet_names) == 4
for name in sheet_names:
if "Sheet3" in name:
assert content[name] == self.content["Sheet3"]
elif "Sheet2" in name:
assert content[name] == self.content["Sheet2"]
elif "Sheet1" in name:
assert content[name] == self.content["Sheet1"]
def test_add_book3(self):
"""
2016-06-09 09:41:07 +02:00
test this scenario: book3 = sheet1 + sheet2
2014-10-13 00:35:04 +02:00
"""
b1 = pyexcel.BookReader(self.testfile)
b2 = pyexcel.BookReader(self.testfile2)
b3 = b1["Sheet1"] + b2["Sheet3"]
2016-10-24 22:35:03 +02:00
content = b3.dict
2014-10-13 00:35:04 +02:00
sheet_names = content.keys()
assert len(sheet_names) == 2
assert content["Sheet3"] == self.content["Sheet3"]
assert content["Sheet1"] == self.content["Sheet1"]
2016-06-09 00:43:46 +02:00
2014-10-13 00:35:04 +02:00
def test_add_book4(self):
"""
2016-06-09 09:41:07 +02:00
test this scenario: book3 = sheet1 + book
2014-10-13 00:35:04 +02:00
"""
b1 = pyexcel.BookReader(self.testfile)
b2 = pyexcel.BookReader(self.testfile2)
b3 = b1["Sheet1"] + b2
2016-10-24 22:35:03 +02:00
content = b3.dict
2014-10-13 00:35:04 +02:00
sheet_names = content.keys()
assert len(sheet_names) == 4
for name in sheet_names:
if "Sheet3" in name:
assert content[name] == self.content["Sheet3"]
elif "Sheet2" in name:
assert content[name] == self.content["Sheet2"]
elif "Sheet1" in name:
assert content[name] == self.content["Sheet1"]
def test_add_book_error(self):
"""
test this scenario: book3 = sheet1 + book
"""
b1 = pyexcel.BookReader(self.testfile)
try:
b1 + 12
2016-06-09 00:43:46 +02:00
assert 1 == 2
2014-10-13 00:35:04 +02:00
except TypeError:
2016-06-09 00:43:46 +02:00
assert 1 == 1
2014-10-13 00:35:04 +02:00
try:
b1 += 12
2016-06-09 00:43:46 +02:00
assert 1 == 2
2014-10-13 00:35:04 +02:00
except TypeError:
2016-06-09 00:43:46 +02:00
assert 1 == 1
2014-10-13 00:35:04 +02:00
def tearDown(self):
if os.path.exists(self.testfile):
os.unlink(self.testfile)
if os.path.exists(self.testfile2):
os.unlink(self.testfile2)
class TestMultiSheetReader:
def setUp(self):
self.testfile = "file_with_an_empty_sheet.ods"
def test_reader_with_correct_sheets(self):
r = pyexcel.BookReader(
os.path.join("tests", "fixtures", self.testfile)
)
2014-10-13 00:35:04 +02:00
assert r.number_of_sheets() == 3
2016-06-09 00:43:46 +02:00
def _produce_ordered_dict():
data_dict = OrderedDict()
data_dict.update({"Sheet1": [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]})
data_dict.update({"Sheet2": [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]]})
data_dict.update(
{"Sheet3": [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}
)
2016-06-09 00:43:46 +02:00
return data_dict