-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathtest_problemset.py
382 lines (328 loc) · 16 KB
/
test_problemset.py
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from datetime import datetime, timezone # pylint: disable=E0611
from django.contrib.auth.models import User
from django.test.utils import override_settings
from django.urls import reverse
from oioioi.base.tests import TestCase
from oioioi.contests.models import Contest
from oioioi.problems.models import AlgorithmTag, AlgorithmTagThrough, Problem
class TestProblemsetPage(TestCase):
fixtures = ['test_users', 'test_problemset_author_problems', 'test_contest']
def test_problemlist(self):
self.assertTrue(self.client.login(username='test_user'))
url = reverse('problemset_main')
response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 200)
public_problems = Problem.objects.filter(visibility=Problem.VISIBILITY_PUBLIC)
for problem in public_problems:
self.assertContains(response, problem.name)
# User with no administered contests doesn't see the button
self.assertNotContains(response, "Add to contest")
url = reverse('problemset_my_problems')
response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 200)
author_user = User.objects.filter(username='test_user').get()
author_problems = Problem.objects.filter(author=author_user)
for problem in author_problems:
self.assertContains(response, problem.name)
# User with no administered contests doesn't see the button
self.assertNotContains(response, "Add to contest")
self.assertNotContains(response, 'All problems')
url = reverse('problemset_all_problems')
response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 403)
self.assertTrue(self.client.login(username='test_admin'))
response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'All problems')
# One link for problem site, another
# for "More contests..." link in "Actions"
self.assertContains(
response, '/problemset/problem/', count=Problem.objects.count() * 2
)
self.assertContains(response, 'Add to contest', count=Problem.objects.count())
class TestTagProposalsOnProbset(TestCase):
fixtures = [
'test_users',
'test_problem_search',
'test_algorithm_tags',
'test_difficulty_tags',
'test_aggregated_tag_proposals',
]
@override_settings(
PROBLEM_TAGS_VISIBLE=False,
SHOW_TAG_PROPOSALS_IN_PROBLEMSET=False,
PROBSET_MIN_AMOUNT_TO_CONSIDER_TAG_PROPOSAL=3,
)
def test_tags_not_visible(self):
self.assertTrue(self.client.login(username='test_user'))
url = reverse('problemset_main')
response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, 'class="badge tag-label')
self.assertNotContains(response, 'show-tag-proposals-checkbox')
self.assertNotContains(response, 'aggregated-proposals')
self.assertNotContains(response, 'tag-label-algorithm-proposal')
@override_settings(
PROBLEM_TAGS_VISIBLE=True,
SHOW_TAG_PROPOSALS_IN_PROBLEMSET=False,
PROBSET_MIN_AMOUNT_TO_CONSIDER_TAG_PROPOSAL=3,
)
def test_tag_proposals_not_visible(self):
self.assertTrue(self.client.login(username='test_user'))
url = reverse('problemset_main')
response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'class="badge tag-label')
self.assertNotContains(response, 'show-tag-proposals-checkbox')
self.assertNotContains(response, 'aggregated-proposals')
self.assertNotContains(response, 'tag-label-algorithm-proposal')
self.assertNotContains(response, 'greedy<span class="tag-proposal-amount')
@override_settings(
PROBLEM_TAGS_VISIBLE=True,
SHOW_TAG_PROPOSALS_IN_PROBLEMSET=True,
PROBSET_MIN_AMOUNT_TO_CONSIDER_TAG_PROPOSAL=3,
)
def test_tag_proposals_visible(self):
self.assertTrue(self.client.login(username='test_user'))
url = reverse('problemset_main')
response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'show-tag-proposals-checkbox')
self.assertContains(response, 'aggregated-proposals')
self.assertContains(response, 'tag-label-algorithm-proposal')
self.assertContains(response, 'greedy<span class="tag-proposal-amount')
self.assertNotContains(response, 'knapsack<span class="tag-proposal-amount')
self.assertNotContains(response, 'dp<span class="tag-proposal-amount')
self.assertNotContains(response, 'lcis<span class="tag-proposal-amount')
@override_settings(
PROBLEM_TAGS_VISIBLE=True,
SHOW_TAG_PROPOSALS_IN_PROBLEMSET=True,
PROBSET_MIN_AMOUNT_TO_CONSIDER_TAG_PROPOSAL=3,
)
def test_duplicate_tag_proposal(self):
AlgorithmTagThrough.objects.create(
problem=Problem.objects.get(pk=2),
tag=AlgorithmTag.objects.get(pk=4),
)
self.assertTrue(self.client.login(username='test_user'))
url = reverse('problemset_main')
response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'show-tag-proposals-checkbox')
self.assertNotContains(response, 'aggregated-proposals')
self.assertNotContains(response, 'tag-label-algorithm-proposal')
self.assertNotContains(response, 'greedy<span class="tag-proposal-amount')
@override_settings(
PROBLEM_TAGS_VISIBLE=True,
SHOW_TAG_PROPOSALS_IN_PROBLEMSET=True,
PROBSET_MIN_AMOUNT_TO_CONSIDER_TAG_PROPOSAL=3,
)
class TestTagProposalSearch(TestCase):
fixtures = [
'test_users',
'test_problem_search',
'test_algorithm_tags',
'test_difficulty_tags',
'test_aggregated_tag_proposals',
]
url = reverse('problemset_main')
def _try_single_search(self, dict, hasZadanko, hasZolc):
response = self.client.get(self.url, dict)
if hasZadanko:
self.assertContains(response, 'Zadanko')
else:
self.assertNotContains(response, 'Zadanko')
if hasZolc:
self.assertContains(response, 'Żółć')
else:
self.assertNotContains(response, 'Żółć')
def setUp(self):
self.client.get('/c/c/')
self.assertTrue(self.client.login(username='test_user'))
@override_settings(PROBSET_MIN_AMOUNT_TO_CONSIDER_TAG_PROPOSAL=3)
def test_search_min_3(self):
self._try_single_search({'algorithm': 'greedy', 'include_proposals': 1}, False, True)
self._try_single_search({'algorithm': 'knapsack', 'include_proposals': 1}, False, False)
self._try_single_search({'algorithm': 'greedy', 'include_proposals': 0}, False, False)
@override_settings(PROBSET_MIN_AMOUNT_TO_CONSIDER_TAG_PROPOSAL=2)
def test_search_min_2(self):
self._try_single_search({'algorithm': 'greedy', 'include_proposals': 1}, False, True)
self._try_single_search({'algorithm': 'knapsack', 'include_proposals': 1}, True, False)
self._try_single_search({'algorithm': 'greedy', 'include_proposals': 0}, False, False)
@override_settings(PROBSET_MIN_AMOUNT_TO_CONSIDER_TAG_PROPOSAL=1)
def test_search_min_1(self):
self._try_single_search({'algorithm': 'greedy', 'include_proposals': 1}, True, True)
self._try_single_search({'algorithm': 'knapsack', 'include_proposals': 1}, True, True)
self._try_single_search({'algorithm': 'greedy', 'include_proposals': 0}, False, False)
@override_settings(
PROBSET_MIN_AMOUNT_TO_CONSIDER_TAG_PROPOSAL=1,
PROBSET_SHOWN_TAG_PROPOSALS_LIMIT=1,
)
def test_search_min_1_limit_1(self):
self._try_single_search({'algorithm': 'greedy', 'include_proposals': 1}, False, True)
self._try_single_search({'algorithm': 'knapsack', 'include_proposals': 1}, True, False)
self._try_single_search({'algorithm': 'greedy', 'include_proposals': 0}, False, False)
@override_settings(
PROBSET_MIN_AMOUNT_TO_CONSIDER_TAG_PROPOSAL=1,
PROBSET_SHOWN_TAG_PROPOSALS_LIMIT=0,
)
def test_search_min_1_limit_0(self):
self._try_single_search({'algorithm': 'greedy', 'include_proposals': 1}, False, False)
self._try_single_search({'algorithm': 'knapsack', 'include_proposals': 1}, False, False)
self._try_single_search({'algorithm': 'greedy', 'include_proposals': 0}, False, False)
class TestAddToProblemsetPermissions(TestCase):
fixtures = ['test_users']
def _assert_can_see_and_add(self):
url_main = reverse('problemset_main')
url_add = reverse('problemset_add_or_update')
response = self.client.get(url_main)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Add problem')
response = self.client.get(url_add, follow=True)
self.assertEqual(response.status_code, 200)
def _assert_can_see_but_cannot_add(self):
url_main = reverse('problemset_main')
url_add = reverse('problemset_add_or_update')
response = self.client.get(url_main, follow=True)
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, 'Add problem')
response = self.client.get(url_add, follow=True)
self.assertEqual(response.status_code, 403)
@override_settings(EVERYBODY_CAN_ADD_TO_PROBLEMSET=False)
def test_default_permissions(self):
self._assert_can_see_but_cannot_add()
self.assertTrue(self.client.login(username='test_admin'))
self._assert_can_see_and_add()
self.assertTrue(self.client.login(username='test_user'))
self._assert_can_see_but_cannot_add()
@override_settings(EVERYBODY_CAN_ADD_TO_PROBLEMSET=True)
def test_everyone_allowed_permissions(self):
self._assert_can_see_but_cannot_add()
self.assertTrue(self.client.login(username='test_admin'))
self._assert_can_see_and_add()
self.assertTrue(self.client.login(username='test_user'))
self._assert_can_see_and_add()
class TestAddToContestFromProblemset(TestCase):
fixtures = [
'test_users',
'test_contest',
'test_full_package',
'test_problem_instance',
'test_submission',
'test_problem_site',
]
def test_add_from_problemlist(self):
self.assertTrue(self.client.login(username='test_admin'))
# Visit contest page to register it in recent contests
contest = Contest.objects.get()
self.client.get('/c/%s/dashboard/' % contest.id)
url = reverse('problemset_all_problems')
response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'All problems')
# One link for problem site, another
# for "More contests..." link in "Actions"
self.assertContains(
response, '/problemset/problem/', count=Problem.objects.count() * 2
)
self.assertContains(response, 'Add to contest', count=Problem.objects.count())
self.assertContains(response, 'data-addorupdate')
self.assertContains(response, 'data-urlkey')
self.assertContains(response, 'add_to_contest')
def test_add_from_problemsite(self):
self.assertTrue(self.client.login(username='test_admin'))
contest = Contest.objects.get()
self.client.get('/c/%s/dashboard/' % contest.id)
url = reverse('problem_site', kwargs={'site_key': '123'})
response = self.client.get(url + '?key=settings', follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Add to contest', count=3)
self.assertContains(response, 'data-addorupdate')
self.assertContains(response, 'data-urlkey')
self.assertContains(response, 'add_to_contest')
self.assertContains(response, '123')
def test_add_from_selectcontest(self):
contest2 = Contest(
id='c2',
name='Contest2',
controller_name='oioioi.contests.tests.PrivateContestController',
)
contest2.save()
contest2.creation_date = datetime(2002, 1, 1, tzinfo=timezone.utc)
contest2.save()
contest3 = Contest(
id='c3',
name='Contest3',
controller_name='oioioi.contests.tests.PrivateContestController',
)
contest3.save()
contest3.creation_date = datetime(2004, 1, 1, tzinfo=timezone.utc)
contest3.save()
contest4 = Contest(
id='c4',
name='Contest4',
controller_name='oioioi.contests.tests.PrivateContestController',
)
contest4.save()
contest4.creation_date = datetime(2003, 1, 1, tzinfo=timezone.utc)
contest4.save()
self.assertTrue(self.client.login(username='test_admin'))
# Now we're not having any contest in recent contests.
# As we are contest administrator, the button should still appear.
url = reverse('problemset_all_problems')
response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'All problems')
self.assertContains(
response, '/problemset/problem/', count=Problem.objects.count() * 2
)
self.assertContains(response, 'Add to contest', count=Problem.objects.count())
# But it shouldn't be able to fill the form
self.assertNotContains(response, 'data-addorupdate')
self.assertNotContains(response, 'data-urlkey')
# And it should point to select_contest page
self.assertContains(response, '/problem/123/add_to_contest/?problem_name=sum')
# Follow the link...
url = reverse('problemset_add_to_contest', kwargs={'site_key': '123'})
url += '?problem_name=sum'
response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'to add the <code>sum</code> problem to')
# This time we should be able to fill the form
self.assertContains(response, 'data-addorupdate')
self.assertContains(response, 'data-urlkey')
self.assertContains(response, 'add_to_contest')
self.assertContains(response, '123')
self.assertEqual(len(response.context['administered_contests']), 4)
self.assertEqual(
list(response.context['administered_contests']),
list(Contest.objects.order_by('-creation_date').all()),
)
self.assertContains(response, 'Contest2', count=1)
self.assertContains(response, 'Contest3', count=1)
self.assertContains(response, 'Contest4', count=1)
content = response.content.decode('utf-8')
self.assertLess(content.index('Contest3'), content.index('Contest4'))
self.assertLess(content.index('Contest4'), content.index('Contest2'))
@override_settings(PROBLEM_STATISTICS_AVAILABLE=True)
class TestProblemsetFilters(TestCase):
fixtures = ['test_users', 'test_statistics_display']
problems = [u'aaa', u'bbb', u'ccc', u'ddd']
filtered_problems = {
'all': [u'aaa', u'bbb', u'ccc', u'ddd'],
'solved': [u'ddd'],
'attempted': [u'bbb', u'ccc'],
'not_attempted': [u'aaa'],
}
def test_filters(self):
self.assertTrue(self.client.login(username='test_user'))
for filter, filtered in self.filtered_problems.items():
url_main = reverse('problemset_main')
response = self.client.get(url_main, {'filter': filter})
self.assertEqual(response.status_code, 200)
for problem in self.problems:
problemTag = f"<td>{problem}</td>"
if problem in filtered:
self.assertContains(response, problemTag, html=True)
else:
self.assertNotContains(response, problemTag, html=True)