Skip to content

Commit 493fc39

Browse files
committed
tests
1 parent e1d72d3 commit 493fc39

File tree

3 files changed

+144
-3
lines changed

3 files changed

+144
-3
lines changed

scaleapi/__init__.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,23 @@ def cancel_task(self, task_id):
8686
"""
8787
return Task(self._postrequest('task/%s/cancel' % task_id), self)
8888

89-
def tasks(self):
90-
"""Returns a list of all your tasks."""
91-
return [Task(json, self) for json in self._getrequest('tasks')['docs']]
89+
def tasks(self, **kwargs):
90+
"""Returns a list of your tasks.
91+
Returns up to 100 at a time, to get more use the offset param.
92+
93+
start/end_time are ISO8601 dates, the time range of tasks to fetch.
94+
status can be 'completed', 'pending', or 'canceled'.
95+
type is the task type.
96+
limit is the max number of results to display per page,
97+
offset is the number of results to skip (for showing more pages).
98+
"""
99+
allowed_kwargs = {'start_time', 'end_time', 'status', 'type', 'limit', 'offset'}
100+
for key in kwargs:
101+
if key not in allowed_kwargs:
102+
raise ScaleInvalidRequest('Illegal parameter %s for ScaleClient.tasks()'
103+
% key, None)
104+
return [Task(json, self) for json in
105+
self._getrequest('tasks', payload=kwargs)['docs']]
92106

93107
def create_categorization_task(self, **kwargs):
94108
validate_payload('categorization', kwargs)

tests/test_client.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import pytest
2+
import scaleapi
3+
import os
4+
5+
try:
6+
test_api_key = os.environ['SCALE_TEST_API_KEY']
7+
client = scaleapi.ScaleClient(test_api_key)
8+
except KeyError:
9+
raise Exception("Please set the environment variable SCALE_TEST_API_KEY to run tests.")
10+
11+
12+
def test_categorize_ok():
13+
task = client.create_categorization_task(
14+
callback_url='http://www.example.com/callback',
15+
instruction='Is this company public or private?',
16+
attachment_type='website',
17+
attachment='http://www.google.com/',
18+
categories=['public', 'private'])
19+
20+
21+
def test_categorize_fail():
22+
with pytest.raises(scaleapi.ScaleInvalidRequest):
23+
client.create_categorization_task(
24+
callback_url='http://www.example.com/callback',
25+
categories=['public', 'private'])
26+
27+
28+
def test_transcrption_ok():
29+
task = client.create_transcription_task(
30+
callback_url='http://www.example.com/callback',
31+
instruction='Transcribe the given fields. Then for each news item on the page, transcribe the information for the row.',
32+
attachment_type='website',
33+
attachment='http://www.google.com/',
34+
fields={
35+
'title': 'Title of Webpage',
36+
'top_result': 'Title of the top result'
37+
},
38+
row_fields={
39+
'username': 'Username of submitter',
40+
'comment_count': 'Number of comments'
41+
})
42+
43+
44+
def test_transcription_fail():
45+
with pytest.raises(scaleapi.ScaleInvalidRequest):
46+
client.create_transcription_task(
47+
callback_url='http://www.example.com/callback',
48+
attachment_type='website')
49+
50+
51+
def test_phonecall_ok():
52+
task = client.create_phonecall_task(
53+
callback_url='http://www.example.com/callback',
54+
instruction='Call this person and follow the script provided, recording responses',
55+
phone_number='5055006865',
56+
entity_name='Alexandr Wang',
57+
script='Hello ! Are you happy today? (pause) One more thing - what is your email address?',
58+
fields={'email': 'Email Address'},
59+
choices=['He is happy', 'He is not happy'])
60+
61+
62+
def test_phonecall_fail():
63+
with pytest.raises(scaleapi.ScaleInvalidRequest):
64+
client.create_phonecall_task(
65+
callback_url='http://www.example.com/callback',
66+
instruction='Call this person and follow the script provided, recording responses')
67+
68+
69+
def test_comparison_ok():
70+
task = client.create_comparison_task(
71+
callback_url='http://www.example.com/callback',
72+
instruction='Do the objects in these images have the same pattern?',
73+
attachment_type='image',
74+
attachments=[
75+
'http://i.ebayimg.com/00/$T2eC16dHJGwFFZKjy5ZjBRfNyMC4Ig~~_32.JPG',
76+
'http://images.wisegeek.com/checkered-tablecloth.jpg'
77+
],
78+
choices=['yes', 'no'])
79+
80+
81+
def test_comparison_fail():
82+
with pytest.raises(scaleapi.ScaleInvalidRequest):
83+
client.create_comparison_task(
84+
callback_url='http://www.example.com/callback',
85+
instruction='Do the objects in these images have the same pattern?',
86+
attachment_type='image')
87+
88+
89+
def test_annotation_ok():
90+
task = client.create_annotation_task(
91+
callback_url='http://www.example.com/callback',
92+
instruction='Draw a box around each **baby cow** and **big cow**',
93+
attachment_type='image',
94+
attachment='http://i.imgur.com/v4cBreD.jpg',
95+
objects_to_annotate=['baby cow', 'big cow'],
96+
with_labels=True)
97+
98+
99+
def test_annotation_fail():
100+
with pytest.raises(scaleapi.ScaleInvalidRequest):
101+
client.create_annotation_task(
102+
callback_url='http://www.example.com/callback',
103+
instruction='Draw a box around each **baby cow** and **big cow**',
104+
attachment_type='image')
105+
106+
107+
def test_cancel():
108+
task = client.create_annotation_task(
109+
callback_url='http://www.example.com/callback',
110+
instruction='Draw a box around each **baby cow** and **big cow**',
111+
attachment_type='image',
112+
attachment='http://i.imgur.com/v4cBreD.jpg',
113+
objects_to_annotate=['baby cow', 'big cow'],
114+
with_labels=True)
115+
116+
# raises a scaleexception, because test tasks complete instantly
117+
with pytest.raises(scaleapi.ScaleException):
118+
task.cancel()

tox.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[tox]
2+
envlist = py27,py35
3+
[testenv]
4+
passenv=SCALE_TEST_API_KEY
5+
deps=
6+
requests
7+
enum34
8+
pytest
9+
commands=py.test

0 commit comments

Comments
 (0)