Skip to content

Commit 1a09058

Browse files
Changing assert_drug_type to show the Drug name instead of enum value
1 parent e679e3c commit 1a09058

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

pages/datasets/investigation_dataset_page.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
get_investigation_dataset_polyp_category,
77
get_investigation_dataset_polyp_algorithm_size,
88
)
9-
from typing import Optional
9+
from typing import Optional, Any, Union, List
1010
import logging
11+
from enum import Enum
1112

1213

1314
class InvestigationDatasetsPage(BasePage):
@@ -750,11 +751,13 @@ def assert_drug_type_text(
750751
locator = self.get_drug_type_locator(drug_type, drug_number)
751752
actual_text = locator.input_value().strip()
752753
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)}')"
754757
)
755758
assert (
756759
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)}'"
758761

759762
def assert_drug_dose_text(
760763
self, drug_type: str, drug_number: int, expected_text: str
@@ -1319,3 +1322,42 @@ class AntibioticsAdministeredDrugTypeOptions(StrEnum):
13191322
TEICOPLANIN = "17944~mg"
13201323
VANCOMYCIN = "17943~g"
13211324
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

Comments
 (0)