Skip to content

Commit

Permalink
Rename error message variable and add Decimal into values accepted fo…
Browse files Browse the repository at this point in the history
…r min/max
  • Loading branch information
VirajP1002 committed Dec 20, 2023
1 parent 06b4792 commit 26b20e0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 6 additions & 4 deletions app/validators/answers/number_answer_validator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from decimal import Decimal

from app.validators.answers.answer_validator import AnswerValidator
from app.validators.routing.types import TYPE_NUMBER, resolve_value_source_json_type

Expand All @@ -22,7 +24,7 @@ class NumberAnswerValidator(AnswerValidator):
GREATER_DECIMALS_ON_ANSWER_REFERENCE = (
"The referenced answer has a greater number of decimal places than answer"
)
MAX_MIN_IS_STRING = "The minimum or maximum value is not a float or an integer"
MIN_OR_MAX_IS_NOT_NUMERIC = "The minimum or maximum value is not a float or an integer"

def __init__(self, schema_element, questionnaire_schema):
super().__init__(schema_element, questionnaire_schema)
Expand Down Expand Up @@ -75,10 +77,10 @@ def validate_min_max_is_number(self):
resolve_value_source_json_type(value, self.questionnaire_schema)
!= TYPE_NUMBER
):
self.add_error(self.MAX_MIN_IS_STRING)
self.add_error(self.MIN_OR_MAX_IS_NOT_NUMERIC)
else:
if not isinstance(value, int | float):
self.add_error(self.MAX_MIN_IS_STRING)
if not isinstance(value, int | float | Decimal):
self.add_error(self.MIN_OR_MAX_IS_NOT_NUMERIC)

def validate_value_in_limits(self):
min_value = self.answer.get("minimum", {}).get("value", 0)
Expand Down
6 changes: 3 additions & 3 deletions tests/validators/answers/test_number_answer_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_maximum_set_as_string():
validator.validate_min_max_is_number()

assert validator.errors[0] == {
"message": validator.MAX_MIN_IS_STRING,
"message": validator.MIN_OR_MAX_IS_NOT_NUMERIC,
"answer_id": "total-percentage",
}

Expand All @@ -119,7 +119,7 @@ def test_minimum_set_as_string():
validator.validate_min_max_is_number()

assert validator.errors[0] == {
"message": validator.MAX_MIN_IS_STRING,
"message": validator.MIN_OR_MAX_IS_NOT_NUMERIC,
"answer_id": "total-percentage",
}

Expand All @@ -143,7 +143,7 @@ def test_min_and_max_set_as_string():
validator.validate_min_max_is_number()

assert validator.errors[1] == {
"message": validator.MAX_MIN_IS_STRING,
"message": validator.MIN_OR_MAX_IS_NOT_NUMERIC,
"answer_id": "total-percentage",
}
assert len(validator.errors) == 2
Expand Down

0 comments on commit 26b20e0

Please sign in to comment.