forked from opensafely-core/cohort-extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_expressions.py
40 lines (33 loc) · 1.45 KB
/
test_expressions.py
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
import pytest
from cohortextractor.expressions import format_expression, InvalidExpressionError
def test_basic_expression_rewritting():
output, names = format_expression(
"foo AND (bar > 3 OR baz = 'hello')",
name_map={
"foo": "table1.foo",
"bar": "table2.bar",
"baz": "other",
"unused": "no",
},
empty_value_map={"foo": 0, "bar": 0, "baz": "", "unused": ""},
)
assert output == "( table1.foo != 0 ) AND ( table2.bar > 3 OR other = 'hello' )"
assert names == {"foo", "bar", "baz"}
def test_validation():
kwargs = dict(name_map={"a": "a", "b": "b"}, empty_value_map={"a": 0, "b": 0})
with pytest.raises(InvalidExpressionError):
format_expression("a AND AND b", **kwargs)
with pytest.raises(InvalidExpressionError):
format_expression("(a AND b", **kwargs)
with pytest.raises(InvalidExpressionError):
format_expression("a > > b", **kwargs)
def test_validate_string():
kwargs = dict(name_map={}, empty_value_map={})
with pytest.raises(ValueError):
format_expression('"no spaces"', **kwargs)
with pytest.raises(ValueError):
format_expression('"no$special$chars"', **kwargs)
with pytest.raises(ValueError):
format_expression('"all_ok_characters_but_just_a_bit_too_long"', **kwargs)
assert format_expression('"quoted"', **kwargs)[0] == "'quoted'"
assert format_expression('""', **kwargs)[0] == "''"