-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest_data_manager_client.py
More file actions
284 lines (255 loc) · 9.65 KB
/
test_data_manager_client.py
File metadata and controls
284 lines (255 loc) · 9.65 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
import random
import uuid
import datetime
import requests
from typing import Dict, List
create_results_route = "nitestmonitor/v2/results"
create_steps_route = "nitestmonitor/v2/steps"
update_results_route = "nitestmonitor/v2/update-results"
update_steps_route = "nitestmonitor/v2/update-steps"
delete_results_route = "nitestmonitor/v2/delete-results"
delete_result_route = "nitestmonitor/v2/results"
api_key = ""
base_url = ""
headers = { 'X-NI-API-KEY': api_key }
def update_headers() -> None:
global headers, api_key
headers = { 'X-NI-API-KEY': api_key }
def set_base_url_and_api_key(server_url: str, key: str) -> None:
global api_key, base_url
base_url = server_url
api_key = key
update_headers()
def create_test_result(
program_name: str = "Power Test",
part_number: str = "NI-ABC-123-PWR",
operator: str = None,
serial_number: str = str(uuid.uuid4()),
started_at: str = str(datetime.datetime.utcnow()),
system_id: str = None,
host_name: str = None,
properties: Dict = None,
file_ids: List = None,
status: Dict = None
) -> Dict:
"""
Creates the result data and
populates it to match the TestStand data model.
:param program_name: The test result's program name.
:param part_number: The test results's part number.
:param operator: The test result's operator.
:param serial_number: The test results's serial number.
:param started_at: The test result's start time.
:param system_id: The test result's system id.
:param host_name: The test result's host_name.
:param properties: The test result's properties.
:param file_ids: The test result's file id.
:param status: The test result's status
:return: The result data used to create a test result.
"""
result_status = status if status else {
"statusType": "RUNNING",
"statusName": "Running"
}
test_result = {
"programName": program_name,
"status": result_status,
"systemId": system_id,
"hostName": host_name,
"properties":properties,
"serialNumber": serial_number,
"operator": operator,
"partNumber": part_number,
"fileIds":file_ids,
"startedAt": started_at,
"totalTimeInSeconds": random.uniform(0, 1) * 10
}
return test_result
def create_test_step(
name: str,
step_type: str,
result_id:str,
parent_id: str = None,
children: List = None,
inputs: List[Dict] = None,
outputs: List[Dict] = None,
parameters: Dict = None,
status: Dict = None,
keywords: List[str] = None,
properties: Dict[str, str] = None,
) -> Dict:
"""
Creates the step data and
populates it to match the TestStand data model.
:param name: The test step's name.
:param step_type: The test step's type.
:param inputs: The test step's input values.
:param outputs: The test step's output values.
:param parameters: The measurement parameters.
:param status: The test step's status.
:param keywords: The test steps's keywords.
:param properties: The test steps's properties.
:return: The step data used to create a test step.
"""
step_status = status if status else {
"statusType": "RUNNING",
"statusName": "Running"
}
step_data = {
"stepId": None,
"parentId": parent_id,
"resultId": result_id,
"children": children,
"data": parameters,
"dataModel": "TestStand",
"name": name,
"startedAt": str(datetime.datetime.utcnow()),
"status": step_status,
"stepType": step_type,
"totalTimeInSeconds": random.uniform(0, 1) * 10,
"inputs": inputs,
"outputs": outputs,
"keywords": keywords,
"properties": properties
}
return step_data
def create_test_result_request(results: List) -> Dict:
"""
Creates a create test result request object.
:param results: List of results to be created
:return: A dictionary which is required for creating the results
"""
return {
"results":results
}
def test_step_create_or_update_request_object(steps: List, update_result_total_time: bool = False) -> Dict:
"""
Creates a create/update test step request object.
:param results: List of steps to be created/updated
:param update_result_total_time: A boolean to determine if result total time needs to be updated
:return: A dictionary which is required for creating/updating the steps
"""
return{
"steps": steps,
"updateResultTotalTime": update_result_total_time
}
def update_test_results_request(results: List, determine_status_from_steps: bool = False) -> Dict:
"""
Creates a update test result request object.
:param results: List of results to be updated
:param determine_status_from_steps: A boolean to determine if the result status needs to be updated
based on the status of the steps
:return: A dictionary which is required for updating the results
"""
return{
"results": results,
"determineStatusFromSteps": determine_status_from_steps
}
def delete_results_request(result_ids: List, delete_steps: bool):
"""
Creates a delete test results request object.
:param result_ids: List of result IDs to be deleted
:param delete_steps: A boolean to determine if the steps associated with results should be deleted or not
:return: A dictionary which is required for deleting the results
"""
return {
"ids":result_ids,
"deleteSteps":delete_steps
}
def create_results(results: List) -> Dict:
"""
Creates new test results from the supplied models. The server automatically generates the result IDs.
:param results: List of results to be created
:return: json response after creating the results
"""
if len(results) == 0 :
raise ValueError("Number of results to be created can not be empty.")
body = create_test_result_request(results)
request_url = base_url + create_results_route
request_response = raise_post_request(request_url, body)
return request_response.json()
def update_results(results: List) -> Dict:
"""
Updates existing test results by merging or replacing values.
:param results: List of results to be updated
:return: json response after updating the results
"""
if len(results) == 0 :
raise ValueError("Number of results to be updated can not be empty.")
body = update_test_results_request(results, determine_status_from_steps = True)
request_url = base_url + update_results_route
request_response = raise_post_request(request_url, body)
return request_response.json()
def create_steps(steps: List) -> Dict:
"""
Creates new test steps from the supplied models.
The result associated with the steps must exist prior to step creation.
The server automatically generates step IDs if not provided.
:param steps: Steps to be created
:return: json response after creating steps
"""
if len(steps) == 0 :
raise ValueError("Number of steps to be created can not be empty.")
body = test_step_create_or_update_request_object(
steps, update_result_total_time = True
)
request_url = base_url + create_steps_route
request_response = raise_post_request(request_url, body)
return request_response.json()
def update_steps(steps: List) -> Dict:
"""
Updates existing steps by merging or replacing values.
:param steps: List of steps to be updated
:return: json response after updating steps
"""
if len(steps) == 0 :
raise ValueError("Number of steps to be updated can not be empty.")
body = test_step_create_or_update_request_object(
steps, update_result_total_time = True
)
request_url = base_url + update_steps_route
request_response = raise_post_request(request_url, body)
return request_response.json()
def delete_result(result_id: str, delete_steps: bool = True) -> None:
"""
Deletes test result.
:param result_id: result ID to be deleted
"""
if result_id == None :
raise ValueError("Missing required parameter 'result_id' in ResultsApi->DeleteResultV2 request.")
request_url = f"{base_url}{delete_result_route}/{result_id}?deleteSteps{delete_steps}"
raise_delete_request(request_url)
def delete_results(result_ids: List, delete_steps: bool = True) -> Dict:
"""
Delete multiple test results.
:param result_ids: List of result IDs to be deleted
:return: json response after deleting the results
"""
if result_ids == None or len(result_ids) == 0:
raise ValueError("result_ids is a required property for ResultsApi->DeleteResultsV2 and cannot be null or empty.")
request_url = f"{base_url}{delete_results_route}"
body = delete_results_request(result_ids, delete_steps)
request_response = raise_post_request(request_url, body)
if request_response.status_code != 204 :
return request_response.json()
else:
return {}
def raise_post_request(url: str, body: Dict) -> requests.Response:
"""
Makes the post request API call.
:param url: request url to be called
:param body: API call body
:return: response of the API call
"""
request_response = requests.post(url, json=body, headers=headers)
request_response.raise_for_status()
return request_response
def raise_delete_request(url: str) -> requests.Response:
"""
Makes the delete request API call.
:param url: request url to be called
:return: response of the API call
"""
request_response = requests.delete(url, headers=headers)
request_response.raise_for_status()
return request_response