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

Download this file

356 lines (281 with data), 10.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from pathlib import Path
import pytest
from ehrql.__main__ import (
BACKEND_ALIASES,
QUERY_ENGINE_ALIASES,
ArgumentTypeError,
DefinitionError,
FileValidationError,
backend_from_id,
import_string,
main,
query_engine_from_id,
valid_output_path,
)
from ehrql.backends.base import SQLBackend
from ehrql.query_engines.base import BaseQueryEngine
from ehrql.query_engines.base_sql import BaseSQLQueryEngine
from ehrql.query_engines.debug import DebugQueryEngine
from ehrql.query_engines.in_memory import InMemoryQueryEngine
from ehrql.utils.module_utils import get_sibling_subclasses
# We just need any old existing file with a ".py" extension for testing purposes, its
# contents are immaterial; this one will do
DATASET_DEFINITON_PATH = __file__
def test_no_args(capsys):
# Verify that when ehrql is called without arguments, help text is shown.
with pytest.raises(SystemExit):
main([])
captured = capsys.readouterr()
assert "usage: ehrql" in captured.out
def test_generate_dataset(mocker):
# Verify that the generate_dataset subcommand can be invoked.
patched = mocker.patch("ehrql.__main__.generate_dataset")
argv = [
"generate-dataset",
DATASET_DEFINITON_PATH,
]
main(argv)
patched.assert_called_once()
def test_generate_dataset_rejects_unknown_extension(capsys):
argv = [
"generate-dataset",
DATASET_DEFINITON_PATH,
"--output",
"out_file.badformat",
]
with pytest.raises(SystemExit):
main(argv)
captured = capsys.readouterr()
assert ".badformat' is not a supported format" in captured.err
def test_generate_dataset_with_definition_error(capsys, mocker):
# Verify that the generate_dataset subcommand can be invoked.
patched = mocker.patch("ehrql.__main__.generate_dataset")
patched.side_effect = DefinitionError("Not a good dataset definition")
argv = [
"generate-dataset",
DATASET_DEFINITON_PATH,
]
with pytest.raises(SystemExit):
main(argv)
captured = capsys.readouterr()
assert "Not a good dataset definition" in captured.err
assert "Traceback" not in captured.err
def test_generate_dataset_with_validation_error(capsys, mocker):
# Verify that the generate_dataset subcommand can be invoked.
patched = mocker.patch("ehrql.__main__.generate_dataset")
patched.side_effect = FileValidationError("Your file was bad")
argv = [
"generate-dataset",
DATASET_DEFINITON_PATH,
]
with pytest.raises(SystemExit):
main(argv)
captured = capsys.readouterr()
assert "Your file was bad" in captured.err
assert "Traceback" not in captured.err
def test_dump_dataset_sql(mocker):
# Verify that the dump_dataset_sql subcommand can be invoked.
patched = mocker.patch("ehrql.__main__.dump_dataset_sql")
argv = [
"dump-dataset-sql",
"--backend",
"ehrql.backends.tpp.TPPBackend",
DATASET_DEFINITON_PATH,
]
main(argv)
patched.assert_called_once()
@pytest.mark.parametrize("output_path", ["dummy_data_path", "dummy_data_path:arrow"])
def test_create_dummy_tables(mocker, output_path):
# Verify that the create_dummy_tables subcommand can be invoked.
patched = mocker.patch("ehrql.__main__.create_dummy_tables")
argv = [
"create-dummy-tables",
DATASET_DEFINITON_PATH,
output_path,
]
main(argv)
patched.assert_called_once()
def test_create_dummy_tables_rejects_unsupported_format(capsys):
argv = [
"create-dummy-tables",
DATASET_DEFINITON_PATH,
"dummy_data_path:invalid",
]
with pytest.raises(SystemExit):
main(argv)
captured = capsys.readouterr()
assert "':invalid' is not a supported format" in captured.err
def test_generate_measures(mocker):
# Verify that the generate_measures subcommand can be invoked.
patched = mocker.patch("ehrql.__main__.generate_measures")
argv = [
"generate-measures",
DATASET_DEFINITON_PATH,
]
main(argv)
patched.assert_called_once()
def test_existing_python_file_missing_file(capsys, tmp_path):
# Verify that a helpful message is shown when a command is invoked with a path to a
# file that should exist but doesn't.
dataset_definition_path = tmp_path / "dataset.py"
argv = [
"generate-dataset",
str(dataset_definition_path),
]
with pytest.raises(SystemExit):
main(argv)
captured = capsys.readouterr()
assert "dataset.py does not exist" in captured.err
def test_existing_python_file_unpythonic_file(capsys, tmp_path):
# Verify that a helpful message is shown when a command is invoked with a path to a
# file that should be a Python file but isn't.
dataset_definition_path = tmp_path / "dataset.cpp"
dataset_definition_path.touch()
argv = [
"generate-dataset",
str(dataset_definition_path),
]
with pytest.raises(SystemExit):
main(argv)
captured = capsys.readouterr()
assert "dataset.cpp is not a Python file" in captured.err
def test_existing_directory_missing_directory(capsys, tmp_path):
dataset_definition_path = tmp_path / "dataset.py"
dataset_definition_path.touch()
argv = [
"generate-dataset",
str(dataset_definition_path),
"--dummy-tables",
"non-existent-directory",
]
with pytest.raises(SystemExit):
main(argv)
captured = capsys.readouterr()
assert "non-existent-directory does not exist" in captured.err
def test_existing_directory_not_a_directory(capsys, tmp_path):
dataset_definition_path = tmp_path / "dataset.py"
dataset_definition_path.touch()
file_path = tmp_path / "not-a-directory.file"
file_path.touch()
argv = [
"generate-dataset",
str(dataset_definition_path),
"--dummy-tables",
str(file_path),
]
with pytest.raises(SystemExit):
main(argv)
captured = capsys.readouterr()
assert "not-a-directory.file is not a directory" in captured.err
def test_existing_file_missing_file(capsys, tmp_path):
dataset_definition_path = tmp_path / "dataset.py"
dataset_definition_path.touch()
argv = [
"generate-dataset",
str(dataset_definition_path),
"--dummy-data-file",
"non-existent-file",
]
with pytest.raises(SystemExit):
main(argv)
captured = capsys.readouterr()
assert "non-existent-file does not exist" in captured.err
def test_existing_file_not_a_file(capsys, tmp_path):
dataset_definition_path = tmp_path / "dataset.py"
dataset_definition_path.touch()
directory_path = tmp_path / "not-a-file"
directory_path.mkdir()
argv = [
"generate-dataset",
str(dataset_definition_path),
"--dummy-data-file",
str(directory_path),
]
with pytest.raises(SystemExit):
main(argv)
captured = capsys.readouterr()
assert "not-a-file is not a file" in captured.err
def test_import_string():
assert import_string("ehrql.__main__.main") is main
def test_import_string_not_a_dotted_path():
with pytest.raises(ArgumentTypeError, match="must be a full dotted path"):
import_string("urllib")
def test_import_string_no_such_module():
with pytest.raises(ArgumentTypeError, match="could not import module"):
import_string("urllib.this_is_not_a_module.Foo")
def test_import_string_no_such_attribute():
with pytest.raises(ArgumentTypeError, match="'urllib.parse' has no attribute"):
import_string("urllib.parse.ThisIsNotAClass")
class DummyQueryEngine:
def get_results_tables(self):
raise NotImplementedError()
def test_query_engine_from_id():
engine_id = f"{DummyQueryEngine.__module__}.{DummyQueryEngine.__name__}"
assert query_engine_from_id(engine_id) is DummyQueryEngine
def test_query_engine_from_id_missing_alias():
with pytest.raises(ArgumentTypeError, match="must be one of"):
query_engine_from_id("missing")
def test_query_engine_from_id_wrong_type():
with pytest.raises(ArgumentTypeError, match="is not a valid query engine"):
query_engine_from_id("pathlib.Path")
class DummyBackend:
def get_table_expression(self):
raise NotImplementedError()
def test_backend_from_id():
engine_id = f"{DummyBackend.__module__}.{DummyBackend.__name__}"
assert backend_from_id(engine_id) is DummyBackend
def test_backend_from_id_missing_alias():
with pytest.raises(ArgumentTypeError, match="must be one of"):
backend_from_id("missing")
def test_backend_from_id_wrong_type():
with pytest.raises(ArgumentTypeError, match="is not a valid backend"):
backend_from_id("pathlib.Path")
@pytest.mark.parametrize("alias", ["expectations", "test"])
def test_backend_from_id_special_case_aliases(alias):
assert backend_from_id(alias) is None
def test_all_query_engine_aliases_are_importable():
for alias in QUERY_ENGINE_ALIASES.keys():
assert query_engine_from_id(alias)
def test_all_backend_aliases_are_importable():
for alias in BACKEND_ALIASES.keys():
assert backend_from_id(alias)
def test_all_query_engines_have_an_alias():
for cls in get_sibling_subclasses(BaseQueryEngine):
if cls in [
BaseSQLQueryEngine,
InMemoryQueryEngine,
DebugQueryEngine,
]:
continue
name = f"{cls.__module__}.{cls.__name__}"
assert name in QUERY_ENGINE_ALIASES.values(), f"No alias defined for '{name}'"
def test_all_backends_have_an_alias():
for cls in get_sibling_subclasses(SQLBackend):
name = f"{cls.__module__}.{cls.__name__}"
assert name in BACKEND_ALIASES.values(), f"No alias defined for '{name}'"
def test_all_backend_aliases_match_display_names():
for alias in BACKEND_ALIASES.keys():
assert backend_from_id(alias).display_name.lower() == alias
@pytest.mark.parametrize(
"path",
[
"some/path/file.csv",
"some/path/dir:csv",
"some/path/dir/:csv",
"some/path/dir.foo:csv",
],
)
def test_valid_output_path(path):
assert valid_output_path(path) == Path(path)
@pytest.mark.parametrize(
"path, message",
[
("no/extension", "No file format supplied"),
("some/path.badfile", "'.badfile' is not a supported format"),
("some/path:baddir", "':baddir' is not a supported format"),
("some/path/:baddir", "':baddir' is not a supported format"),
],
)
def test_valid_output_path_errors(path, message):
with pytest.raises(ArgumentTypeError, match=message):
valid_output_path(path)