Skip to content

Commit

Permalink
🔬 really test pyexcel-libxlsxw and implement write datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed Oct 16, 2020
1 parent 72848c2 commit 04c9c84
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 38 deletions.
35 changes: 35 additions & 0 deletions pyexcel_libxlsxw/xlsxw.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
:copyright: (c) 2016-2020 by Onni Software Ltd & its contributors
:license: New BSD License
"""
from datetime import date, time, datetime

from libxlsxwpy import Book
from pyexcel_io.plugin_api import IWriter, ISheetWriter

Expand All @@ -29,6 +31,39 @@ def write_row(self, array):
self.xlsx_sheet.write_number(self.current_row, index, cell)
elif isinstance(cell, bool):
self.xlsx_sheet.write_boolean(self.current_row, index, cell)
elif isinstance(cell, datetime):
self.xlsx_sheet.write_datetime(
self.current_row,
index,
cell.year,
cell.month,
cell.day,
cell.hour,
cell.minute,
cell.second,
)
elif isinstance(cell, date):
self.xlsx_sheet.write_datetime(
self.current_row,
index,
cell.year,
cell.month,
cell.day,
0,
0,
0,
)
elif isinstance(cell, time):
self.xlsx_sheet.write_datetime(
self.current_row,
index,
0,
0,
0,
cell.hour,
cell.minute,
cell.second,
)
else:
self.xlsx_sheet.write_string(
self.current_row, index, str(cell)
Expand Down
20 changes: 16 additions & 4 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def create_sample_file1(file):
table.append(data[:4])
table.append(data[4:8])
table.append(data[8:12])
pyexcel.save_as(array=table, dest_file_name=file)
pyexcel.save_as(
array=table, dest_file_name=file, library="pyexcel-libxlsxw"
)


class PyexcelHatWriterBase:
Expand All @@ -26,7 +28,11 @@ class PyexcelHatWriterBase:
}

def test_series_table(self):
pyexcel.save_as(adict=self.content, dest_file_name=self.testfile)
pyexcel.save_as(
adict=self.content,
dest_file_name=self.testfile,
library="pyexcel-libxlsxw",
)
r = pyexcel.get_sheet(file_name=self.testfile, name_columns_by_row=0)
eq_(r.dict, self.content)

Expand All @@ -47,7 +53,9 @@ class PyexcelWriterBase:
]

def _create_a_file(self, file):
pyexcel.save_as(dest_file_name=file, array=self.content)
pyexcel.save_as(
dest_file_name=file, array=self.content, library="pyexcel-libxlsxw"
)

def test_write_array(self):
self._create_a_file(self.testfile)
Expand All @@ -58,7 +66,11 @@ def test_write_array(self):

class PyexcelMultipleSheetBase:
def _write_test_file(self, filename):
pyexcel.save_book_as(bookdict=self.content, dest_file_name=filename)
pyexcel.save_book_as(
bookdict=self.content,
dest_file_name=filename,
library="pyexcel-libxlsxw",
)

def _clean_up(self):
if os.path.exists(self.testfile2):
Expand Down
15 changes: 10 additions & 5 deletions tests/test_bug_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
import pyexcel as pe
from pyexcel_libxlsxw import save_data

from nose.tools import eq_


class TestBugFix(TestCase):
def setUp(self):
self.maxDiff = None

def test_pyexcel_issue_5(self):
"""pyexcel issue #5
Expand All @@ -21,10 +26,10 @@ def test_pyexcel_issue_5(self):
s = pe.load(
os.path.join("tests", "test-fixtures", "test-date-format.xls")
)
s.save_as("issue5.xlsx")
s.save_as("issue5.xlsx", library="pyexcel-libxlsxw")
s2 = pe.load("issue5.xlsx")
assert s[0, 0] == datetime.datetime(2015, 11, 11, 11, 12, 0)
assert s2[0, 0] == datetime.datetime(2015, 11, 11, 11, 12, 0)
eq_(s[0, 0], datetime.datetime(2015, 11, 11, 11, 12, 0))
eq_(s2[0, 0], datetime.datetime(2015, 11, 11, 11, 12, 0))

