Skip to content

Commit 734345b

Browse files
resolve comments
Signed-off-by: Jerry Guo <Jerry.Jinfeng.Guo@alliander.com>
1 parent 3829df7 commit 734345b

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/power_grid_model_io/converters/tabular_converter.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,21 +376,19 @@ def _normalize_extra_col_def(self, col_def: Any) -> Any:
376376

377377
# Collect all non-optional_extra column names
378378
regular_columns = set()
379-
normalized_list = []
380379

381380
for item in col_def:
382381
if isinstance(item, dict) and len(item) == 1 and "optional_extra" in item:
383382
# This is an optional_extra section - we'll process it later
384-
normalized_list.append(item)
383+
pass
385384
else:
386385
# This is a regular column
387386
if isinstance(item, str):
388387
regular_columns.add(item)
389-
normalized_list.append(item)
390388

391389
# Now process optional_extra sections and remove duplicates
392390
final_list = []
393-
for item in normalized_list:
391+
for item in col_def:
394392
if isinstance(item, dict) and len(item) == 1 and "optional_extra" in item:
395393
optional_cols = item["optional_extra"]
396394
if isinstance(optional_cols, list):

tests/unit/converters/test_tabular_converter.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,20 @@ def test_optional_extra__mixed_with_required(converter: TabularConverter):
14201420
assert list(result["guid"]) == ["guid1", "guid2"]
14211421

14221422

1423+
def test_optional_extra__mixed_with_required_missing_column(converter: TabularConverter):
1424+
"""Test that missing required column raises error even with optional_extra present"""
1425+
# Arrange
1426+
data = TabularData(test_table=pd.DataFrame({"id": [1, 2], "name": ["node1", "node2"], "guid": ["guid1", "guid2"]}))
1427+
# "missing_col" is required but missing
1428+
col_def = ["name", "missing_col", {"optional_extra": ["guid", "station"]}]
1429+
1430+
# Act & Assert
1431+
with pytest.raises(KeyError, match="missing_col"):
1432+
converter._parse_col_def(
1433+
data=data, table="test_table", col_def=col_def, table_mask=None, extra_info=None, allow_missing=False
1434+
)
1435+
1436+
14231437
def test_optional_extra__in_extra_info(converter: TabularConverter):
14241438
"""Test that optional_extra works correctly with _handle_extra_info"""
14251439
# Arrange
@@ -1444,6 +1458,23 @@ def test_optional_extra__in_extra_info(converter: TabularConverter):
14441458
assert "station" not in extra_info[200]
14451459

14461460

1461+
def test_optional_extra__empty_list_with_extra_info(converter: TabularConverter):
1462+
"""Test that empty optional_extra list results in no updates to extra_info"""
1463+
# Arrange
1464+
data = TabularData(test_table=pd.DataFrame({"id": [1, 2], "name": ["node1", "node2"], "guid": ["guid1", "guid2"]}))
1465+
uuids = np.array([100, 200])
1466+
extra_info: ExtraInfo = {}
1467+
col_def: dict[str, list[str]] = {"optional_extra": []}
1468+
1469+
# Act
1470+
converter._handle_extra_info(
1471+
data=data, table="test_table", col_def=col_def, uuids=uuids, table_mask=None, extra_info=extra_info
1472+
)
1473+
1474+
# Assert
1475+
assert len(extra_info) == 0
1476+
1477+
14471478
def test_optional_extra__all_missing_no_extra_info(converter: TabularConverter):
14481479
"""Test that when all optional columns are missing, no extra_info entries are created"""
14491480
# Arrange

0 commit comments

Comments
 (0)