Repository navigation
Expand file tree
/
Copy pathtest_models.py
More file actions
157 lines (133 loc) · 6.34 KB
/
Copy pathtest_models.py
File metadata and controls
157 lines (133 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""Test the pydantic models."""
import numpy as np
import pydantic
import pytest
from typing import Callable
import RAT.models
@pytest.mark.parametrize(["model", "model_name"], [
(RAT.models.Background, "Background"),
(RAT.models.Contrast, "Contrast"),
(RAT.models.CustomFile, "Custom File"),
(RAT.models.Data, "Data"),
(RAT.models.DomainContrast, "Domain Contrast"),
(RAT.models.Layer, "Layer"),
(RAT.models.Parameter, "Parameter"),
(RAT.models.Resolution, "Resolution"),
])
def test_default_names(model: Callable, model_name: str) -> None:
"""When initialising multiple models without specifying a name, they should be given a default name with the
format: "New <model name> <integer>".
"""
model_1 = model()
model_2 = model()
model_3 = model(name='Given Name')
model_4 = model()
assert model_1.name == f"New {model_name} 1"
assert model_2.name == f"New {model_name} 2"
assert model_3.name == "Given Name"
assert model_4.name == f"New {model_name} 3"
@pytest.mark.parametrize("model", [
RAT.models.Background,
RAT.models.Contrast,
RAT.models.ContrastWithRatio,
RAT.models.CustomFile,
RAT.models.Data,
RAT.models.DomainContrast,
RAT.models.Layer,
RAT.models.AbsorptionLayer,
RAT.models.Parameter,
RAT.models.Resolution,
])
class TestModels(object):
def test_initialise_with_wrong_type(self, model: Callable) -> None:
"""When initialising a model with the wrong type for the "name" field, we should raise a ValidationError."""
with pytest.raises(pydantic.ValidationError, match='Input should be a valid string'):
model(name=1)
def test_assignment_with_wrong_type(self, model: Callable) -> None:
"""When assigning the "name" field of a model with the wrong type, we should raise a ValidationError."""
test_model = model()
with pytest.raises(pydantic.ValidationError, match='Input should be a valid string'):
test_model.name = 1
def test_initialise_with_zero_length_name(self, model: Callable) -> None:
"""When initialising a model with a zero length name, we should raise a ValidationError."""
with pytest.raises(pydantic.ValidationError, match='String should have at least 1 characters'):
model(name='')
def test_initialise_with_extra_fields(self, model: Callable) -> None:
"""When initialising a model with unspecified fields, we should raise a ValidationError."""
with pytest.raises(pydantic.ValidationError, match='Extra inputs are not permitted'):
model(new_field=1)
@pytest.mark.parametrize("input_data", [
(np.array([[1.0, 1.0, 1.0]])),
])
def test_data_dimension(input_data: np.ndarray[float]) -> None:
"""The "data" field of the "Data" model should be a two-dimensional numpy array with at least three values in the
second dimension.
"""
test_data = RAT.models.Data(data=input_data)
assert (test_data.data == input_data).all()
@pytest.mark.parametrize("input_data", [
(np.array([])),
(np.array([1.0, 1.0])),
])
def test_data_too_few_dimensions(input_data: np.ndarray[float]) -> None:
"""If the "data" field of the "Data" model is not a two-dimensional numpy array we should raise a ValidationError.
"""
with pytest.raises(pydantic.ValidationError, match='1 validation error for Data\ndata\n Value error, "data" must '
'have at least two dimensions'):
RAT.models.Data(data=input_data)
@pytest.mark.parametrize("input_data", [
(np.array([[]])),
(np.array([[1.0]])),
(np.array([[1.0, 1.0]])),
])
def test_data_too_few_values(input_data: np.ndarray[float]) -> None:
"""If the second dimension of the array in the "data" field of the "Data" model has fewer than three values we
should raise a ValidationError.
"""
with pytest.raises(pydantic.ValidationError, match='1 validation error for Data\ndata\n Value error, "data" must '
'have at least three columns'):
RAT.models.Data(data=input_data)
@pytest.mark.parametrize("input_range", [
([1.0, 2.0]),
])
def test_data_ranges(input_range: list[float]) -> None:
"""The "data_range" and "simulation_range" fields of the "Data" model should contain exactly two values."""
assert RAT.models.Data(data_range=input_range).data_range == input_range
assert RAT.models.Data(simulation_range=input_range).simulation_range == input_range
@pytest.mark.parametrize("input_range", [
([]),
([1.0]),
([1.0, 2.0, 3.0]),
])
def test_two_values_in_data_range(input_range: list[float]) -> None:
"""If the "data_range" field of the "Data" model contains more or less than two values, we should raise a
ValidationError.
"""
with pytest.raises(pydantic.ValidationError, match='1 validation error for Data\ndata_range\n Value error, '
'data_range must contain exactly two values'):
RAT.models.Data(data_range=input_range)
@pytest.mark.parametrize("input_range", [
([]),
([1.0]),
([1.0, 2.0, 3.0]),
])
def test_two_values_in_simulation_range(input_range: list[float]) -> None:
"""If the "simulation_range" field of the "Data" model contains more or less than two values, we should raise a
ValidationError.
"""
with pytest.raises(pydantic.ValidationError, match='1 validation error for Data\nsimulation_range\n Value error, '
'simulation_range must contain exactly two values'):
RAT.models.Data(simulation_range=input_range)
@pytest.mark.parametrize("minimum, value, maximum", [
(0.0, 2.0, 1.0),
(0, 1, 0),
(1, -1, 1),
])
def test_parameter_range(minimum: float, value: float, maximum: float) -> None:
"""For the "Parameter" model, if the value of the "value" field does not lie with the values given in the "min" and
"max" fields, we should raise a ValidationError.
"""
with pytest.raises(pydantic.ValidationError, match=f'1 validation error for Parameter\n Value error, value '
f'{float(value)} is not within the defined range: '
f'{float(minimum)} <= value <= {float(maximum)}'):
RAT.models.Parameter(min=minimum, value=value, max=maximum)