More unit testing
This commit is contained in:
@@ -11,4 +11,5 @@ logilab-common==0.60.0
|
|||||||
pep8==1.4.6
|
pep8==1.4.6
|
||||||
pyflakes==0.7.3
|
pyflakes==0.7.3
|
||||||
pylint==1.0.0
|
pylint==1.0.0
|
||||||
|
six==1.3.0
|
||||||
wsgiref==0.1.2
|
wsgiref==0.1.2
|
||||||
|
|||||||
@@ -14,13 +14,7 @@ def check_answer(request, answerid, questionid):
|
|||||||
Check whether answerid is the correct answer for questionid.
|
Check whether answerid is the correct answer for questionid.
|
||||||
Returns a boolean in 'result'
|
Returns a boolean in 'result'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
question = Question.objects.get(id=int(questionid))
|
question = Question.objects.get(id=int(questionid))
|
||||||
|
result = question.check_answer(answerid)
|
||||||
result = False
|
|
||||||
for answer in question.correct_answers():
|
|
||||||
print type(answer.id)
|
|
||||||
if answer.id == int(answerid):
|
|
||||||
result = True
|
|
||||||
|
|
||||||
return simplejson.dumps({'result': result})
|
return simplejson.dumps({'result': result})
|
||||||
|
|||||||
@@ -58,6 +58,16 @@ class Question(models.Model):
|
|||||||
"""
|
"""
|
||||||
return self.answers.filter(correct=True)
|
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):
|
def __unicode__(self):
|
||||||
return self.text
|
return self.text
|
||||||
|
|
||||||
|
|||||||
@@ -38,8 +38,30 @@ class ModelsTest(TestCase):
|
|||||||
self.assertEqual("Test", testanswer.__unicode__())
|
self.assertEqual("Test", testanswer.__unicode__())
|
||||||
|
|
||||||
def test_no_correct_answers(self):
|
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)
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user