diff --git a/backend/tournesol/models/comparisons.py b/backend/tournesol/models/comparisons.py index 31133f3ed5..f111ca18b7 100644 --- a/backend/tournesol/models/comparisons.py +++ b/backend/tournesol/models/comparisons.py @@ -5,12 +5,11 @@ import uuid import computed_property -from django.core.validators import MaxValueValidator, MinValueValidator +from django.core.validators import MinValueValidator from django.db import models from django.db.models import F, ObjectDoesNotExist, Q from core.models import User -from tournesol.utils.constants import COMPARISON_MAX from .poll import Poll @@ -149,10 +148,15 @@ def __str__(self): return f"{self.comparison}/{self.criteria}/{self.score}" def save(self, *args, **kwargs): - if self.score_max is not None: - if abs(self.score) > abs(self.score_max): - raise ValueError( - f"The absolute value of the score {self.score} given to the criterion " - f"{self.criteria} can't be superior to the score_max {self.score_max}." - ) + if self.score_max is None: + raise TypeError("The value of score_max cannot be None.") + + if self.score_max <= 0: + raise ValueError("The value of score_max must be greater than or equal to 1.") + + if abs(self.score) > self.score_max: + raise ValueError( + f"The absolute value of the score {self.score} given to the criterion " + f"{self.criteria} can't be greater than the value of score_max {self.score_max}." + ) return super().save(*args, **kwargs) diff --git a/backend/tournesol/tests/test_model_comparisoncriteriascore.py b/backend/tournesol/tests/test_model_comparisoncriteriascore.py index 214722ed98..10cdc07fff 100644 --- a/backend/tournesol/tests/test_model_comparisoncriteriascore.py +++ b/backend/tournesol/tests/test_model_comparisoncriteriascore.py @@ -2,7 +2,7 @@ All test cases of the `ComparisonCriteriaScore` model. """ -from django.db import IntegrityError, transaction +from django.core.exceptions import ValidationError from django.test import TestCase from core.tests.factories.user import UserFactory @@ -21,11 +21,25 @@ class ComparisonCriteriaScoreTestCase(TestCase): def setUp(self): self.poll = PollWithCriteriasFactory() self.user = UserFactory(username=self._user) - self.comparison = ComparisonFactory( - user=self.user, - poll=self.poll, + self.comparison = ComparisonFactory(poll=self.poll, user=self.user) + + def test_validators_score_max(self): + score = ComparisonCriteriaScore( + comparison=self.comparison, + criteria=self.poll.main_criteria, + score=0, + score_max=0, ) + # score_max cannot be zero. + with self.assertRaises(ValidationError): + score.clean_fields() + + score.score_max = -1 + # score_max cannot be negative. + with self.assertRaises(ValidationError): + score.clean_fields() + def test_save_validate_score(self): score = ComparisonCriteriaScore( comparison=self.comparison, @@ -34,19 +48,48 @@ def test_save_validate_score(self): score_max=10, ) - # The score cannot be superior to the score_max. + # The score cannot be greater than score_max. with self.assertRaises(ValueError): score.save() score.score = -11 - # The absolute value of the score cannot be superior to the score_max. + # The absolute value of the score cannot be greater than score_max. with self.assertRaises(ValueError): score.save() - # The score can be equal to the magnitude. + # The score can be zero. + score.score = 0 + score.save() + + # The score can be equal to the score_max. score.score = score.score_max score.save() - # The absolute value of the score can be inferior to the score_max. - score.score = 0 + # The absolute value of the score can be lesser than score_max. + score.score = -1 + score.save() + + def test_save_validate_score_max(self): + score = ComparisonCriteriaScore( + comparison=self.comparison, + criteria=self.poll.main_criteria, + score=5, + score_max=None, + ) + + # score_max cannot be None. + with self.assertRaises(TypeError): + score.save() + + score.score_max = 0 + # score_max cannot be zero. + with self.assertRaises(ValueError): + score.save() + + score.score_max = -10 + # score_max cannot be negative. + with self.assertRaises(ValueError): + score.save() + + score.score_max = 10 score.save()