Skip to content

Commit 5d9fd64

Browse files
authored
Update numerical file comparisons in tests (#231)
* Update numerical file comparisons in tests * News * Dictionary numeric comparison * Renaming for clarity
1 parent b4763e1 commit 5d9fd64

File tree

3 files changed

+65
-34
lines changed

3 files changed

+65
-34
lines changed

news/update_tests.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

tests/test_morphio.py

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ def isfloat(s):
5050
return False
5151

5252

53+
def are_files_same(file1, file2):
54+
"""Assert that two files have (approximately) the same numerical
55+
values."""
56+
for f1_row, f2_row in zip(file1, file2):
57+
f1_arr = f1_row.split()
58+
f2_arr = f2_row.split()
59+
assert len(f1_arr) == len(f2_arr)
60+
for idx, _ in enumerate(f1_arr):
61+
if isfloat(f1_arr[idx]) and isfloat(f2_arr[idx]):
62+
assert np.isclose(float(f1_arr[idx]), float(f2_arr[idx]))
63+
else:
64+
assert f1_arr[idx] == f2_arr[idx]
65+
66+
5367
class TestApp:
5468
@pytest.fixture
5569
def setup(self):
@@ -106,9 +120,9 @@ def test_morph_outputs(self, setup, tmp_path):
106120
for file in common:
107121
with open(tmp_succinct.joinpath(file)) as gf:
108122
with open(test_saving_succinct.joinpath(file)) as tf:
109-
generated = filter(ignore_path, gf)
110-
target = filter(ignore_path, tf)
111-
assert all(x == y for x, y in zip(generated, target))
123+
actual = filter(ignore_path, gf)
124+
expected = filter(ignore_path, tf)
125+
are_files_same(actual, expected)
112126

113127
# Save multiple verbose morphs
114128
tmp_verbose = tmp_path.joinpath("verbose")
@@ -147,9 +161,9 @@ def test_morph_outputs(self, setup, tmp_path):
147161
for file in common:
148162
with open(tmp_verbose.joinpath(file)) as gf:
149163
with open(test_saving_verbose.joinpath(file)) as tf:
150-
generated = filter(ignore_path, gf)
151-
target = filter(ignore_path, tf)
152-
assert all(x == y for x, y in zip(generated, target))
164+
actual = filter(ignore_path, gf)
165+
expected = filter(ignore_path, tf)
166+
are_files_same(actual, expected)
153167

154168
def test_morphsqueeze_outputs(self, setup, tmp_path):
155169
# The file squeeze_morph has a squeeze and stretch applied
@@ -182,19 +196,9 @@ def test_morphsqueeze_outputs(self, setup, tmp_path):
182196
# Check squeeze morph generates the correct output
183197
with open(sqr) as mf:
184198
with open(target_file) as tf:
185-
morphed = filter(ignore_path, mf)
186-
target = filter(ignore_path, tf)
187-
for m, t in zip(morphed, target):
188-
m_row = m.split()
189-
t_row = t.split()
190-
assert len(m_row) == len(t_row)
191-
for idx, _ in enumerate(m_row):
192-
if isfloat(m_row[idx]) and isfloat(t_row[idx]):
193-
assert np.isclose(
194-
float(m_row[idx]), float(t_row[idx])
195-
)
196-
else:
197-
assert m_row[idx] == t_row[idx]
199+
actual = filter(ignore_path, mf)
200+
expected = filter(ignore_path, tf)
201+
are_files_same(actual, expected)
198202

199203
def test_morphfuncy_outputs(self, tmp_path):
200204
def quadratic(x, y, a0, a1, a2):
@@ -215,16 +219,6 @@ def quadratic(x, y, a0, a1, a2):
215219

216220
with open(testdata_dir.joinpath("funcy_target.cgr")) as tf:
217221
with open(tmp_path.joinpath("funcy_target.cgr")) as gf:
218-
generated = filter(ignore_path, gf)
219-
target = filter(ignore_path, tf)
220-
for m, t in zip(generated, target):
221-
m_row = m.split()
222-
t_row = t.split()
223-
assert len(m_row) == len(t_row)
224-
for idx, _ in enumerate(m_row):
225-
if isfloat(m_row[idx]) and isfloat(t_row[idx]):
226-
assert np.isclose(
227-
float(m_row[idx]), float(t_row[idx])
228-
)
229-
else:
230-
assert m_row[idx] == t_row[idx]
222+
actual = filter(ignore_path, gf)
223+
expected = filter(ignore_path, tf)
224+
are_files_same(actual, expected)

tests/test_morphpy.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@ class Chain:
9191
assert np.allclose(
9292
[rw], [self.morphapp_results[target_file.name]["Rw"]]
9393
)
94-
assert morph_results == self.morphapp_results
94+
# Check values in dictionaries are approximately equal
95+
for file in morph_results.keys():
96+
morph_params = morph_results[file]
97+
morphapp_params = self.morphapp_results[file]
98+
for key in morph_params.keys():
99+
assert morph_params[key] == pytest.approx(
100+
morphapp_params[key], abs=1e-08
101+
)
95102

96103
def test_morphpy(self, setup_morph):
97104
morph_results = {}
@@ -113,7 +120,14 @@ class Chain:
113120
assert np.allclose(
114121
[rw], [self.morphapp_results[target_file.name]["Rw"]]
115122
)
116-
assert morph_results == self.morphapp_results
123+
# Check values in dictionaries are approximately equal
124+
for file in morph_results.keys():
125+
morph_params = morph_results[file]
126+
morphapp_params = self.morphapp_results[file]
127+
for key in morph_params.keys():
128+
assert morph_params[key] == pytest.approx(
129+
morphapp_params[key], abs=1e-08
130+
)
117131

118132
def test_morphfuncy(self, setup_morph):
119133
def gaussian(x, mu, sigma):

0 commit comments

Comments
 (0)