Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Damien Jeandemange <damien.jeandemange@artelys.com>
  • Loading branch information
jeandemanged committed Nov 21, 2024
1 parent 312fc4f commit 903298b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,11 @@ static Triple<ShuntCompensator, ShuntCompensatorNonLinearModel.Section, Integer>
int section = dataframe.getIntValue("section", index)
.orElseThrow(() -> new PowsyblException("section is missing"));
// careful: shunt section number starts at 1, but position in array starts at 0
return Triple.of(shuntCompensator, shuntNonLinear.getAllSections().get(section - 1), section);
List<ShuntCompensatorNonLinearModel.Section> allSections = shuntNonLinear.getAllSections();
if (section < 1 || section > allSections.size()) {
throw new PowsyblException(String.format("Section number must be between 1 and %d, inclusive", allSections.size()));
}
return Triple.of(shuntCompensator, allSections.get(section - 1), section);
}
}

Expand Down
20 changes: 17 additions & 3 deletions tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -1459,11 +1459,25 @@ def test_voltage_levels():
pd.testing.assert_frame_equal(expected, n.get_voltage_levels(), check_dtype=False)


def test_update_with_keywords():
def test_update_non_linear_shunt_with_keywords():
n = util.create_non_linear_shunt_network()
n.update_non_linear_shunt_compensator_sections(id='SHUNT', section=1, g=0.2, b=0.000001)
assert 0.2 == n.get_non_linear_shunt_compensator_sections().loc['SHUNT', 1]['g']
assert 0.000001 == n.get_non_linear_shunt_compensator_sections().loc['SHUNT', 1]['b']
n.update_non_linear_shunt_compensator_sections(id='SHUNT', section=2, g=0.3, b=0.000002)
sections = n.get_non_linear_shunt_compensator_sections()
assert 0.2 == sections.loc['SHUNT', 1]['g']
assert 0.000001 == sections.loc['SHUNT', 1]['b']
assert 0.3 == sections.loc['SHUNT', 2]['g']
assert 0.000002 == sections.loc['SHUNT', 2]['b']


def test_update_non_linear_shunt_wrong_section():
n = util.create_non_linear_shunt_network()
with pytest.raises(PyPowsyblError) as exc:
n.update_non_linear_shunt_compensator_sections(id='SHUNT', section=0, g=0.2, b=0.000001)
assert exc.match('Section number must be between 1 and 2, inclusive')
with pytest.raises(PyPowsyblError) as exc:
n.update_non_linear_shunt_compensator_sections(id='SHUNT', section=3, g=0.2, b=0.000001)
assert exc.match('Section number must be between 1 and 2, inclusive')


def test_update_generators_with_keywords():
Expand Down

0 comments on commit 903298b

Please sign in to comment.