Skip to content

Commit

Permalink
Raise when duplicate GEN_KW keys
Browse files Browse the repository at this point in the history
Report duplicate GEN_KW keys encountered
Add GEN_KW duplicate key test
  • Loading branch information
andreas-el authored Sep 19, 2023
1 parent eb7c46c commit 5f097a8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ert/config/gen_kw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,17 @@ def _check_non_negative_parameter(param: str, prior: PriorDict) -> None:
).set_context(self.name)
)

unique_keys = set()
for prior in self.get_priors():
key = prior["key"]
if key in unique_keys:
errors.append(
ErrorInfo(
f"Duplicate GEN_KW keys {key!r} found, keys must be unique."
).set_context(self.name)
)
unique_keys.add(key)

if prior["function"] == "LOGNORMAL":
_check_non_negative_parameter("MEAN", prior)
_check_non_negative_parameter("STD", prior)
Expand Down
20 changes: 20 additions & 0 deletions tests/unit_tests/config/test_gen_kw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ def test_gen_kw_config():
assert len(conf.transfer_functions) == 3


@pytest.mark.usefixtures("use_tmpdir")
def test_gen_kw_config_duplicate_keys_raises():
with pytest.raises(
ConfigValidationError,
match="Duplicate GEN_KW keys 'KEY2' found, keys must be unique.",
):
GenKwConfig(
name="KEY",
forward_init=False,
template_file="",
transfer_function_definitions=[
"KEY1 UNIFORM 0 1",
"KEY2 UNIFORM 0 1",
"KEY2 UNIFORM 0 1",
"KEY3 UNIFORM 0 1",
],
output_file="kw.txt",
)


@pytest.mark.usefixtures("use_tmpdir")
def test_gen_kw_config_get_priors():
parameter_file = "parameters.txt"
Expand Down

0 comments on commit 5f097a8

Please sign in to comment.