-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests.py
460 lines (410 loc) · 17.3 KB
/
tests.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db.utils import IntegrityError
from .models import Affiliation, Competition, Individual
from .models import Track, Subtrack
from .evaluators import cmdline
from .evaluators import icfhr14_kws_tool, transkribusBaseLineMetricTool, transkribusErrorRate, icfhr16_HTR_tool, icdar2017_writer_identification, icdar2017_kws_tool, icfhr18_atr_tool
def create_new_user():
user = User.objects.create_user(
username='sampleuser',
password='mypassword',
email='a@a.gr',
first_name='John',
last_name='Doe',
)
affiliation = Affiliation.objects.create(name='John Does university')
user.individual.shortbio = 'This is the story of my life'
user.individual.affiliations.add(affiliation)
user.individual.save()
return user
def create_competitions_tracks_subtracks(
num_comps,
num_tracks_per_comp,
num_subtracks_per_track,
sampledata_filename='test_private_data.txt',
sampledata_filename_contents=b'This is test data',
):
"""
A helper function that creates test competitions, tracks and subtracks.
Creates a set number of competitions, then the given number of tracks per competition,
and the given number of subtracks per track.
"""
for cnum in range(1, num_comps+1):
comp = Competition.objects.create(name='Competition {}'.format(cnum))
print('Created {}'.format(comp))
for tnum in range(1, num_tracks_per_comp+1):
track = Track.objects.create(name='Track {}'.format(tnum), competition=comp)
print('Created {}'.format(track))
for snum in range(1, num_subtracks_per_track+1):
subtrack = Subtrack.objects.create(
name='Subtrack {}'.format(snum),
track=track,
private_data=SimpleUploadedFile(sampledata_filename, sampledata_filename_contents)
) #public_data=SimpleUploadedFile(sampledata_filename, sampledata_filename_contents),
print('Created {}'.format(subtrack))
def show_all_competitions_tracks_subtracks():
print(Competition.objects.all())
print(Track.objects.all())
print(Subtrack.objects.all())
class UrlTests(TestCase):
"""
Check that the regexps in urls.py match to what we should expect
"""
def test_regexp_index(self):
a = reverse('index')
self.assertEqual(a, '/competitions/')
def test_regexp_competitions_comp_track_subtrack(self):
a = reverse('competition', kwargs = { 'competition_id': '1', 'track_id': '1', 'subtrack_id': '1', })
self.assertEqual(a, '/competitions/1/1/1/')
def test_regexp_competitions_comp_track(self):
a = reverse('competition', kwargs = { 'competition_id': '1', 'track_id': '1', })
self.assertEqual(a, '/competitions/1/1/')
def test_regexp_competitions_comp(self):
a = reverse('competition', kwargs = { 'competition_id': '1', })
self.assertEqual(a, '/competitions/1/')
def test_regexp_viewresults_comp_track_subtrack(self):
a = reverse('viewresults', kwargs = {'competition_id': '1', 'track_id': '1', 'subtrack_id': '1',})
self.assertEqual(a, '/competitions/1/1/1/viewresults/')
def test_regexp_viewresults_comp_track(self):
a = reverse('viewresults', kwargs = {'competition_id': '1', 'track_id': '1', })
self.assertEqual(a, '/competitions/1/1/viewresults/')
def test_regexp_viewresults_comp(self):
a = reverse('viewresults', kwargs = {'competition_id': '1', })
self.assertEqual(a, '/competitions/1/viewresults/')
class ViewForwardTests(TestCase):
"""
Check that redirections work for various forms of given regexps.
They should match those found at urls.py
"""
def test_forward_index(self):
response = self.client.get('/competitions/')
self.assertEqual(response.status_code, 200)
def test_forward_competitions_with_empty_dbase(self):
response = self.client.get('/competitions/1/1/1/')
self.assertEqual(response.status_code, 404)
response = self.client.get('/competitions/1/1/1')
self.assertEqual(response.status_code, 301)
response = self.client.get('/competitions/1/1/')
self.assertEqual(response.status_code, 404)
response = self.client.get('/competitions/1/1')
self.assertEqual(response.status_code, 301)
response = self.client.get('/competitions/1/')
self.assertEqual(response.status_code, 404)
response = self.client.get('/competitions/1')
self.assertEqual(response.status_code, 301)
def test_forward_competitions(self):
create_competitions_tracks_subtracks(3, 3, 3)
response = self.client.get('/competitions/1/')
self.assertEqual(response.status_code, 200)
response = self.client.get('/competitions/1')
self.assertEqual(response.status_code, 301)
response = self.client.get('/competitions/1/1/')
self.assertEqual(response.status_code, 200)
response = self.client.get('/competitions/1/1')
self.assertEqual(response.status_code, 301)
response = self.client.get('/competitions/1/1/1/')
self.assertEqual(response.status_code, 200)
response = self.client.get('/competitions/1/1/1')
self.assertEqual(response.status_code, 301)
class ViewReverseTests(TestCase):
"""
Check that django.core.urlresolvers.reverse works
for all views that reside in urls.py
"""
def test_reverse_index(self):
response = self.client.get(reverse('index'))
self.assertEqual(response.status_code, 200)
def test_reverse_competition_with_empty_dbase_1(self):
response = self.client.get(reverse('competition',
kwargs={
'competition_id':'1',
'track_id':'1',
'subtrack_id':'1',
}
)
)
self.assertEqual(response.status_code, 404)
def test_reverse_competition_with_empty_dbase_2(self):
response = self.client.get(reverse('competition',
kwargs={
'competition_id':'2',
'track_id':'3',
'subtrack_id':'1',
}
)
)
self.assertEqual(response.status_code, 404)
def test_reverse_competition(self):
create_competitions_tracks_subtracks(1, 2, 2)
response = self.client.get(reverse('competition',
kwargs={
'competition_id':'1',
'track_id':'2',
'subtrack_id':'1',
}
)
)
self.assertEqual(response.status_code, 200)
def test_reverse_viewresults_nosubmissions(self):
"""
TODO: Do the same after having created some submissions
"""
create_competitions_tracks_subtracks(1, 2, 2)
response = self.client.get(reverse('viewresults',
kwargs={
'competition_id':'1',
'track_id':'2',
'subtrack_id':'1',
}
)
)
self.assertEqual(response.status_code, 200)
def test_reverse_scoreboard_nosubmissions(self):
"""
TODO: Do the same after having created some submissions
"""
create_competitions_tracks_subtracks(1, 2, 2)
response = self.client.get(reverse('scoreboard',
kwargs={
'competition_id':'1',
}
)
)
self.assertEqual(response.status_code, 200)
def test_reverse_submit(self):
create_competitions_tracks_subtracks(1, 2, 2)
response = self.client.get(reverse('submit',
kwargs={
'competition_id':'1',
'track_id':'2',
'subtrack_id':'1',
}
)
)
self.assertEqual(response.status_code, 200)
class ModelTests(TestCase):
def test_affiliation_creation(self):
"""
Create an affiliation and test that it appears
on the master page -- should reside in one of
the buttons of the register block
"""
testname = 'Test affiliation'
w = Affiliation.objects.create(name=testname)
response = self.client.get(reverse('index'))
self.assertContains(response, testname)
def test_competition_creation(self):
"""
Create a competition and test that it appears
on the master page -- there is a special block
that shows all competitions
"""
testname = 'Test competition'
w = Competition.objects.create(name=testname)
response = self.client.get(reverse('index'))
self.assertContains(response, testname)
def test_track_uniquepercomp_ids(self):
"""
Check that all track 'unique per competition ids' are indeed unique
"""
num_comps = 3
num_tracks = 3
create_competitions_tracks_subtracks(num_comps, num_tracks, 0)
self.assertEqual(len(Track.objects.all()), num_comps*num_tracks)
for t in Track.objects.all():
my_id = t.percomp_uniqueid
my_competition = t.competition
self.assertEqual(len(Track.objects.filter(competition=my_competition).filter(percomp_uniqueid=my_id)), 1)
def test_subtrack_uniquepercomp_ids(self):
"""
Check that all subtrack 'unique per competition ids' are indeed unique
"""
num_comps = 2
num_tracks = 2
num_subtracks = 5
create_competitions_tracks_subtracks(num_comps, num_tracks, num_subtracks)
self.assertEqual(len(Subtrack.objects.all()), num_comps*num_tracks*num_subtracks)
for st in Subtrack.objects.all():
my_id = st.pertrack_uniqueid
my_track = st.track
self.assertEqual(len(Subtrack.objects.filter(track=my_track).filter(pertrack_uniqueid=my_id)), 1)
def test_subtrack_files(self):
"""
Check public/private files functionality is ok.
Check that data work if they are either uncompressed or compressed.
"""
self.assertEqual(1, 1)
def test_user_creation(self):
"""
Creation of a user should automatically create an 'Individual',
since Individual is on a 1-1 relationship with User, ie it is
a 'User profile'.
"""
testname = 'Test user'
w = User.objects.create_user(username=testname)
q = User.objects.get(username=testname)
self.assertEqual(w, q)
iq = Individual.objects.all()
self.assertEqual(len(iq), 1)
class AuthenticationTests(TestCase):
"""
Here, register and login using the backend solely.
In this sense, these tests are complementary with FormTests
that should test register, login and submit on the frontend.
"""
def test_register_user(self):
user = create_new_user()
self.assertIsNotNone(user)
self.assertEqual(len(User.objects.all()), 1)
self.assertEqual(len(Individual.objects.all()), 1)
self.assertEqual(user.first_name, 'John')
def test_register_existing_user(self):
"""
Register with a username that already exists.
This should throw an IntegrityError
"""
user = create_new_user()
try:
user2 = create_new_user()
except(IntegrityError):
self.assertTrue(True)
else:
self.assertTrue(False)
def test_login_unknown_user(self):
self.assertFalse(self.client.login(username='temporary', password='temporary'))
def test_login_bad_password(self):
create_new_user()
self.assertTrue(User.objects.all()[0].username, 'sampleuser')
self.assertFalse(self.client.login(username='sampleuser', password='badpassword'))
def test_user_creation_and_login(self):
create_new_user()
self.assertTrue(User.objects.all()[0].username, 'sampleuser')
self.assertTrue(self.client.login(username='sampleuser', password='mypassword'))
class FormTests(TestCase):
def test_registerform(self):
self.assertEqual(1, 1)
def test_registerform_existinguser(self):
self.assertEqual(1, 1)
def test_loginform(self):
self.assertEqual(1, 1)
def test_submitresultform(self):
self.assertEqual(1, 1)
class ThirdpartyTests(TestCase):
def test_p7zip(self):
self.assertIn('p7zip Version', cmdline('7zr'))
#
# Evaluator tests.
#
class EvaluatorTests(TestCase):
def test_icfhr14_kws_tool(self):
(res, logfile) = icfhr14_kws_tool()
self.assertEqual(res, {
'pr-curve': '["1.0000", "1.0000", "1.0000", "1.0000", "1.0000", "0.6667", "0.0000", "0.0000", "0.0000", "0.0000", "0.0000"]',
'p@5': '0.9000',
'r-precision': '0.5000',
'map': '0.5185',
'ndcg': '0.6395',
'p@10': '0.5000',
'ndcg-binary': '0.6817'
}
)
def test_transkribusBaseLineMetricTool(self):
(res, logfile) = transkribusBaseLineMetricTool()
self.assertEqual(res, {
'bl-avg-P-value': '0.788',
'bl-avg-R-value': '0.8245',
'bl-F_1-value': '0.8058',
}
)
def test_icdar2017_writer_identification(self):
(res, logfile) = icdar2017_writer_identification(
privatedata='competitions/executables/ICDAR2017WriterIdentification/',
resultdata='competitions/executables/ICDAR2017WriterIdentification/test_submission.csv'
)
self.assertEqual(res, {
'WI-precision': '0.899038',
'WI-map': '0.769177',
}
)
def test_icdar2017_kws_tool(self):
# Generic test
wdir = 'competitions/executables/Icdar17KwsEval'
res = icdar2017_kws_tool()
self.assertEqual(res, {'gAP': 0.00058712, 'mAP': 0.000426333})
# Wrong submission format
res = icdar2017_kws_tool(
resultdata = '%s/QbS_ValidGT.txt' % wdir,
privatedata = '%s/QbS_ValidGT.txt' % wdir,
querylist = '%s/QbS_ValidQry.txt' % wdir)
self.assertEqual(res, {'gAP' : None, 'mAP' : None})
def test_icfhr18_atr_tool(self):
(res,output) = icfhr18_atr_tool(
privatedata="competitions/executables/TranskribusErrorRate/testresources/icfhr18/unpacked/",
resultdata="competitions/executables/TranskribusErrorRate/testresources/icfhr18/hyp_txt.tar.gz",
execpath="competitions/executables/TranskribusErrorRate/"
)
self.assertEquals(res['CER'],(3/144))
self.assertTrue("WARNING" in output)
class EvaluatorTests_HTR2016(TestCase):
def test_htr2016_dependencies(self):
self.assertIn('hello', cmdline('echo hello | ascii2uni') )
self.assertIn('hello', cmdline('echo hello | uni2ascii') )
self.assertIn('nan', cmdline('./tasas /tmp', cwd='competitions/executables/EvaluationCERandWER/') )
self.assertIn('ERROR PRHLT null', cmdline('./page_format_tool', cwd='competitions/executables/EvaluationCERandWER/'))
def test_htr2016_evaluationtool(self):
res = icfhr16_HTR_tool(privatedata='competitions/executables/EvaluationCERandWER/fixtures/gt_smaller/',
resultdata='competitions/executables/EvaluationCERandWER/fixtures/RWTH_smaller.tar')
print(res)
self.assertEqual(res, {
'CER': '4.12',
'WER': '14.86',
})
class EvaluatorTests_TranskribusErrorRate(TestCase):
def _test_transkribuserrorrate(self, params="", tgt={}):
res = transkribusErrorRate(privatedata="competitions/executables/TranskribusErrorRate/testresources/gt.tgz",
resultdata="competitions/executables/TranskribusErrorRate/testresources/hyp.tgz",
tmpfolder="competitions/executables/TranskribusErrorRate/testresources/tmp",
execpath="competitions/executables/TranskribusErrorRate/", params=params)
print("output of test is '" + str(res) + "'.")
print("output of tgt is '" + str(tgt) + "'.")
for key in tgt:
self.assertEqual(float(tgt.get(key)), float(res.get(key)))
def test_transkribuserrorrate_cer(self):
self._test_transkribuserrorrate(
params="",
tgt={
'DEL': '0.05',
'ERR': '0.465',
'SUB': '0.215',
'INS': '0.2'
})
def test_transkribuserrorrate_wer(self):
self._test_transkribuserrorrate(
params="-w",
tgt={
'DEL': '0.1',
'ERR': '0.78',
'SUB': '0.3',
'INS': '0.38',
})
def test_transkribuserrorrate_cer_upper(self):
self._test_transkribuserrorrate(
params="-u",
tgt={
'DEL': '0.05',
'ERR': '0.335',
'SUB': '0.085',
'INS': '0.2',
})
def test_transkribuserrorrate_wer_letter(self):
self._test_transkribuserrorrate(
params="-l -w",
tgt={
'DEL': '0.02',
'ERR': '0.74',
'SUB': '0.32',
'INS': '0.4',
})