test_task_repository.py
18.1 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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa. If not, see <http://www.gnu.org/licenses/>.
# Cache global variables for timeouts
from default import Test, db
from nose.tools import assert_raises
from factories import TaskFactory, TaskRunFactory, ProjectFactory
from pybossa.repositories import TaskRepository, ProjectRepository
from pybossa.exc import WrongObjectError, DBIntegrityError
project_repo = ProjectRepository(db)
class TestTaskRepositoryForTaskQueries(Test):
def setUp(self):
super(TestTaskRepositoryForTaskQueries, self).setUp()
self.task_repo = TaskRepository(db)
def test_get_task_return_none_if_no_task(self):
"""Test get_task method returns None if there is no task with the
specified id"""
task = self.task_repo.get_task(200)
assert task is None, task
def test_get_task_returns_task(self):
"""Test get_task method returns a task if exists"""
task = TaskFactory.create()
retrieved_task = self.task_repo.get_task(task.id)
assert task == retrieved_task, retrieved_task
def test_get_task_by(self):
"""Test get_task_by returns a task with the specified attribute"""
task = TaskFactory.create(state='done')
retrieved_task = self.task_repo.get_task_by(state=task.state)
assert task == retrieved_task, retrieved_task
def test_get_task_by_returns_none_if_no_task(self):
"""Test get_task_by returns None if no task matches the query"""
TaskFactory.create(state='done')
task = self.task_repo.get_task_by(state='ongoing')
assert task is None, task
def test_filter_tasks_by_no_matches(self):
"""Test filter_tasks_by returns an empty list if no tasks match the query"""
TaskFactory.create(state='done', n_answers=17)
retrieved_tasks = self.task_repo.filter_tasks_by(state='ongoing')
assert isinstance(retrieved_tasks, list)
assert len(retrieved_tasks) == 0, retrieved_tasks
def test_filter_tasks_by_one_condition(self):
"""Test filter_tasks_by returns a list of tasks that meet the filtering
condition"""
TaskFactory.create_batch(3, state='done')
should_be_missing = TaskFactory.create(state='ongoing')
retrieved_tasks = self.task_repo.filter_tasks_by(state='done')
assert len(retrieved_tasks) == 3, retrieved_tasks
assert should_be_missing not in retrieved_tasks, retrieved_tasks
def test_filter_tasks_by_multiple_conditions(self):
"""Test filter_tasks_by supports multiple-condition queries"""
TaskFactory.create(state='done', n_answers=17)
task = TaskFactory.create(state='done', n_answers=99)
retrieved_tasks = self.task_repo.filter_tasks_by(state='done',
n_answers=99)
assert len(retrieved_tasks) == 1, retrieved_tasks
assert task in retrieved_tasks, retrieved_tasks
def test_filter_tasks_support_yield_option(self):
"""Test that filter_tasks_by with the yielded=True option returns the
results as a generator"""
tasks = TaskFactory.create_batch(2, state='done')
yielded_tasks = self.task_repo.filter_tasks_by(state='done', yielded=True)
import types
assert isinstance(yielded_tasks.__iter__(), types.GeneratorType)
for task in yielded_tasks:
assert task in tasks
def test_filter_tasks_limit_offset(self):
"""Test that filter_tasks_by supports limit and offset options"""
TaskFactory.create_batch(4)
all_tasks = self.task_repo.filter_tasks_by()
first_two = self.task_repo.filter_tasks_by(limit=2)
last_two = self.task_repo.filter_tasks_by(limit=2, offset=2)
assert len(first_two) == 2, first_two
assert len(last_two) == 2, last_two
assert first_two == all_tasks[:2]
assert last_two == all_tasks[2:]
def test_count_tasks_with_no_matches(self):
"""Test count_tasks_with returns 0 if no tasks match the query"""
TaskFactory.create(state='done', n_answers=17)
count = self.task_repo.count_tasks_with(state='ongoing')
assert count == 0, count
def test_count_tasks_with_one_condition(self):
"""Test count_tasks_with returns the number of tasks that meet the
filtering condition"""
TaskFactory.create_batch(3, state='done')
should_be_missing = TaskFactory.create(state='ongoing')
count = self.task_repo.count_tasks_with(state='done')
assert count == 3, count
def test_count_tasks_with_multiple_conditions(self):
"""Test count_tasks_with supports multiple-condition queries"""
TaskFactory.create(state='done', n_answers=17)
task = TaskFactory.create(state='done', n_answers=99)
count = self.task_repo.count_tasks_with(state='done', n_answers=99)
assert count == 1, count
class TestTaskRepositoryForTaskrunQueries(Test):
def setUp(self):
super(TestTaskRepositoryForTaskrunQueries, self).setUp()
self.task_repo = TaskRepository(db)
def test_get_task_run_return_none_if_no_task_run(self):
"""Test get_task_run method returns None if there is no taskrun with the
specified id"""
taskrun = self.task_repo.get_task_run(200)
assert taskrun is None, taskrun
def test_get_task_run_returns_task_run(self):
"""Test get_task_run method returns a taskrun if exists"""
taskrun = TaskRunFactory.create()
retrieved_taskrun = self.task_repo.get_task_run(taskrun.id)
assert taskrun == retrieved_taskrun, retrieved_taskrun
def test_get_task_run_by(self):
"""Test get_task_run_by returns a taskrun with the specified attribute"""
taskrun = TaskRunFactory.create(info='info')
retrieved_taskrun = self.task_repo.get_task_run_by(info=taskrun.info)
assert taskrun == retrieved_taskrun, retrieved_taskrun
def test_get_task_run_by_returns_none_if_no_task_run(self):
"""Test get_task_run_by returns None if no taskrun matches the query"""
TaskRunFactory.create(info='info')
taskrun = self.task_repo.get_task_run_by(info='other info')
assert taskrun is None, taskrun
def test_filter_task_runs_by_no_matches(self):
"""Test filter_task_runs_by returns an empty list if no taskruns match
the query"""
TaskRunFactory.create(info='info')
retrieved_taskruns = self.task_repo.filter_task_runs_by(info='other')
assert isinstance(retrieved_taskruns, list)
assert len(retrieved_taskruns) == 0, retrieved_taskruns
def test_filter_task_runs_by_one_condition(self):
"""Test filter_task_runs_by returns a list of taskruns that meet the
filtering condition"""
TaskRunFactory.create_batch(3, info='info')
should_be_missing = TaskFactory.create(info='other info')
retrieved_taskruns = self.task_repo.filter_task_runs_by(info='info')
assert len(retrieved_taskruns) == 3, retrieved_taskruns
assert should_be_missing not in retrieved_taskruns, retrieved_taskruns
def test_filter_task_runs_by_multiple_conditions(self):
"""Test filter_task_runs_by supports multiple-condition queries"""
TaskRunFactory.create(info='info', user_ip='8.8.8.8')
taskrun = TaskRunFactory.create(info='info', user_ip='1.1.1.1')
retrieved_taskruns = self.task_repo.filter_task_runs_by(info='info',
user_ip='1.1.1.1')
assert len(retrieved_taskruns) == 1, retrieved_taskruns
assert taskrun in retrieved_taskruns, retrieved_taskruns
def test_filter_task_runs_support_yield_option(self):
"""Test that filter_task_runs_by with the yielded=True option returns
the results as a generator"""
task_runs = TaskRunFactory.create_batch(2, info='info')
yielded_task_runs = self.task_repo.filter_task_runs_by(info='info',
yielded=True)
import types
assert isinstance(yielded_task_runs.__iter__(), types.GeneratorType)
for taskrun in yielded_task_runs:
assert taskrun in task_runs
def test_filter_tasks_limit_offset(self):
"""Test that filter_tasks_by supports limit and offset options"""
TaskRunFactory.create_batch(4)
all_task_runs = self.task_repo.filter_task_runs_by()
first_two = self.task_repo.filter_task_runs_by(limit=2)
last_two = self.task_repo.filter_task_runs_by(limit=2, offset=2)
assert len(first_two) == 2, first_two
assert len(last_two) == 2, last_two
assert first_two == all_task_runs[:2]
assert last_two == all_task_runs[2:]
def test_count_task_runs_with_no_matches(self):
"""Test count_task_runs_with returns 0 if no taskruns match the query"""
TaskRunFactory.create(info='info')
count = self.task_repo.count_task_runs_with(info='other info')
assert count == 0, count
def test_count_task_runs_with_one_condition(self):
"""Test count_task_runs_with returns the number of taskruns that meet the
filtering condition"""
TaskRunFactory.create_batch(3, info='info')
should_be_missing = TaskRunFactory.create(info='other info')
count = self.task_repo.count_task_runs_with(info='info')
assert count == 3, count
def test_count_task_runs_with_multiple_conditions(self):
"""Test count_task_runs_with supports multiple-condition queries"""
TaskRunFactory.create(info='info', user_ip='8.8.8.8')
taskrun = TaskRunFactory.create(info='info', user_ip='1.1.1.1')
count = self.task_repo.count_task_runs_with(info='info',
user_ip='1.1.1.1')
assert count == 1, count
class TestTaskRepositorySaveDeleteUpdate(Test):
def setUp(self):
super(TestTaskRepositorySaveDeleteUpdate, self).setUp()
self.task_repo = TaskRepository(db)
def test_save_saves_tasks(self):
"""Test save persists Task instances"""
task = TaskFactory.build()
assert self.task_repo.get_task(task.id) is None
self.task_repo.save(task)
assert self.task_repo.get_task(task.id) == task, "Task not saved"
def test_save_saves_taskruns(self):
"""Test save persists TaskRun instances"""
taskrun = TaskRunFactory.build()
assert self.task_repo.get_task_run(taskrun.id) is None
self.task_repo.save(taskrun)
assert self.task_repo.get_task_run(taskrun.id) == taskrun, "TaskRun not saved"
def test_save_fails_if_integrity_error(self):
"""Test save raises a DBIntegrityError if the instance to be saved lacks
a required value"""
task = TaskFactory.build(project_id=None, project=None)
assert_raises(DBIntegrityError, self.task_repo.save, task)
def test_save_only_saves_tasks_and_taskruns(self):
"""Test save raises a WrongObjectError when an object which is neither
a Task nor a Taskrun instance is saved"""
bad_object = dict()
assert_raises(WrongObjectError, self.task_repo.save, bad_object)
def test_update_task(self):
"""Test update persists the changes made to Task instances"""
task = TaskFactory.create(state='ongoing')
task.state = 'done'
self.task_repo.update(task)
updated_task = self.task_repo.get_task(task.id)
assert updated_task.state == 'done', updated_task
def test_update_taskrun(self):
"""Test update persists the changes made to TaskRun instances"""
taskrun = TaskRunFactory.create(info='info')
taskrun.info = 'updated info!'
self.task_repo.update(taskrun)
updated_taskrun = self.task_repo.get_task_run(taskrun.id)
assert updated_taskrun.info == 'updated info!', updated_taskrun
def test_update_fails_if_integrity_error(self):
"""Test update raises a DBIntegrityError if the instance to be updated
lacks a required value"""
task = TaskFactory.create()
task.project_id = None
assert_raises(DBIntegrityError, self.task_repo.update, task)
def test_update_only_updates_tasks_and_taskruns(self):
"""Test update raises a WrongObjectError when an object which is neither
a Task nor a TaskRun instance is updated"""
bad_object = dict()
assert_raises(WrongObjectError, self.task_repo.update, bad_object)
def test_delete_task(self):
"""Test delete removes the Task instance"""
task = TaskFactory.create()
self.task_repo.delete(task)
deleted = self.task_repo.get_task(task.id)
assert deleted is None, deleted
def test_delete_task_deletes_dependent_taskruns(self):
"""Test delete removes the dependent TaskRun instances"""
task = TaskFactory.create()
taskrun = TaskRunFactory.create(task=task)
self.task_repo.delete(task)
deleted = self.task_repo.get_task_run(taskrun.id)
assert deleted is None, deleted
def test_delete_taskrun(self):
"""Test delete removes the TaskRun instance"""
taskrun = TaskRunFactory.create()
self.task_repo.delete(taskrun)
deleted = self.task_repo.get_task_run(taskrun.id)
assert deleted is None, deleted
def test_delete_only_deletes_tasks(self):
"""Test delete raises a WrongObjectError if is requested to delete other
than a task"""
bad_object = dict()
assert_raises(WrongObjectError, self.task_repo.delete, bad_object)
def test_delete_valid_from_project_deletes_many_tasks(self):
"""Test delete_valid_from_project deletes many tasks at once"""
tasks = TaskFactory.create_batch(2)
project = project_repo.get(tasks[0].project_id)
self.task_repo.delete_valid_from_project(project)
tasks = self.task_repo.filter_tasks_by(project_id=project.id)
assert len(tasks) == 0, len(tasks)
def test_delete_valid_from_project_deletes_dependent(self):
"""Test delete_valid_from_project deletes dependent taskruns too"""
task = TaskFactory.create()
taskrun = TaskRunFactory.create(task=task)
task_run_id = taskrun.id
project = project_repo.get(task.project_id)
self.task_repo.delete_valid_from_project(project)
deleted = self.task_repo.get_task_run(id=task_run_id)
assert deleted is None, deleted
def test_delete_valid_from_project_deletes_dependent_without_result(self):
"""Test delete_valid_from_project deletes dependent taskruns without result"""
task = TaskFactory.create(n_answers=1)
project = project_repo.get(task.project_id)
taskrun = TaskRunFactory.create(task=task)
task2 = TaskFactory.create(project=project)
TaskRunFactory.create(task=task2)
self.task_repo.delete_valid_from_project(project)
non_deleted = self.task_repo.filter_tasks_by(project_id=project.id)
err_msg = "There should be one task, as it belongs to a result"
assert len(non_deleted) == 1, err_msg
assert non_deleted[0].id == task.id, err_msg
non_deleted = self.task_repo.filter_task_runs_by(project_id=project.id)
err_msg = "There should be one task_run, as it belongs to a result"
assert len(non_deleted) == 1, err_msg
assert non_deleted[0].id == taskrun.id, err_msg
def test_update_tasks_redundancy_changes_all_project_tasks_redundancy(self):
"""Test update_tasks_redundancy updates the n_answers value for every
task in the project"""
project = ProjectFactory.create()
TaskFactory.create_batch(2, project=project, n_answers=1)
self.task_repo.update_tasks_redundancy(project, 2)
tasks = self.task_repo.filter_tasks_by(project_id=project.id)
for task in tasks:
assert task.n_answers == 2, task.n_answers
def test_update_tasks_redundancy_updates_state_when_incrementing(self):
"""Test update_tasks_redundancy changes 'completed' tasks to 'ongoing'
if n_answers is incremented enough"""
project = ProjectFactory.create()
tasks = TaskFactory.create_batch(2, project=project, n_answers=2)
TaskRunFactory.create_batch(2, task=tasks[0])
tasks[0].state = 'completed'
self.task_repo.update(tasks[0])
assert tasks[0].state == 'completed', tasks[0].state
assert tasks[1].state == 'ongoing', tasks[1].state
self.task_repo.update_tasks_redundancy(project, 3)
tasks = self.task_repo.filter_tasks_by(project_id=project.id)
for task in tasks:
assert task.state == 'ongoing', task.state
def test_update_tasks_redundancy_updates_state_when_decrementing(self):
"""Test update_tasks_redundancy changes 'ongoing' tasks to 'completed'
if n_answers is decremented enough"""
project = ProjectFactory.create()
tasks = TaskFactory.create_batch(2, project=project, n_answers=2)
TaskRunFactory.create_batch(2, task=tasks[0])
TaskRunFactory.create(task=tasks[1])
tasks[0].state = 'completed'
self.task_repo.update(tasks[0])
assert tasks[0].state == 'completed', tasks[0].state
assert tasks[1].state == 'ongoing', tasks[1].state
self.task_repo.update_tasks_redundancy(project, 1)
tasks = self.task_repo.filter_tasks_by(project_id=project.id)
for task in tasks:
assert task.state == 'completed', task.state