Skip to content

Commit 04c9c84

Browse files
committed
🔬 really test pyexcel-libxlsxw and implement write datetime
1 parent 72848c2 commit 04c9c84

File tree

6 files changed

+75
-38
lines changed

6 files changed

+75
-38
lines changed

pyexcel_libxlsxw/xlsxw.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
:copyright: (c) 2016-2020 by Onni Software Ltd & its contributors
88
:license: New BSD License
99
"""
10+
from datetime import date, time, datetime
11+
1012
from libxlsxwpy import Book
1113
from pyexcel_io.plugin_api import IWriter, ISheetWriter
1214

@@ -29,6 +31,39 @@ def write_row(self, array):
2931
self.xlsx_sheet.write_number(self.current_row, index, cell)
3032
elif isinstance(cell, bool):
3133
self.xlsx_sheet.write_boolean(self.current_row, index, cell)
34+
elif isinstance(cell, datetime):
35+
self.xlsx_sheet.write_datetime(
36+
self.current_row,
37+
index,
38+
cell.year,
39+
cell.month,
40+
cell.day,
41+
cell.hour,
42+
cell.minute,
43+
cell.second,
44+
)
45+
elif isinstance(cell, date):
46+
self.xlsx_sheet.write_datetime(
47+
self.current_row,
48+
index,
49+
cell.year,
50+
cell.month,
51+
cell.day,
52+
0,
53+
0,
54+
0,
55+
)
56+
elif isinstance(cell, time):
57+
self.xlsx_sheet.write_datetime(
58+
self.current_row,
59+
index,
60+
0,
61+
0,
62+
0,
63+
cell.hour,
64+
cell.minute,
65+
cell.second,
66+
)
3267
else:
3368
self.xlsx_sheet.write_string(
3469
self.current_row, index, str(cell)

tests/base.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ def create_sample_file1(file):
1111
table.append(data[:4])
1212
table.append(data[4:8])
1313
table.append(data[8:12])
14-
pyexcel.save_as(array=table, dest_file_name=file)
14+
pyexcel.save_as(
15+
array=table, dest_file_name=file, library="pyexcel-libxlsxw"
16+
)
1517

1618

1719
class PyexcelHatWriterBase:
@@ -26,7 +28,11 @@ class PyexcelHatWriterBase:
2628
}
2729

2830
def test_series_table(self):
29-
pyexcel.save_as(adict=self.content, dest_file_name=self.testfile)
31+
pyexcel.save_as(
32+
adict=self.content,
33+
dest_file_name=self.testfile,
34+
library="pyexcel-libxlsxw",
35+
)
3036
r = pyexcel.get_sheet(file_name=self.testfile, name_columns_by_row=0)
3137
eq_(r.dict, self.content)
3238

@@ -47,7 +53,9 @@ class PyexcelWriterBase:
4753
]
4854

4955
def _create_a_file(self, file):
50-
pyexcel.save_as(dest_file_name=file, array=self.content)
56+
pyexcel.save_as(
57+
dest_file_name=file, array=self.content, library="pyexcel-libxlsxw"
58+
)
5159

5260
def test_write_array(self):
5361
self._create_a_file(self.testfile)
@@ -58,7 +66,11 @@ def test_write_array(self):
5866

5967
class PyexcelMultipleSheetBase:
6068
def _write_test_file(self, filename):
61-
pyexcel.save_book_as(bookdict=self.content, dest_file_name=filename)
69+
pyexcel.save_book_as(
70+
bookdict=self.content,
71+
dest_file_name=filename,
72+
library="pyexcel-libxlsxw",
73+
)
6274

6375
def _clean_up(self):
6476
if os.path.exists(self.testfile2):

tests/test_bug_fixes.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
import pyexcel as pe
1212
from pyexcel_libxlsxw import save_data
1313

14+
from nose.tools import eq_
15+
1416

1517
class TestBugFix(TestCase):
18+
def setUp(self):
19+
self.maxDiff = None
20+
1621
def test_pyexcel_issue_5(self):
1722
"""pyexcel issue #5
1823
@@ -21,10 +26,10 @@ def test_pyexcel_issue_5(self):
2126
s = pe.load(
2227
os.path.join("tests", "test-fixtures", "test-date-format.xls")
2328
)
24-
s.save_as("issue5.xlsx")
29+
s.save_as("issue5.xlsx", library="pyexcel-libxlsxw")
2530
s2 = pe.load("issue5.xlsx")
26-
assert s[0, 0] == datetime.datetime(2015, 11, 11, 11, 12, 0)
27-
assert s2[0, 0] == datetime.datetime(2015, 11, 11, 11, 12, 0)
31+
eq_(s[0, 0], datetime.datetime(2015, 11, 11, 11, 12, 0))
32+
eq_(s2[0, 0], datetime.datetime(2015, 11, 11, 11, 12, 0))
2833

