Skip to content

Commit 0f210dd

Browse files
committed
update test for keyerror
Signed-off-by: zhen0427 <[email protected]>
1 parent cf1fb09 commit 0f210dd

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

src/power_grid_model_io/converters/tabular_converter.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import numpy as np
1414
import pandas as pd
15+
from enum import Enum
1516
import yaml
1617
from power_grid_model import initialize_array
1718
from power_grid_model.data_types import Dataset
@@ -145,9 +146,9 @@ def _parse_data(self, data: TabularData, data_type: str, extra_info: Optional[Ex
145146
def _convert_table_to_component( # pylint: disable = too-many-arguments,too-many-positional-arguments
146147
self,
147148
data: TabularData,
148-
data_type: str,
149+
data_type: Union[str, Enum],
149150
table: str,
150-
component: str,
151+
component: Union[str, Enum],
151152
attributes: InstanceAttributes,
152153
extra_info: Optional[ExtraInfo],
153154
) -> Optional[np.ndarray]:
@@ -165,9 +166,9 @@ def _convert_table_to_component( # pylint: disable = too-many-arguments,too-man
165166
extra_info: an optional dictionary where extra component info (that can't be specified in
166167
power-grid-model data) can be specified
167168
data: TabularData:
168-
data_type: str:
169+
data_type: Union[str, Enum]:
169170
table: str:
170-
component: str:
171+
component: Union[str, Enum]:
171172
attributes: InstanceAttributes:
172173
extra_info: Optional[ExtraInfo]:
173174
@@ -187,13 +188,16 @@ def _convert_table_to_component( # pylint: disable = too-many-arguments,too-man
187188

188189
n_records = np.sum(table_mask) if table_mask is not None else len(data[table])
189190

191+
component_str = component.value if isinstance(component, Enum) else component
192+
data_type_str = data_type.value if isinstance(data_type, Enum) else data_type
193+
190194
try:
191-
pgm_data = initialize_array(data_type=data_type, component_type=component, shape=n_records)
195+
pgm_data = initialize_array(data_type=data_type_str, component_type=component_str, shape=n_records)
192196
except KeyError as ex:
193-
raise KeyError(f"Invalid component type '{component}' or data type '{data_type}'") from ex
197+
raise KeyError(f"Invalid component type '{component_str}' or data type '{data_type_str}'") from ex
194198

195199
if "id" not in attributes:
196-
raise KeyError(f"No mapping for the attribute 'id' for '{component}s'!")
200+
raise KeyError(f"No mapping for the attribute 'id' for '{component_str}s'!")
197201

198202
# Make sure that the "id" column is always parsed first (at least before "extra" is parsed)
199203
attributes_without_filter = {k: v for k, v in attributes.items() if k != "filters"}
@@ -207,7 +211,7 @@ def _convert_table_to_component( # pylint: disable = too-many-arguments,too-man
207211
data=data,
208212
pgm_data=pgm_data,
209213
table=table,
210-
component=component,
214+
component=component_str,
211215
attr=attr,
212216
col_def=col_def,
213217
table_mask=table_mask,
@@ -232,7 +236,7 @@ def _convert_col_def_to_attribute( # pylint: disable = too-many-arguments,too-m
232236
data: TabularData,
233237
pgm_data: np.ndarray,
234238
table: str,
235-
component: str,
239+
component: Union[str, Enum],
236240
attr: str,
237241
col_def: Any,
238242
table_mask: Optional[np.ndarray],
@@ -254,7 +258,7 @@ def _convert_col_def_to_attribute( # pylint: disable = too-many-arguments,too-m
254258
data: TabularData:
255259
pgm_data: np.ndarray:
256260
table: str:
257-
component: str:
261+
component: Union[str, Enum]:
258262
attr: str:
259263
col_def: Any:
260264
extra_info: Optional[ExtraInfo]:
@@ -265,12 +269,15 @@ def _convert_col_def_to_attribute( # pylint: disable = too-many-arguments,too-m
265269
"""
266270
# To avoid mistakes, the attributes in the mapping should exist. There is one extra attribute called
267271
# 'extra' in which extra information can be captured.
272+
273+
component_str = component.value if isinstance(component, Enum) else component
274+
268275
if pgm_data.dtype.names is None:
269-
raise ValueError(f"pgm_data for '{component}s' has no attributes defined. (dtype.names is None)")
276+
raise ValueError(f"pgm_data for '{component_str}s' has no attributes defined. (dtype.names is None)")
270277

271278
if attr not in pgm_data.dtype.names and attr not in ["extra", "filters"]:
272279
attrs = ", ".join(pgm_data.dtype.names)
273-
raise KeyError(f"Could not find attribute '{attr}' for '{component}s'. (choose from: {attrs})")
280+
raise KeyError(f"Could not find attribute '{attr}' for '{component_str}s'. (choose from: {attrs})")
274281

275282
if attr == "extra":
276283
# Extra info must be linked to the object IDs, therefore the uuids should be known before extra info can

tests/unit/converters/test_pandapower_converter_input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ def test_create_input_data():
442442
("create_fn", "table"),
443443
[
444444
(PandaPowerConverter._create_pgm_input_nodes, "bus"),
445-
(PandaPowerConverter._create_pgm_input_lines, ComponentType.line),
445+
(PandaPowerConverter._create_pgm_input_lines, "line"),
446446
(PandaPowerConverter._create_pgm_input_sources, "ext_grid"),
447-
(PandaPowerConverter._create_pgm_input_shunts, ComponentType.shunt),
447+
(PandaPowerConverter._create_pgm_input_shunts, "shunt"),
448448
(PandaPowerConverter._create_pgm_input_sym_gens, "sgen"),
449449
(PandaPowerConverter._create_pgm_input_sym_loads, "load"),
450450
(PandaPowerConverter._create_pgm_input_transformers, "trafo"),

tests/unit/converters/test_tabular_converter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_convert_table_to_component(converter: TabularConverter, tabular_data_no
116116
)
117117
assert none_data is None
118118
# wrong component
119-
with pytest.raises(KeyError, match="Invalid component type \'dummy\' or data type \'DatasetType.input\'"):
119+
with pytest.raises(KeyError, match="Invalid component type 'dummy' or data type 'input'"):
120120
converter._convert_table_to_component(
121121
data=tabular_data_no_units_no_substitutions,
122122
data_type=DatasetType.input,
@@ -126,7 +126,7 @@ def test_convert_table_to_component(converter: TabularConverter, tabular_data_no
126126
extra_info=None,
127127
)
128128
# wrong data_type
129-
with pytest.raises(KeyError, match="Invalid component type \'ComponentType.node\' or data type 'some_type'"):
129+
with pytest.raises(KeyError, match="Invalid component type 'node' or data type 'some_type'"):
130130
converter._convert_table_to_component(
131131
data=tabular_data_no_units_no_substitutions,
132132
data_type="some_type",
@@ -136,7 +136,7 @@ def test_convert_table_to_component(converter: TabularConverter, tabular_data_no
136136
extra_info=None,
137137
)
138138
# no 'id' in attributes
139-
with pytest.raises(KeyError, match="No mapping for the attribute \'id\' for \'ComponentType.nodes\'!"):
139+
with pytest.raises(KeyError, match="No mapping for the attribute 'id' for 'nodes'!"):
140140
converter._convert_table_to_component(
141141
data=tabular_data_no_units_no_substitutions,
142142
data_type=DatasetType.input,
@@ -236,7 +236,7 @@ def test_convert_col_def_to_attribute(
236236
):
237237
with pytest.raises(
238238
KeyError,
239-
match=r"Could not find attribute \'incorrect_attribute\' for \'ComponentType.nodes\'. \(choose from: id, u_rated\)",
239+
match=r"Could not find attribute 'incorrect_attribute' for 'nodes'. \(choose from: id, u_rated\)",
240240
):
241241
converter._convert_col_def_to_attribute(
242242
data=tabular_data_no_units_no_substitutions,

0 commit comments

Comments
 (0)