diff --git a/src/ert/shared/storage/key_utils.py b/src/ert/shared/storage/key_utils.py index f44e010cbaa..032a6be25b4 100644 --- a/src/ert/shared/storage/key_utils.py +++ b/src/ert/shared/storage/key_utils.py @@ -119,7 +119,8 @@ class SummaryVarType(Enum): @staticmethod def determine_var_type(var: str) -> "SummaryVarType": - # Default case for single characters or unknown vars + if var in special_vars: + return SummaryVarType.RD_SMSPEC_MISC_VAR default_case = { "A": SummaryVarType.RD_SMSPEC_AQUIFER_VAR, "B": SummaryVarType.RD_SMSPEC_BLOCK_VAR, @@ -131,7 +132,6 @@ def determine_var_type(var: str) -> "SummaryVarType": "W": SummaryVarType.RD_SMSPEC_WELL_VAR, } - # Special case for 'L' with nested logic if var.startswith("L"): secondary = var[1] if len(var) > 1 else "" return { @@ -140,7 +140,6 @@ def determine_var_type(var: str) -> "SummaryVarType": "W": SummaryVarType.RD_SMSPEC_LOCAL_WELL_VAR, }.get(secondary, SummaryVarType.RD_SMSPEC_MISC_VAR) - # Special case for 'R' with more complex nested logic if var.startswith("R"): if len(var) == 3 and var[2] == "F": return SummaryVarType.RD_SMSPEC_REGION_2_REGION_VAR @@ -154,35 +153,22 @@ def determine_var_type(var: str) -> "SummaryVarType": return SummaryVarType.RD_SMSPEC_REGION_2_REGION_VAR return SummaryVarType.RD_SMSPEC_REGION_VAR - # Fallback to default cases or miscellaneous if not matched + # default cases or miscellaneous if not matched return default_case.get(var[0], SummaryVarType.RD_SMSPEC_MISC_VAR) -def identify_var_type(key: str) -> SummaryVarType: - if key not in special_vars: - return SummaryVarType.determine_var_type(key) - return SummaryVarType.RD_SMSPEC_MISC_VAR - - def match_keyword_vector(start: int, rate_vars: List[str], keyword: str) -> bool: - """ - Check if 'keyword' matches any item in 'rate_vars' starting from - index 'start' onwards. - """ # Get the suffix of keyword starting from 'start' index (if not out of range) suffix = keyword[start:] if len(keyword) > start else "" return any(suffix.startswith(var) for var in rate_vars) def match_keyword_string(start: int, rate_string: str, keyword: str) -> bool: - """ - Check if 'keyword' matches 'rate_string' starting from index 'start'. - """ return keyword[start:].startswith(rate_string) def is_rate(key: str) -> bool: - var_type = identify_var_type(key) + var_type = SummaryVarType.determine_var_type(key) if var_type in { SummaryVarType.RD_SMSPEC_WELL_VAR, SummaryVarType.RD_SMSPEC_GROUP_VAR, @@ -193,7 +179,6 @@ def is_rate(key: str) -> bool: SummaryVarType.RD_SMSPEC_LOCAL_COMPLETION_VAR, SummaryVarType.RD_SMSPEC_NETWORK_VAR, }: - # Process local and network vars differently if var_type in { SummaryVarType.RD_SMSPEC_LOCAL_WELL_VAR, SummaryVarType.RD_SMSPEC_LOCAL_COMPLETION_VAR, diff --git a/tests/unit_tests/shared/test_rate_keys.py b/tests/unit_tests/shared/test_rate_keys.py new file mode 100644 index 00000000000..109ff70dc0c --- /dev/null +++ b/tests/unit_tests/shared/test_rate_keys.py @@ -0,0 +1,53 @@ +import hypothesis.strategies as st +from hypothesis import given +from src.ert.shared.storage.key_utils import is_rate + + +def nonempty_string_without_whitespace(): + return st.text( + st.characters(whitelist_categories=("Lu", "Ll", "Nd", "P")), min_size=1 + ) + + +@given(key=nonempty_string_without_whitespace()) +def test_is_rate_does_not_raise_error(key): + print(key) + is_rate_bool = is_rate(key) + assert isinstance(is_rate_bool, bool) + + +examples = [ + ("OPR", False), + ("WOPR:OP_4", True), + ("WGIR", True), + ("FOPT", False), + ("GGPT", False), + ("RWPT", False), + ("COPR", True), + ("LPR", False), + ("LWPR", False), + ("LCOPR", True), + ("RWGIR", True), + ("RTPR", True), + ("RXFR", True), + ("XXX", False), + ("YYYY", False), + ("ZZT", False), + ("SGPR", False), + ("AAPR", False), + ("JOPR", False), + ("ROPRT", True), + ("RNFT", False), + ("RFR", False), + ("RRFRT", True), + ("ROC", False), + ("BPR:123", False), + ("FWIR", True), +] + + +@given(st.sampled_from(examples)) +def test_is_rate_determmines_rate_key(example): + key, rate = example + is_rate_bool = is_rate(key) + assert is_rate_bool == rate