Skip to content

Commit 64c4a18

Browse files
committed
added binary tests, added complete binary support
1 parent 0759bb5 commit 64c4a18

File tree

5 files changed

+77
-22
lines changed

5 files changed

+77
-22
lines changed

adk/ADK.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ def load(self):
4343
print("PIPE_INIT_COMPLETE")
4444
sys.stdout.flush()
4545

46+
def apply(self, payload):
47+
try:
48+
if self.load_result:
49+
apply_result = self.apply_func(payload, self.load_result)
50+
else:
51+
apply_result = self.apply_func(payload)
52+
response_obj = self.format_response(apply_result)
53+
return response_obj
54+
except Exception as e:
55+
response_obj = self.create_exception(e)
56+
return response_obj
57+
4658
def format_data(self, request):
4759
if request["content_type"] in ["text", "json"]:
4860
data = request["data"]
@@ -114,27 +126,15 @@ def create_exception(self, exception, loading_exception=False):
114126
return response
115127

116128
def process_loop(self):
117-
response_obj = ""
118129
for line in sys.stdin:
119-
try:
120-
request = json.loads(line)
121-
formatted_input = self.format_data(request)
122-
if self.load_result:
123-
apply_result = self.apply_func(formatted_input, self.load_result)
124-
else:
125-
apply_result = self.apply_func(formatted_input)
126-
response_obj = self.format_response(apply_result)
127-
except Exception as e:
128-
response_obj = self.create_exception(e)
129-
finally:
130-
self.write_to_pipe(response_obj)
130+
request = json.loads(line)
131+
formatted_input = self.format_data(request)
132+
result = self.apply(formatted_input)
133+
self.write_to_pipe(result)
131134

132135
def process_local(self, local_payload, pprint):
133-
if self.load_result:
134-
apply_result = self.apply_func(local_payload, self.load_result)
135-
else:
136-
apply_result = self.apply_func(local_payload)
137-
pprint(self.format_response(apply_result))
136+
result = self.apply(local_payload)
137+
self.write_to_pipe(result, pprint=pprint)
138138

139139
def init(self, local_payload=None, pprint=print):
140140
self.load()

run_tests.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from tests.test_adk_remote import RemoteTest
22
from tests.test_adk_local import LocalTest
33
import unittest
4-
4+
import os
55
if __name__ == "__main__":
6+
if os.getenv('ALGORITHMIA_API_KEY', None) == None:
7+
raise Exception("api key not provided, please export your ALGORITHMIA_API_KEY environment variable.")
68
unittest.main()

tests/adk_algorithms.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import Algorithmia
2-
2+
import base64
33

44
# -- Apply functions --- #
55
def apply_basic(input):
66
return "hello " + input
77

8+
def apply_binary(input):
9+
if isinstance(input, bytes):
10+
input = input.decode('utf8')
11+
return bytes("hello " + input, encoding='utf8')
812

913
def apply_input_or_context(input, globals=None):
1014
if isinstance(globals, dict):

tests/test_adk_local.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_error_loading(self):
7777
input = "Algorithmia"
7878
expected_output = {'error':
7979
{'message': 'This exception was thrown in loading',
80-
'error_type': 'AlgorithmError',
80+
'error_type': 'LoadingError',
8181
'stacktrace': ''
8282
}
8383
}
@@ -87,6 +87,29 @@ def test_error_loading(self):
8787
actual_output["error"]["stacktrace"] = ''
8888
self.assertEqual(expected_output, actual_output)
8989

90+
def test_error_binary_data(self):
91+
input = b"payload"
92+
expected_output = {'error':
93+
{'message': 'can only concatenate str (not "bytes") to str',
94+
'error_type': 'AlgorithmError',
95+
'stacktrace': ''
96+
}
97+
}
98+
actual_output = json.loads(self.execute_without_load(input, apply_basic))
99+
actual_output["error"]["stacktrace"] = ''
100+
self.assertEqual(expected_output, actual_output)
101+
102+
def test_binary_data(self):
103+
input = b"payload"
104+
expected_output = {'metadata':
105+
{
106+
'content_type': 'binary'
107+
},
108+
'result': "aGVsbG8gcGF5bG9hZA=="
109+
}
110+
actual_output = json.loads(self.execute_without_load(input, apply_binary))
111+
self.assertEqual(expected_output, actual_output)
112+
90113

91114
def run_test():
92115
unittest.main()

tests/test_adk_remote.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44
import os
55
from adk import ADK
6+
import base64
67
from tests.adk_algorithms import *
78

89

@@ -123,7 +124,7 @@ def test_error_loading(self):
123124
input = {'content_type': 'json', 'data': 'Algorithmia'}
124125
expected_output = {'error':
125126
{'message': 'This exception was thrown in loading',
126-
'error_type': 'AlgorithmError',
127+
'error_type': 'LoadingError',
127128
'stacktrace': ''
128129
}
129130
}
@@ -134,6 +135,31 @@ def test_error_loading(self):
134135
actual_output["error"]["stacktrace"] = ''
135136
self.assertEqual(expected_output, actual_output)
136137

138+
def test_error_binary_data(self):
139+
input = {"content_type": "binary", "data": "cGF5bG9hZA=="}
140+
expected_output = {'error':
141+
{'message': 'can only concatenate str (not "bytes") to str',
142+
'error_type': 'AlgorithmError',
143+
'stacktrace': ''
144+
}
145+
}
146+
input = [str(json.dumps(input))]
147+
actual_output = self.execute_without_load(input, apply_basic)
148+
actual_output["error"]["stacktrace"] = ''
149+
self.assertEqual(expected_output, actual_output)
150+
151+
def test_binary_data(self):
152+
input = {"content_type": "binary", "data": "cGF5bG9hZA=="}
153+
expected_output = {'metadata':
154+
{
155+
'content_type': 'binary'
156+
},
157+
'result': "aGVsbG8gcGF5bG9hZA=="
158+
}
159+
input = [str(json.dumps(input))]
160+
actual_output = self.execute_without_load(input, apply_binary)
161+
self.assertEqual(expected_output, actual_output)
162+
137163

138164
def run_test():
139165
unittest.main()

0 commit comments

Comments
 (0)