Skip to content

Commit

Permalink
refactor(tests): move test_negative_inputs to unit test
Browse files Browse the repository at this point in the history
Although it uses Param and Model, it is focussed on testing functionality of Model in detecting invalid parameters, so feels appropriately classified as a unit test
  • Loading branch information
amyheather committed Feb 5, 2025
1 parent 3f1d018 commit c140f9e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
36 changes: 0 additions & 36 deletions tests/test_functionaltest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,6 @@
from simulation.model import Param, Model, Runner, MonitoredResource


@pytest.mark.parametrize('param_name, value, rule', [
('patient_inter', 0, 'positive'),
('mean_n_consult_time', 0, 'positive'),
('number_of_runs', 0, 'positive'),
('audit_interval', 0, 'positive'),
('warm_up_period', -1, 'non_negative'),
('data_collection_period', -1, 'non_negative')
])
def test_negative_inputs(param_name, value, rule):
"""
Check that the model fails when inputs that are zero or negative are used.
Arguments:
param_name (string):
Name of parameter to change in the Param() class.
value (float|int):
Invalid value for parameter.
rule (string):
Either 'positive' (if value must be > 0) or 'non-negative' (if
value must be >= 0).
"""
# Create parameter class with an invalid value
param = Param(**{param_name: value})

# Construct the expected error message
if rule == 'positive':
expected_message = f'Parameter "{param_name}" must be greater than 0.'
else:
expected_message = (f'Parameter "{param_name}" must be greater than ' +
'or equal to 0.')

# Verify that initialising the model raises the correct error
with pytest.raises(ValueError, match=expected_message):
Model(param=param, run_number=0)


def test_negative_results():
"""
Check that values are non-negative.
Expand Down
38 changes: 37 additions & 1 deletion tests/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pytest

from simulation.logging import SimLogger
from simulation.model import Param, Exponential
from simulation.model import Param, Model, Exponential


def test_new_attribute():
Expand All @@ -38,6 +38,42 @@ def test_new_attribute():
param.new_entry = 3


@pytest.mark.parametrize('param_name, value, rule', [
('patient_inter', 0, 'positive'),
('mean_n_consult_time', 0, 'positive'),
('number_of_runs', 0, 'positive'),
('audit_interval', 0, 'positive'),
('warm_up_period', -1, 'non_negative'),
('data_collection_period', -1, 'non_negative')
])
def test_negative_inputs(param_name, value, rule):
"""
Check that the model fails when inputs that are zero or negative are used.
Arguments:
param_name (string):
Name of parameter to change in the Param() class.
value (float|int):
Invalid value for parameter.
rule (string):
Either 'positive' (if value must be > 0) or 'non-negative' (if
value must be >= 0).
"""
# Create parameter class with an invalid value
param = Param(**{param_name: value})

# Construct the expected error message
if rule == 'positive':
expected_message = f'Parameter "{param_name}" must be greater than 0.'
else:
expected_message = (f'Parameter "{param_name}" must be greater than ' +
'or equal to 0.')

# Verify that initialising the model raises the correct error
with pytest.raises(ValueError, match=expected_message):
Model(param=param, run_number=0)


def test_exponentional():
"""
Test that the Exponentional class behaves as expected.
Expand Down

0 comments on commit c140f9e

Please sign in to comment.