forked from scaleapi/scaleapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
259 lines (205 loc) · 8.82 KB
/
test_client.py
File metadata and controls
259 lines (205 loc) · 8.82 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
import pytest
import scaleapi
import time
from datetime import datetime
import os
try:
test_api_key = os.environ['SCALE_TEST_API_KEY']
client = scaleapi.ScaleClient(test_api_key)
except KeyError:
raise Exception("Please set the environment variable SCALE_TEST_API_KEY to run tests.")
def make_a_task():
return client.create_comparison_task(
callback_url='http://www.example.com/callback',
instruction='Do the objects in these images have the same pattern?',
attachment_type='image',
attachments=[
'http://i.ebayimg.com/00/$T2eC16dHJGwFFZKjy5ZjBRfNyMC4Ig~~_32.JPG',
'http://images.wisegeek.com/checkered-tablecloth.jpg'
],
choices=['yes', 'no'])
def test_categorize_ok():
task = client.create_categorization_task(
callback_url='http://www.example.com/callback',
instruction='Is this company public or private?',
attachment_type='website',
attachment='http://www.google.com/',
categories=['public', 'private'])
def test_categorize_fail():
with pytest.raises(scaleapi.ScaleInvalidRequest):
client.create_categorization_task(
callback_url='http://www.example.com/callback',
categories=['public', 'private'])
def test_transcription_ok():
task = client.create_transcription_task(
callback_url='http://www.example.com/callback',
instruction='Transcribe the given fields. Then for each news item on the page, transcribe the information for the row.',
attachment_type='website',
attachment='http://www.google.com/',
fields={
'title': 'Title of Webpage',
'top_result': 'Title of the top result'
},
row_fields={
'username': 'Username of submitter',
'comment_count': 'Number of comments'
})
def test_transcription_fail():
with pytest.raises(scaleapi.ScaleInvalidRequest):
client.create_transcription_task(
callback_url='http://www.example.com/callback',
attachment_type='website')
@pytest.mark.skip(reason="Deprecated at the moment")
def test_phonecall_ok():
task = client.create_phonecall_task(
callback_url='http://www.example.com/callback',
instruction='Call this person and follow the script provided, recording responses',
phone_number='5055006865',
entity_name='Alexandr Wang',
script='Hello ! Are you happy today? (pause) One more thing - what is your email address?',
fields={'email': 'Email Address'},
choices=['He is happy', 'He is not happy'])
@pytest.mark.skip(reason="Deprecated at the moment")
def test_phonecall_fail():
with pytest.raises(scaleapi.ScaleInvalidRequest):
client.create_phonecall_task(
callback_url='http://www.example.com/callback',
instruction='Call this person and follow the script provided, recording responses')
def test_comparison_ok():
task = client.create_comparison_task(
callback_url='http://www.example.com/callback',
instruction='Do the objects in these images have the same pattern?',
attachment_type='image',
attachments=[
'http://i.ebayimg.com/00/$T2eC16dHJGwFFZKjy5ZjBRfNyMC4Ig~~_32.JPG',
'http://images.wisegeek.com/checkered-tablecloth.jpg'
],
choices=['yes', 'no'])
def test_comparison_fail():
with pytest.raises(scaleapi.ScaleInvalidRequest):
client.create_comparison_task(
callback_url='http://www.example.com/callback',
instruction='Do the objects in these images have the same pattern?',
attachment_type='image')
def test_annotation_ok():
task = client.create_annotation_task(
callback_url='http://www.example.com/callback',
instruction='Draw a box around each **baby cow** and **big cow**',
attachment_type='image',
attachment='http://i.imgur.com/v4cBreD.jpg',
min_width='30',
min_height='30',
objects_to_annotate=['baby cow', 'big cow'],
with_labels=True)
# min_width and min_height should be optional
task2 = client.create_annotation_task(
callback_url='http://www.example.com/callback',
instruction='Draw a box around each **baby cow** and **big cow**',
attachment_type='image',
attachment='http://i.imgur.com/v4cBreD.jpg',
objects_to_annotate=['baby cow', 'big cow'],
with_labels=True)
def test_annotation_fail():
with pytest.raises(scaleapi.ScaleInvalidRequest):
client.create_annotation_task(
callback_url='http://www.example.com/callback',
instruction='Draw a box around each **baby cow** and **big cow**',
attachment_type='image')
def test_polygonannotation_ok():
task = client.create_polygonannotation_task(
callback_url='http://www.example.com/callback',
instruction='Draw a tight shape around the big cow',
attachment_type='image',
attachment='http://i.imgur.com/v4cBreD.jpg',
objects_to_annotate=['big cow'],
with_labels=True)
def test_polygonannotation_fail():
with pytest.raises(scaleapi.ScaleInvalidRequest):
client.create_polygonannotation_task(
callback_url='http://www.example.com/callback',
instruction='Draw a tight shape around the big cow',
attachment_type='image')
def test_lineannotation_ok():
task = client.create_lineannotation_task(
callback_url='http://www.example.com/callback',
instruction='Draw a tight shape around the big cow',
attachment_type='image',
attachment='http://i.imgur.com/v4cBreD.jpg',
objects_to_annotate=['big cow'],
with_labels=True)
def test_lineannotation_fail():
with pytest.raises(scaleapi.ScaleInvalidRequest):
client.create_lineannotation_task(
callback_url='http://www.example.com/callback',
instruction='Draw a tight shape around the big cow',
attachment_type='image')
def test_datacollection_ok():
task = client.create_datacollection_task(
callback_url='http://www.example.com/callback',
instruction='Find the URL for the hiring page for the company with attached website.',
attachment_type='website',
attachment='http://www.google.com/',
fields={ 'hiring_page': 'Hiring Page URL' })
def test_datacollection_fail():
with pytest.raises(scaleapi.ScaleInvalidRequest):
client.create_datacollection_task(
callback_url='http://www.example.com/callback',
attachment_type='website')
def test_audiotranscription_ok():
task = client.create_audiotranscription_task(
callback_url='http://www.example.com/callback',
attachment_type='audio',
attachment='https://storage.googleapis.com/deepmind-media/pixie/knowing-what-to-say/second-list/speaker-3.wav',
verbatim=False,
phrases=['avocado', 'stone']
)
def test_audiotranscription_fail():
with pytest.raises(scaleapi.ScaleInvalidRequest):
client.create_audiotranscription_task(
callback_url='http://www.example.com/callback',
attachment_type='audio')
def test_audiotranscription_fail2():
with pytest.raises(scaleapi.ScaleInvalidRequest):
client.create_audiotranscription_task(
callback_url='http://www.example.com/callback',
attachment='some_non_url')
def test_cancel():
task = make_a_task()
# raises a scaleexception, because test tasks complete instantly
with pytest.raises(scaleapi.ScaleException):
task.cancel()
def test_task_retrieval():
task = make_a_task()
task2 = client.fetch_task(task.id)
assert task.status == 'pending'
assert task2.status == 'completed'
assert task2.id == task.id
assert task2.callback_url == task.callback_url
assert task2.instruction == task.instruction
assert task2.attachment_type == task.attachment_type
assert task2.attachments == task.attachments
assert task2.choices == task.choices
assert task2.metadata == task.metadata
assert task2.type == task.type
assert task2.created_at == task.created_at
def test_task_retrieval_time():
task = make_a_task()
time.sleep(0.5)
start_time = datetime.utcnow().isoformat()
time.sleep(0.5)
end_time = datetime.utcnow().isoformat()
tasks = client.tasks(start_time=start_time, end_time=end_time)
assert tasks.docs == []
def test_task_retrieval_fail():
with pytest.raises(scaleapi.ScaleException):
client.fetch_task('fake_id_qwertyuiop')
def test_tasks():
tasks = []
for i in range(3):
tasks.append(make_a_task())
task_ids = {task.id for task in tasks}
for task in client.tasks(limit=3):
assert task.id in task_ids
def test_tasks_invalid():
with pytest.raises(scaleapi.ScaleException):
client.tasks(bogus=0)