[e988c2]: / tests / spec / case_expressions / test_case.py

Download this file

103 lines (89 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
 98
 99
100
101
102
from ehrql import case, when
from ..tables import p
title = "Logical case expressions"
table_data = {
p: """
| i1
--+----
1 | 6
2 | 7
3 | 8
4 | 9
5 |
""",
}
def test_case_with_expression(spec_test):
spec_test(
table_data,
case(
when(p.i1 < 8).then(p.i1),
when(p.i1 > 8).then(100),
),
{
1: 6,
2: 7,
3: None,
4: 100,
5: None,
},
)
def test_case_with_default(spec_test):
spec_test(
table_data,
case(
when(p.i1 < 8).then(p.i1),
when(p.i1 > 8).then(100),
otherwise=0,
),
{
1: 6,
2: 7,
3: 0,
4: 100,
5: 0,
},
)
def test_case_with_boolean_column(spec_test):
"""
Note that individual boolean columns can be converted to the integers 0 and 1 using
the `as_int()` method.
"""
table_data = {
p: """
| i1 | b1
--+----+----
1 | 6 | T
2 | 7 | F
3 | 9 | F
4 |
""",
}
spec_test(
table_data,
case(
when(p.b1).then(p.i1),
when(p.i1 > 8).then(100),
),
{
1: 6,
2: None,
3: 100,
4: None,
},
)
def test_case_with_explicit_null(spec_test):
spec_test(
table_data,
case(
when(p.i1 < 8).then(None),
when(p.i1 > 8).then(100),
otherwise=200,
),
{
1: None,
2: None,
3: 200,
4: 100,
5: 200,
},
)