-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_launch.py
142 lines (102 loc) · 3.61 KB
/
test_launch.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
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
import json
from dataclasses import dataclass
import pytest
from pydantic import create_model
from pytest_mock import MockerFixture
from ruamel.yaml import YAML
from typer.testing import CliRunner
from fastcs.__main__ import __version__
from fastcs.attributes import AttrR
from fastcs.controller import Controller
from fastcs.datatypes import Int
from fastcs.exceptions import LaunchError
from fastcs.launch import TransportOptions, _launch, get_controller_schema, launch
@dataclass
class SomeConfig:
name: str
class SingleArg(Controller):
def __init__(self):
super().__init__()
class NotHinted(Controller):
def __init__(self, arg):
super().__init__()
class IsHinted(Controller):
read = AttrR(Int())
def __init__(self, arg: SomeConfig) -> None:
super().__init__()
class ManyArgs(Controller):
def __init__(self, arg: SomeConfig, too_many):
super().__init__()
runner = CliRunner()
def test_single_arg_schema():
target_model = create_model(
"SingleArg",
transport=(TransportOptions, ...),
__config__={"extra": "forbid"},
)
target_dict = target_model.model_json_schema()
app = _launch(SingleArg)
result = runner.invoke(app, ["schema"])
assert result.exit_code == 0
result_dict = json.loads(result.stdout)
assert result_dict == target_dict
def test_is_hinted_schema(data):
target_model = create_model(
"IsHinted",
controller=(SomeConfig, ...),
transport=(TransportOptions, ...),
__config__={"extra": "forbid"},
)
target_dict = target_model.model_json_schema()
app = _launch(IsHinted)
result = runner.invoke(app, ["schema"])
assert result.exit_code == 0
result_dict = json.loads(result.stdout)
assert result_dict == target_dict
# # store a schema to use for debugging
# with open(data / "schema.json", mode="w") as f:
# json.dump(result_dict, f, indent=2)
def test_not_hinted_schema():
error = (
"Expected typehinting in 'NotHinted.__init__' but received "
"(self, arg). Add a typehint for `arg`."
)
with pytest.raises(LaunchError) as exc_info:
launch(NotHinted)
assert str(exc_info.value) == error
def test_over_defined_schema():
error = (
""
"Expected no more than 2 arguments for 'ManyArgs.__init__' "
"but received 3 as `(self, arg: tests.test_launch.SomeConfig, too_many)`"
)
with pytest.raises(LaunchError) as exc_info:
launch(ManyArgs)
assert str(exc_info.value) == error
def test_version():
impl_version = "0.0.1"
expected = f"SingleArg: {impl_version}\nFastCS: {__version__}\n"
app = _launch(SingleArg, version=impl_version)
result = runner.invoke(app, ["--version"])
assert result.exit_code == 0
assert result.stdout == expected
def test_no_version():
expected = f"FastCS: {__version__}\n"
app = _launch(SingleArg)
result = runner.invoke(app, ["--version"])
assert result.exit_code == 0
assert result.stdout == expected
def test_launch(mocker: MockerFixture, data):
run = mocker.patch("fastcs.launch.FastCS.run")
gui = mocker.patch("fastcs.launch.FastCS.create_gui")
docs = mocker.patch("fastcs.launch.FastCS.create_docs")
app = _launch(IsHinted)
result = runner.invoke(app, ["run", str(data / "config.yaml")])
assert result.exit_code == 0
run.assert_called_once()
gui.assert_called_once()
docs.assert_called_once()
def test_get_schema(data):
ref_schema = YAML(typ="safe").load(data / "schema.json")
target_schema = get_controller_schema(IsHinted)
assert target_schema == ref_schema