[e988c2]: / tests / lib / test_gentest_example_simplify.py

Download this file

81 lines (69 with data), 2.4 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
import importlib.util
import textwrap
from pathlib import Path
from hypothesis.vendor.pretty import pretty
from ehrql.query_model.nodes import (
AggregateByPatient,
Case,
Dataset,
Function,
InlinePatientTable,
SelectColumn,
SelectPatientTable,
Value,
)
from tests.generative.test_query_model import data_setup, schema
from tests.lib.gentest_example_simplify import simplify
# Basic smoketest as a crude guard against breakage
def test_gentest_example_simplify():
population = AggregateByPatient.Exists(
InlinePatientTable(rows=(), schema=schema),
)
# This silly example was constructed purely for the purposes of getting 100%
# coverage of `QueryModelRepr`
variable = Function.In(
Case(
{
Value(True): Function.MaximumOf(
(SelectColumn(SelectPatientTable("p0", schema), "i1"),),
)
},
default=None,
),
Value(frozenset({1, 2, 3})),
)
dataset = Dataset(
population=population, variables={"v": variable}, events={}, measures=None
)
data = [
{"type": data_setup.P0, "patient_id": 1},
]
partial_output = textwrap.dedent(
f"""\
# As might be formatted when copied directly from Hypothesis output
dataset={pretty(dataset)},
data={pretty(data)}
"""
)
source = simplify(partial_output)
module = exec_as_module(source)
assert module.dataset == dataset
assert module.data == data
# Confirm we're idempotent
assert source == simplify(source)
# Check that the simplify command works on a file which is itself tested to be
# executable by the gentests
def test_gentest_example_simplify_on_real_example():
orig_source = (
Path(__file__).parents[1].joinpath("generative", "example.py").read_text()
)
orig_module = exec_as_module(orig_source)
simplified_source = simplify(orig_source)
simplified_module = exec_as_module(simplified_source)
assert simplified_module.dataset == orig_module.dataset
assert simplified_module.data == orig_module.data
def exec_as_module(source):
spec = importlib.util.spec_from_loader("some_module", loader=None)
module = importlib.util.module_from_spec(spec)
exec(source, module.__dict__)
return module