8000 test pathlib feature #263 · pyexcel/pyexcel@53e51c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 53e51c0

Browse files
committed
test pathlib feature #263
1 parent e2d4b5a commit 53e51c0

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

tests/test_signature_fuction.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from types import GeneratorType
3+
from pathlib import Path
34

45
import pyexcel as pe
56

@@ -112,6 +113,12 @@ def test_get_sheet_from_txt(self):
112113
expected = [[1, 2, 3]]
113114
eq_(sheet.to_array(), expected)
114115

116+
def test_get_sheet_from_txt_via_pathlib(self):
117+
test_file = os.path.join("tests", "fixtures", "force_type.txt")
118+
sheet = pe.get_sheet(file_name=Path(test_file), force_file_type="csv")
119+
expected = [[1, 2, 3]]
120+
eq_(sheet.to_array(), expected)
121+
115122

116123
class TestGetArray:
117124
def setUp(self):
@@ -190,6 +197,15 @@ def test_get_dict_from_file(self):
190197
assert result == {"X": [1, 4], "Y": [2, 5], "Z": [3, 6]}
191198
os.unlink(testfile)
192199

200+
def test_get_dict_from_file_via_path(self):
201+
data = [["X", "Y", "Z"], [1, 2, 3], [4, 5, 6]]
202+
sheet = pe.Sheet(data)
203+
testfile = "testfile.xls"
204+
sheet.save_as(testfile)
205+
result = pe.get_dict(file_name=Path(testfile))
206+
assert result == {"X": [1, 4], "Y": [2, 5], "Z": [3, 6]}
207+
os.unlink(testfile)
208+
193209
def test_get_dict_from_memory(self):
194210
data = [["X", "Y", "Z"], [1, 2, 3], [4, 5, 6]]
195211
content = pe.save_as(dest_file_type="xls", array=data)
@@ -223,6 +239,15 @@ def test_get_records_from_file(self):
223239
eq_(result, [{"X": 1, "Y": 2, "Z": 3}, {"X": 4, "Y": 5, "Z": 6}])
224240
os.unlink(testfile)
225241

242+
def test_get_records_from_file_path(self):
243+
data = [["X", "Y", "Z"], [1, 2, 3], [4, 5, 6]]
244+
sheet = pe.Sheet(data)
245+
testfile = "testfile.xls"
246+
sheet.save_as(testfile)
247+
result = pe.get_records(file_name=Path(testfile))
248+
eq_(result, [{"X": 1, "Y": 2, "Z": 3}, {"X": 4, "Y": 5, "Z": 6}])
249+
os.unlink(testfile)
250+
226251
def test_get_records_from_memory(self):
227252
data = [["X", "Y", "Z"], [1, 2, 3], [4, 5, 6]]
228253
content = pe.save_as(dest_file_type="xls", array=data)
@@ -261,6 +286,15 @@ def test_get_records_from_file(self):
261286
eq_(list(result), [{"X": 1, "Y": 2, "Z": 3}, {"X": 4, "Y": 5, "Z": 6}])
262287
os.unlink(testfile)
263288

289+
def test_get_records_from_file_path(self):
290+
data = [["X", "Y", "Z"], [1, 2, 3], [4, 5, 6]]
291+
sheet = pe.Sheet(data)
292+
testfile = "testfile.xls"
293+
sheet.save_as(testfile)
294+
result = pe.iget_records(file_name=Path(testfile))
295+
eq_(list(result), [{"X": 1, "Y": 2, "Z": 3}, {"X": 4, "Y": 5, "Z": 6}])
296+
os.unlink(testfile)
297+
264298
def test_get_records_from_memory(self):
265299
data = [["X", "Y", "Z"], [1, 2, 3], [4, 5, 6]]
266300
content = pe.save_as(dest_file_type="xls", array=data)
@@ -552,6 +586,16 @@ def test_get_book_from_file(self):
552586
assert book2.to_dict() == content
553587
os.unlink(test_file)
554588

