Skip to content

Commit

Permalink
test: close fd
Browse files Browse the repository at this point in the history
  • Loading branch information
tvandera committed Jun 11, 2024
1 parent 67aa573 commit 4f83d03
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions h5sparse/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@

import h5sparse

def close_remove(fd, path):
os.close(fd)
os.remove(path)


class AbstractTestH5Sparse():
def test_create_empty_sparse_dataset(self):
h5_path = mkstemp(suffix=".h5")[1]
h5_fd, h5_path = mkstemp(suffix=".h5")
format_str = h5sparse.get_format_str(self.sparse_class((0, 0)))
with h5sparse.File(h5_path, 'w') as h5f:
h5f.create_dataset('sparse/matrix', sparse_format=format_str)
Expand All @@ -26,11 +30,11 @@ def test_create_empty_sparse_dataset(self):
assert h5f['sparse']['matrix'].shape == (0, 0)
assert h5f['sparse']['matrix'].dtype == np.float64

os.remove(h5_path)
close_remove(h5_fd, h5_path)

def test_create_dataset_from_dataset(self):
from_h5_path = mkstemp(suffix=".h5")[1]
to_h5_path = mkstemp(suffix=".h5")[1]
from_h5_fd, from_h5_path = mkstemp(suffix=".h5")
to_h5_fd, to_h5_path = mkstemp(suffix=".h5")
sparse_matrix = self.sparse_class([[0, 1, 0],
[0, 0, 1],
[0, 0, 0],
Expand All @@ -45,42 +49,42 @@ def test_create_dataset_from_dataset(self):
assert 'matrix' in to_h5f['sparse']
assert (to_h5f['sparse/matrix'][()] != sparse_matrix).size == 0

os.remove(from_h5_path)
os.remove(to_h5_path)
close_remove(from_h5_fd, from_h5_path)
close_remove(to_h5_fd, to_h5_path)

def test_numpy_array(self):
h5_path = mkstemp(suffix=".h5")[1]
h5_fd, h5_path = mkstemp(suffix=".h5")
matrix = np.random.rand(3, 5)
with h5sparse.File(h5_path, 'w') as h5f:
h5f.create_dataset('matrix', data=matrix)
assert 'matrix' in h5f
np.testing.assert_equal(h5f['matrix'][()], matrix)
os.remove(h5_path)
close_remove(h5_fd, h5_path)

def test_bytestring(self):
h5_path = mkstemp(suffix=".h5")[1]
h5_fd, h5_path = mkstemp(suffix=".h5")
strings = [str(i) for i in range(100)]
data = json.dumps(strings).encode('utf8')
with h5sparse.File(h5_path, 'w') as h5f:
h5f.create_dataset('strings', data=data)
assert 'strings' in h5f
assert strings == json.loads(h5f['strings'][()].decode('utf8'))
os.remove(h5_path)
close_remove(h5_fd, h5_path)

def test_create_empty_dataset(self):
h5_path = mkstemp(suffix=".h5")[1]
h5_fd, h5_path = mkstemp(suffix=".h5")
with h5sparse.File(h5_path, 'w') as h5f:
h5f.create_dataset('empty_data', shape=(100, 200))
with h5sparse.File(h5_path, 'r') as h5f:
assert h5f['empty_data'].shape == (100, 200)
os.remove(h5_path)
close_remove(h5_fd, h5_path)


class Test5HCSR(unittest.TestCase, AbstractTestH5Sparse):
sparse_class = ss.csr_matrix

def test_create_and_read_dataset(self):
h5_path = mkstemp(suffix=".h5")[1]
h5_fd, h5_path = mkstemp(suffix=".h5")
sparse_matrix = self.sparse_class([[0, 1, 0],
[0, 0, 1],
[0, 0, 0],
Expand All @@ -98,10 +102,10 @@ def test_create_and_read_dataset(self):
assert (h5f['sparse']['matrix'][:-2] != sparse_matrix[:-2]).size == 0
assert (h5f['sparse']['matrix'][()] != sparse_matrix).size == 0

os.remove(h5_path)
close_remove(h5_fd, h5_path)

def test_dataset_append(self):
h5_path = mkstemp(suffix=".h5")[1]
h5_fd, h5_path = mkstemp(suffix=".h5")
sparse_matrix = self.sparse_class([[0, 1, 0],
[0, 0, 1],
[0, 0, 0],
Expand All @@ -118,10 +122,10 @@ def test_dataset_append(self):
h5f['matrix'].append(to_append)
assert (h5f['matrix'][()] != appended_matrix).size == 0

os.remove(h5_path)
close_remove(h5_fd, h5_path)

def test_create_dataset_with_format_change(self):
h5_path = mkstemp(suffix=".h5")[1]
h5_fd, h5_path = mkstemp(suffix=".h5")
sparse_matrix = self.sparse_class([[0, 1, 0, 1],
[0, 0, 1, 0],
[0, 0, 0, 1],
Expand All @@ -142,7 +146,7 @@ def test_create_dataset_with_format_change(self):
assert (h5f['sparse']['matrix'][-2:] != sparse_matrix[:, -2:]).size == 0
assert (h5f['sparse']['matrix'][:-2] != sparse_matrix[:, :-2]).size == 0

os.remove(h5_path)
close_remove(h5_fd, h5_path)


class Test5HCSC(unittest.TestCase, AbstractTestH5Sparse):
Expand Down

0 comments on commit 4f83d03

Please sign in to comment.