Skip to content

Commit

Permalink
Check answers for integer and float
Browse files Browse the repository at this point in the history
  • Loading branch information
g-normand committed Sep 5, 2022
1 parent 27f209b commit 14d7dc3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
41 changes: 28 additions & 13 deletions survey/models/answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,34 @@ def values(self):
def check_answer_body(self, question, body):
if question.type in [Question.RADIO, Question.SELECT, Question.SELECT_MULTIPLE]:
choices = question.get_clean_choices()
if body:
if body[0] == "[":
answers = []
for i, part in enumerate(body.split("'")):
if i % 2 == 1:
answers.append(part)
else:
answers = [body]
for answer in answers:
if answer not in choices:
msg = f"Impossible answer '{body}'"
msg += f" should be in {choices} "
raise ValidationError(msg)
self.check_answer_for_select(choices, body)
if question.type == Question.INTEGER and body and body != '':
try:
body = int(body)
except ValueError:
msg = 'Answer is not an integer'
raise ValidationError(msg)
if question.type == Question.FLOAT and body and body != '':
try:
body = float(body)
except ValueError:
msg = 'Answer is not a number'
raise ValidationError(msg)

def check_answer_for_select(self, choices, body):
answers = []
if body:
if body[0] == "[":
for i, part in enumerate(body.split("'")):
if i % 2 == 1:
answers.append(part)
else:
answers = [body]
for answer in answers:
if answer not in choices:
msg = f"Impossible answer '{body}'"
msg += f" should be in {choices} "
raise ValidationError(msg)

def __str__(self):
return f"{self.__class__.__name__} to '{self.question}' : '{self.body}'"
1 change: 1 addition & 0 deletions survey/tests/models/base_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def setUp(self):
[Question.SELECT_MULTIPLE, "Yes", "Yes, No, Maybe"],
[Question.INTEGER, 42, None],
[Question.SELECT_MULTIPLE, "[u'2', u'4']", "2, 4, 6"],
[Question.FLOAT, 28.5, None],
]
for i, data in enumerate(self.data):
qtype, answer_body, answer_choices = data
Expand Down
9 changes: 8 additions & 1 deletion survey/tests/models/test_answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ def test_unicode(self):

def test_init(self):
"""We raise validation error if the answer is not a possible choice"""
self.assertRaises(ValidationError, Answer, response=self.response, question=self.questions[4], body="Dd")
self.assertRaises(ValidationError, Answer, response=self.response,
question=self.questions[4], body="Dd")
self.assertRaises(ValidationError, Answer, response=self.response,
question=self.questions[5], body="not an int")
self.assertRaises(ValidationError, Answer, response=self.response,
question=self.questions[7], body="not a float")

def test_values(self):
"""We can have multiple nasty values ans it will be detected."""
self.assertEqual(self.answers[0].values, ["Mytext"])
self.assertEqual(self.answers[4].values, ["Yes"])
self.assertEqual(self.answers[5].body, 42)
self.assertEqual(self.answers[6].values, ["2", "4"])
self.assertEqual(self.answers[7].body, 28.5)

0 comments on commit 14d7dc3

Please sign in to comment.