Skip to content

Commit e67d935

Browse files
fix: Error in converting results/steps to dataframe when status is none (#115)
1 parent ebb7abd commit e67d935

File tree

2 files changed

+35
-41
lines changed

2 files changed

+35
-41
lines changed

nisystemlink/clients/testmonitor/utilities/_dataframe_utilities.py

+18-40
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ def convert_results_to_dataframe(
4141
- Properties: All the properties will be split into separate columns. For example,
4242
properties.property1, properties.property2, etc.
4343
"""
44-
results_dict = [result.dict(exclude_none=True) for result in results]
45-
results_dict_with_normalized_status = __normalize_results_status(results_dict)
46-
normalized_dataframe = pd.json_normalize(
47-
results_dict_with_normalized_status, sep="."
48-
)
44+
results_dict = []
45+
for result in results:
46+
data = result.dict(exclude_none=True)
47+
__normalize_status(data)
48+
results_dict.append(data)
49+
50+
normalized_dataframe = pd.json_normalize(results_dict, sep=".")
4951
normalized_dataframe = __format_results_columns(
5052
results_dataframe=normalized_dataframe
5153
)
@@ -98,26 +100,20 @@ def convert_steps_to_dataframe(
98100
return steps_dataframe.reindex(columns=grouped_columns, copy=False)
99101

100102

101-
def __normalize_results_status(
102-
results_dict: List[Dict[str, Any]],
103-
) -> List[Dict[str, Any]]:
104-
"""Gets dictionary of results data and modifies the status object.
103+
def __normalize_status(
104+
data: Dict[str, Any],
105+
) -> None:
106+
"""Normalizes the status object into a string.
105107
106108
Args:
107-
results: List of results.
109+
data: Dictionary containing status information.
108110
109-
Returns:
110-
A list of result fields as dictionary. If status.status_type is "CUSTOM"
111-
the status field takes the value of "status_name", else value of "status_type" is used.
112111
"""
113-
for result in results_dict:
114-
status = result.get("status", {})
115-
if status.get("status_type") == "CUSTOM":
116-
result["status"] = status["status_name"]
117-
else:
118-
result["status"] = status["status_type"].value
119-
120-
return results_dict
112+
status = data.get("status", {})
113+
if status.get("status_type") == "CUSTOM":
114+
data["status"] = status.get("status_name", None)
115+
else:
116+
data["status"] = getattr(status.get("status_type", None), "value", None)
121117

122118

123119
def __format_results_columns(results_dataframe: pd.DataFrame) -> pd.DataFrame:
@@ -208,7 +204,7 @@ def __convert_steps_to_dict(
208204
single_step_dict = step.dict(exclude_none=True)
209205
__filter_invalid_measurements(single_step_dict, step, is_valid_measurement)
210206
__normalize_inputs_outputs(single_step_dict, step)
211-
__normalize_step_status(single_step_dict)
207+
__normalize_status(single_step_dict)
212208
steps_dict.append(single_step_dict)
213209
return steps_dict
214210

@@ -266,24 +262,6 @@ def __normalize_inputs_outputs(
266262
)
267263

268264

269-
def __normalize_step_status(step_dict: Dict[str, Any]) -> None:
270-
"""Normalizes the step status field. If status_type is "CUSTOM",
271-
then status.status_name is used, else status.status_type.value is assigned.
272-
273-
Args:
274-
step_dict: A dictionary with step information.
275-
276-
Returns:
277-
None: The function modifies step_dict in place with the normalized step
278-
"""
279-
step_status = step_dict.get("status", {})
280-
step_dict["status"] = (
281-
step_status.get("status_name", None)
282-
if step_status.get("status_type") == "CUSTOM"
283-
else step_status.get("status_type", None).value
284-
)
285-
286-
287265
def __explode_and_normalize(
288266
dataframe: pd.DataFrame, column: str, prefix: str
289267
) -> pd.DataFrame:

tests/testmonitor/test_testmonitor_dataframe_utilities.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,23 @@ def test__convert_results_with_specific_fields_to_dataframe__returns_results_dat
227227
self, results
228228
):
229229
results = results[1:]
230+
results.append(
231+
Result(
232+
status=None,
233+
started_at=datetime.datetime(
234+
2018, 5, 7, 18, 58, 5, 219692, tzinfo=datetime.timezone.utc
235+
),
236+
updated_at=datetime.datetime(
237+
2018, 5, 7, 18, 58, 5, 219692, tzinfo=datetime.timezone.utc
238+
),
239+
program_name="My Program Name",
240+
id=uuid.uuid1().hex,
241+
file_ids=[uuid.uuid1().hex],
242+
status_type_summary={StatusType.PASSED: 0, StatusType.FAILED: 1},
243+
workspace=uuid.uuid1().hex,
244+
)
245+
)
246+
230247
expected_results_dataframe = self.__get_expected_results_dataframe(
231248
results=results
232249
)
@@ -429,7 +446,6 @@ def test__convert_steps_to_dataframe_with_invalid_data_parameters__returns_only_
429446
name="step_name",
430447
step_id="5ffb2bf6771fa11e877838dd6",
431448
result_id="5ffb2bf6771fa11e877838dd8",
432-
status=Status.PASSED(),
433449
data=StepData(
434450
text="data1",
435451
parameters=[Measurement(name="parameter_123", status="Passed")],

0 commit comments

Comments
 (0)