-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_test_results.py
More file actions
412 lines (373 loc) · 15.5 KB
/
Copy pathtest_test_results.py
File metadata and controls
412 lines (373 loc) · 15.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
from __future__ import annotations
import uuid
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import ClassVar
import grpc
import pytest
from grpc import aio as aiogrpc
from sift_client.resources import TestResultsAPI, TestResultsAPIAsync
from sift_client.sift_types.test_report import (
ErrorInfo,
NumericBounds,
TestMeasurement,
TestMeasurementCreate,
TestMeasurementType,
TestReport,
TestReportUpdate,
TestStatus,
TestStep,
TestStepCreate,
TestStepType,
)
from sift_client.util import cel_utils as cel
pytestmark = pytest.mark.integration
def test_client_binding(sift_client):
assert sift_client.test_results
assert isinstance(sift_client.test_results, TestResultsAPI)
assert sift_client.async_.test_results
assert isinstance(sift_client.async_.test_results, TestResultsAPIAsync)
class TestResultsTest:
test_reports: ClassVar[dict[str, TestReport]] = {}
test_steps: ClassVar[dict[str, TestStep]] = {}
test_measurements: ClassVar[dict[str, TestMeasurement]] = {}
def test_create_test_report(self, sift_client, nostromo_run):
# Create a test report
simulated_time = datetime.now(timezone.utc)
test_report = sift_client.test_results.create(
{
"status": TestStatus.PASSED,
"name": "Test Report with Steps and Measurements",
"test_system_name": "Test System",
"test_case": "Test Case",
"serial_number": str(uuid.uuid4()),
"part_number": "1234567890",
"start_time": simulated_time,
"end_time": simulated_time,
"run_id": nostromo_run._id_or_error,
},
)
assert test_report.id_ is not None
assert test_report.run_id == nostromo_run.id_
self.test_reports["basic_test_report"] = test_report
def test_create_test_steps(self, sift_client):
test_report = self.test_reports.get("basic_test_report")
if not test_report:
pytest.skip("Need to create a test report first")
return
simulated_time = test_report.start_time
# Create multiple test steps using TestStepCreate
step1 = sift_client.test_results.create_step(
TestStepCreate(
test_report_id=test_report._id_or_error,
name="Step 1: Initialization",
description="Initialize the test environment",
step_type=TestStepType.ACTION,
step_path="1",
status=TestStatus.PASSED,
start_time=simulated_time,
end_time=simulated_time + timedelta(seconds=10),
),
)
simulated_time = simulated_time + timedelta(seconds=10.1)
# Create a step using a dict
step1_1 = sift_client.test_results.create_step(
{
"test_report_id": test_report._id_or_error,
"parent_step_id": step1._id_or_error,
"name": "Step 1.1: Substep 1",
"description": "Substep 1 of Step 1",
"step_type": TestStepType.ACTION,
"step_path": "1.1",
"status": TestStatus.PASSED,
"start_time": simulated_time,
"end_time": simulated_time + timedelta(seconds=10),
},
)
simulated_time = simulated_time + timedelta(seconds=10.1)
step2 = sift_client.test_results.create_step(
TestStepCreate(
test_report_id=test_report._id_or_error,
name="Step 2: Data Collection",
description="Collect sensor data",
step_type=TestStepType.ACTION,
step_path="2",
status=TestStatus.PASSED,
start_time=simulated_time,
end_time=simulated_time + timedelta(seconds=10),
)
)
simulated_time = simulated_time + timedelta(seconds=10.1)
step3 = sift_client.test_results.create_step(
TestStepCreate(
test_report_id=test_report._id_or_error,
name="Step 3: Validation",
description="Validate collected data",
step_type=TestStepType.ACTION,
step_path="3",
status=TestStatus.IN_PROGRESS,
start_time=simulated_time,
end_time=simulated_time + timedelta(seconds=10),
),
)
step3_1 = sift_client.test_results.create_step(
TestStepCreate(
test_report_id=test_report._id_or_error,
parent_step_id=step3._id_or_error,
name="Step 3.1: Substep 3.1",
description="Error demo",
step_type=TestStepType.ACTION,
step_path="3.1",
status=TestStatus.FAILED,
start_time=simulated_time,
end_time=simulated_time + timedelta(seconds=11),
error_info=ErrorInfo(
error_code=1,
error_message="Demo error message",
),
),
)
assert step1.id_ is not None
assert step1_1.id_ is not None
assert step2.id_ is not None
assert step3.id_ is not None
assert step3_1.id_ is not None
self.test_steps["step1"] = step1
self.test_steps["step1_1"] = step1_1
self.test_steps["step2"] = step2
self.test_steps["step3"] = step3
self.test_steps["step3_1"] = step3_1
def test_update_test_steps(self, sift_client):
step3 = self.test_steps.get("step3")
step3_1 = self.test_steps.get("step3_1")
if not step3 or not step3_1:
pytest.skip("Need to create a step first")
return
step3 = sift_client.test_results.update_step(
step3,
{"status": TestStatus.PASSED},
)
# Update the step using class function.
step3_1 = step3_1.update(
{"description": "Error demo w/ updated description"},
)
assert step3.status == TestStatus.PASSED
assert step3_1.description == "Error demo w/ updated description"
def test_create_test_measurements(self, sift_client):
step1 = self.test_steps.get("step1")
step2 = self.test_steps.get("step2")
step3 = self.test_steps.get("step3")
step1_1 = self.test_steps.get("step1_1")
if not step1 or not step2 or not step3 or not step1_1:
pytest.skip("Need to create steps first")
return
# Create measurements for each step using TestMeasurementCreate
measurement1 = sift_client.test_results.create_measurement(
TestMeasurementCreate(
test_step_id=step1._id_or_error,
name="Temperature Reading",
measurement_type=TestMeasurementType.DOUBLE,
numeric_value=25.5,
numeric_bounds=NumericBounds(
min=24,
max=26,
),
unit="Celsius",
passed=True,
timestamp=step1.start_time,
),
update_step=True,
)
# Create a measurement using a dict
measurement2 = sift_client.test_results.create_measurement(
{
"test_step_id": step2._id_or_error,
"name": "FW Version",
"measurement_type": TestMeasurementType.STRING,
"string_value": "1.10.3",
"passed": True,
"timestamp": step2.start_time,
"unit": "K",
},
update_step=True,
)
measurement3 = sift_client.test_results.create_measurement(
TestMeasurementCreate(
test_step_id=step3._id_or_error,
name="Status Check",
measurement_type=TestMeasurementType.BOOLEAN,
boolean_value=True,
passed=True,
timestamp=step3.start_time,
),
update_step=True,
)
assert step1_1.start_time
measurement4 = sift_client.test_results.create_measurement(
TestMeasurementCreate(
test_step_id=step1_1._id_or_error,
name="Substep 1.1: Substep 1.1.1",
measurement_type=TestMeasurementType.BOOLEAN,
boolean_value=True,
passed=True,
timestamp=step1_1.start_time,
)
)
assert measurement1.id_ is not None
assert measurement2.id_ is not None
assert measurement3.id_ is not None
assert measurement4.id_ is not None
assert measurement2.unit == "K"
self.test_measurements["measurement1"] = measurement1
self.test_measurements["measurement2"] = measurement2
self.test_measurements["measurement3"] = measurement3
self.test_measurements["measurement4"] = measurement4
def test_update_test_measurements(self, sift_client):
measurement2 = self.test_measurements.get("measurement2")
measurement4 = self.test_measurements.get("measurement4")
if not measurement2 or not measurement4:
pytest.skip("Need to create measurements first")
return
measurement2 = sift_client.test_results.update_measurement(
measurement2,
update={
"passed": False,
"string_expected_value": "1.10.4",
"unit": "C",
},
update_step=True,
)
assert measurement2.passed == False
assert measurement2.string_expected_value == "1.10.4"
assert measurement2.unit == "C"
# Update the measurement using class function.
measurement4 = measurement4.update(
{
"passed": False,
"numeric_bounds": NumericBounds(
min=10,
max=20,
),
},
update_step=True,
)
assert measurement4.passed == False
assert measurement4.numeric_bounds == NumericBounds(
min=10,
max=20,
)
# Verify update_step propogated the status.
updated_step = sift_client.test_results.get_step(test_step=measurement4.test_step_id)
assert updated_step.status == TestStatus.FAILED
self.test_measurements["measurement2"] = measurement2
self.test_measurements["measurement4"] = measurement4
def test_update_test_report(self, sift_client):
test_report = self.test_reports.get("basic_test_report")
if not test_report:
pytest.skip("Need to create a test report first")
return
new_end_time = test_report.start_time + timedelta(seconds=42)
# Update the report with metadata
updated_report = sift_client.test_results.update(
test_report=test_report,
update=TestReportUpdate(
metadata={
"test_environment": "production",
"temperature": 22.5,
"humidity": 45.0,
"automated": True,
},
end_time=new_end_time,
run_id="",
),
)
# Update the report using class function.
updated_report = updated_report.update(
{"status": TestStatus.FAILED},
)
assert updated_report.metadata == {
"test_environment": "production",
"temperature": 22.5,
"humidity": 45.0,
"automated": True,
}
assert updated_report.status == TestStatus.FAILED
assert updated_report.end_time == new_end_time
assert updated_report.run_id is None
self.test_reports["basic_test_report"] = updated_report
def test_list_test_reports(self, sift_client):
reports = sift_client.test_results.list_(
filter_query=cel.not_(cel.equals("serial_number", ""))
and cel.not_(cel.equals("part_number", "")),
)
existing_report = reports[0]
assert len(reports)
existing_report = reports[0]
reports = sift_client.test_results.list_(
status=existing_report.status,
test_system_name=existing_report.test_system_name,
test_case=existing_report.test_case,
serial_numbers=[existing_report.serial_number],
part_numbers=[existing_report.part_number],
system_operator=existing_report.system_operator,
)
assert existing_report in reports
def test_list_test_steps(self, sift_client):
steps = sift_client.test_results.list_steps()
existing_step = None
for step in steps:
if step.parent_step_id is not None:
existing_step = step
break
assert existing_step is not None
assert len(steps)
steps = sift_client.test_results.list_steps(
test_reports=[existing_step.test_report_id],
parent_steps=[existing_step.parent_step_id],
name=existing_step.name,
step_type=existing_step.step_type,
status=existing_step.status,
)
assert existing_step in steps
def test_list_test_measurements(self, sift_client):
measurements = sift_client.test_results.list_measurements()
assert len(measurements)
existing_measurement = measurements[0]
measurements = sift_client.test_results.list_measurements(
test_steps=[existing_measurement.test_step_id],
name=existing_measurement.name,
measurement_type=existing_measurement.measurement_type,
passed=existing_measurement.passed,
)
assert existing_measurement in measurements
def test_archive_and_delete_test_report(self, sift_client):
test_report = self.test_reports.get("basic_test_report")
if not test_report:
pytest.skip("Need to create a test report first")
return
# Archive the report
archived_report = sift_client.test_results.archive(test_report=test_report)
assert archived_report.is_archived
sift_client.test_results.delete(test_report=test_report)
try:
deleted_report = sift_client.test_results.get(test_report_id=test_report._id_or_error)
assert deleted_report is None # Shouldn't reach here so error if we get something.
except aiogrpc.AioRpcError as e:
self.test_reports.pop("basic_test_report")
assert e.code() == grpc.StatusCode.NOT_FOUND # noqa: PT017
def test_import_test_report(self, sift_client):
# Import a test report from a file
create_time = datetime.now(timezone.utc)
current_dir = Path(__file__).parent
test_file = Path(current_dir, "test_files", "demo_test_report.xml")
test_report = sift_client.test_results.import_(test_file=test_file)
# Excercise find_report, custom_filter, and filtering by commonon-proto fields such as created_date
found_report = sift_client.test_results.find(
filter_query=f"test_report_id == '{test_report._id_or_error}' && created_date >= timestamp('{create_time}')"
)
assert found_report is not None
assert found_report.id_ == test_report.id_
self.test_reports["imported_test_report"] = found_report
def test_delete_test_reports(self, sift_client):
for test_report in self.test_reports.values():
sift_client.test_results.delete(test_report=test_report)