[e988c2]: / tests / spec / population / test_population.py

Download this file

98 lines (85 with data), 1.8 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
from ehrql import case, when
from ..tables import e, p
title = "Defining a population"
text = """
`define_population` is used to limit the population from which data is extracted.
"""
def test_population_with_single_table(spec_test):
"""
Extract a column from a patient table after limiting the population by another column.
"""
table_data = {
p: """
| b1 | i1
--+----+---
1 | F | 10
2 | T | 20
3 | F | 30
""",
}
spec_test(
table_data,
p.i1,
{
1: 10,
3: 30,
},
population=~p.b1,
)
def test_population_with_multiple_tables(spec_test):
"""
Limit the patient population by a column in one table, and return values from another
table.
"""
table_data = {
p: """
| i1
--+----
1 | 10
2 | 20
3 | 0
""",
e: """
| i1
--+-----
1 | 101
1 | 102
3 | 301
4 | 401
""",
}
spec_test(
table_data,
e.exists_for_patient(),
{
1: True,
2: False,
},
population=p.i1 > 0,
)
def test_case_with_case_expression(spec_test):
"""
Limit the patient population by a case expression.
"""
table_data = {
p: """
| i1
--+---
1 | 6
2 | 7
3 | 9
4 |
""",
}
spec_test(
table_data,
p.i1,
{
1: 6,
2: 7,
},
population=case(
when(p.i1 <= 8).then(True),
when(p.i1 > 8).then(False),
),
)