Skip to content

Commit

Permalink
Update max_number and min_number values (#191)
Browse files Browse the repository at this point in the history
Co-authored-by: rmccar <[email protected]>
Co-authored-by: Mebin Abraham <[email protected]>
  • Loading branch information
3 people authored May 3, 2023
1 parent fb87a63 commit 842bc89
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 31 deletions.
35 changes: 32 additions & 3 deletions app/validators/answers/number_answer_validator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from app.validators.answers.answer_validator import AnswerValidator

MAX_NUMBER = 9999999999
MIN_NUMBER = -999999999
MAX_NUMBER = 999_999_999_999_999
MIN_NUMBER = -99_999_999_999_999
MAX_DECIMAL_PLACES = 6


Expand Down Expand Up @@ -31,7 +31,6 @@ def validate(self):

self.validate_decimal_places()
self.validate_mandatory_has_no_default()
self.validate_value_in_limits()
self.validate_decimals()

if self.questionnaire_schema:
Expand All @@ -51,6 +50,8 @@ def validate(self):
self.questionnaire_schema.numeric_answer_ranges
)

self.validate_value_in_limits()

return self.errors

def validate_mandatory_has_no_default(self):
Expand All @@ -65,11 +66,39 @@ def validate_value_in_limits(self):
self.add_error(
self.MINIMUM_LESS_THAN_LIMIT, value=min_value, limit=MIN_NUMBER
)
elif isinstance(min_value, dict):
source = min_value["source"]
answer_ranges = self.questionnaire_schema.numeric_answer_ranges
referred_answer = (
self.questionnaire_schema.get_numeric_value_for_value_source(
source, min_value, answer_ranges
)
)
if referred_answer["min"] < MIN_NUMBER:
self.add_error(
self.MINIMUM_LESS_THAN_LIMIT,
value=referred_answer["min"],
limit=MIN_NUMBER,
)

if isinstance(max_value, int) and max_value > MAX_NUMBER:
self.add_error(
self.MAXIMUM_GREATER_THAN_LIMIT, value=max_value, limit=MAX_NUMBER
)
elif isinstance(max_value, dict):
source = max_value["source"]
answer_ranges = self.questionnaire_schema.numeric_answer_ranges
referred_answer = (
self.questionnaire_schema.get_numeric_value_for_value_source(
source, max_value, answer_ranges
)
)
if referred_answer["max"] > MAX_NUMBER:
self.add_error(
self.MAXIMUM_GREATER_THAN_LIMIT,
value=referred_answer["max"],
limit=MAX_NUMBER,
)

def are_decimal_places_valid(self):
if "calculated" in self.answer:
Expand Down
42 changes: 21 additions & 21 deletions app/validators/questionnaire_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from app.answer_type import AnswerType

MAX_NUMBER = 9999999999
MIN_NUMBER = -999999999
MAX_NUMBER = 999_999_999_999_999
MIN_NUMBER = -99_999_999_999_999
MAX_DECIMAL_PLACES = 6


Expand Down Expand Up @@ -409,29 +409,11 @@ def _get_answer_maximum(
return maximum_value - (1 / 10**decimal_places)
return maximum_value

def _get_numeric_value_for_value_source(
self, value_source, defined_value, answer_ranges
):
referred_answer = None
if value_source == "answers":
referred_answer = answer_ranges.get(defined_value["identifier"])
elif value_source == "calculated_summary":
calculated_summary_block = self.get_block(defined_value["identifier"])
answers_to_calculate = self.get_calculated_answer_ids(
calculated_summary_block
)

for answer_id in answers_to_calculate:
referred_answer = answer_ranges.get(answer_id)
if referred_answer is None:
return None
return referred_answer

def _get_numeric_value(self, defined_value, system_default, answer_ranges):
if not isinstance(defined_value, dict):
return defined_value
if source := defined_value.get("source"):
referred_answer = self._get_numeric_value_for_value_source(
referred_answer = self.get_numeric_value_for_value_source(
value_source=source,
defined_value=defined_value,
answer_ranges=answer_ranges,
Expand Down Expand Up @@ -468,3 +450,21 @@ def get_parent_section_for_block(self, block_id) -> dict | None:
def is_block_in_repeating_section(self, block_id: str) -> bool:
parent_section = self.get_parent_section_for_block(block_id)
return parent_section and self.is_repeating_section(parent_section["id"])

def get_numeric_value_for_value_source(
self, value_source, defined_value, answer_ranges
):
referred_answer = None
if value_source == "answers":
referred_answer = answer_ranges.get(defined_value["identifier"])
elif value_source == "calculated_summary":
calculated_summary_block = self.get_block(defined_value["identifier"])
answers_to_calculate = self.get_calculated_answer_ids(
calculated_summary_block
)

for answer_id in answers_to_calculate:
referred_answer = answer_ranges.get(answer_id)
if referred_answer is None:
return None
return referred_answer
18 changes: 11 additions & 7 deletions tests/validators/answers/test_number_answer_validator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from app.validators.answers import NumberAnswerValidator
from app.validators.questionnaire_schema import QuestionnaireSchema
from app.validators.questionnaire_schema import (
MAX_NUMBER,
MIN_NUMBER,
QuestionnaireSchema,
)
from tests.conftest import get_mock_schema_with_data_version
from tests.utils import _open_and_load_schema_file

Expand All @@ -9,8 +13,8 @@ def test_minimum_value():
"id": "answer-4",
"label": "Max/Min out of system limits",
"mandatory": False,
"maximum": {"value": 99999999999},
"minimum": {"value": -99999999999},
"maximum": {"value": 9_999_999_999_999_999},
"minimum": {"value": -999_999_999_999_999},
"type": "Number",
}

Expand All @@ -20,15 +24,15 @@ def test_minimum_value():

assert validator.errors[0] == {
"message": validator.MINIMUM_LESS_THAN_LIMIT,
"value": -99999999999,
"limit": -999999999,
"value": -999_999_999_999_999,
"limit": MIN_NUMBER,
"answer_id": "answer-4",
}

assert validator.errors[1] == {
"message": validator.MAXIMUM_GREATER_THAN_LIMIT,
"value": 99999999999,
"limit": 9999999999,
"value": 9_999_999_999_999_999,
"limit": MAX_NUMBER,
"answer_id": "answer-4",
}

Expand Down

0 comments on commit 842bc89

Please sign in to comment.