-
Notifications
You must be signed in to change notification settings - Fork 142
/
test_amt.py
417 lines (340 loc) · 17.5 KB
/
test_amt.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
from builtins import range
from builtins import object
import pytest
try:
import mock
from mock import patch, PropertyMock
except ImportError:
from unittest import mock
from unittest.mock import patch, PropertyMock
import pickle
import os
import boto3
import datetime
import ciso8601
from psiturk import psiturk_statuses
from psiturk.psiturk_exceptions import *
SANDBOX_ENDPOINT_URL = 'https://mturk-requester-sandbox.us-east-1.amazonaws.com'
LIVE_ENDPOINT_URL = 'https://mturk-requester.us-east-1.amazonaws.com'
# pytest_plugins = ['pytest_profiling']
class TestAmtServices(object):
def test_wrapper_hit_create(self, amt_services_wrapper, helpers, create_dummy_hit, stubber_prepare_create_hit):
create_dummy_hit('3XJOUITW8URHJMX7F00H20LGRIAQTX')
create_dummy_hit('ABCDUITW8URHJMX7F00H20LGRIAQTX')
from psiturk.models import Hit
# confirm that it's in the local db...
assert Hit.query.get('3XJOUITW8URHJMX7F00H20LGRIAQTX') is not None
# confirm that it's in the local db...
assert Hit.query.get('ABCDUITW8URHJMX7F00H20LGRIAQTX') is not None
def test_wrapper_get_all_hits(self, amt_services_wrapper, create_dummy_hit, list_hits, helpers):
create_dummy_hit('3XJOUITW8URHJMX7F00H20LGRIAQTX')
hits = list_hits()
hit_ids = [hit.options['hitid'] for hit in hits]
assert '3XJOUITW8URHJMX7F00H20LGRIAQTX' in hit_ids
def test_wrapper_get_all_hits_all_studies(self, amt_services_wrapper, create_dummy_hit, list_hits, helpers):
create_dummy_hit('3XJOUITW8URHJMX7F00H20LGRIAQTX')
create_dummy_hit('3BFNCI9LYKQ2ENUY4MLKKW0NSU437W')
from psiturk.db import db_session
from psiturk.models import Hit
Hit.query.filter_by(hitid='3BFNCI9LYKQ2ENUY4MLKKW0NSU437W').delete()
db_session.commit()
hits = list_hits(all_studies=False)
assert '3BFNCI9LYKQ2ENUY4MLKKW0NSU437W' not in [
hit.options['hitid'] for hit in hits]
hits = list_hits(all_studies=True)
assert '3BFNCI9LYKQ2ENUY4MLKKW0NSU437W' in [
hit.options['hitid'] for hit in hits]
def test_wrapper_get_active_hits(self, activate_a_hit, expire_a_hit, amt_services_wrapper, create_dummy_hit, list_hits, helpers):
hits_json = helpers.get_boto3_return('list_hits.json')
index_of_hit_to_expire = 0
id_of_hit_to_expire = hits_json['HITs'][index_of_hit_to_expire]['HITId']
index_of_hit_to_be_active = 1
id_of_hit_to_be_active = hits_json['HITs'][index_of_hit_to_be_active]['HITId']
hits_json = activate_a_hit(hits_json)
hits_json = expire_a_hit(hits_json)
create_dummy_hit(id_of_hit_to_expire)
create_dummy_hit(id_of_hit_to_be_active)
active_hits = list_hits(hits_json=hits_json,
all_studies=False, active=True)
active_hit_ids = [hit.options['hitid'] for hit in active_hits]
assert id_of_hit_to_expire not in active_hit_ids
assert id_of_hit_to_be_active in active_hit_ids
def test_wrapper_extend_hit(self, stubber, helpers, amt_services_wrapper):
# uh, just test that boto doesn't throw an error...
stubber.add_response('create_additional_assignments_for_hit', {})
amt_services_wrapper.extend_hit(
'3BFNCI9LYKQ2ENUY4MLKKW0NSU437W', assignments=10)
stubber.add_response(
'get_hit', helpers.get_boto3_return('get_hit.json'))
amt_services_wrapper.extend_hit(
'3BFNCI9LYKQ2ENUY4MLKKW0NSU437W', minutes=10)
stubber.add_response('create_additional_assignments_for_hit', {})
stubber.add_response(
'get_hit', helpers.get_boto3_return('get_hit.json'))
amt_services_wrapper.extend_hit(
'3BFNCI9LYKQ2ENUY4MLKKW0NSU437W', assignments=10, minutes=10)
def test_wrapper_expire_hit(self, stubber, amt_services_wrapper, helpers):
# a list of ids...
hit_ids = ['123', 'abc']
for hit_id in hit_ids:
stubber.add_response('update_expiration_for_hit', {})
amt_services_wrapper.expire_hit(hit_id)
def test_wrapper_expire_all_hits(self, stubber, amt_services_wrapper, helpers):
# expire all...
# This would ideally test that only hits for the current study are being expired...
hits_json = helpers.get_boto3_return('list_hits.json')
stubber.add_response('list_hits', hits_json)
for i in hits_json['HITs']:
stubber.add_response('update_expiration_for_hit', {})
amt_services_wrapper.expire_all_hits()
pass
def test_wrapper_delete_hit(self, stubber, amt_services_wrapper):
hit_ids = ['123', 'abc']
for hit_id in hit_ids:
stubber.add_response('delete_hit', {})
amt_services_wrapper.delete_hit(hit_id)
def test_wrapper_delete_all_hits(self, stubber, amt_services_wrapper, helpers):
hits_json = helpers.get_boto3_return('list_hits.json')
stubber.add_response('list_hits', hits_json)
for hit in hits_json['HITs']:
stubber.add_response('delete_hit', {})
amt_services_wrapper.delete_all_hits()
def test_wrapper_tally_hits(self, stubber, activate_a_hit, create_dummy_hit, helpers, amt_services_wrapper):
hits_json = helpers.get_boto3_return('list_hits.json')
hits_json = activate_a_hit(hits_json)
hit_ids = [hit['HITId'] for hit in hits_json['HITs']]
for hit_id in hit_ids:
create_dummy_hit(hit_id)
stubber.add_response('list_hits', hits_json)
count = amt_services_wrapper.tally_hits()
@pytest.fixture()
def get_assignment_json(self, helpers):
def do_it():
assignment = helpers.get_boto3_return('get_assignment.json')
return assignment
return do_it
@pytest.fixture()
def get_submitted_assignment_json(self, get_assignment_json):
def do_it(assignment=None):
if not assignment:
assignment = get_assignment_json()
assignment['Assignment']['AssignmentStatus'] = 'Submitted'
return assignment
return do_it
@pytest.fixture()
def get_rejected_assignment_json(self, get_assignment_json):
def do_it(assignment=None):
if not assignment:
assignment = get_assignment_json()
assignment['Assignment']['AssignmentStatus'] = 'Rejected'
return assignment
return do_it
@pytest.fixture()
def get_approved_assignment_json(self, get_assignment_json):
def do_it(assignment=None):
if not assignment:
assignment = get_assignment_json()
assignment['Assignment']['AssignmentStatus'] = 'Approved'
return assignment
return do_it
def test_wrapper_get_assignments_for_hits(self, stubber, amt_services_wrapper, helpers, create_dummy_hit, get_submitted_assignment_json):
assignments_data = helpers.get_boto3_return(
'list_assignments_for_hit.json')
hit_ids = list(set([assignment['HITId']
for assignment in assignments_data['Assignments']]))
[create_dummy_hit(hit_id) for hit_id in hit_ids]
[stubber.add_response('list_assignments_for_hit',
assignments_data) for hit_id in hit_ids]
assignments = amt_services_wrapper.get_assignments(hit_ids=hit_ids)
def test_wrapper_approve_single_assignment(self, stubber, create_dummy_assignment, amt_services_wrapper):
create_dummy_assignment({
'hitid': '123',
'beginhit': datetime.datetime.utcnow() - datetime.timedelta(days=-2),
'assignmentid': 'ABC',
'status': psiturk_statuses.SUBMITTED,
'mode': 'sandbox'})
create_dummy_assignment({'hitid': '123',
'beginhit': datetime.datetime.utcnow(),
'assignmentid': 'DEF',
'status': psiturk_statuses.SUBMITTED,
'mode': 'sandbox'
})
stubber.add_response('approve_assignment', {}, {'AssignmentId':'ABC', 'OverrideRejection': False})
response = amt_services_wrapper.approve_assignment_by_assignment_id('ABC')
assert response.success
def test_wrapper_approve_all_assignments(self, stubber, activate_a_hit, helpers, create_dummy_assignment, create_dummy_hit, amt_services_wrapper):
hits_json = helpers.get_boto3_return('list_hits.json')
index_of_hit_to_be_active = 0
hits_json = activate_a_hit(
hits_json, index_of_hit_to_be_active=index_of_hit_to_be_active)
active_hit_id = hits_json['HITs'][index_of_hit_to_be_active]['HITId']
assignment_1 = create_dummy_assignment(
{'hitid': active_hit_id, 'status': psiturk_statuses.COMPLETED, 'mode': 'sandbox'})
# do not create dummy hit
assignment_2 = create_dummy_assignment(
{'hitid': active_hit_id, 'status': psiturk_statuses.COMPLETED, 'mode': 'sandbox'})
# create_dummy_hit(assignment_2.hitid)
assignments = [assignment_1, assignment_2]
[stubber.add_response('approve_assignment', {})
for assignment in assignments]
results = (amt_services_wrapper.approve_all_assignments(
all_studies=False)).data['results']
assert len(
[result for result in results if result.status == 'success']) == 2
stubber.add_response('list_hits', hits_json)
assignments_data = helpers.get_boto3_return(
'list_assignments_for_hit.json')
number_approved = 0
for i in range(len(hits_json['HITs'])):
stubber.add_response('list_assignments_for_hit', assignments_data)
for i in range(len(hits_json['HITs'])):
for j in range(len(assignments_data['Assignments'])):
stubber.add_response('approve_assignment', {})
number_approved += 1
results = (amt_services_wrapper.approve_all_assignments(
all_studies=True)).data['results']
assert len([result for result in results if result.status ==
'success']) == number_approved
def test_wrapper_approve_all_for_hit(self, stubber, activate_a_hit, helpers, create_dummy_assignment, create_dummy_hit, amt_services_wrapper):
hits_json = helpers.get_boto3_return('list_hits.json')
[activate_a_hit(hits_json, i) for (i, hit) in enumerate(hits_json['HITs'])]
first_hitid = hits_json['HITs'][0]['HITId']
second_hitid = hits_json['HITs'][1]['HITId']
# set two to be for the first hit
mode = 'sandbox'
a_1 = create_dummy_assignment(
{'hitid': first_hitid, 'status': psiturk_statuses.COMPLETED, 'mode': mode})
a_2 = create_dummy_assignment(
{'hitid': first_hitid, 'status': psiturk_statuses.COMPLETED, 'mode': mode})
a_3 = create_dummy_assignment(
{'hitid': second_hitid, 'status': psiturk_statuses.COMPLETED, 'mode': mode})
#set up stubber to expect two 'approve_hit' calls
stubber.add_response('approve_assignment', {})
stubber.add_response('approve_assignment', {})
response = amt_services_wrapper.approve_assignments_for_hit(first_hitid)
for r in response.data['results']:
assert r.success
assert len(response.data['results']) == 2
def test_wrapper_reject_unreject_assignments(self, stubber, amt_services_wrapper, create_dummy_assignment, helpers):
# reject all for a given hit, local only...
assignment_1 = create_dummy_assignment({'hitid': 'abc', 'status': 4, 'mode': 'sandbox'})
stubber.add_response('reject_assignment', {})
results = (amt_services_wrapper.reject_assignments_for_hit(
'abc', all_studies=False)).data['results']
assert len(results) == 1
hits_data = helpers.get_boto3_return('list_hits.json')
assignments_data = helpers.get_boto3_return(
'list_assignments_for_hit.json')
def prep_list_hits():
stubber.add_response('list_hits', hits_data)
for i in range(len(hits_data['HITs'])):
stubber.add_response(
'list_assignments_for_hit', assignments_data)
# reject all for a given hit,, all studies...
prep_list_hits()
def prep_operation_all_for_hit(operation=None):
number_to_be_operated_on = 0
for i in range(len(hits_data['HITs'])):
for j in range(len(assignments_data['Assignments'])):
stubber.add_response(operation, {})
number_to_be_operated_on += 1
return number_to_be_operated_on
number_to_be_approved = prep_operation_all_for_hit('reject_assignment')
results = (amt_services_wrapper.reject_assignments_for_hit(
'abc', all_studies=True)).data['results']
assert len(results) == number_to_be_approved
for result in results:
assert result.status == 'success'
# reject one at a time
reject_these = ['abc', '123']
[stubber.add_response('reject_assignment', {}) for i in reject_these]
result = amt_services_wrapper.reject_assignments(reject_these)
# unreject one at a time
[stubber.add_response('approve_assignment', {}) for i in reject_these]
results = (amt_services_wrapper.unreject_assignments(
reject_these)).data['results']
assert len(results) == len(reject_these)
# unreject all for a given hit for local study only
results = (amt_services_wrapper.unreject_assignments_for_hit(
['abc'], all_studies=False)).data['results']
assert len(results) == 1
# unreject all for a given hit across all studies
prep_list_hits()
number_to_be_unrejected = prep_operation_all_for_hit(
'approve_assignment')
results = (amt_services_wrapper.unreject_assignments_for_hit(
'abc', all_studies=True)).data['results']
assert len(results) == number_to_be_unrejected
for result in results:
assert result.status == 'success'
def test_wrapper_bonus_assignment(self, create_dummy_assignment, amt_services_wrapper, stubber):
from psiturk.db import db_session
# ## filters
# with hit_id
hit_id = '123'
# amount = 0.01
amount = 'auto'
reason = 'yee'
override_bonused_status = False
def edit_assignment(assignment, new_config):
for k, v in list(new_config.items()):
setattr(assignment, k, v)
db_session.add(assignment)
db_session.commit()
assignment_1 = create_dummy_assignment({'hitid': hit_id})
assignment_2 = create_dummy_assignment({'hitid': hit_id})
edit_assignment(assignment_1, {'status': psiturk_statuses.BONUSED})
edit_assignment(assignment_2, {'status': psiturk_statuses.BONUSED})
results = amt_services_wrapper.bonus_assignments_for_hit(
hit_id, amount, reason, override_bonused_status)
results = results.data['results']
assert isinstance(results[0].exception,
AssignmentAlreadyBonusedError)
edit_assignment(
assignment_1, {'status': psiturk_statuses.CREDITED, 'bonus': 0.00})
results = (amt_services_wrapper.bonus_assignments_for_hit(
'123', amount, reason)).data['results']
assert isinstance(results[0].exception, (BadBonusAmountError, NoAutoBonusAmountSetError))
stubber.add_response('send_bonus', {})
edit_assignment(
assignment_1, {'status': psiturk_statuses.BONUSED, 'bonus': 0.50})
results = (amt_services_wrapper.bonus_assignments_for_hit(
'123', amount, reason, override_bonused_status=True)).data['results']
assert results[0].status == 'success'
assignment_id = 'abc'
amount = 0.50
reason = 'yee'
assignment_2 = create_dummy_assignment({
'hitid': hit_id,
'assignmentid': assignment_id,
'status': psiturk_statuses.CREDITED
})
stubber.add_response('send_bonus', {})
stubber.add_response('send_bonus', {})
results = [amt_services_wrapper.bonus_assignment_for_assignment_id(
assignment_id, amount, reason) for assignment_id in [assignment_id, '123']]
assert len(
[result for result in results if result.status == 'success']) == 1
for assignment in [assignment_1, assignment_2]:
edit_assignment(assignment, {
'status': psiturk_statuses.CREDITED, 'mode': 'sandbox', 'bonus': 0.50})
stubber.add_response('send_bonus', {})
amount = 'auto'
reason = 'yee'
results = (amt_services_wrapper.bonus_all_local_assignments(
amount, reason)).data['results']
assert len(
[result for result in results if result.status == 'success']) == 2
for i, assignment in enumerate([assignment_1, assignment_2]):
edit_assignment(assignment, {
'assignmentid': '{}bc'.format(i+1),
'status': psiturk_statuses.CREDITED,
'mode': 'sandbox',
'bonus': 0.50
})
stubber.add_response('send_bonus', {})
results = [amt_services_wrapper.bonus_assignment_for_assignment_id(
assignment_id, amount=amount, reason='') for assignment_id in ['1bc', '2bc']]
for result in results:
assert isinstance(
result.exception, BonusReasonMissingError)