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

Download this file

46 lines (32 with data), 1.0 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
from datetime import date
from ehrql.tables import Constraint
def test_categorical_validation():
c = Constraint.Categorical((1, "a"))
assert c.validate("a")
assert c.validate(None)
assert not c.validate("")
assert not c.validate("b")
def test_not_null_validation():
c = Constraint.NotNull()
assert c.validate(1)
assert not c.validate(None)
def test_unique_validation():
c = Constraint.Unique()
assert c.validate(1)
def test_first_of_month_validation():
c = Constraint.FirstOfMonth()
assert c.validate(date(2024, 1, 1))
assert c.validate(None)
assert not c.validate(date(2024, 1, 2))
def test_regex_validation():
c = Constraint.Regex("E020[0-9]{5}")
assert c.validate("E02012345")
assert c.validate(None)
assert not c.validate("")
assert not c.validate("E020")
def test_closed_range_validation():
c = Constraint.ClosedRange(1, 3)
assert c.validate(2)
assert c.validate(None)
assert not c.validate(0)
assert not c.validate(4)