More unit testing

This commit is contained in:
2013-08-25 21:10:22 +10:00
parent 5ab0b8c837
commit ff050cc141
4 changed files with 36 additions and 9 deletions

View File

@@ -14,13 +14,7 @@ def check_answer(request, answerid, questionid):
Check whether answerid is the correct answer for questionid.
Returns a boolean in 'result'
"""
question = Question.objects.get(id=int(questionid))
result = False
for answer in question.correct_answers():
print type(answer.id)
if answer.id == int(answerid):
result = True
result = question.check_answer(answerid)
return simplejson.dumps({'result': result})

View File

@@ -58,6 +58,16 @@ class Question(models.Model):
"""
return self.answers.filter(correct=True)
def check_answer(self, answerid):
"""
Check if answerid is correct. Returns True if correct, else False
"""
for answer in self.correct_answers():
if answer.id == int(answerid):
return True
return False
def __unicode__(self):
return self.text

View File

@@ -38,8 +38,30 @@ class ModelsTest(TestCase):
self.assertEqual("Test", testanswer.__unicode__())
def test_no_correct_answers(self):
testquestion = Question(text="Test")
testquestion = Question(text="Test", id=1)
Answer(text="Test", correct=False, question=testquestion)
answer = Answer(text="Test", correct=False, question=testquestion)
answer2 = Answer(text="Test2", correct=False, question=testquestion)
testquestion.answers = [answer, answer2, ]
self.assertEqual(len(testquestion.correct_answers()), 0)
def test_correct_answers(self):
testquestion = Question(text="Test", id=1)
answer = Answer(text="Test", correct=True, question=testquestion)
answer2 = Answer(text="Test2", correct=False, question=testquestion)
testquestion.answers = [answer, answer2]
self.assertEqual(len(testquestion.correct_answers()), 1)
def test_multiple_correct_answers(self):
testquestion = Question(text="Test", id=1)
answer = Answer(text="Test", correct=True, question=testquestion)
answer2 = Answer(text="Test2", correct=False, question=testquestion)
answer3 = Answer(text="Test3", correct=True, question=testquestion)
testquestion.answers = [answer, answer2, answer3, ]
self.assertEqual(len(testquestion.correct_answers()), 2)