|
| 1 | +from django.test import TestCase |
| 2 | +from django.utils import timezone |
| 3 | +from ..models.quiz_models import Question, Tag, Answer |
| 4 | + |
| 5 | +import datetime |
| 6 | + |
| 7 | + |
| 8 | +class QuestionTestCase(TestCase): |
| 9 | + def setUp(self): |
| 10 | + self.creation_time = timezone.now() |
| 11 | + Question.objects.create(text="Test multiple choice question", |
| 12 | + question_type= |
| 13 | + Question.QuestionType.MULTICHOICE) |
| 14 | + Question.objects.create(text="Test numerical answer question", |
| 15 | + question_type=Question.QuestionType.NUMERIC) |
| 16 | + Question.objects.create(text="Test short answer question", |
| 17 | + question_type= |
| 18 | + Question.QuestionType.SHORT_ANSWER) |
| 19 | + |
| 20 | + def test_text(self): |
| 21 | + mc = Question.objects.get(text="Test multiple choice question") |
| 22 | + self.assertEqual(str(mc), "Test multiple choice question") |
| 23 | + |
| 24 | + na = Question.objects.get(text="Test numerical answer question") |
| 25 | + self.assertEqual(str(na), "Test numerical answer question") |
| 26 | + |
| 27 | + sa = Question.objects.get(text="Test short answer question") |
| 28 | + self.assertEqual(str(sa), "Test short answer question") |
| 29 | + |
| 30 | + def test_question_type(self): |
| 31 | + mc = Question.objects.get(text="Test multiple choice question") |
| 32 | + self.assertEqual(mc.question_type, "MC") |
| 33 | + |
| 34 | + na = Question.objects.get(text="Test numerical answer question") |
| 35 | + self.assertEqual(na.question_type, "NA") |
| 36 | + |
| 37 | + sa = Question.objects.get(text="Test short answer question") |
| 38 | + self.assertEqual(sa.question_type, "SA") |
| 39 | + |
| 40 | + def test_creator(self): |
| 41 | + questions = Question.objects.all() |
| 42 | + for q in questions: |
| 43 | + self.assertIsNone(q.creator) |
| 44 | + |
| 45 | + def test_date_created(self): |
| 46 | + q = Question.objects.get(text="Test multiple choice question") |
| 47 | + self.assertLess((q.date_created-self.creation_time).total_seconds(), |
| 48 | + 0.1) |
| 49 | + |
| 50 | + |
| 51 | +class TagTestCase(TestCase): |
| 52 | + def setUp(self): |
| 53 | + q = Question.objects.create(text="This is a physics question", |
| 54 | + question_type= |
| 55 | + Question.QuestionType.NUMERIC) |
| 56 | + t = Tag.objects.create(name="Unit 3 Physics") |
| 57 | + q.tag_set.add(t) |
| 58 | + |
| 59 | + def test(self): |
| 60 | + q = Question.objects.get(text="This is a physics question") |
| 61 | + t = Tag.objects.get(name="Unit 3 Physics") |
| 62 | + self.assertEqual(str(t), "Unit 3 Physics") |
| 63 | + |
| 64 | + self.assertTrue(t in q.tag_set.all()) |
| 65 | + |
| 66 | + |
| 67 | +class AnswerTestCase(TestCase): |
| 68 | + def setUp(self): |
| 69 | + q = Question.objects.create(text="Question?", |
| 70 | + question_type= |
| 71 | + Question.QuestionType.SHORT_ANSWER) |
| 72 | + Answer.objects.create(text="Answer!", question=q, is_correct=True) |
| 73 | + |
| 74 | + def test(self): |
| 75 | + q = Question.objects.get(text="Question?") |
| 76 | + a = Answer.objects.get(text="Answer!") |
| 77 | + self.assertEquals(str(a), "Answer!") |
| 78 | + self.assertEquals(a.text, "Answer!") |
| 79 | + |
| 80 | + self.assertTrue(a.is_correct) |
| 81 | + |
| 82 | + self.assertEquals(a.question, q) |
| 83 | + |
0 commit comments