-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_expansion.py
275 lines (240 loc) · 8.34 KB
/
test_expansion.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
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
import os
import pytest
import arrow
from typer.testing import CliRunner
from pathlib import Path
from aerie_cli.__main__ import app
from aerie_cli.schemas.client import ActivityPlanCreate
from aerie_cli.schemas.client import Parcel
from .conftest import client, MODEL_JAR, MODEL_NAME, MODEL_VERSION, ARTIFACTS_PATH
runner = CliRunner(mix_stderr = False)
TEST_DIR = os.path.dirname(os.path.abspath(__file__))
FILES_PATH = os.path.join(TEST_DIR, "files")
DOWNLOADED_FILE_NAME = "downloaded_file.test"
EXPANSION_ARTIFACTS_PATH = Path(ARTIFACTS_PATH).joinpath("expansion")
EXPANSION_ARTIFACTS_PATH.mkdir()
# Model Variables
model_id = -1
# Plan Variables
PLANS_PATH = os.path.join(FILES_PATH, "plans")
PLAN_PATH = os.path.join(PLANS_PATH, "bake_bread_plan.json")
plan_id = -1
# Simulation Variables
sim_id = -1
# Command Dictionary Variables
COMMAND_DICTIONARIES_PATH = os.path.join(FILES_PATH, "dictionaries")
COMMAND_DICTIONARY_PATH = os.path.join(COMMAND_DICTIONARIES_PATH, "command_banananation.xml")
command_dictionary_id = -1
# Expansion Variables
expansion_set_id = -1
expansion_sequence_id = 1
EXPANSION_FILES_PATH = os.path.join(FILES_PATH, "expansion")
EXPANSION_DEPLOY_CONFIG_PATH = os.path.join(EXPANSION_FILES_PATH, "expansion_deploy_config.json")
@pytest.fixture(scope="module", autouse=True)
def set_up_environment(request):
resp = client.get_mission_models()
# delete all plans and models
for api_mission_model in resp:
client.delete_mission_model(api_mission_model.id)
for plan in client.get_all_activity_plans():
client.delete_plan(plan.id)
# upload model
global model_id
model_id = client.upload_mission_model(
mission_model_path=MODEL_JAR,
project_name=MODEL_NAME,
mission="",
version=MODEL_VERSION)
global command_dictionary_id
with open(COMMAND_DICTIONARY_PATH, 'r') as fid:
command_dictionary_id = client.create_dictionary(fid.read())
global parcel_id
parcel_id = client.create_parcel(Parcel("Integration Test", command_dictionary_id, None, None, []))
# upload plan
with open(PLAN_PATH) as fid:
contents = fid.read()
plan_to_create = ActivityPlanCreate.from_json(contents)
plan_to_create.name += arrow.utcnow().format("YYYY-MM-DDTHH-mm-ss")
global plan_id
plan_id = client.create_activity_plan(model_id, plan_to_create)
# simulate plan
global sim_id
sim_id = client.simulate_plan(plan_id)
#######################
# TEST EXPANSION SEQUENCES
# Uses plan and simulation dataset
#######################
def test_get_typescript_dictionary():
ts_dict = client.get_typescript_dictionary(command_dictionary_id)
with open(EXPANSION_ARTIFACTS_PATH.joinpath("command_dict.ts"), "w") as fid:
fid.write(ts_dict)
def test_expansion_sequence_create():
result = runner.invoke(
app,
["expansion", "sequences", "create"],
input=str(sim_id) + "\n" + str(expansion_sequence_id) + "\n" + str(2) + "\n",
catch_exceptions=False,)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{result.stderr}"
assert "Successfully created sequence" in result.stdout
def test_expansion_sequence_list():
result = runner.invoke(
app,
["expansion", "sequences", "list"],
input="2" + "\n" + str(sim_id) + "\n",
catch_exceptions=False,)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{result.stderr}"
assert "All sequences for Simulation Dataset" in result.stdout
def test_expansion_sequence_download():
result = runner.invoke(
app,
["expansion", "sequences", "download"],
input=str(sim_id) + "\n" + str(expansion_sequence_id) + "\n" + DOWNLOADED_FILE_NAME + "\n",
catch_exceptions=False,)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{result.stderr}"
path_to_sequence = Path(DOWNLOADED_FILE_NAME)
assert path_to_sequence.exists()
path_to_sequence.unlink()
def test_expansion_sequence_delete():
result = runner.invoke(
app,
["expansion", "sequences", "delete"],
input=str(sim_id) + "\n" + str(expansion_sequence_id) + "\n",
catch_exceptions=False,)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{result.stderr}"
assert "Successfully deleted sequence" in result.stdout
#######################
# TEST EXPANSION SETS
# Uses model, command dictionary, and activity types
#######################
def test_expansion_deploy():
result = runner.invoke(
app,
[
"expansion",
"deploy",
"-m",
str(model_id),
"-p",
str(parcel_id),
"-c",
EXPANSION_DEPLOY_CONFIG_PATH,
"--rules-path",
EXPANSION_FILES_PATH,
"--time-tag"
],
catch_exceptions=False
)
assert result.exit_code == 0, \
f"{result.stdout}"\
f"{result.stderr}"
assert "Created expansion rule integration_test_BakeBananaBread" in result.stdout
assert "Created expansion rule integration_test_BiteBanana" in result.stdout
assert "Failed to create expansion rule integration_test_bad" in result.stdout
assert "Created expansion set integration_test_set" in result.stdout
def test_expansion_set_create():
client.create_expansion_rule(
expansion_logic="""
export default function MyExpansion(props: {
activityInstance: ActivityType
}): ExpansionReturn {
const { activityInstance } = props;
return [];
}
""",
activity_name="BakeBananaBread",
model_id=model_id,
parcel_id=parcel_id
)
result = runner.invoke(
app,
[
"expansion",
"sets",
"create",
"-m",
str(model_id),
"-p",
str(parcel_id),
"-n",
"integration_test-" + arrow.utcnow().format("YYYY-MM-DDTHH-mm-ss"),
"-a",
"BakeBananaBread"
],
catch_exceptions=False,)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{result.stderr}"
global expansion_set_id
for line in result.stdout.splitlines():
if not "Created expansion set: " in line:
continue
# get expansion id from the end of the line
expansion_set_id = int(line.split(": ")[1])
assert expansion_set_id != -1, "Could not find expansion run ID, expansion create may have failed"\
f"{result.stdout}"\
f"{result.stderr}"
def test_expansion_set_get():
result = runner.invoke(
app,
["expansion", "sets", "get"],
input=str(expansion_set_id) + "\n",
catch_exceptions=False,)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{result.stderr}"
assert "Expansion Set" in result.stdout and "Contents" in result.stdout
def test_expansion_set_list():
result = runner.invoke(
app,
["expansion", "sets", "list"],
catch_exceptions=False,)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{result.stderr}"
assert "Expansion Sets" in result.stdout
assert "integration_test" in result.stdout
#######################
# TEST EXPANSION RUNS
# Uses plan and simulation dataset
#######################
def test_expansion_run_create():
result = runner.invoke(
app,
["expansion", "runs", "create"],
input=str(sim_id) + "\n" + str(expansion_set_id) + "\n",
catch_exceptions=False,)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{result.stderr}"
assert "Expansion Run ID: " in result.stdout
def test_expansion_runs_list():
result = runner.invoke(
app,
["expansion", "runs", "list"],
input="2" + "\n" + str(sim_id) + "\n",
catch_exceptions=False,)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{result.stderr}"
assert "Expansion Runs" in result.stdout
#######################
# DELETE MODELS
#######################
def test_model_delete():
result = runner.invoke(
app,
["models", "delete"],
input=str(model_id),
catch_exceptions=False,)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{result.stderr}"
assert f"ID: {model_id} has been removed" in result.stdout