Skip to content

Commit 7407423

Browse files
committed
TST: Added pathlib.Path tests for io functions
1 parent 4bbc20a commit 7407423

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

numpy/lib/tests/test_io.py

+24-20
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,12 @@ def test_header_footer(self):
471471
assert_equal(c.read(),
472472
asbytes('1 2\n3 4\n' + commentstr + test_header_footer + '\n'))
473473

474-
def test_file_roundtrip(self):
474+
@pytest.mark.parametrize("filename_type", [Path, str])
475+
def test_file_roundtrip(self, filename_type):
475476
with temppath() as name:
476477
a = np.array([(1, 2), (3, 4)])
477-
np.savetxt(name, a)
478-
b = np.loadtxt(name)
478+
np.savetxt(filename_type(name), a)
479+
b = np.loadtxt(filename_type(name))
479480
assert_array_equal(a, b)
480481

481482
def test_complex_arrays(self):
@@ -2567,10 +2568,10 @@ def test_save_load_memmap(self):
25672568
break_cycles()
25682569

25692570
@pytest.mark.xfail(IS_WASM, reason="memmap doesn't work correctly")
2570-
def test_save_load_memmap_readwrite(self):
2571-
# Test that pathlib.Path instances can be written mem-mapped.
2571+
@pytest.mark.parametrize("filename_type", [Path, str])
2572+
def test_save_load_memmap_readwrite(self, filename_type):
25722573
with temppath(suffix='.npy') as path:
2573-
path = Path(path)
2574+
path = filename_type(path)
25742575
a = np.array([[1, 2], [3, 4]], int)
25752576
np.save(path, a)
25762577
b = np.load(path, mmap_mode='r+')
@@ -2583,35 +2584,37 @@ def test_save_load_memmap_readwrite(self):
25832584
data = np.load(path)
25842585
assert_array_equal(data, a)
25852586

2586-
def test_savez_load(self):
2587-
# Test that pathlib.Path instances can be used with savez.
2587+
@pytest.mark.parametrize("filename_type", [Path, str])
2588+
def test_savez_load(self, filename_type):
25882589
with temppath(suffix='.npz') as path:
2589-
path = Path(path)
2590+
path = filename_type(path)
25902591
np.savez(path, lab='place holder')
25912592
with np.load(path) as data:
25922593
assert_array_equal(data['lab'], 'place holder')
25932594

2594-
def test_savez_compressed_load(self):
2595-
# Test that pathlib.Path instances can be used with savez.
2595+
@pytest.mark.parametrize("filename_type", [Path, str])
2596+
def test_savez_compressed_load(self, filename_type):
25962597
with temppath(suffix='.npz') as path:
2597-
path = Path(path)
2598+
path = filename_type(path)
25982599
np.savez_compressed(path, lab='place holder')
25992600
data = np.load(path)
26002601
assert_array_equal(data['lab'], 'place holder')
26012602
data.close()
26022603

2603-
def test_genfromtxt(self):
2604+
@pytest.mark.parametrize("filename_type", [Path, str])
2605+
def test_genfromtxt(self, filename_type):
26042606
with temppath(suffix='.txt') as path:
2605-
path = Path(path)
2607+
path = filename_type(path)
26062608
a = np.array([(1, 2), (3, 4)])
26072609
np.savetxt(path, a)
26082610
data = np.genfromtxt(path)
26092611
assert_array_equal(a, data)
26102612

2611-
def test_recfromtxt(self):
2613+
@pytest.mark.parametrize("filename_type", [Path, str])
2614+
def test_recfromtxt(self, filename_type):
26122615
with temppath(suffix='.txt') as path:
2613-
path = Path(path)
2614-
with path.open('w') as f:
2616+
path = filename_type(path)
2617+
with open(path, 'w') as f:
26152618
f.write('A,B\n0,1\n2,3')
26162619

26172620
kwargs = dict(delimiter=",", missing_values="N/A", names=True)
@@ -2621,10 +2624,11 @@ def test_recfromtxt(self):
26212624
assert_(isinstance(test, np.recarray))
26222625
assert_equal(test, control)
26232626

2624-
def test_recfromcsv(self):
2627+
@pytest.mark.parametrize("filename_type", [Path, str])
2628+
def test_recfromcsv(self, filename_type):
26252629
with temppath(suffix='.txt') as path:
2626-
path = Path(path)
2627-
with path.open('w') as f:
2630+
path = filename_type(path)
2631+
with open(path, 'w') as f:
26282632
f.write('A,B\n0,1\n2,3')
26292633

26302634
kwargs = dict(missing_values="N/A", names=True, case_sensitive=True)

0 commit comments

Comments
 (0)