|
| 1 | +import datetime |
| 2 | +import uuid |
| 3 | +from typing import List |
| 4 | + |
| 5 | +import pandas as pd |
| 6 | +import pytest |
| 7 | +from nisystemlink.clients.testmonitor.models._result import Result |
| 8 | +from nisystemlink.clients.testmonitor.models._status import Status, StatusType |
| 9 | +from nisystemlink.clients.testmonitor.utilities._dataframe_utilities import ( |
| 10 | + convert_results_to_dataframe, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(scope="class") |
| 15 | +def results() -> List[Result]: |
| 16 | + """Sample results for testing purposes.""" |
| 17 | + results = [ |
| 18 | + Result( |
| 19 | + status=Status.PASSED(), |
| 20 | + started_at=datetime.datetime( |
| 21 | + 2018, 5, 7, 18, 58, 5, 219692, tzinfo=datetime.timezone.utc |
| 22 | + ), |
| 23 | + updated_at=datetime.datetime( |
| 24 | + 2018, 5, 7, 18, 58, 5, 219692, tzinfo=datetime.timezone.utc |
| 25 | + ), |
| 26 | + program_name="My Program Name", |
| 27 | + id=uuid.uuid1().hex, |
| 28 | + system_id=uuid.uuid1().hex, |
| 29 | + host_name="host name", |
| 30 | + part_number=uuid.uuid1().hex, |
| 31 | + serial_number=uuid.uuid1().hex, |
| 32 | + total_time_in_seconds=16.76845106446358, |
| 33 | + keywords=["keyword1", "keyword2"], |
| 34 | + properties={"property1": "value1", "property2": "value2"}, |
| 35 | + operator="sample operator", |
| 36 | + file_ids=[uuid.uuid1().hex, uuid.uuid1().hex], |
| 37 | + data_table_ids=[uuid.uuid1().hex, uuid.uuid1().hex], |
| 38 | + status_type_summary={StatusType.PASSED: 1, StatusType.FAILED: 0}, |
| 39 | + workspace=uuid.uuid1().hex, |
| 40 | + ), |
| 41 | + Result( |
| 42 | + status=Status.FAILED(), |
| 43 | + started_at=datetime.datetime( |
| 44 | + 2018, 5, 7, 18, 58, 5, 219692, tzinfo=datetime.timezone.utc |
| 45 | + ), |
| 46 | + updated_at=datetime.datetime( |
| 47 | + 2018, 5, 7, 18, 58, 5, 219692, tzinfo=datetime.timezone.utc |
| 48 | + ), |
| 49 | + program_name="My Program Name", |
| 50 | + id=uuid.uuid1().hex, |
| 51 | + part_number=uuid.uuid1().hex, |
| 52 | + total_time_in_seconds=16.76845106446358, |
| 53 | + keywords=[], |
| 54 | + properties={"property3": "value3"}, |
| 55 | + file_ids=[uuid.uuid1().hex], |
| 56 | + status_type_summary={StatusType.PASSED: 0, StatusType.FAILED: 1}, |
| 57 | + workspace=uuid.uuid1().hex, |
| 58 | + ), |
| 59 | + Result( |
| 60 | + status=Status(status_type=StatusType.CUSTOM, status_name="custom_status"), |
| 61 | + started_at=datetime.datetime( |
| 62 | + 2018, 5, 7, 18, 58, 5, 219692, tzinfo=datetime.timezone.utc |
| 63 | + ), |
| 64 | + updated_at=datetime.datetime( |
| 65 | + 2018, 5, 7, 18, 58, 5, 219692, tzinfo=datetime.timezone.utc |
| 66 | + ), |
| 67 | + program_name="My Program Name", |
| 68 | + id=uuid.uuid1().hex, |
| 69 | + file_ids=[uuid.uuid1().hex], |
| 70 | + status_type_summary={StatusType.PASSED: 0, StatusType.FAILED: 1}, |
| 71 | + workspace=uuid.uuid1().hex, |
| 72 | + ), |
| 73 | + ] |
| 74 | + |
| 75 | + return results |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.enterprise |
| 79 | +class TestTestmonitorDataframeUtilities: |
| 80 | + def test__convert_results_with_all_fields_to_dataframe__returns_whole_results_dataframe( |
| 81 | + self, results |
| 82 | + ): |
| 83 | + expected_results_dataframe = self.__get_expected_results_dataframe( |
| 84 | + results=results |
| 85 | + ) |
| 86 | + |
| 87 | + results_dataframe = convert_results_to_dataframe( |
| 88 | + results=results, set_id_as_index=False |
| 89 | + ) |
| 90 | + |
| 91 | + assert not results_dataframe.empty |
| 92 | + assert len(results_dataframe.columns.tolist()) == 20 |
| 93 | + pd.testing.assert_frame_equal( |
| 94 | + results_dataframe, expected_results_dataframe, check_dtype=True |
| 95 | + ) |
| 96 | + assert isinstance(results_dataframe["status"].iloc[0], str) |
| 97 | + assert results_dataframe["started_at"].dtype == "datetime64[ns, UTC]" |
| 98 | + assert results_dataframe["updated_at"].dtype == "datetime64[ns, UTC]" |
| 99 | + assert results_dataframe["file_ids"].dtype == "object" |
| 100 | + assert isinstance(results_dataframe["file_ids"].iloc[0], List) |
| 101 | + assert results_dataframe["data_table_ids"].dtype == "object" |
| 102 | + assert isinstance(results_dataframe["data_table_ids"].iloc[0], List) |
| 103 | + assert results_dataframe["keywords"].dtype == "object" |
| 104 | + assert isinstance(results_dataframe["keywords"].iloc[0], List) |
| 105 | + |
| 106 | + def test__convert_results_with_specific_fields_to_dataframe__returns_results_dataframe_with_specific_fields( |
| 107 | + self, results |
| 108 | + ): |
| 109 | + results = results[1:] |
| 110 | + expected_results_dataframe = self.__get_expected_results_dataframe( |
| 111 | + results=results |
| 112 | + ) |
| 113 | + |
| 114 | + results_dataframe = convert_results_to_dataframe( |
| 115 | + results=results, set_id_as_index=False |
| 116 | + ) |
| 117 | + |
| 118 | + assert not results_dataframe.empty |
| 119 | + assert len(results_dataframe.columns.tolist()) == 13 |
| 120 | + pd.testing.assert_frame_equal( |
| 121 | + results_dataframe, expected_results_dataframe, check_dtype=True |
| 122 | + ) |
| 123 | + assert isinstance(results_dataframe["status"].iloc[0], str) |
| 124 | + assert results_dataframe["started_at"].dtype == "datetime64[ns, UTC]" |
| 125 | + assert results_dataframe["updated_at"].dtype == "datetime64[ns, UTC]" |
| 126 | + assert results_dataframe["file_ids"].dtype == "object" |
| 127 | + assert isinstance(results_dataframe["file_ids"].iloc[0], List) |
| 128 | + assert results_dataframe["keywords"].dtype == "object" |
| 129 | + assert isinstance(results_dataframe["keywords"].iloc[0], List) |
| 130 | + |
| 131 | + def test__convert_results_to_dataframe_with_id_index__returns_results_dataframe_with_id_index( |
| 132 | + self, results |
| 133 | + ): |
| 134 | + expected_results_dataframe = self.__get_expected_results_dataframe( |
| 135 | + results=results |
| 136 | + ) |
| 137 | + expected_results_dataframe = expected_results_dataframe.set_index("id") |
| 138 | + |
| 139 | + results_dataframe = convert_results_to_dataframe(results=results) |
| 140 | + |
| 141 | + assert not results_dataframe.empty |
| 142 | + assert len(results_dataframe.columns.tolist()) == 19 |
| 143 | + pd.testing.assert_frame_equal( |
| 144 | + results_dataframe, expected_results_dataframe, check_dtype=True |
| 145 | + ) |
| 146 | + assert isinstance(results_dataframe["status"].iloc[0], str) |
| 147 | + assert results_dataframe["started_at"].dtype == "datetime64[ns, UTC]" |
| 148 | + assert results_dataframe["updated_at"].dtype == "datetime64[ns, UTC]" |
| 149 | + assert results_dataframe["file_ids"].dtype == "object" |
| 150 | + assert isinstance(results_dataframe["file_ids"].iloc[0], List) |
| 151 | + assert results_dataframe["data_table_ids"].dtype == "object" |
| 152 | + assert isinstance(results_dataframe["data_table_ids"].iloc[0], List) |
| 153 | + assert results_dataframe["keywords"].dtype == "object" |
| 154 | + assert isinstance(results_dataframe["keywords"].iloc[0], List) |
| 155 | + |
| 156 | + def test__convert_results_to_dataframe_with_no_results__returns_empty_dataframe( |
| 157 | + self, |
| 158 | + ): |
| 159 | + results_dataframe = convert_results_to_dataframe(results=[]) |
| 160 | + |
| 161 | + assert isinstance(results_dataframe, pd.DataFrame) |
| 162 | + assert results_dataframe.empty |
| 163 | + |
| 164 | + def __get_expected_results_dataframe(self, results: List[Result]): |
| 165 | + results_dict = [] |
| 166 | + for result in results: |
| 167 | + status = { |
| 168 | + "status": ( |
| 169 | + result.status.status_type.value |
| 170 | + if result.status and result.status.status_type != "CUSTOM" |
| 171 | + else result.status.status_name if result.status else None |
| 172 | + ) |
| 173 | + } |
| 174 | + status_type_summary = ( |
| 175 | + { |
| 176 | + f"status_type_summary.{key}": value |
| 177 | + for key, value in result.status_type_summary.items() |
| 178 | + } |
| 179 | + if result.status_type_summary |
| 180 | + else {} |
| 181 | + ) |
| 182 | + properties = ( |
| 183 | + {f"properties.{key}": value for key, value in result.properties.items()} |
| 184 | + if result.properties |
| 185 | + else {} |
| 186 | + ) |
| 187 | + results_dict.append( |
| 188 | + { |
| 189 | + **{ |
| 190 | + "started_at": result.started_at, |
| 191 | + "updated_at": result.updated_at, |
| 192 | + "program_name": result.program_name, |
| 193 | + "id": result.id, |
| 194 | + "system_id": result.system_id, |
| 195 | + "host_name": result.host_name, |
| 196 | + "part_number": result.part_number, |
| 197 | + "serial_number": result.serial_number, |
| 198 | + "total_time_in_seconds": result.total_time_in_seconds, |
| 199 | + "keywords": result.keywords, |
| 200 | + "operator": result.operator, |
| 201 | + "file_ids": result.file_ids, |
| 202 | + "data_table_ids": result.data_table_ids, |
| 203 | + "workspace": result.workspace, |
| 204 | + }, |
| 205 | + **status, |
| 206 | + **status_type_summary, |
| 207 | + **properties, |
| 208 | + } |
| 209 | + ) |
| 210 | + |
| 211 | + results_df = pd.DataFrame(results_dict) |
| 212 | + results_df = results_df[ |
| 213 | + ["status"] + [col for col in results_df.columns if col != "status"] |
| 214 | + ] |
| 215 | + results_df.dropna(axis="columns", how="all", inplace=True) |
| 216 | + |
| 217 | + return results_df |
0 commit comments