589+
def test_get_book_from_file_path(self):
590+
test_file = "test_get_book.xls"
591+
content = _produce_ordered_dict()
592+
593+
book = pe.Book(content)
594+
book.save_as(test_file)
595+
book2 = pe.get_book(file_name=Path(test_file))
596+
assert book2.to_dict() == content
597+
os.unlink(test_file)
598+
555599
def test_get_book_from_memory(self):
556600
content = _produce_ordered_dict()
557601
io = pe.save_book_as(dest_file_type="xls", bookdict=content)
@@ -619,6 +663,18 @@ def test_get_book_from_file(self):
619663
eq_(book3.to_dict(), content)
620664
os.unlink(test_file)
621665

666+
def test_get_book_from_file_path(self):
667+
test_file = "test_get_book.xls"
668+
content = _produce_ordered_dict()
669+
670+
book = pe.Book(content)
671+
book.save_as(test_file)
672+
book_stream = pe.iget_book(file_name=Path(test_file))
673+
assert book_stream.to_dict() != content
674+
book3 = pe.Book(book_stream.to_dict())
675+
eq_(book3.to_dict(), content)
676+
os.unlink(test_file)
677+
622678
def test_get_book_from_memory(self):
623679
content = _produce_ordered_dict()
624680
io = pe.save_book_as(dest_file_type="xls", bookdict=content)
@@ -700,6 +756,16 @@ def test_force_file_type(self):
700756
eq_([[1, 2]], actual)
701757
os.unlink("a.txt")
702758

759+
def test_force_file_type_with_pathlib(self):
760+
pe.save_as(
761+
array=[[1, 2]],
762+
dest_file_name="a.txt",
763+
dest_force_file_type="csv",
764+
)
765+
actual = pe.get_array(file_name=Path("a.txt"), force_file_type="csv")
766+
eq_([[1, 2]], actual)
767+
os.unlink("a.txt")
768+
703769
def test_force_file_type_for_save_book_as(self):
704770
pe.save_as(
705771
bookdict={"sheet1": [[1, 2]]},
@@ -723,6 +789,19 @@ def test_save_file_as_another_one(self):
723789
os.unlink(testfile)
724790
os.unlink(testfile2)
725791

792+
def test_save_file_as_another_one_using_path(self):
793+
data = [["X", "Y", "Z"], [1, 2, 3], [4, 5, 6]]
794+
sheet = pe.Sheet(data)
795+
testfile = "testfile.xls"
796+
testfile2 = "testfile2.csv"
797+
sheet.save_as(testfile)
798+
pe.save_as(file_name=Path(testfile), dest_file_name=Path(testfile2))
799+
sheet = pe.get_sheet(file_name=testfile2)
800+
sheet.format(int)
801+
eq_(sheet.to_array(), data)
802+
os.unlink(testfile)
803+
os.unlink(testfile2)
804+
726805
def test_save_as_and_append_colnames(self):
727806
data = [[1, 2, 3], [4, 5, 6]]
728807
sheet = pe.Sheet(data)
@@ -754,6 +833,18 @@ def test_save_file_as_another_one(self):
754833
os.unlink(testfile)
755834
os.unlink(testfile2)
756835

836+
def test_save_file_as_another_one_using_pathlib(self):
837+
data = [["X", "Y", "Z"], [1, 2, 3], [4, 5, 6]]
838+
sheet = pe.Sheet(data)
839+
testfile = "testfile.xls"
840+
testfile2 = "testfile2.csv"
841+
sheet.save_as(testfile)
842+
pe.isave_as(file_name=Path(testfile), dest_file_name=Path(testfile2))
843+
sheet = pe.get_sheet(file_name=testfile2)
844+
eq_(sheet.to_array(), data)
845+
os.unlink(testfile)
846+
os.unlink(testfile2)
847+
757848
@raises(Exception)
758849
def test_save_as_invalid_params(self):
759850
data = [["X", "Y", "Z"], [1, 2, 3], [4, 5, 6]]

0 commit comments

Comments
 (0)
0