def test_pyexcel_issue_8_with_physical_file(self):
"""pyexcel issue #8
Expand All @@ -33,7 +38,7 @@ def test_pyexcel_issue_8_with_physical_file(self):
"""
tmp_file = "issue_8_save_as.xlsx"
s = pe.load(os.path.join("tests", "test-fixtures", "test8.xlsx"))
s.save_as(tmp_file)
s.save_as(tmp_file, library="pyexcel-libxlsxw")
s2 = pe.load(tmp_file)
self.assertEqual(str(s), str(s2))
content = dedent(
Expand All @@ -58,7 +63,7 @@ def test_pyexcel_issue_8_with_memory_file(self):
tmp_file = "issue_8_save_as.xlsx"
f = open(os.path.join("tests", "test-fixtures", "test8.xlsx"), "rb")
s = pe.load_from_memory("xlsx", f.read())
s.save_as(tmp_file)
s.save_as(tmp_file, library="pyexcel-libxlsxw")
s2 = pe.load(tmp_file)
self.assertEqual(str(s), str(s2))
content = dedent(
Expand Down
6 changes: 5 additions & 1 deletion tests/test_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ def test_writing_date_format(self):
datetime.datetime(2014, 12, 25, 11, 11, 11),
]
]
pe.save_as(dest_file_name=excel_filename, array=data)
pe.save_as(
dest_file_name=excel_filename,
array=data,
library="pyexcel-libxlsxw",
)
r = pe.get_sheet(file_name=excel_filename, library="pyexcel-xls")
assert isinstance(r[0, 0], datetime.date) is True
assert r[0, 0].strftime("%d/%m/%y") == "25/12/14"
Expand Down
17 changes: 5 additions & 12 deletions tests/test_multiple_sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ def _write_test_file(self, file):
3,3,3,3
"""
self.rows = 3
pyexcel.save_book_as(bookdict=self.content, dest_file_name=file)
pyexcel.save_book_as(
bookdict=self.content,
dest_file_name=file,
library="pyexcel-libxlsxw",
)

def setUp(self):
self.testfile = "multiple3.xlsx"
Expand Down Expand Up @@ -219,17 +223,6 @@ def tearDown(self):
os.unlink(self.testfile2)


class TestMultiSheetReader:
def setUp(self):
self.testfile = "file_with_an_empty_sheet.xlsx"

def test_reader_with_correct_sheets(self):
r = pyexcel.BookReader(
os.path.join("tests", "fixtures", self.testfile)
)
assert r.number_of_sheets() == 3


def _produce_ordered_dict():
data_dict = OrderedDict()
data_dict.update({"Sheet1": [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]})
Expand Down
20 changes: 4 additions & 16 deletions tests/test_stringio.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import os

import pyexcel
from base import create_sample_file1


from nose.tools import eq_


class TestStringIO:
def test_ods_stringio(self):
odsfile = "cute.xlsx"
create_sample_file1(odsfile)
with open(odsfile, "rb") as f:
content = f.read()
r = pyexcel.get_sheet(file_type="xlsx", file_content=content)
result = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", 1.1, 1]
actual = list(r.enumerate())
eq_(result, actual)
if os.path.exists(odsfile):
os.unlink(odsfile)

def test_xls_output_stringio(self):
data = [[1, 2, 3], [4, 5, 6]]
io = pyexcel.save_as(dest_file_type="xlsx", array=data)
io = pyexcel.save_as(
dest_file_type="xlsx", array=data, library="pyexcel-libxlsxw"
)
r = pyexcel.get_sheet(file_type="xlsx", file_content=io.getvalue())
result = [1, 2, 3, 4, 5, 6]
actual = list(r.enumerate())
Expand Down

0 comments on commit 04c9c84

Please sign in to comment.