1
1
import os
2
2
from types import GeneratorType
3
+ from pathlib import Path
3
4
4
5
import pyexcel as pe
5
6
@@ -112,6 +113,12 @@ def test_get_sheet_from_txt(self):
112
113
expected = [[1 , 2 , 3 ]]
113
114
eq_ (sheet .to_array (), expected )
114
115
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
+
115
122
116
123
class TestGetArray :
117
124
def setUp (self ):
@@ -190,6 +197,15 @@ def test_get_dict_from_file(self):
190
197
assert result == {"X" : [1 , 4 ], "Y" : [2 , 5 ], "Z" : [3 , 6 ]}
191
198
os .unlink (testfile )
192
199
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
+
193
209
def test_get_dict_from_memory (self ):
194
210
data = [["X" , "Y" , "Z" ], [1 , 2 , 3 ], [4 , 5 , 6 ]]
195
211
content = pe .save_as (dest_file_type = "xls" , array = data )
@@ -223,6 +239,15 @@ def test_get_records_from_file(self):
223
239
eq_ (result , [{"X" : 1 , "Y" : 2 , "Z" : 3 }, {"X" : 4 , "Y" : 5 , "Z" : 6 }])
224
240
os .unlink (testfile )
225
241
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
+
226
251
def test_get_records_from_memory (self ):
227
252
data = [["X" , "Y" , "Z" ], [1 , 2 , 3 ], [4 , 5 , 6 ]]
228
253
content = pe .save_as (dest_file_type = "xls" , array = data )
@@ -261,6 +286,15 @@ def test_get_records_from_file(self):
261
286
eq_ (list (result ), [{"X" : 1 , "Y" : 2 , "Z" : 3 }, {"X" : 4 , "Y" : 5 , "Z" : 6 }])
262
287
os .unlink (testfile )
263
288
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
+
264
298
def test_get_records_from_memory (self ):
265
299
data = [["X" , "Y" , "Z" ], [1 , 2 , 3 ], [4 , 5 , 6 ]]
266
300
content = pe .save_as (dest_file_type = "xls" , array = data )
@@ -552,6 +586,16 @@ def test_get_book_from_file(self):
552
586
assert book2 .to_dict () == content
553
587
os .unlink (test_file )
554
588
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
+
555
599
def test_get_book_from_memory (self ):
556
600
content = _produce_ordered_dict ()
557
601
io = pe .save_book_as (dest_file_type = "xls" , bookdict = content )
@@ -619,6 +663,18 @@ def test_get_book_from_file(self):
619
663
eq_ (book3 .to_dict (), content )
620
664
os .unlink (test_file )
621
665
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
+
622
678
def test_get_book_from_memory (self ):
623
679
content = _produce_ordered_dict ()
624
680
io = pe .save_book_as (dest_file_type = "xls" , bookdict = content )
@@ -700,6 +756,16 @@ def test_force_file_type(self):
700
756
eq_ ([[1 , 2 ]], actual )
701
757
os .unlink ("a.txt" )
702
758
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
+
703
769
def test_force_file_type_for_save_book_as (self ):
704
770
pe .save_as (
705
771
bookdict = {"sheet1" : [[1 , 2 ]]},
@@ -723,6 +789,19 @@ def test_save_file_as_another_one(self):
723
789
os .unlink (testfile )
724
790
os .unlink (testfile2 )
725
791
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
+
726
805
def test_save_as_and_append_colnames (self ):
727
806
data = [[1 , 2 , 3 ], [4 , 5 , 6 ]]
728
807
sheet = pe .Sheet (data )
@@ -754,6 +833,18 @@ def test_save_file_as_another_one(self):
754
833
os .unlink (testfile )
755
834
os .unlink (testfile2 )
756
835
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
+
757
848
@raises (Exception )
758
849
def test_save_as_invalid_params (self ):
759
850
data = [["X" , "Y" , "Z" ], [1 , 2 , 3 ], [4 , 5 , 6 ]]
0 commit comments