Skip to content

Commit

Permalink
[back] fix: validate score and score_max in ComparisonCriteriaScore form
Browse files Browse the repository at this point in the history
  • Loading branch information
GresilleSiffle committed Jul 22, 2024
1 parent f1f68bc commit fc2cf91
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 21 deletions.
12 changes: 11 additions & 1 deletion backend/tournesol/models/comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import uuid

import computed_property
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator
from django.db import models
from django.db.models import F, ObjectDoesNotExist, Q
Expand Down Expand Up @@ -147,7 +148,7 @@ class Meta:
def __str__(self):
return f"{self.comparison}/{self.criteria}/{self.score}"

def save(self, *args, **kwargs):
def _validate_score_max(self):
if self.score_max is None:
raise TypeError("The value of score_max cannot be None.")

Expand All @@ -159,4 +160,13 @@ def save(self, *args, **kwargs):
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}."
)

def clean(self):
try:
self._validate_score_max()
except (TypeError, ValueError) as err:
raise ValidationError({"score_max": err.args[0]})

def save(self, *args, **kwargs):
self._validate_score_max()
return super().save(*args, **kwargs)
146 changes: 126 additions & 20 deletions backend/tournesol/tests/test_model_comparisoncriteriascore.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def test_validators_score_max(self):
score.score_max = 1
score.clean_fields()

def test_save_validate_score(self):
score = ComparisonCriteriaScore(
def test_method_validate_score_max(self):
score_test = ComparisonCriteriaScore(
comparison=self.comparison,
criteria=self.poll.main_criteria,
score=11,
Expand All @@ -53,27 +53,133 @@ def test_save_validate_score(self):

# The score cannot be greater than score_max.
with self.assertRaises(ValueError):
score.save()
score_test._validate_score_max()

score.score = -11
score_test.score = -11
# The absolute value of the score cannot be greater than score_max.
with self.assertRaises(ValueError):
score.save()
score_test._validate_score_max()

# The score can be zero.
score.score = 0
score.save()
score_test.score = 0
score_test._validate_score_max()

# The score can be equal to the score_max.
score.score = score.score_max
score.save()
score_test.score = score_test.score_max
score_test._validate_score_max()

# The absolute value of the score can be lesser than score_max.
score.score = -1
score.save()
score_test.score = -1
score_test._validate_score_max()

def test_save_validate_score_max(self):
score = ComparisonCriteriaScore(
score_max_test = ComparisonCriteriaScore(
comparison=self.comparison,
criteria=self.poll.main_criteria,
score=5,
score_max=None,
)

# score_max cannot be None.
with self.assertRaises(TypeError):
score_max_test._validate_score_max()

score_max_test.score_max = 0
# score_max cannot be zero.
with self.assertRaises(ValueError):
score_max_test._validate_score_max()

score_max_test.score_max = -10
# score_max cannot be negative.
with self.assertRaises(ValueError):
score_max_test._validate_score_max()

score_max_test.score_max = 10
score_max_test._validate_score_max()

def test_method_clean_calls_validate_score_max(self):
score_test = ComparisonCriteriaScore(
comparison=self.comparison,
criteria=self.poll.main_criteria,
score=11,
score_max=10,
)

# The score cannot be greater than score_max.
with self.assertRaises(ValidationError):
score_test.clean()

score_test.score = -11
# The absolute value of the score cannot be greater than score_max.
with self.assertRaises(ValidationError):
score_test.clean()

# The score can be zero.
score_test.score = 0
score_test.clean()

# The score can be equal to the score_max.
score_test.score = score_test.score_max
score_test.clean()

# The absolute value of the score can be lesser than score_max.
score_test.score = -1
score_test.clean()

score_max_test = ComparisonCriteriaScore(
comparison=self.comparison,
criteria=self.poll.main_criteria,
score=5,
score_max=None,
)

# score_max cannot be None.
with self.assertRaises(ValidationError):
score_max_test.clean()

score_max_test.score_max = 0
# score_max cannot be zero.
with self.assertRaises(ValidationError):
score_max_test.clean()

score_max_test.score_max = -10
# score_max cannot be negative.
with self.assertRaises(ValidationError):
score_max_test.clean()

score_max_test.score_max = 10
score_max_test.clean()

def test_method_save_calls_validate_score_max(self):
score_test = ComparisonCriteriaScore(
comparison=self.comparison,
criteria=self.poll.main_criteria,
score=11,
score_max=10,
)

# The score cannot be greater than score_max.
with self.assertRaises(ValueError):
score_test.save()

score_test.score = -11
# The absolute value of the score cannot be greater than score_max.
with self.assertRaises(ValueError):
score_test.save()

# The score can be zero.
score_test.score = 0
score_test.save()

# The score can be equal to the score_max.
score_test.score = score_test.score_max
score_test.save()

# The absolute value of the score can be lesser than score_max.
score_test.score = -1
score_test.save()
score_test.delete()

score_max_test = ComparisonCriteriaScore(
comparison=self.comparison,
criteria=self.poll.main_criteria,
score=5,
Expand All @@ -82,17 +188,17 @@ def test_save_validate_score_max(self):

# score_max cannot be None.
with self.assertRaises(TypeError):
score.save()
score_max_test.save()

score.score_max = 0
score_max_test.score_max = 0
# score_max cannot be zero.
with self.assertRaises(ValueError):
score.save()
score_max_test.save()

score.score_max = -10
score_max_test.score_max = -10
# score_max cannot be negative.
with self.assertRaises(ValueError):
score.save()
score_max_test.save()

score.score_max = 10
score.save()
score_max_test.score_max = 10
score_max_test.save()

0 comments on commit fc2cf91

Please sign in to comment.