2934
def test_pyexcel_issue_8_with_physical_file(self):
3035
"""pyexcel issue #8
@@ -33,7 +38,7 @@ def test_pyexcel_issue_8_with_physical_file(self):
3338
"""
3439
tmp_file = "issue_8_save_as.xlsx"
3540
s = pe.load(os.path.join("tests", "test-fixtures", "test8.xlsx"))
36-
s.save_as(tmp_file)
41+
s.save_as(tmp_file, library="pyexcel-libxlsxw")
3742
s2 = pe.load(tmp_file)
3843
self.assertEqual(str(s), str(s2))
3944
content = dedent(
@@ -58,7 +63,7 @@ def test_pyexcel_issue_8_with_memory_file(self):
5863
tmp_file = "issue_8_save_as.xlsx"
5964
f = open(os.path.join("tests", "test-fixtures", "test8.xlsx"), "rb")
6065
s = pe.load_from_memory("xlsx", f.read())
61-
s.save_as(tmp_file)
66+
s.save_as(tmp_file, library="pyexcel-libxlsxw")
6267
s2 = pe.load(tmp_file)
6368
self.assertEqual(str(s), str(s2))
6469
content = dedent(

tests/test_formatters.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ def test_writing_date_format(self):
1414
datetime.datetime(2014, 12, 25, 11, 11, 11),
1515
]
1616
]
17-
pe.save_as(dest_file_name=excel_filename, array=data)
17+
pe.save_as(
18+
dest_file_name=excel_filename,
19+
array=data,
20+
library="pyexcel-libxlsxw",
21+
)
1822
r = pe.get_sheet(file_name=excel_filename, library="pyexcel-xls")
1923
assert isinstance(r[0, 0], datetime.date) is True
2024
assert r[0, 0].strftime("%d/%m/%y") == "25/12/14"

tests/test_multiple_sheets.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ def _write_test_file(self, file):
3333
3,3,3,3
3434
"""
3535
self.rows = 3
36-
pyexcel.save_book_as(bookdict=self.content, dest_file_name=file)
36+
pyexcel.save_book_as(
37+
bookdict=self.content,
38+
dest_file_name=file,
39+
library="pyexcel-libxlsxw",
40+
)
3741

3842
def setUp(self):
3943
self.testfile = "multiple3.xlsx"
@@ -219,17 +223,6 @@ def tearDown(self):
219223
os.unlink(self.testfile2)
220224

221225

222-
class TestMultiSheetReader:
223-
def setUp(self):
224-
self.testfile = "file_with_an_empty_sheet.xlsx"
225-
226-
def test_reader_with_correct_sheets(self):
227-
r = pyexcel.BookReader(
228-
os.path.join("tests", "fixtures", self.testfile)
229-
)
230-
assert r.number_of_sheets() == 3
231-
232-
233226
def _produce_ordered_dict():
234227
data_dict = OrderedDict()
235228
data_dict.update({"Sheet1": [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]})

tests/test_stringio.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
1-
import os
2-
31
import pyexcel
4-
from base import create_sample_file1
2+
53

64
from nose.tools import eq_
75

86

97
class TestStringIO:
10-
def test_ods_stringio(self):
11-
odsfile = "cute.xlsx"
12-
create_sample_file1(odsfile)
13-
with open(odsfile, "rb") as f:
14-
content = f.read()
15-
r = pyexcel.get_sheet(file_type="xlsx", file_content=content)
16-
result = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", 1.1, 1]
17-
actual = list(r.enumerate())
18-
eq_(result, actual)
19-
if os.path.exists(odsfile):
20-
os.unlink(odsfile)
21-
228
def test_xls_output_stringio(self):
239
data = [[1, 2, 3], [4, 5, 6]]
24-
io = pyexcel.save_as(dest_file_type="xlsx", array=data)
10+
io = pyexcel.save_as(
11+
dest_file_type="xlsx", array=data, library="pyexcel-libxlsxw"
12+
)
2513
r = pyexcel.get_sheet(file_type="xlsx", file_content=io.getvalue())
2614
result = [1, 2, 3, 4, 5, 6]
2715
actual = list(r.enumerate())

0 commit comments

Comments
 (0)