Skip to content

Commit

Permalink
add validation for the field score_max
Browse files Browse the repository at this point in the history
  • Loading branch information
GresilleSiffle committed May 2, 2024
1 parent 95dcd6e commit 4efa27d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
20 changes: 12 additions & 8 deletions backend/tournesol/models/comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
61 changes: 52 additions & 9 deletions backend/tournesol/tests/test_model_comparisoncriteriascore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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()

0 comments on commit 4efa27d

Please sign in to comment.