Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Feb 13, 2025
1 parent 4bc2109 commit 0dfb230
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
14 changes: 2 additions & 12 deletions src/antares/craft/model/binding_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ class ConstraintTerm(ConstraintTermData):
def weight_offset(self) -> str:
return f"{self.weight}%{self.offset}" if self.offset != 0 else f"{self.weight}"

def from_update_model(self, update_model: ConstraintTermUpdate) -> "ConstraintTerm":
return ConstraintTerm(
data=self.data, weight=update_model.weight or self.weight, offset=update_model.offset or self.offset
)


@dataclass
class BindingConstraintPropertiesUpdate:
Expand Down Expand Up @@ -159,13 +154,8 @@ def delete_term(self, term: ConstraintTerm) -> None:

def update_term(self, term: ConstraintTermUpdate) -> None:
existing_term = self._terms[term.id]
if not term.weight:
term.weight = existing_term.weight
self._binding_constraint_service.update_binding_constraint_term(self.id, term)
if term.offset:
existing_term.offset = term.offset
existing_term.weight = term.weight
self._terms[term.id] = existing_term
new_term = self._binding_constraint_service.update_binding_constraint_term(self.id, term, existing_term)
self._terms[term.id] = new_term

def update_properties(self, properties: BindingConstraintPropertiesUpdate) -> None:
new_properties = self._binding_constraint_service.update_binding_constraint_properties(self, properties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,24 @@ def delete_binding_constraint_term(self, constraint_id: str, term_id: str) -> No
raise ConstraintTermDeletionError(constraint_id, term_id, e.message) from e

@override
def update_binding_constraint_term(self, constraint_id: str, term: ConstraintTermUpdate) -> None:
def update_binding_constraint_term(
self, constraint_id: str, term: ConstraintTermUpdate, existing_term: ConstraintTerm
) -> ConstraintTerm:
url = f"{self._base_url}/studies/{self.study_id}/bindingconstraints/{constraint_id}/term"
try:
body: dict[str, Any] = {"data": asdict(term.data), "weight": term.weight}
body: dict[str, Any] = {"data": asdict(term.data), "weight": term.weight or existing_term.weight}
if term.offset:
body["offset"] = term.offset
self._wrapper.put(url, json=body)
except APIError as e:
raise ConstraintTermEditionError(constraint_id, term.id, e.message) from e

if term.weight:
existing_term.weight = term.weight
if term.offset:
existing_term.offset = term.offset
return existing_term

@override
def update_binding_constraint_properties(
self, binding_constraint: BindingConstraint, properties: BindingConstraintPropertiesUpdate
Expand Down
7 changes: 5 additions & 2 deletions src/antares/craft/service/base_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,14 @@ def delete_binding_constraint_term(self, constraint_id: str, term_id: str) -> No
pass

@abstractmethod
def update_binding_constraint_term(self, constraint_id: str, term: "ConstraintTermUpdate") -> None:
def update_binding_constraint_term(
self, constraint_id: str, term: "ConstraintTermUpdate", existing_term: "ConstraintTerm"
) -> "ConstraintTerm":
"""
Args:
constraint_id: binding constraint's id containing the term
term: binding constraint term to be updated
term: term with new values
existing_term: existing term with existing values
"""
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ def delete_binding_constraint_term(self, constraint_id: str, term_id: str) -> No
raise NotImplementedError

@override
def update_binding_constraint_term(self, constraint_id: str, term: ConstraintTermUpdate) -> None:
def update_binding_constraint_term(
self, constraint_id: str, term: ConstraintTermUpdate, existing_term: ConstraintTerm
) -> ConstraintTerm:
raise NotImplementedError

@override
Expand Down

0 comments on commit 0dfb230

Please sign in to comment.