Skip to content

Commit

Permalink
Add tests for is_rate evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
xjules committed Sep 11, 2024
1 parent 5cdd928 commit 31c343b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
23 changes: 4 additions & 19 deletions src/ert/shared/storage/key_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand Down
53 changes: 53 additions & 0 deletions tests/unit_tests/shared/test_rate_keys.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 31c343b

Please sign in to comment.