From 0dfb230afc19da4baae991846a9080f523a4ed2e Mon Sep 17 00:00:00 2001 From: belthlemar Date: Thu, 13 Feb 2025 11:37:16 +0100 Subject: [PATCH] add tests --- src/antares/craft/model/binding_constraint.py | 14 ++------------ .../api_services/services/binding_constraint.py | 12 ++++++++++-- src/antares/craft/service/base_services.py | 7 +++++-- .../local_services/services/binding_constraint.py | 4 +++- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/antares/craft/model/binding_constraint.py b/src/antares/craft/model/binding_constraint.py index 8107c31f..a1753c39 100644 --- a/src/antares/craft/model/binding_constraint.py +++ b/src/antares/craft/model/binding_constraint.py @@ -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: @@ -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) diff --git a/src/antares/craft/service/api_services/services/binding_constraint.py b/src/antares/craft/service/api_services/services/binding_constraint.py index db4105e3..8c49bb3d 100644 --- a/src/antares/craft/service/api_services/services/binding_constraint.py +++ b/src/antares/craft/service/api_services/services/binding_constraint.py @@ -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 diff --git a/src/antares/craft/service/base_services.py b/src/antares/craft/service/base_services.py index 34dcffb7..38aa0291 100644 --- a/src/antares/craft/service/base_services.py +++ b/src/antares/craft/service/base_services.py @@ -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 diff --git a/src/antares/craft/service/local_services/services/binding_constraint.py b/src/antares/craft/service/local_services/services/binding_constraint.py index c0cbae8d..a59dc171 100644 --- a/src/antares/craft/service/local_services/services/binding_constraint.py +++ b/src/antares/craft/service/local_services/services/binding_constraint.py @@ -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