test_cache_helpers.py
10.5 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
# -*- 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/>.
from default import Test, db, with_context
from factories import (ProjectFactory, TaskFactory, TaskRunFactory,
AnonymousTaskRunFactory, UserFactory)
from pybossa.cache import helpers
class TestHelpersCache(Test):
def test_n_available_tasks_no_tasks_authenticated_user(self):
"""Test n_available_tasks returns 0 for authenticated user if the project
has no tasks"""
project = ProjectFactory.create()
n_available_tasks = helpers.n_available_tasks(project.id, user_id=1)
assert n_available_tasks == 0, n_available_tasks
def test_n_available_tasks_no_tasks_anonymous_user(self):
"""Test n_available_tasks returns 0 for anonymous user if the project
has no tasks"""
project = ProjectFactory.create()
n_available_tasks = helpers.n_available_tasks(project.id, user_ip='127.0.0.1')
assert n_available_tasks == 0, n_available_tasks
def test_n_available_tasks_no_taskruns_authenticated_user(self):
"""Test n_available_tasks returns 1 for authenticated user
if there are no taskruns"""
project = ProjectFactory.create()
task = TaskFactory.create(project=project)
n_available_tasks = helpers.n_available_tasks(project.id, user_id=1)
assert n_available_tasks == 1, n_available_tasks
def test_n_available_tasks_no_taskruns_anonymous_user(self):
"""Test n_available_tasks returns 1 for anonymous user
if there are no taskruns"""
project = ProjectFactory.create()
task = TaskFactory.create(project=project)
n_available_tasks = helpers.n_available_tasks(project.id, user_ip='127.0.0.1')
assert n_available_tasks == 1, n_available_tasks
def test_n_available_tasks_all_tasks_completed_authenticated_user(self):
"""Test n_available_tasks returns 0 for authenticated user if all the
tasks are completed"""
project = ProjectFactory.create()
task = TaskFactory.create(project=project, state='completed')
n_available_tasks = helpers.n_available_tasks(project.id, user_id=1)
assert n_available_tasks == 0, n_available_tasks
def test_n_available_tasks_all_tasks_completed_anonymous_user(self):
"""Test n_available_tasks returns 0 for anonymous user if all the
tasks are completed"""
project = ProjectFactory.create()
task = TaskFactory.create(project=project, state='completed')
n_available_tasks = helpers.n_available_tasks(project.id, user_ip='127.0.0.1')
assert n_available_tasks == 0, n_available_tasks
def test_n_available_tasks_all_tasks_answered_by_authenticated_user(self):
"""Test n_available_tasks returns 0 for authenticated user if he has
submitted taskruns for all the tasks"""
project = ProjectFactory.create()
task = TaskFactory.create(project=project, n_answers=2)
user = UserFactory.create()
taskrun = TaskRunFactory.create(task=task, user=user)
n_available_tasks = helpers.n_available_tasks(project.id, user_id=user.id)
assert task.state != 'completed', task.state
assert n_available_tasks == 0, n_available_tasks
def test_n_available_tasks_all_tasks_answered_by_anonymous_user(self):
"""Test n_available_tasks returns 0 for anonymous user if he has
submitted taskruns for all the tasks"""
project = ProjectFactory.create()
task = TaskFactory.create(project=project, n_answers=2)
taskrun = AnonymousTaskRunFactory.create(task=task)
n_available_tasks = helpers.n_available_tasks(project.id, user_ip=taskrun.user_ip)
assert task.state != 'completed', task.state
assert n_available_tasks == 0, n_available_tasks
def test_n_available_tasks_some_tasks_answered_by_authenticated_user(self):
"""Test n_available_tasks returns 1 for authenticated user if he has
submitted taskruns for one of the tasks but there is still another task"""
project = ProjectFactory.create()
answered_task = TaskFactory.create(project=project)
available_task = TaskFactory.create(project=project)
user = UserFactory.create()
taskrun = TaskRunFactory.create(task=answered_task, user=user)
n_available_tasks = helpers.n_available_tasks(project.id, user_id=user.id)
assert n_available_tasks == 1, n_available_tasks
def test_n_available_tasks_some_tasks_answered_by_anonymous_user(self):
"""Test n_available_tasks returns 1 for anonymous user if he has
submitted taskruns for one of the tasks but there is still another task"""
project = ProjectFactory.create()
answered_task = TaskFactory.create(project=project)
available_task = TaskFactory.create(project=project)
taskrun = AnonymousTaskRunFactory.create(task=answered_task)
n_available_tasks = helpers.n_available_tasks(project.id, user_ip=taskrun.user_ip)
assert n_available_tasks == 1, n_available_tasks
def test_n_available_tasks_some_task_answered_by_another_user(self):
"""Test n_available_tasks returns 1 for a user if another
user has submitted taskruns for the task but he hasn't"""
project = ProjectFactory.create()
task = TaskFactory.create(project=project)
user = UserFactory.create()
taskrun = TaskRunFactory.create(task=task)
n_available_tasks = helpers.n_available_tasks(project.id, user_id=user.id)
assert n_available_tasks == 1, n_available_tasks
def test_check_contributing_state_completed(self):
"""Test check_contributing_state returns 'completed' for a project with all
tasks completed and user that has contributed to it"""
project = ProjectFactory.create()
task = TaskFactory.create(project=project, n_answers=1)
user = UserFactory.create()
TaskRunFactory.create_batch(1, task=task, user=user)
contributing_state = helpers.check_contributing_state(project=project,
user_id=user.id)
assert task.state == 'completed', task.state
assert contributing_state == 'completed', contributing_state
def test_check_contributing_state_completed_user_not_contributed(self):
"""Test check_contributing_state returns 'completed' for a project with all
tasks completed even if the user has not contributed to it"""
project = ProjectFactory.create()
task = TaskFactory.create(project=project, n_answers=2)
TaskRunFactory.create_batch(2, task=task)
user = UserFactory.create()
contributing_state = helpers.check_contributing_state(project=project,
user_id=user.id)
assert task.state == 'completed', task.state
assert contributing_state == 'completed', contributing_state
def test_check_contributing_state_ongoing_tasks_not_contributed(self):
"""Test check_contributing_state returns 'can_contribute' for a project
with ongoing tasks a user has not contributed to"""
project = ProjectFactory.create()
task = TaskFactory.create(project=project)
user = UserFactory.create()
contributing_state = helpers.check_contributing_state(project=project,
user_id=user.id)
assert contributing_state == 'can_contribute', contributing_state
def test_check_contributing_state_ongoing_tasks_contributed(self):
"""Test check_contributing_state returns 'cannot_contribute' for a project
with ongoing tasks to which the user has already contributed"""
project = ProjectFactory.create()
task = TaskFactory.create(project=project, n_answers=3)
user = UserFactory.create()
TaskRunFactory.create(task=task, user=user)
contributing_state = helpers.check_contributing_state(project=project,
user_id=user.id)
assert contributing_state == 'cannot_contribute', contributing_state
def test_check_contributing_state_draft(self):
"""Test check_contributing_state returns 'draft' for a project that has
ongoing tasks but has no presenter"""
project = ProjectFactory.create(published=False, info={})
task = TaskFactory.create(project=project)
user = UserFactory.create()
contributing_state = helpers.check_contributing_state(project=project,
user_id=user.id)
assert 'task_presenter' not in project.info
assert contributing_state == 'draft', contributing_state
def test_check_contributing_state_draft_presenter(self):
"""Test check_contributing_state returns 'draft' for a project that has
no tasks but has a presenter"""
project = ProjectFactory.create(published=False)
user = UserFactory.create()
contributing_state = helpers.check_contributing_state(project=project,
user_id=user.id)
assert 'task_presenter' in project.info
assert contributing_state == 'draft', contributing_state
def test_check_contributing_state_publish(self):
"""Test check_contributing_state returns 'publish' for a project that is
not published but is ready to be validated for publication (i.e. has both
tasks and a task presenter"""
project = ProjectFactory.create(published=False)
task = TaskFactory.create(project=project)
user = UserFactory.create()
contributing_state = helpers.check_contributing_state(project=project,
user_id=user.id)
assert contributing_state == 'publish', contributing_state