Skip to content

Commit

Permalink
Utilize setUp and tearDown for removing tmp files
Browse files Browse the repository at this point in the history
  • Loading branch information
mataton committed Apr 15, 2024
1 parent e38dae5 commit 563eda1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
9 changes: 7 additions & 2 deletions biom/tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,19 @@ def setUp(self):
self.legacy_otu_table1 = legacy_otu_table1
self.otu_table1 = otu_table1
self.otu_table1_floats = otu_table1_floats
self.files_to_remove = []
self.to_remove = []
self.biom_minimal_sparse = biom_minimal_sparse

self.classic_otu_table1_w_tax = classic_otu_table1_w_tax.split('\n')
self.classic_otu_table1_no_tax = classic_otu_table1_no_tax.split('\n')
self.classic_table_with_complex_metadata = \
classic_table_with_complex_metadata.split('\n')

def tearDown(self):
if self.to_remove:
for f in self.to_remove:
os.remove(f)

def test_from_tsv_bug_854(self):
data = StringIO('#FeatureID\tSample1')
exp = Table([], [], ['Sample1'])
Expand Down Expand Up @@ -294,7 +299,7 @@ def test_save_table_filepath(self):
save_table(t, tmpfile.name)
obs = load_table(tmpfile.name)
self.assertEqual(obs, t)
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)

def test_load_table_filepath(self):
cwd = os.getcwd()
Expand Down
30 changes: 15 additions & 15 deletions biom/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ def test_to_from_hdf5_bug_861(self):
h5 = h5py.File(tmpfile.name, 'r')
obs = Table.from_hdf5(h5)
h5.close()
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)

self.assertEqual(obs, t)

Expand All @@ -1041,7 +1041,7 @@ def test_to_from_hdf5_creation_date(self):
obs = Table.from_hdf5(h5)
self.assertEqual(obs.create_date, current)
h5.close()
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)

self.assertEqual(obs, t)

Expand All @@ -1053,7 +1053,7 @@ def test_to_hdf5_empty_table(self):
h5 = h5py.File(tmpfile.name, 'w')
t.to_hdf5(h5, 'tests')
h5.close()
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)

def test_to_hdf5_empty_table_bug_619(self):
"""Successfully writes an empty OTU table in HDF5 format"""
Expand All @@ -1062,14 +1062,14 @@ def test_to_hdf5_empty_table_bug_619(self):
h5 = h5py.File(tmpfile.name, 'w')
t.to_hdf5(h5, 'tests')
h5.close()
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)

t = example_table.filter({}, inplace=False)
with NamedTemporaryFile(delete=False) as tmpfile:
h5 = h5py.File(tmpfile.name, 'w')
t.to_hdf5(h5, 'tests')
h5.close()
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)

def test_to_hdf5_missing_metadata_observation(self):
# exercises a vlen_list
Expand All @@ -1081,7 +1081,7 @@ def test_to_hdf5_missing_metadata_observation(self):
with h5py.File(tmpfile.name, 'w') as h5:
t.to_hdf5(h5, 'tests')
obs = load_table(tmpfile.name)
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)
self.assertEqual(obs.metadata(axis='observation'),
({'taxonomy': None},
{'taxonomy': ['foo', 'baz']}))
Expand All @@ -1096,7 +1096,7 @@ def test_to_hdf5_missing_metadata_sample(self):
with h5py.File(tmpfile.name, 'w') as h5:
t.to_hdf5(h5, 'tests')
obs = load_table(tmpfile.name)
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)
self.assertEqual(obs.metadata(axis='sample'),
({'dat': ''},
{'dat': 'foo'}))
Expand All @@ -1111,7 +1111,7 @@ def test_to_hdf5_inconsistent_metadata_categories_observation(self):
with self.assertRaisesRegex(ValueError,
'inconsistent metadata'):
t.to_hdf5(h5, 'tests')
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)

