diff --git a/news/update_tests.rst b/news/update_tests.rst new file mode 100644 index 00000000..790d30b1 --- /dev/null +++ b/news/update_tests.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/tests/test_morphio.py b/tests/test_morphio.py index b16d414a..08699dcb 100644 --- a/tests/test_morphio.py +++ b/tests/test_morphio.py @@ -50,6 +50,20 @@ def isfloat(s): return False +def are_files_same(file1, file2): + """Assert that two files have (approximately) the same numerical + values.""" + for f1_row, f2_row in zip(file1, file2): + f1_arr = f1_row.split() + f2_arr = f2_row.split() + assert len(f1_arr) == len(f2_arr) + for idx, _ in enumerate(f1_arr): + if isfloat(f1_arr[idx]) and isfloat(f2_arr[idx]): + assert np.isclose(float(f1_arr[idx]), float(f2_arr[idx])) + else: + assert f1_arr[idx] == f2_arr[idx] + + class TestApp: @pytest.fixture def setup(self): @@ -106,9 +120,9 @@ def test_morph_outputs(self, setup, tmp_path): for file in common: with open(tmp_succinct.joinpath(file)) as gf: with open(test_saving_succinct.joinpath(file)) as tf: - generated = filter(ignore_path, gf) - target = filter(ignore_path, tf) - assert all(x == y for x, y in zip(generated, target)) + actual = filter(ignore_path, gf) + expected = filter(ignore_path, tf) + are_files_same(actual, expected) # Save multiple verbose morphs tmp_verbose = tmp_path.joinpath("verbose") @@ -147,9 +161,9 @@ def test_morph_outputs(self, setup, tmp_path): for file in common: with open(tmp_verbose.joinpath(file)) as gf: with open(test_saving_verbose.joinpath(file)) as tf: - generated = filter(ignore_path, gf) - target = filter(ignore_path, tf) - assert all(x == y for x, y in zip(generated, target)) + actual = filter(ignore_path, gf) + expected = filter(ignore_path, tf) + are_files_same(actual, expected) def test_morphsqueeze_outputs(self, setup, tmp_path): # The file squeeze_morph has a squeeze and stretch applied @@ -182,19 +196,9 @@ def test_morphsqueeze_outputs(self, setup, tmp_path): # Check squeeze morph generates the correct output with open(sqr) as mf: with open(target_file) as tf: - morphed = filter(ignore_path, mf) - target = filter(ignore_path, tf) - for m, t in zip(morphed, target): - m_row = m.split() - t_row = t.split() - assert len(m_row) == len(t_row) - for idx, _ in enumerate(m_row): - if isfloat(m_row[idx]) and isfloat(t_row[idx]): - assert np.isclose( - float(m_row[idx]), float(t_row[idx]) - ) - else: - assert m_row[idx] == t_row[idx] + actual = filter(ignore_path, mf) + expected = filter(ignore_path, tf) + are_files_same(actual, expected) def test_morphfuncy_outputs(self, tmp_path): def quadratic(x, y, a0, a1, a2): @@ -215,16 +219,6 @@ def quadratic(x, y, a0, a1, a2): with open(testdata_dir.joinpath("funcy_target.cgr")) as tf: with open(tmp_path.joinpath("funcy_target.cgr")) as gf: - generated = filter(ignore_path, gf) - target = filter(ignore_path, tf) - for m, t in zip(generated, target): - m_row = m.split() - t_row = t.split() - assert len(m_row) == len(t_row) - for idx, _ in enumerate(m_row): - if isfloat(m_row[idx]) and isfloat(t_row[idx]): - assert np.isclose( - float(m_row[idx]), float(t_row[idx]) - ) - else: - assert m_row[idx] == t_row[idx] + actual = filter(ignore_path, gf) + expected = filter(ignore_path, tf) + are_files_same(actual, expected) diff --git a/tests/test_morphpy.py b/tests/test_morphpy.py index 848fe1a9..0a288271 100644 --- a/tests/test_morphpy.py +++ b/tests/test_morphpy.py @@ -91,7 +91,14 @@ class Chain: assert np.allclose( [rw], [self.morphapp_results[target_file.name]["Rw"]] ) - assert morph_results == self.morphapp_results + # Check values in dictionaries are approximately equal + for file in morph_results.keys(): + morph_params = morph_results[file] + morphapp_params = self.morphapp_results[file] + for key in morph_params.keys(): + assert morph_params[key] == pytest.approx( + morphapp_params[key], abs=1e-08 + ) def test_morphpy(self, setup_morph): morph_results = {} @@ -113,7 +120,14 @@ class Chain: assert np.allclose( [rw], [self.morphapp_results[target_file.name]["Rw"]] ) - assert morph_results == self.morphapp_results + # Check values in dictionaries are approximately equal + for file in morph_results.keys(): + morph_params = morph_results[file] + morphapp_params = self.morphapp_results[file] + for key in morph_params.keys(): + assert morph_params[key] == pytest.approx( + morphapp_params[key], abs=1e-08 + ) def test_morphfuncy(self, setup_morph): def gaussian(x, mu, sigma):