[e988c2]: / tests / unit / query_model / test_population_validation.py

Download this file

195 lines (177 with data), 5.2 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import pytest
from ehrql.query_model.nodes import (
AggregateByPatient,
Case,
Column,
Function,
SelectColumn,
SelectPatientTable,
SelectTable,
TableSchema,
Value,
)
from ehrql.query_model.population_validation import (
EmptyQueryEngine,
ValidationError,
validate_population_definition,
)
def test_rejects_non_series():
aint_no_series = "I am not a Series"
with pytest.raises(ValidationError, match="must be a `query_model.Series`"):
validate_population_definition(aint_no_series)
def test_rejects_invalid_dimension():
event_series = SelectColumn(
SelectTable("events", schema=TableSchema(value=Column(bool))), "value"
)
with pytest.raises(ValidationError, match="one-row-per-patient series"):
validate_population_definition(event_series)
def test_rejects_invalid_type():
int_series = SelectColumn(
SelectPatientTable("patients", schema=TableSchema(value=Column(int))), "value"
)
with pytest.raises(ValidationError, match="boolean type"):
validate_population_definition(int_series)
def test_accepts_basic_reasonable_population():
has_registration = AggregateByPatient.Exists(
SelectTable("registrations", schema=TableSchema(value=Column(int)))
)
assert validate_population_definition(has_registration)
def test_rejects_basic_unreasonable_population():
not_died = Function.Not(
AggregateByPatient.Exists(
SelectTable("ons_deaths", schema=TableSchema(value=Column(int)))
)
)
with pytest.raises(ValidationError, match="must not evaluate as True for NULL"):
validate_population_definition(not_died)
# TEST SERIES EVALUATION AGAINST EMPTY DATABASE
#
patients = SelectPatientTable("patients", schema=TableSchema(value=Column(int)))
events = SelectTable("events", schema=TableSchema(value=Column(int)))
patients_value = SelectColumn(patients, "value")
events_value = SelectColumn(events, "value")
cases = [
(
True,
# events.count_for_patient() + 3 >= 3
Function.GE(
Function.Add(AggregateByPatient.Count(events), Value(3)),
Value(3),
),
),
(
False,
# events.count_for_patient() + 3 >= 4
Function.GE(
Function.Add(AggregateByPatient.Count(events), Value(3)),
Value(4),
),
),
(
True,
# patients.v.is_null()
Function.IsNull(patients_value),
),
(
False,
# ~patients.v.is_null()
Function.Not(Function.IsNull(patients_value)),
),
(
True,
# events.v.max_for_patient().is_null()
Function.IsNull(AggregateByPatient.Max(events_value)),
),
(
False,
# events.v.max_for_patient() > 100
Function.GT(
AggregateByPatient.Max(events_value),
Value(100),
),
),
(
True,
# patients.v == 1 | (10 < 20)
Function.Or(
Function.EQ(patients_value, Value(1)),
Function.LT(Value(2), Value(3)),
),
),
(
False,
# patients.v == 100 | (patients.v <= 20)
Function.Or(
Function.EQ(patients_value, Value(100)),
Function.LE(patients_value, Value(3)),
),
),
(
False,
# ~patients.v.is_null | events.exists_for_patient()
Function.Or(
Function.Not(Function.IsNull(patients_value)),
AggregateByPatient.Exists(events),
),
),
(
True,
# (1 == 1) & (2 == 2)
Function.And(
Function.EQ(Value(1), Value(1)),
Function.EQ(Value(2), Value(2)),
),
),
(
False,
# ~(patients.v == 100 & events.exists_for_patient())
Function.And(
Function.EQ(patients_value, Value(100)),
AggregateByPatient.Exists(events),
),
),
(
False,
# patients.v == 1 & events.v.sum_for_patient() == 2
Function.And(
Function.EQ(patients_value, Value(1)),
Function.EQ(AggregateByPatient.Sum(events_value), Value(2)),
),
),
(
True,
Function.In(Value(10), Value(frozenset([30, 20, 10]))),
),
(
True,
Case(
{
Function.EQ(patients_value, Value(1)): Value(False),
Function.EQ(Value(1), Value(1)): Value(True),
},
default=None,
),
),
(
True,
Case(
{
Function.EQ(patients_value, Value(1)): Value(False),
Function.EQ(patients_value, Value(2)): Value(False),
},
default=Value(True),
),
),
(
True,
Function.StringContains(Value("foobar"), Value("oba")),
),
(
False,
Function.StringContains(Value("foo bar"), Value("oba")),
),
]
@pytest.mark.parametrize("expected,query", cases)
def test_series_evaluates_true(expected, query):
result = EmptyQueryEngine(None).series_evaluates_true(query)
assert result == expected, f"Expected {expected}, got {result} in:\n{query}"