33import collections
44import sys
55import gzip
6+ from pathlib import Path
67from platform import system
78import lzma
89import bz2
910
1011from functional import seq , pseq
1112from functional .streams import Stream , ParallelStream
1213
14+ project_root = Path (__file__ ).parent .parent .parent .absolute ()
15+
1316
1417class TestStreams (unittest .TestCase ):
1518 def setUp (self ):
1619 self .seq = seq
1720 self .seq_c_disabled = Stream (disable_compression = True )
1821
1922 def test_open (self ):
20- with open ("LICENSE.txt" , encoding = "utf8" ) as f :
23+ file_name = f"{ project_root } /LICENSE.txt"
24+ with open (file_name , encoding = "utf8" ) as f :
2125 data = f .readlines ()
22- self .assertListEqual (data , self .seq .open ("LICENSE.txt" ).to_list ())
26+ self .assertListEqual (data , self .seq .open (file_name ).to_list ())
2327
2428 text = "" .join (data ).split ("," )
25- self .assertListEqual (
26- text , self .seq .open ("LICENSE.txt" , delimiter = "," ).to_list ()
27- )
29+ self .assertListEqual (text , self .seq .open (file_name , delimiter = "," ).to_list ())
2830
2931 with self .assertRaises (ValueError ):
30- self .seq .open ("LICENSE.txt" , mode = "w" ).to_list ()
32+ self .seq .open (file_name , mode = "w" ).to_list ()
3133
3234 def test_open_gzip (self ):
3335 expect = ["line0\n " , "line1\n " , "line2" ]
3436 self .assertListEqual (
3537 expect ,
36- self .seq .open ("functional/test/data/test.txt.gz" , mode = "rt" ).to_list (),
38+ self .seq .open (
39+ f"{ project_root } /functional/test/data/test.txt.gz" , mode = "rt"
40+ ).to_list (),
3741 )
3842
3943 def test_open_bz2 (self ):
4044 expect = ["line0\n " , "line1\n " , "line2" ]
4145 self .assertListEqual (
4246 expect ,
43- self .seq .open ("functional/test/data/test.txt.bz2" , mode = "rt" ).to_list (),
47+ self .seq .open (
48+ f"{ project_root } /functional/test/data/test.txt.bz2" , mode = "rt"
49+ ).to_list (),
4450 )
4551
4652 def test_open_xz (self ):
4753 expect = ["line0\n " , "line1\n " , "line2" ]
4854 self .assertListEqual (
4955 expect ,
50- self .seq .open ("functional/test/data/test.txt.xz" , mode = "rt" ).to_list (),
56+ self .seq .open (
57+ f"{ project_root } /functional/test/data/test.txt.xz" , mode = "rt"
58+ ).to_list (),
5159 )
5260
5361 def test_disable_compression (self ):
54- file_name = " functional/test/data/test.txt.gz"
62+ file_name = f" { project_root } / functional/test/data/test.txt.gz"
5563 with open (file_name , "rb" ) as f :
5664 expect = f .readlines ()
5765 self .assertListEqual (
@@ -119,26 +127,26 @@ def test_chain(self):
119127 self .assertEqual ([1 ], self .seq .chain ([1 ]).to_list ())
120128
121129 def test_csv (self ):
122- result = self .seq .csv ("functional/test/data/test.csv" ).to_list ()
130+ file_name = f"{ project_root } /functional/test/data/test.csv"
131+ result = self .seq .csv (file_name ).to_list ()
123132 expect = [["1" , "2" , "3" , "4" ], ["a" , "b" , "c" , "d" ]]
124133 self .assertEqual (expect , result )
125- with open ("functional/test/data/test.csv" , "r" , encoding = "utf8" ) as csv_file :
134+ with open (file_name , "r" , encoding = "utf8" ) as csv_file :
126135 self .assertEqual (expect , self .seq .csv (csv_file ).to_list ())
127136 with self .assertRaises (ValueError ):
128137 self .seq .csv (1 )
129138
130139 def test_csv_dict_reader (self ):
131- result = self .seq .csv_dict_reader (
132- "functional/test/data/test_header.csv"
133- ).to_list ()
140+ file_name = f"{ project_root } /functional/test/data/test_header.csv"
141+ result = self .seq .csv_dict_reader (file_name ).to_list ()
134142 self .assertEqual (result [0 ]["a" ], "1" )
135143 self .assertEqual (result [0 ]["b" ], "2" )
136144 self .assertEqual (result [0 ]["c" ], "3" )
137145 self .assertEqual (result [1 ]["a" ], "4" )
138146 self .assertEqual (result [1 ]["b" ], "5" )
139147 self .assertEqual (result [1 ]["c" ], "6" )
140148
141- with open ("functional/test/data/test_header.csv" , "r" , encoding = "utf8" ) as f :
149+ with open (file_name , "r" , encoding = "utf8" ) as f :
142150 result = self .seq .csv_dict_reader (f ).to_list ()
143151 self .assertEqual (result [0 ]["a" ], "1" )
144152 self .assertEqual (result [0 ]["b" ], "2" )
@@ -151,52 +159,66 @@ def test_csv_dict_reader(self):
151159 self .seq .csv_dict_reader (1 )
152160
153161 def test_gzip_csv (self ):
154- result = self .seq .csv ("functional/test/data/test.csv.gz" ).to_list ()
162+ result = self .seq .csv (
163+ f"{ project_root } /functional/test/data/test.csv.gz"
164+ ).to_list ()
155165 expect = [["1" , "2" , "3" , "4" ], ["a" , "b" , "c" , "d" ]]
156166 self .assertEqual (expect , result )
157167 with self .assertRaises (ValueError ):
158168 self .seq .csv (1 )
159169
160170 def test_bz2_csv (self ):
161- result = self .seq .csv ("functional/test/data/test.csv.bz2" ).to_list ()
171+ result = self .seq .csv (
172+ f"{ project_root } /functional/test/data/test.csv.bz2"
173+ ).to_list ()
162174 expect = [["1" , "2" , "3" , "4" ], ["a" , "b" , "c" , "d" ]]
163175 self .assertEqual (expect , result )
164176 with self .assertRaises (ValueError ):
165177 self .seq .csv (1 )
166178
167179 def test_xz_csv (self ):
168- result = self .seq .csv ("functional/test/data/test.csv.xz" ).to_list ()
180+ result = self .seq .csv (
181+ f"{ project_root } /functional/test/data/test.csv.xz"
182+ ).to_list ()
169183 expect = [["1" , "2" , "3" , "4" ], ["a" , "b" , "c" , "d" ]]
170184 self .assertEqual (expect , result )
171185 with self .assertRaises (ValueError ):
172186 self .seq .csv (1 )
173187
174188 def test_jsonl (self ):
175- result_0 = self .seq .jsonl ("functional/test/data/test.jsonl" ).to_list ()
189+ result_0 = self .seq .jsonl (
190+ f"{ project_root } /functional/test/data/test.jsonl"
191+ ).to_list ()
176192 expect_0 = [[1 , 2 , 3 ], {"a" : 1 , "b" : 2 , "c" : 3 }]
177193 self .assertEqual (expect_0 , result_0 )
178194 result_1 = self .seq .jsonl (["[1, 2, 3]" , "[4, 5, 6]" ])
179195 expect_1 = [[1 , 2 , 3 ], [4 , 5 , 6 ]]
180196 self .assertEqual (expect_1 , result_1 )
181197
182198 def test_gzip_jsonl (self ):
183- result_0 = self .seq .jsonl ("functional/test/data/test.jsonl.gz" ).to_list ()
199+ result_0 = self .seq .jsonl (
200+ f"{ project_root } /functional/test/data/test.jsonl.gz"
201+ ).to_list ()
184202 expect_0 = [[1 , 2 , 3 ], {"a" : 1 , "b" : 2 , "c" : 3 }]
185203 self .assertEqual (expect_0 , result_0 )
186204
187205 def test_bz2_jsonl (self ):
188- result_0 = self .seq .jsonl ("functional/test/data/test.jsonl.bz2" ).to_list ()
206+ result_0 = self .seq .jsonl (
207+ f"{ project_root } /functional/test/data/test.jsonl.bz2"
208+ ).to_list ()
189209 expect_0 = [[1 , 2 , 3 ], {"a" : 1 , "b" : 2 , "c" : 3 }]
190210 self .assertEqual (expect_0 , result_0 )
191211
192212 def test_xz_jsonl (self ):
193- result_0 = self .seq .jsonl ("functional/test/data/test.jsonl.xz" ).to_list ()
213+ result_0 = self .seq .jsonl (
214+ f"{ project_root } /functional/test/data/test.jsonl.xz"
215+ ).to_list ()
194216 expect_0 = [[1 , 2 , 3 ], {"a" : 1 , "b" : 2 , "c" : 3 }]
195217 self .assertEqual (expect_0 , result_0 )
196218
197219 def test_json (self ):
198- list_test_path = " functional/test/data/test_list.json"
199- dict_test_path = " functional/test/data/test_dict.json"
220+ list_test_path = f" { project_root } / functional/test/data/test_list.json"
221+ dict_test_path = f" { project_root } / functional/test/data/test_dict.json"
200222 list_expect = [1 , 2 , 3 , 4 , 5 ]
201223 dict_expect = list ({"a" : 1 , "b" : 2 , "c" : 3 }.items ())
202224
@@ -216,8 +238,8 @@ def test_json(self):
216238 self .seq .json (1 )
217239
218240 def test_gzip_json (self ):
219- list_test_path = " functional/test/data/test_list.json.gz"
220- dict_test_path = " functional/test/data/test_dict.json.gz"
241+ list_test_path = f" { project_root } / functional/test/data/test_list.json.gz"
242+ dict_test_path = f" { project_root } / functional/test/data/test_dict.json.gz"
221243 list_expect = [1 , 2 , 3 , 4 , 5 ]
222244 dict_expect = list ({"a" : 1 , "b" : 2 , "c" : 3 }.items ())
223245
@@ -230,8 +252,8 @@ def test_gzip_json(self):
230252 self .seq .json (1 )
231253
232254 def test_bz2_json (self ):
233- list_test_path = " functional/test/data/test_list.json.bz2"
234- dict_test_path = " functional/test/data/test_dict.json.bz2"
255+ list_test_path = f" { project_root } / functional/test/data/test_list.json.bz2"
256+ dict_test_path = f" { project_root } / functional/test/data/test_dict.json.bz2"
235257 list_expect = [1 , 2 , 3 , 4 , 5 ]
236258 dict_expect = list ({"a" : 1 , "b" : 2 , "c" : 3 }.items ())
237259
@@ -244,8 +266,8 @@ def test_bz2_json(self):
244266 self .seq .json (1 )
245267
246268 def test_xz_json (self ):
247- list_test_path = " functional/test/data/test_list.json.xz"
248- dict_test_path = " functional/test/data/test_dict.json.xz"
269+ list_test_path = f" { project_root } / functional/test/data/test_list.json.xz"
270+ dict_test_path = f" { project_root } / functional/test/data/test_dict.json.xz"
249271 list_expect = [1 , 2 , 3 , 4 , 5 ]
250272 dict_expect = list ({"a" : 1 , "b" : 2 , "c" : 3 }.items ())
251273
@@ -258,7 +280,7 @@ def test_xz_json(self):
258280 self .seq .json (1 )
259281
260282 def test_sqlite3 (self ):
261- db_file = " functional/test/data/test_sqlite3.db"
283+ db_file = f" { project_root } / functional/test/data/test_sqlite3.db"
262284
263285 # test failure case
264286 with self .assertRaises (ValueError ):
@@ -313,7 +335,7 @@ def test_pandas(self):
313335 pass
314336
315337 def test_to_file (self ):
316- tmp_path = " functional/test/data/tmp/output.txt"
338+ tmp_path = f" { project_root } / functional/test/data/tmp/output.txt"
317339 sequence = self .seq (1 , 2 , 3 , 4 )
318340 sequence .to_file (tmp_path )
319341 with open (tmp_path , "r" , encoding = "utf8" ) as output :
@@ -324,7 +346,7 @@ def test_to_file(self):
324346 self .assertEqual ("1:2:3:4" , output .readlines ()[0 ])
325347
326348 def test_to_file_compressed (self ):
327- tmp_path = " functional/test/data/tmp/output.txt"
349+ tmp_path = f" { project_root } / functional/test/data/tmp/output.txt"
328350 sequence = self .seq (1 , 2 , 3 , 4 )
329351 sequence .to_file (tmp_path , compression = "gzip" )
330352 with gzip .open (tmp_path , "rt" ) as output :
@@ -339,7 +361,7 @@ def test_to_file_compressed(self):
339361 self .assertEqual ("[1, 2, 3, 4]" , output .readlines ()[0 ])
340362
341363 def test_to_jsonl (self ):
342- tmp_path = " functional/test/data/tmp/output.txt"
364+ tmp_path = f" { project_root } / functional/test/data/tmp/output.txt"
343365 elements = [{"a" : 1 , "b" : 2 }, {"c" : 3 }, {"d" : 4 }]
344366 sequence = self .seq (elements )
345367
@@ -348,7 +370,7 @@ def test_to_jsonl(self):
348370 self .assertEqual (elements , result )
349371
350372 def test_to_jsonl_compressed (self ):
351- tmp_path = " functional/test/data/tmp/output.txt"
373+ tmp_path = f" { project_root } / functional/test/data/tmp/output.txt"
352374 elements = [{"a" : 1 , "b" : 2 }, {"c" : 3 }, {"d" : 4 }]
353375 sequence = self .seq (elements )
354376
@@ -365,7 +387,7 @@ def test_to_jsonl_compressed(self):
365387 self .assertEqual (elements , result )
366388
367389 def test_to_json (self ):
368- tmp_path = " functional/test/data/tmp/output.txt"
390+ tmp_path = f" { project_root } / functional/test/data/tmp/output.txt"
369391 elements = [["a" , 1 ], ["b" , 2 ], ["c" , 3 ]]
370392 sequence = self .seq (elements )
371393
@@ -379,7 +401,7 @@ def test_to_json(self):
379401 self .assertEqual (dict_expect , result )
380402
381403 def test_to_json_compressed (self ):
382- tmp_path = " functional/test/data/tmp/output.txt"
404+ tmp_path = f" { project_root } / functional/test/data/tmp/output.txt"
383405 elements = [["a" , 1 ], ["b" , 2 ], ["c" , 3 ]]
384406 dict_expect = {"a" : 1 , "b" : 2 , "c" : 3 }
385407 sequence = self .seq (elements )
@@ -409,7 +431,7 @@ def test_to_json_compressed(self):
409431 self .assertEqual (dict_expect , result )
410432
411433 def test_to_csv (self ):
412- tmp_path = " functional/test/data/tmp/output.txt"
434+ tmp_path = f" { project_root } / functional/test/data/tmp/output.txt"
413435 elements = [[1 , 2 , 3 ], [4 , 5 , 6 ], ["a" , "b" , "c" ]]
414436 expect = [["1" , "2" , "3" ], ["4" , "5" , "6" ], ["a" , "b" , "c" ]]
415437 sequence = self .seq (elements )
@@ -419,7 +441,7 @@ def test_to_csv(self):
419441
420442 @unittest .skipUnless (system ().startswith ("Win" ), "Skip CSV test if not on Windows" )
421443 def test_to_csv_win (self ):
422- tmp_path = " functional/test/data/tmp/output.txt"
444+ tmp_path = f" { project_root } / functional/test/data/tmp/output.txt"
423445 elements = [[1 , 2 , 3 ], [4 , 5 , 6 ], ["a" , "b" , "c" ]]
424446 expect = [["1" , "2" , "3" ], [], ["4" , "5" , "6" ], [], ["a" , "b" , "c" ], []]
425447 sequence = self .seq (elements )
@@ -428,7 +450,7 @@ def test_to_csv_win(self):
428450 self .assertNotEqual (expect , result )
429451
430452 def test_to_csv_compressed (self ):
431- tmp_path = " functional/test/data/tmp/output.txt"
453+ tmp_path = f" { project_root } / functional/test/data/tmp/output.txt"
432454 elements = [[1 , 2 , 3 ], [4 , 5 , 6 ], ["a" , "b" , "c" ]]
433455 expect = [["1" , "2" , "3" ], ["4" , "5" , "6" ], ["a" , "b" , "c" ]]
434456 sequence = self .seq (elements )
@@ -452,7 +474,7 @@ def test_to_sqlite3_failure(self):
452474 self .seq (elements ).to_sqlite3 (1 , insert_sql )
453475
454476 def test_to_sqlite3_file (self ):
455- tmp_path = " functional/test/data/tmp/test.db"
477+ tmp_path = f" { project_root } / functional/test/data/tmp/test.db"
456478
457479 with sqlite3 .connect (tmp_path ) as conn :
458480 conn .execute ("DROP TABLE IF EXISTS user;" )
0 commit comments