Skip to content

Commit

Permalink
fix code to make the test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Jan 22, 2025
1 parent ae00351 commit 54ee492
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
11 changes: 7 additions & 4 deletions antarest/study/storage/variantstudy/model/command/update_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ def _apply(self, study_data: FileStudy, listener: t.Optional[ICommandListener] =
version = study_data.config.version
area_from, area_to = sorted([self.area1, self.area2])

properties = study_data.tree.get(["input", "links", area_from, "properties", area_to])

internal_link = LinkInternal.model_validate(self.parameters)

# Updates ini properties
Expand All @@ -61,6 +59,7 @@ def _apply(self, study_data: FileStudy, listener: t.Optional[ICommandListener] =
to_exclude.update("filter-synthesis", "filter-year-by-year")
new_ini_properties = internal_link.model_dump(by_alias=True, exclude=to_exclude, exclude_unset=True)
if new_ini_properties: # If no new INI properties were given we shouldn't update the INI file
properties = study_data.tree.get(["input", "links", area_from, "properties", area_to])
properties.update(new_ini_properties)
study_data.tree.save(properties, ["input", "links", area_from, "properties", area_to])

Expand All @@ -80,6 +79,8 @@ def _apply(self, study_data: FileStudy, listener: t.Optional[ICommandListener] =
if not old_parameters:
db_properties = LinkTsGeneration.model_validate(db_properties_json)
new_parameters = db_properties.to_db_model(study_id, area_from, area_to)
db.session.add(new_parameters)
db.session.commit()
else:
old_props = LinkTsGeneration.from_db_model(old_parameters).model_dump(mode="json")
old_props.update(db_properties_json)
Expand All @@ -89,8 +90,10 @@ def _apply(self, study_data: FileStudy, listener: t.Optional[ICommandListener] =
# We should keep the same matrices
new_parameters.modulation = old_parameters.modulation
new_parameters.project = old_parameters.prepro
db.session.merge(new_parameters)
db.session.commit()
# todo: I should do it more efficiently but a merge creates 2 entry ...
db.session.delete(old_parameters)
db.session.add(new_parameters)
db.session.commit()

# Updates matrices
if self.series:
Expand Down
12 changes: 5 additions & 7 deletions tests/variantstudy/model/command/test_update_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,31 @@ def test_apply(self, empty_study: FileStudy, command_context: CommandContext):

# Asserts the ini file is well modified (new value + old values unmodified)
link = IniReader()
link_data = link.read(study_path / "input" / "links" / area1_id / "properties.ini")
ini_path = study_path / "input" / "links" / area1_id / "properties.ini"
link_data = link.read(ini_path)
assert link_data[area2_id]["hurdles-cost"] == parameters["hurdles-cost"]
assert link_data[area2_id]["asset-type"] == parameters["asset-type"]
assert link_data[area2_id]["colorb"] == new_parameters["colorb"]

# Updating a DB property
new_parameters = {"nominal_capacity": 111}
update_parameters = {"parameters": new_parameters, **command_parameters}
# Removes the ini file to show we don't need it as we're only updating the DB
ini_path.unlink()

with DBStatementRecorder(db.session.bind) as db_recorder:
UpdateLink.model_validate(update_parameters).apply(study_data=empty_study)
# todo: this check seems random, don't know why :/
# assert len(db_recorder.sql_statements) == 2

# todo: Could be nice to check that the tree isn't called. Don't know how.
# assert len(db_recorder.sql_statements) == 3

# Checks the DB state. Old properties should remain the same and the new one should be updated
ts_gen_properties = (
db.session.query(LinksParametersTsGeneration)
.filter_by(study_id=study_id, area_from=area1_id, area_to=area2_id)
.all()
)
# todo: Why do I have 2 entries here ????
"""
assert len(ts_gen_properties) == 1
link_ts_gen_props = ts_gen_properties[0]
assert link_ts_gen_props.unit_count == parameters["unit_count"]
assert link_ts_gen_props.law_planned == parameters["law_planned"]
assert link_ts_gen_props.nominal_capacity == new_parameters["nominal_capacity"]
"""

0 comments on commit 54ee492

Please sign in to comment.