Skip to content

Commit e5575bf

Browse files
committed
Get project root relative to the test files and use as parent of file paths, creating single instance of path
1 parent d1b6479 commit e5575bf

File tree

2 files changed

+53
-45
lines changed

2 files changed

+53
-45
lines changed

functional/test/test_io.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import unittest
22

33
from functional.io import ReusableFile, GZFile, BZ2File, XZFile, universal_write_open
4+
from pathlib import Path
5+
6+
project_root = Path(__file__).parent.parent.parent.absolute()
47

58

69
class TestUtil(unittest.TestCase):
710
def test_reusable_file(self):
8-
license_file_lf = ReusableFile("LICENSE.txt")
9-
with open("LICENSE.txt", encoding="utf8") as license_file:
11+
file_name = f"{project_root}/LICENSE.txt"
12+
license_file_lf = ReusableFile(file_name)
13+
with open(file_name, encoding="utf8") as license_file:
1014
self.assertEqual(list(license_file), list(license_file_lf))
1115
iter_1 = iter(license_file_lf)
1216
iter_2 = iter(license_file_lf)
1317
self.assertEqual(list(iter_1), list(iter_2))
1418

1519
def test_gzip_file(self):
16-
file_name = "functional/test/data/test.txt.gz"
20+
file_name = f"{project_root}/functional/test/data/test.txt.gz"
1721
expect = [
1822
"line0\n",
1923
"line1\n",
@@ -31,7 +35,7 @@ def test_gzip_file(self):
3135
self.assertListEqual(expect, list(GZFile(file_name, mode="rb")))
3236

3337
def test_bz2_file(self):
34-
file_name = "functional/test/data/test.txt.bz2"
38+
file_name = f"{project_root}/functional/test/data/test.txt.bz2"
3539
expect = [
3640
"line0\n",
3741
"line1\n",
@@ -49,7 +53,7 @@ def test_bz2_file(self):
4953
self.assertListEqual(expect, list(BZ2File(file_name, mode="rb")))
5054

5155
def test_xz_file(self):
52-
file_name = "functional/test/data/test.txt.xz"
56+
file_name = f"{project_root}/functional/test/data/test.txt.xz"
5357
expect = [
5458
"line0\n",
5559
"line1\n",

functional/test/test_streams.py

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,59 @@
33
import collections
44
import sys
55
import gzip
6+
from pathlib import Path
67
from platform import system
78
import lzma
89
import bz2
910

1011
from functional import seq, pseq
1112
from functional.streams import Stream, ParallelStream
1213

14+
project_root = Path(__file__).parent.parent.parent.absolute()
15+
1316

1417
class 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(",")
2529
self.assertListEqual(
26-
text, self.seq.open("LICENSE.txt", delimiter=",").to_list()
30+
text, self.seq.open(file_name, delimiter=",").to_list()
2731
)
2832

2933
with self.assertRaises(ValueError):
30-
self.seq.open("LICENSE.txt", mode="w").to_list()
34+
self.seq.open(file_name, mode="w").to_list()
3135

3236
def test_open_gzip(self):
3337
expect = ["line0\n", "line1\n", "line2"]
3438
self.assertListEqual(
3539
expect,
36-
self.seq.open("functional/test/data/test.txt.gz", mode="rt").to_list(),
40+
self.seq.open(f"{project_root}/functional/test/data/test.txt.gz", mode="rt").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(f"{project_root}/functional/test/data/test.txt.bz2", mode="rt").to_list(),
4448
)
4549

4650
def test_open_xz(self):
4751
expect = ["line0\n", "line1\n", "line2"]
4852
self.assertListEqual(
4953
expect,
50-
self.seq.open("functional/test/data/test.txt.xz", mode="rt").to_list(),
54+
self.seq.open(f"{project_root}/functional/test/data/test.txt.xz", mode="rt").to_list(),
5155
)
5256

5357
def test_disable_compression(self):
54-
file_name = "functional/test/data/test.txt.gz"
58+
file_name = f"{project_root}/functional/test/data/test.txt.gz"
5559
with open(file_name, "rb") as f:
5660
expect = f.readlines()
5761
self.assertListEqual(
@@ -119,26 +123,26 @@ def test_chain(self):
119123
self.assertEqual([1], self.seq.chain([1]).to_list())
120124

121125
def test_csv(self):
122-
result = self.seq.csv("functional/test/data/test.csv").to_list()
126+
file_name = f"{project_root}/functional/test/data/test.csv"
127+
result = self.seq.csv(file_name).to_list()
123128
expect = [["1", "2", "3", "4"], ["a", "b", "c", "d"]]
124129
self.assertEqual(expect, result)
125-
with open("functional/test/data/test.csv", "r", encoding="utf8") as csv_file:
130+
with open(file_name, "r", encoding="utf8") as csv_file:
126131
self.assertEqual(expect, self.seq.csv(csv_file).to_list())
127132
with self.assertRaises(ValueError):
128133
self.seq.csv(1)
129134

130135
def test_csv_dict_reader(self):
131-
result = self.seq.csv_dict_reader(
132-
"functional/test/data/test_header.csv"
133-
).to_list()
136+
file_name = f"{project_root}/functional/test/data/test_header.csv"
137+
result = self.seq.csv_dict_reader(file_name).to_list()
134138
self.assertEqual(result[0]["a"], "1")
135139
self.assertEqual(result[0]["b"], "2")
136140
self.assertEqual(result[0]["c"], "3")
137141
self.assertEqual(result[1]["a"], "4")
138142
self.assertEqual(result[1]["b"], "5")
139143
self.assertEqual(result[1]["c"], "6")
140144

141-
with open("functional/test/data/test_header.csv", "r", encoding="utf8") as f:
145+
with open(file_name, "r", encoding="utf8") as f:
142146
result = self.seq.csv_dict_reader(f).to_list()
143147
self.assertEqual(result[0]["a"], "1")
144148
self.assertEqual(result[0]["b"], "2")
@@ -151,52 +155,52 @@ def test_csv_dict_reader(self):
151155
self.seq.csv_dict_reader(1)
152156

153157
def test_gzip_csv(self):
154-
result = self.seq.csv("functional/test/data/test.csv.gz").to_list()
158+
result = self.seq.csv(f"{project_root}/functional/test/data/test.csv.gz").to_list()
155159
expect = [["1", "2", "3", "4"], ["a", "b", "c", "d"]]
156160
self.assertEqual(expect, result)
157161
with self.assertRaises(ValueError):
158162
self.seq.csv(1)
159163

160164
def test_bz2_csv(self):
161-
result = self.seq.csv("functional/test/data/test.csv.bz2").to_list()
165+
result = self.seq.csv(f"{project_root}/functional/test/data/test.csv.bz2").to_list()
162166
expect = [["1", "2", "3", "4"], ["a", "b", "c", "d"]]
163167
self.assertEqual(expect, result)
164168
with self.assertRaises(ValueError):
165169
self.seq.csv(1)
166170

167171
def test_xz_csv(self):
168-
result = self.seq.csv("functional/test/data/test.csv.xz").to_list()
172+
result = self.seq.csv(f"{project_root}/functional/test/data/test.csv.xz").to_list()
169173
expect = [["1", "2", "3", "4"], ["a", "b", "c", "d"]]
170174
self.assertEqual(expect, result)
171175
with self.assertRaises(ValueError):
172176
self.seq.csv(1)
173177

174178
def test_jsonl(self):
175-
result_0 = self.seq.jsonl("functional/test/data/test.jsonl").to_list()
179+
result_0 = self.seq.jsonl(f"{project_root}/functional/test/data/test.jsonl").to_list()
176180
expect_0 = [[1, 2, 3], {"a": 1, "b": 2, "c": 3}]
177181
self.assertEqual(expect_0, result_0)
178182
result_1 = self.seq.jsonl(["[1, 2, 3]", "[4, 5, 6]"])
179183
expect_1 = [[1, 2, 3], [4, 5, 6]]
180184
self.assertEqual(expect_1, result_1)
181185

182186
def test_gzip_jsonl(self):
183-
result_0 = self.seq.jsonl("functional/test/data/test.jsonl.gz").to_list()
187+
result_0 = self.seq.jsonl(f"{project_root}/functional/test/data/test.jsonl.gz").to_list()
184188
expect_0 = [[1, 2, 3], {"a": 1, "b": 2, "c": 3}]
185189
self.assertEqual(expect_0, result_0)
186190

187191
def test_bz2_jsonl(self):
188-
result_0 = self.seq.jsonl("functional/test/data/test.jsonl.bz2").to_list()
192+
result_0 = self.seq.jsonl(f"{project_root}/functional/test/data/test.jsonl.bz2").to_list()
189193
expect_0 = [[1, 2, 3], {"a": 1, "b": 2, "c": 3}]
190194
self.assertEqual(expect_0, result_0)
191195

192196
def test_xz_jsonl(self):
193-
result_0 = self.seq.jsonl("functional/test/data/test.jsonl.xz").to_list()
197+
result_0 = self.seq.jsonl(f"{project_root}/functional/test/data/test.jsonl.xz").to_list()
194198
expect_0 = [[1, 2, 3], {"a": 1, "b": 2, "c": 3}]
195199
self.assertEqual(expect_0, result_0)
196200

197201
def test_json(self):
198-
list_test_path = "functional/test/data/test_list.json"
199-
dict_test_path = "functional/test/data/test_dict.json"
202+
list_test_path = f"{project_root}/functional/test/data/test_list.json"
203+
dict_test_path = f"{project_root}/functional/test/data/test_dict.json"
200204
list_expect = [1, 2, 3, 4, 5]
201205
dict_expect = list({"a": 1, "b": 2, "c": 3}.items())
202206

@@ -216,8 +220,8 @@ def test_json(self):
216220
self.seq.json(1)
217221

218222
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"
223+
list_test_path = f"{project_root}/functional/test/data/test_list.json.gz"
224+
dict_test_path = f"{project_root}/functional/test/data/test_dict.json.gz"
221225
list_expect = [1, 2, 3, 4, 5]
222226
dict_expect = list({"a": 1, "b": 2, "c": 3}.items())
223227

@@ -230,8 +234,8 @@ def test_gzip_json(self):
230234
self.seq.json(1)
231235

232236
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"
237+
list_test_path = f"{project_root}/functional/test/data/test_list.json.bz2"
238+
dict_test_path = f"{project_root}/functional/test/data/test_dict.json.bz2"
235239
list_expect = [1, 2, 3, 4, 5]
236240
dict_expect = list({"a": 1, "b": 2, "c": 3}.items())
237241

@@ -244,8 +248,8 @@ def test_bz2_json(self):
244248
self.seq.json(1)
245249

246250
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"
251+
list_test_path = f"{project_root}/functional/test/data/test_list.json.xz"
252+
dict_test_path = f"{project_root}/functional/test/data/test_dict.json.xz"
249253
list_expect = [1, 2, 3, 4, 5]
250254
dict_expect = list({"a": 1, "b": 2, "c": 3}.items())
251255

@@ -258,7 +262,7 @@ def test_xz_json(self):
258262
self.seq.json(1)
259263

260264
def test_sqlite3(self):
261-
db_file = "functional/test/data/test_sqlite3.db"
265+
db_file = f"{project_root}/functional/test/data/test_sqlite3.db"
262266

263267
# test failure case
264268
with self.assertRaises(ValueError):
@@ -313,7 +317,7 @@ def test_pandas(self):
313317
pass
314318

315319
def test_to_file(self):
316-
tmp_path = "functional/test/data/tmp/output.txt"
320+
tmp_path = f"{project_root}/functional/test/data/tmp/output.txt"
317321
sequence = self.seq(1, 2, 3, 4)
318322
sequence.to_file(tmp_path)
319323
with open(tmp_path, "r", encoding="utf8") as output:
@@ -324,7 +328,7 @@ def test_to_file(self):
324328
self.assertEqual("1:2:3:4", output.readlines()[0])
325329

326330
def test_to_file_compressed(self):
327-
tmp_path = "functional/test/data/tmp/output.txt"
331+
tmp_path = f"{project_root}/functional/test/data/tmp/output.txt"
328332
sequence = self.seq(1, 2, 3, 4)
329333
sequence.to_file(tmp_path, compression="gzip")
330334
with gzip.open(tmp_path, "rt") as output:
@@ -339,7 +343,7 @@ def test_to_file_compressed(self):
339343
self.assertEqual("[1, 2, 3, 4]", output.readlines()[0])
340344

341345
def test_to_jsonl(self):
342-
tmp_path = "functional/test/data/tmp/output.txt"
346+
tmp_path = f"{project_root}/functional/test/data/tmp/output.txt"
343347
elements = [{"a": 1, "b": 2}, {"c": 3}, {"d": 4}]
344348
sequence = self.seq(elements)
345349

@@ -348,7 +352,7 @@ def test_to_jsonl(self):
348352
self.assertEqual(elements, result)
349353

350354
def test_to_jsonl_compressed(self):
351-
tmp_path = "functional/test/data/tmp/output.txt"
355+
tmp_path = f"{project_root}/functional/test/data/tmp/output.txt"
352356
elements = [{"a": 1, "b": 2}, {"c": 3}, {"d": 4}]
353357
sequence = self.seq(elements)
354358

@@ -365,7 +369,7 @@ def test_to_jsonl_compressed(self):
365369
self.assertEqual(elements, result)
366370

367371
def test_to_json(self):
368-
tmp_path = "functional/test/data/tmp/output.txt"
372+
tmp_path = f"{project_root}/functional/test/data/tmp/output.txt"
369373
elements = [["a", 1], ["b", 2], ["c", 3]]
370374
sequence = self.seq(elements)
371375

@@ -379,7 +383,7 @@ def test_to_json(self):
379383
self.assertEqual(dict_expect, result)
380384

381385
def test_to_json_compressed(self):
382-
tmp_path = "functional/test/data/tmp/output.txt"
386+
tmp_path = f"{project_root}/functional/test/data/tmp/output.txt"
383387
elements = [["a", 1], ["b", 2], ["c", 3]]
384388
dict_expect = {"a": 1, "b": 2, "c": 3}
385389
sequence = self.seq(elements)
@@ -409,7 +413,7 @@ def test_to_json_compressed(self):
409413
self.assertEqual(dict_expect, result)
410414

411415
def test_to_csv(self):
412-
tmp_path = "functional/test/data/tmp/output.txt"
416+
tmp_path = f"{project_root}/functional/test/data/tmp/output.txt"
413417
elements = [[1, 2, 3], [4, 5, 6], ["a", "b", "c"]]
414418
expect = [["1", "2", "3"], ["4", "5", "6"], ["a", "b", "c"]]
415419
sequence = self.seq(elements)
@@ -419,7 +423,7 @@ def test_to_csv(self):
419423

420424
@unittest.skipUnless(system().startswith("Win"), "Skip CSV test if not on Windows")
421425
def test_to_csv_win(self):
422-
tmp_path = "functional/test/data/tmp/output.txt"
426+
tmp_path = f"{project_root}/functional/test/data/tmp/output.txt"
423427
elements = [[1, 2, 3], [4, 5, 6], ["a", "b", "c"]]
424428
expect = [["1", "2", "3"], [], ["4", "5", "6"], [], ["a", "b", "c"], []]
425429
sequence = self.seq(elements)
@@ -428,7 +432,7 @@ def test_to_csv_win(self):
428432
self.assertNotEqual(expect, result)
429433

430434
def test_to_csv_compressed(self):
431-
tmp_path = "functional/test/data/tmp/output.txt"
435+
tmp_path = f"{project_root}/functional/test/data/tmp/output.txt"
432436
elements = [[1, 2, 3], [4, 5, 6], ["a", "b", "c"]]
433437
expect = [["1", "2", "3"], ["4", "5", "6"], ["a", "b", "c"]]
434438
sequence = self.seq(elements)
@@ -452,7 +456,7 @@ def test_to_sqlite3_failure(self):
452456
self.seq(elements).to_sqlite3(1, insert_sql)
453457

454458
def test_to_sqlite3_file(self):
455-
tmp_path = "functional/test/data/tmp/test.db"
459+
tmp_path = f"{project_root}/functional/test/data/tmp/test.db"
456460

457461
with sqlite3.connect(tmp_path) as conn:
458462
conn.execute("DROP TABLE IF EXISTS user;")

0 commit comments

Comments
 (0)