Skip to content

Commit a4282b5

Browse files
committed
test(quiz): shared_models unit tests
1 parent a5a7d9f commit a4282b5

5 files changed

Lines changed: 124 additions & 3 deletions

File tree

documentation/docs/backend/quiz/shared_models.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ There are three shared quiz models:
2424
## Serializers
2525
The `shared_models` app provides serializers for each of the above models.
2626

27+
## Testing
28+
Unit tests can be run from within the backend Docker container using the command `python manage.py test` (more verbosely if executing from a terminal not SSH'd into the container: `docker exec -it elucidate_server python manage.py test`). Ensure you are executing the command from the same directory as `manage.py`.
29+

server/api/apps/shared_models/tests.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

server/api/apps/shared_models/tests/__init__.py

Whitespace-only changes.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from django.test import TestCase
2+
from django.utils import timezone
3+
from ..models.quiz_models import Question, Answer
4+
from ..models.statistics_models import QuestionResponse
5+
6+
import datetime
7+
8+
9+
class QuestionTestCase(TestCase):
10+
def setUp(self):
11+
self.creation_time = timezone.now()
12+
q = Question.objects.create(text="Question?",
13+
question_type=
14+
Question.QuestionType.MULTICHOICE)
15+
a = Answer.objects.create(text="Answer!",
16+
question=q,
17+
is_correct=True)
18+
QuestionResponse.objects.create(question=q,
19+
selected_answer=a,
20+
is_correct=a.is_correct,
21+
time_taken=
22+
datetime.timedelta(seconds=5))
23+
24+
def test(self):
25+
q = Question.objects.get(text="Question?")
26+
a = Answer.objects.get(text="Answer!")
27+
qr = QuestionResponse.objects.all()[0]
28+
29+
self.assertEquals(str(qr), str((q, a)))
30+
self.assertIsNone(qr.user)
31+
self.assertEquals(qr.question, q)
32+
self.assertEquals(qr.selected_answer, a)
33+
self.assertEquals(qr.is_correct, a.is_correct)
34+
self.assertEquals(qr.is_correct, True)
35+
self.assertEquals(qr.time_taken, datetime.timedelta(seconds=5))
36+
self.assertLess((qr.date_created-self.creation_time).total_seconds(),
37+
0.1)
38+

0 commit comments

Comments
 (0)