-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_generate_openapi_schema.py
More file actions
74 lines (60 loc) · 1.97 KB
/
test_generate_openapi_schema.py
File metadata and controls
74 lines (60 loc) · 1.97 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
import sys
from pathlib import Path
import pytest
from pytest_mock import MockerFixture
import murfey
from murfey.cli.generate_openapi_schema import run
params_matrix: tuple[tuple[str | None, str | None, bool], ...] = (
# Target | Output | To File
(None, None, False),
("instrument-server", "json", True),
("server", "yaml", False),
("instrument-server", "yaml", False),
("server", "json", True),
)
@pytest.mark.parametrize("test_params", params_matrix)
def test_run(
mocker: MockerFixture,
tmp_path: Path,
test_params: tuple[str | None, str | None, bool],
):
# Unpack test params
target, output, to_file = test_params
# Mock out print() and exit()
mock_print = mocker.patch("builtins.print")
mock_exit = mocker.patch("builtins.exit")
# Construct the CLI args
sys_args = [""]
if target is not None:
sys_args.extend(["-t", target])
if output is not None:
sys_args.extend(["-o", output])
target = target if target is not None else "server"
output = output if output is not None else "yaml"
if to_file:
save_path = tmp_path / f"openapi.{output}"
sys_args.extend(["-f", str(save_path)])
else:
save_path = Path(murfey.__path__[0]) / "util" / f"openapi-{target}.{output}"
sys_args.extend(["--debug"])
sys.argv = sys_args
# Run the function and check that it runs as expected
run()
print_calls = mock_print.call_args_list
last_print_call = print_calls[-1]
last_printed = last_print_call.args[0]
assert last_printed.startswith("OpenAPI schema saved to")
mock_exit.assert_called_once()
assert save_path.exists()
failure_params_matrix = (
["-t", "blah"],
["-o", "blah"],
)
@pytest.mark.parametrize("test_params", failure_params_matrix)
def test_run_fails(test_params: list[str]):
# Construct the CLI args
sys_args = [""]
sys_args.extend(test_params)
sys.argv = sys_args
with pytest.raises(ValueError):
run()