This repository was archived by the owner on Jun 13, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtests.py
More file actions
172 lines (155 loc) · 5.91 KB
/
tests.py
File metadata and controls
172 lines (155 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from django.test import TestCase
from .models import Question, Answer
from django.contrib.auth.models import User
from django.test import Client
from django.urls import reverse
from django.core.files.uploadedfile import SimpleUploadedFile
# Create your tests here.
def create_user(username, password):
user = User(username=username)
user.set_password(password)
user.save()
return user
def create_question(language="PYTHON"):
with open("coder/testfiles/output.txt") as file:
f = SimpleUploadedFile("normal.txt", str.encode(file.read()))
question = Question(title="Test",
content="Test",
solution=f,
typeof=language)
question.save()
return question
class CoderCreateViewTests(TestCase):
def setup(self):
self.client = Client()
def test_correct_answer_works_python(self):
"""
Check if checking works correctly for python program
"""
create_user(username="TestUser1", password="test000")
self.client.login(username="TestUser1", password="test000")
ques1 = create_question("PYTHON")
with open("coder/testfiles/normal.py") as f:
self.client.post(
reverse("coder:submit", kwargs={"qslug": ques1.slug}),
data={
"language": "PYTHON",
"result": f
},
)
self.assertTrue(Answer.objects.all()[0].iscorrect)
def test_correct_answer_works_java(self):
"""
Check if checking works correctly for java program
"""
create_user(username="TestUser1", password="test000")
self.client.login(username="TestUser1", password="test000")
ques1 = create_question()
with open("coder/testfiles/normal.java") as f:
self.client.post(
reverse("coder:submit", kwargs={"qslug": ques1.slug}),
data={
"language": "JAVA",
"result": f
},
)
self.assertTrue(Answer.objects.all()[0].iscorrect)
def test_correct_answer_works_cpp(self):
"""
Check if checking works correctly for c++ program
"""
create_user(username="TestUser1", password="test000")
self.client.login(username="TestUser1", password="test000")
ques1 = create_question()
with open("coder/testfiles/normal.cpp") as f:
self.client.post(
reverse("coder:submit", kwargs={"qslug": ques1.slug}),
data={
"language": "C++",
"result": f
},
)
self.assertTrue(Answer.objects.all()[0].iscorrect)
def test_correct_answer_works_rust(self):
"""
Check if checking works correctly for rust program
"""
create_user(username="TestUser1", password="test000")
self.client.login(username="TestUser1", password="test000")
ques1 = create_question()
with open("coder/testfiles/normal.rs") as f:
self.client.post(
reverse("coder:submit", kwargs={"qslug": ques1.slug}),
data={
"language": "RUST",
"result": f
},
)
self.assertTrue(Answer.objects.all()[0].iscorrect)
def test_correct_answer_works_go(self):
"""
Check if checking works correctly for rust program
"""
create_user(username="TestUser1", password="test000")
self.client.login(username="TestUser1", password="test000")
ques1 = create_question()
with open("coder/testfiles/normal.go") as f:
self.client.post(
reverse("coder:submit", kwargs={"qslug": ques1.slug}),
data={
"language": "GO",
"result": f
},
)
self.assertTrue(Answer.objects.all()[0].iscorrect)
def test_correct_answer_works_c(self):
"""
Check if checking works correctly for rust program
"""
create_user(username="TestUser1", password="test000")
self.client.login(username="TestUser1", password="test000")
ques1 = create_question()
with open("coder/testfiles/normal.c") as f:
self.client.post(
reverse("coder:submit", kwargs={"qslug": ques1.slug}),
data={
"language": "C",
"result": f
},
)
self.assertTrue(Answer.objects.all()[0].iscorrect)
def test_multiple_correct_answers_increase_domes_once(self):
"""
Even if the user submits the answer after having solved the question once
their points shouldn't increase
"""
user = create_user(username="TestUser1", password="test000")
self.client.login(username="TestUser1", password="test000")
ques1 = create_question("PYTHON")
with open("coder/testfiles/normal.java") as f:
self.client.post(
reverse("coder:submit", kwargs={"qslug": ques1.slug}),
data={
"language": "PYTHON",
"result": f
},
)
user.refresh_from_db()
self.assertEqual(user.profile.domes, 0)
with open("coder/testfiles/normal.py") as f:
self.client.post(
reverse("coder:submit", kwargs={"qslug": ques1.slug}),
data={
"language": "PYTHON",
"result": f
},
)
self.client.post(
reverse("coder:submit", kwargs={"qslug": ques1.slug}),
data={
"language": "PYTHON",
"result": f
},
) # Post the correct answer twice
user.refresh_from_db()
self.assertEqual(user.profile.domes, 15)