Skip to content

Commit de84c3a

Browse files
committed
python/tests/ 100% coverage of tests.
* python/tests/conftest.py (immutable_call): Remove unused freeze argument. * python/tests/test_immutability.py: Make sure all paths are tested. * python/tests/test_moocore.py (test_read_datasets_data): expected_name cannot be empty.
1 parent 801a193 commit de84c3a

3 files changed

Lines changed: 18 additions & 20 deletions

File tree

python/tests/conftest.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ def immutable_call():
2323
Call a function and assert that selected arguments remain immutable.
2424
2525
Usage:
26-
immutable_call(fn, *args, **kwargs, immutable=[0, "y"], freeze=True)
26+
immutable_call(fn, *args, **kwargs, immutable=[0, "y"])
2727
"""
2828

29-
def _call(fn, *args, immutable=None, freeze=True, **kwargs):
29+
def _call(fn, *args, immutable=None, **kwargs):
3030
# If immutable is None, then all positional and keyword args are
3131
# immutable.
3232
if immutable is None:
@@ -37,14 +37,13 @@ def _call(fn, *args, immutable=None, freeze=True, **kwargs):
3737
args_copy = [deep_copy(a) for a in args]
3838
kwargs_copy = {k: deep_copy(v) for k, v in kwargs.items()}
3939

40-
# Freeze mutable args if needed
41-
if freeze:
42-
for idx, a in enumerate(args):
43-
if idx in immutable:
44-
freeze_numpy(a)
45-
for k, v in kwargs.items():
46-
if k in immutable:
47-
freeze_numpy(v)
40+
# Freeze mutable args.
41+
for idx, a in enumerate(args):
42+
if idx in immutable:
43+
freeze_numpy(a)
44+
for k, v in kwargs.items():
45+
if k in immutable:
46+
freeze_numpy(v)
4847

4948
# Run the function — any exception is a failure
5049
try:

python/tests/test_immutability.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ def test_partial_immutability_positional(immutable_call):
1919
"""Check partial immutable_call works."""
2020

2121
def bad(x, y):
22-
x[0] = 99 # SHOULD be caught
22+
y[0] = 99 # SHOULD be caught
2323
return x.sum() + y.sum() # nocov
2424

2525
def good(x, y):
26-
y[0] = 88 # allowed
26+
x[0] = 88 # allowed
2727
return x.sum() + y.sum()
2828

2929
x = np.array([1, 2, 3])
3030
y = np.array([5, 6, 7])
3131

3232
with pytest.raises(AssertionError):
33-
immutable_call(bad, x, y, immutable=[0]) # x must remain immutable
33+
immutable_call(bad, x, y, immutable=[1]) # x must remain immutable
3434

35-
immutable_call(good, x, y, immutable=[0]) # x must remain immutable
35+
immutable_call(good, x, y, immutable=[1]) # x must remain immutable
3636

3737

3838
def test_partial_immutability_keyword(immutable_call):
@@ -50,9 +50,9 @@ def good(a, b):
5050
b = np.array([3, 4])
5151

5252
with pytest.raises(AssertionError):
53-
immutable_call(bad, a, b=b, immutable=["b"])
53+
immutable_call(bad, a=a, b=b, immutable=["b"])
5454

55-
immutable_call(good, a, b=b, immutable=["b"])
55+
immutable_call(good, a=a, b=b, immutable=["b"])
5656

5757

5858
def test_nested_structures(immutable_call):

python/tests/test_moocore.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ def test_read_datasets_data(test_datapath, test, expected_name, expected_shape):
3939
assert testdata.shape == expected_shape, (
4040
f"Read data array has incorrect shape, should be {expected_shape} but is {testdata.shape}"
4141
)
42-
if expected_name != "":
43-
check_data = np.loadtxt(
44-
test_datapath("expected_output/read_datasets/" + expected_name)
45-
)
42+
check_data = np.loadtxt(
43+
test_datapath("expected_output/read_datasets/" + expected_name)
44+
)
4645
assert_allclose(
4746
testdata,
4847
check_data,

0 commit comments

Comments
 (0)