[e988c2]: / tests / unit / test_docs.py

Download this file

268 lines (228 with data), 7.5 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
from ehrql.docs.__main__ import generate_docs, render
from ehrql.docs.render_includes.specs import render_specs
def test_generate_docs():
data = generate_docs()
expected = {"EMIS", "TPP"}
output = {b["name"] for b in data["backends"]}
assert expected <= output
# Find all series strings
all_series = [
paragraph["series"]
for spec in data["specs"]
for section in spec["sections"]
for paragraph in section["paragraphs"]
]
# Split the series string into its series and define_population components, if necessary
# assert that each component string has no leading whitespace for the first and last lines,
# and other lines have a multiple of 4 spaces
for series in all_series:
series_lines = series.split("\n")
population_lines = []
define_population_index = next(
(
i
for i, line in enumerate(series_lines)
if line.startswith("define_population")
),
None,
)
if define_population_index:
population_lines = series_lines[define_population_index:]
series_lines = series_lines[:define_population_index]
for lines_list in [series_lines, population_lines]:
for i, line in enumerate(lines_list):
if i in [0, len(lines_list) - 1]:
assert len(line.strip()) == len(line)
else:
leading_whitespace_count = len(line) - len(line.strip())
assert leading_whitespace_count % 4 == 0
def test_render(tmp_path):
assert not set(tmp_path.iterdir())
render(generate_docs(), tmp_path)
assert {pt.name for pt in tmp_path.iterdir()} == {
"backends.md",
"cli.md",
"language__codelists.md",
"language__dataset.md",
"language__date_arithmetic.md",
"language__frames.md",
"language__functions.md",
"language__measures.md",
"language__series.md",
"schemas",
"schemas.md",
"specs.md",
}
def test_render_specs():
specs = [
{
"id": "1",
"title": "Filtering an event frame",
"sections": [
{
"id": "1.1",
"title": "Including rows",
"paragraphs": [
{
"id": "1.1.1",
"title": "Take with column",
"tables": {
"e": [
["", "i1", "b1"],
["1", "101", "T"],
["2", "201", "T"],
["2", "203", "F"],
["3", "302", "F"],
]
},
"series": "e.where(e.b1).i1.sum_for_patient()",
"output": [["1", "203"], ["2", "201"], ["3", ""]],
}
],
}
],
}
]
expected = """## 1 Filtering an event frame
### 1.1 Including rows
#### 1.1.1 Take with column
This example makes use of an event-level table named `e` containing the following data:
| |i1|b1 |
| - | - | - |
| 1|101|T |
| 2|201|T |
| 2|203|F |
| 3|302|F |
```python
e.where(e.b1).i1.sum_for_patient()
```
returns the following patient series:
| patient | value |
| - | - |
| 1|203 |
| 2|201 |
| 3| |
"""
assert render_specs(specs) == expected
def test_render_specs_with_multiline_series():
specs = [
{
"id": "1",
"title": "Logical case expressions",
"sections": [
{
"id": "1.1",
"title": "Logical case expressions",
"paragraphs": [
{
"id": "1.1.1",
"title": "Case with expression",
"tables": {
"p": [
["patient", "i1"],
["1", "6"],
["2", "7"],
["3", "8"],
["4", "9"],
["5", ""],
]
},
"series": "case(\n when(p.i1 < 8).then(p.i1),\n when(p.i1 > 8).then(100),\n)",
"output": [
["1", "6"],
["2", "7"],
["3", ""],
["4", "100"],
["5", ""],
],
}
],
}
],
}
]
expected = """## 1 Logical case expressions
### 1.1 Logical case expressions
#### 1.1.1 Case with expression
This example makes use of a patient-level table named `p` containing the following data:
| patient|i1 |
| - | - |
| 1|6 |
| 2|7 |
| 3|8 |
| 4|9 |
| 5| |
```python
case(
when(p.i1 < 8).then(p.i1),
when(p.i1 > 8).then(100),
)
```
returns the following patient series:
| patient | value |
| - | - |
| 1|6 |
| 2|7 |
| 3| |
| 4|100 |
| 5| |
"""
assert render_specs(specs) == expected
def test_specs_with_additional_text():
specs = [
{
"id": "1",
"title": "Filtering an event frame",
"text": "Chapters may have additional descriptive text blocks",
"sections": [
{
"id": "1.1",
"title": "Including rows",
"text": "Additional text can also be added at a section level",
"paragraphs": [
{
"id": "1.1.1",
"title": "Take with column",
"text": "Further additional text can be provided for a paragraph",
"tables": {
"e": [
["", "i1", "b1"],
["1", "101", "T"],
["2", "201", "T"],
["2", "203", "F"],
["3", "302", "F"],
]
},
"series": "e.where(e.b1).i1.sum_for_patient()",
"output": [["1", "203"], ["2", "201"], ["3", ""]],
}
],
}
],
}
]
assert render_specs(specs) == (
"""## 1 Filtering an event frame
Chapters may have additional descriptive text blocks
### 1.1 Including rows
Additional text can also be added at a section level
#### 1.1.1 Take with column
Further additional text can be provided for a paragraph
This example makes use of an event-level table named `e` containing the following data:
| |i1|b1 |
| - | - | - |
| 1|101|T |
| 2|201|T |
| 2|203|F |
| 3|302|F |
```python
e.where(e.b1).i1.sum_for_patient()
```
returns the following patient series:
| patient | value |
| - | - |
| 1|203 |
| 2|201 |
| 3| |
"""
)