Skip to content

Commit 811a43d

Browse files
fix(extforce_convert): ensure case-insensitive matching for unsupported quantities (#988)
fix(extforce_convert): ensure case-insensitive matching for unsupported quantities - Applied `.lower()` to handle case-insensitive comparisons in unsupported quantities - Added dedicated tests for unsupported quantities validation - Moved assertions outside of exception blocks for clearer error reporting - Added explanatory comments for maintainability ref: #983
1 parent ba3be08 commit 811a43d

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

hydrolib/tools/extforce_convert/main_converter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,11 @@ def update(
359359
self._update_mdu_file()
360360

361361
if self.un_supported_quantities:
362+
# replace the forcings by the forcings that were not converted (the unsupported quantities)
362363
forcing = [
363364
forcing
364365
for forcing in self.extold_model.forcing
365-
if forcing.quantity in self.un_supported_quantities
366+
if forcing.quantity.lower() in self.un_supported_quantities
366367
]
367368
self.extold_model.forcing = forcing
368369

tests/tools/test_main_converter.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
recursive_converter,
2828
)
2929
from hydrolib.tools.extforce_convert.mdu_parser import MDUParser
30+
from hydrolib.tools.extforce_convert.utils import UnSupportedQuantitiesError
3031

3132

3233
class TestExtOldToNewFromMDU:
@@ -529,6 +530,84 @@ def test_externalforcingconverter_os_style_converted_to_path_style(
529530
initial_file = tmp_path / "new-initial-conditions.ini"
530531
assert setup_absolute_path_files["raw_poly_file"] in initial_file.read_text()
531532

533+
@pytest.fixture
534+
def old_model(self) -> Dict[str, Any]:
535+
return {
536+
"forcing": [
537+
{
538+
"quantity": "InternalTidesFrictionCoefficient",
539+
"filename": "file.xyz",
540+
"filetype": 7,
541+
"method": 1,
542+
"operand": "O",
543+
},
544+
{
545+
"quantity": "unsupported_quantity",
546+
"filename": "file2.xyz",
547+
"filetype": 7,
548+
"method": 4,
549+
"operand": "O",
550+
"value": 0.0125,
551+
},
552+
]
553+
}
554+
555+
@pytest.mark.parametrize(
556+
"unsupported_quantity",
557+
["waveperiod", "waqfunctionTau", "waqfunctionradsurfave"],
558+
ids=["quantity", "prefix_capitalized", "prefix_lowercase"],
559+
)
560+
def test_debug_unsupported_quantities_conversion(
561+
self, old_model, unsupported_quantity: str
562+
):
563+
old_model["forcing"][1]["quantity"] = unsupported_quantity
564+
ext_old_model = ExtOldModel(**old_model)
565+
ext_old_model.filepath = Path("tests/data/input/mock_file.ext")
566+
converter = ExternalForcingConverter(extold_model=ext_old_model, debug=True)
567+
converter.update()
568+
569+
assert converter.un_supported_quantities == {unsupported_quantity.lower()}
570+
assert ext_old_model.forcing[0].quantity == unsupported_quantity
571+
572+
@pytest.mark.parametrize(
573+
"unsupported_quantity",
574+
["waveperiod", "waqfunctionTau", "waqfunctionradsurfave"],
575+
ids=["quantity", "prefix_capitalized", "prefix_lowercase"],
576+
)
577+
def test_no_debug_unsupported_quantities_conversion(
578+
self, old_model, unsupported_quantity: str
579+
):
580+
old_model["forcing"][1]["quantity"] = unsupported_quantity
581+
ext_old_model = ExtOldModel(**old_model)
582+
ext_old_model.filepath = Path("tests/data/input/mock_file.ext")
583+
with pytest.raises(UnSupportedQuantitiesError) as error:
584+
ExternalForcingConverter(extold_model=ext_old_model, debug=False)
585+
quantity = {unsupported_quantity.lower()}
586+
assert (
587+
f"The following quantities are not supported by the converter yet: {quantity}"
588+
in str(error)
589+
)
590+
591+
@pytest.mark.parametrize(
592+
"unsupported_quantity",
593+
["waveperiod", "waqfunctionTau", "waqfunctionradsurfave"],
594+
ids=["quantity", "prefix_capitalized", "prefix_lowercase"],
595+
)
596+
def test_debug_unsupported_quantities_save(
597+
self, old_model, tmp_path: Path, unsupported_quantity: str
598+
):
599+
old_model["forcing"][1]["quantity"] = unsupported_quantity
600+
ext_old_model = ExtOldModel(**old_model)
601+
ext_old_model.filepath = tmp_path / "mock_file.ext"
602+
converter = ExternalForcingConverter(extold_model=ext_old_model, debug=True)
603+
converter.update()
604+
with patch(
605+
"hydrolib.tools.extforce_convert.main_converter.ExtOldModel.save"
606+
) as mock_save:
607+
converter.save()
608+
mock_save.assert_called_once()
609+
assert converter.un_supported_quantities == {unsupported_quantity.lower()}
610+
532611

533612
class TestUpdate:
534613

0 commit comments

Comments
 (0)