def test_to_hdf5_inconsistent_metadata_categories_sample(self):
t = Table(np.array([[0, 1], [2, 3]]), ['a', 'b'], ['c', 'd'],
Expand All @@ -1124,7 +1124,7 @@ def test_to_hdf5_inconsistent_metadata_categories_sample(self):
with self.assertRaisesRegex(ValueError,
'inconsistent metadata'):
t.to_hdf5(h5, 'tests')
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)

def test_to_hdf5_malformed_taxonomy(self):
t = Table(np.array([[0, 1], [2, 3]]), ['a', 'b'], ['c', 'd'],
Expand All @@ -1135,7 +1135,7 @@ def test_to_hdf5_malformed_taxonomy(self):
with h5py.File(tmpfile.name, 'w') as h5:
t.to_hdf5(h5, 'tests')
obs = load_table(tmpfile.name)
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)
self.assertEqual(obs.metadata(axis='observation'),
({'taxonomy': ['foo', 'bar']},
{'taxonomy': ['foo', 'baz']}))
Expand All @@ -1150,7 +1150,7 @@ def test_to_hdf5_general_fallback_to_list(self):
h5 = h5py.File(tmpfile.name, 'w')
st_rich.to_hdf5(h5, 'tests')
h5.close()
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)

def test_to_hdf5_custom_formatters(self):
self.st_rich = Table(self.vals,
Expand Down Expand Up @@ -1186,7 +1186,7 @@ def bc_formatter(grp, category, md, compression):
self.assertNotEqual(m1['barcode'], m2['barcode'])
self.assertEqual(m1['barcode'].lower(), m2['barcode'])
h5.close()
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)

def test_to_hdf5(self):
"""Write a file"""
Expand All @@ -1208,7 +1208,7 @@ def test_to_hdf5(self):
obs = Table.from_hdf5(h5)
self.assertEqual(obs, self.st_rich)
h5.close()
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)

# Test with a collapsed table
with NamedTemporaryFile(delete=False) as tmpfile:
Expand Down Expand Up @@ -1254,7 +1254,7 @@ def bin_f(id_, x):
[{'collapsed_ids': ['a', 'c']},
{'collapsed_ids': ['b']}])
self.assertEqual(obs, exp)
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)

# Test with table having a None on taxonomy
with NamedTemporaryFile(delete=False) as tmpfile:
Expand All @@ -1279,7 +1279,7 @@ def bin_f(id_, x):
obs = Table.from_hdf5(h5)
h5.close()
self.assertEqual(obs, t)
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)

def test_from_tsv(self):
tab1_fh = StringIO(otu_table1)
Expand Down
12 changes: 9 additions & 3 deletions biom/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class UtilTests(TestCase):

def setUp(self):
self.biom_otu_table1_w_tax = parse_biom_table(biom_otu_table1_w_tax)
self.to_remove = []

def tearDown(self):
if self.to_remove:
for f in self.to_remove:
os.remove(f)

def test_generate_subsamples(self):
table = Table(np.array([[3, 1, 1], [0, 3, 3]]), ['O1', 'O2'],
Expand Down Expand Up @@ -253,7 +259,7 @@ def test_safe_md5(self):

obs = safe_md5(open(tmp_f.name))
tmp_f.close()
os.unlink(tmp_f.name)
self.to_remove.append(tmp_f.name)
self.assertEqual(obs, exp)

obs = safe_md5(['foo\n'])
Expand All @@ -268,7 +274,7 @@ def test_biom_open_hdf5_pathlib_write(self):
with NamedTemporaryFile(delete=False) as tmpfile:
with biom_open(pathlib.Path(tmpfile.name), 'w') as fp:
t.to_hdf5(fp, 'tests')
os.unlink(tmpfile.name)
self.to_remove.append(tmpfile.name)

def test_biom_open_hdf5_pathlib_read(self):
cwd = os.getcwd()
Expand Down Expand Up @@ -318,7 +324,7 @@ def test_load_classic(self):
fp.flush()

obs = load_table(fp.name)
os.unlink(fp.name)
self.to_remove.append(fp.name)

npt.assert_equal(obs.ids(), tab.ids())
npt.assert_equal(obs.ids(axis='observation'),
Expand Down

0 comments on commit 563eda1

Please sign in to comment.