|
6 | 6 | get_investigation_dataset_polyp_category, |
7 | 7 | get_investigation_dataset_polyp_algorithm_size, |
8 | 8 | ) |
9 | | -from typing import Optional |
| 9 | +from typing import Optional, Any, Union, List |
10 | 10 | import logging |
| 11 | +from enum import Enum |
11 | 12 |
|
12 | 13 |
|
13 | 14 | class InvestigationDatasetsPage(BasePage): |
@@ -750,11 +751,13 @@ def assert_drug_type_text( |
750 | 751 | locator = self.get_drug_type_locator(drug_type, drug_number) |
751 | 752 | actual_text = locator.input_value().strip() |
752 | 753 | logging.info( |
753 | | - f"Drug type text for drug {drug_number}: '{actual_text}' (expected: '{expected_text}')" |
| 754 | + f"Drug type text for drug {drug_number}: " |
| 755 | + f"'{to_enum_name_or_value(actual_text)}' " |
| 756 | + f"(expected: '{to_enum_name_or_value(expected_text)}')" |
754 | 757 | ) |
755 | 758 | assert ( |
756 | 759 | actual_text == expected_text |
757 | | - ), f"Expected drug type text '{expected_text}' but found '{actual_text}'" |
| 760 | + ), f"Expected drug type text '{to_enum_name_or_value(expected_text)}' but found '{to_enum_name_or_value(actual_text)}'" |
758 | 761 |
|
759 | 762 | def assert_drug_dose_text( |
760 | 763 | self, drug_type: str, drug_number: int, expected_text: str |
@@ -1319,3 +1322,42 @@ class AntibioticsAdministeredDrugTypeOptions(StrEnum): |
1319 | 1322 | TEICOPLANIN = "17944~mg" |
1320 | 1323 | VANCOMYCIN = "17943~g" |
1321 | 1324 | OTHER_ANTIBIOTIC = "305493" |
| 1325 | + |
| 1326 | + |
| 1327 | +# Registry of all known Enums to search when matching string values |
| 1328 | +ALL_ENUMS: List[type[Enum]] = [ |
| 1329 | + DrugTypeOptions, |
| 1330 | + AntibioticsAdministeredDrugTypeOptions, |
| 1331 | +] |
| 1332 | + |
| 1333 | + |
| 1334 | +def to_enum_name_or_value(val: Any) -> Union[str, Any]: |
| 1335 | + """ |
| 1336 | + Convert an Enum member or matching string value to its Enum name. |
| 1337 | +
|
| 1338 | + If the input is: |
| 1339 | + - An Enum member → returns the `.name` (e.g., "KLEAN_PREP") |
| 1340 | + - A string matching any Enum value in ALL_ENUMS → returns that member's `.name` |
| 1341 | + - Anything else → returns the value unchanged |
| 1342 | +
|
| 1343 | + Args: |
| 1344 | + val (Any): The value to convert. Can be an Enum member, a string, |
| 1345 | + or any other type. |
| 1346 | +
|
| 1347 | + Returns: |
| 1348 | + Union[str, Any]: The Enum name (string) if matched, otherwise the original value. |
| 1349 | + """ |
| 1350 | + # Directly handle Enum instances |
| 1351 | + if isinstance(val, Enum): |
| 1352 | + return val.name |
| 1353 | + |
| 1354 | + # Handle strings that match known Enum values |
| 1355 | + if isinstance(val, str): |
| 1356 | + for enum_cls in ALL_ENUMS: |
| 1357 | + try: |
| 1358 | + return enum_cls(val).name |
| 1359 | + except ValueError: |
| 1360 | + continue |
| 1361 | + |
| 1362 | + # Fallback: return unchanged |
| 1363 | + return val |
0 commit comments