-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_adk_remote.py
More file actions
181 lines (159 loc) · 6.29 KB
/
Copy pathtest_adk_remote.py
File metadata and controls
181 lines (159 loc) · 6.29 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
import sys
import json
import unittest
import os
from tests.AdkTest import ADKTest
import base64
from tests.adk_algorithms import *
class RemoteTest(unittest.TestCase):
if os.name == "posix":
fifo_pipe_path = "/tmp/algoout"
fifo_pipe = None
def setUp(self):
try:
os.mkfifo(self.fifo_pipe_path)
except Exception:
pass
def tearDown(self):
if os.name == "posix":
os.remove(self.fifo_pipe_path)
def read_in(self):
if os.name == "posix":
return self.read_from_pipe()
if os.name == "nt":
return self.read_from_stdin()
def read_from_stdin(self):
return json.loads(sys.stdin)
def read_from_pipe(self):
read_obj = os.read(self.fifo_pipe, 10000)
if isinstance(read_obj, bytes):
read_obj = read_obj.decode("utf-8")
actual_output = json.loads(read_obj)
os.close(self.fifo_pipe)
return actual_output
def open_pipe(self):
if os.name == "posix":
self.fifo_pipe = os.open(self.fifo_pipe_path, os.O_RDONLY | os.O_NONBLOCK)
def execute_example(self, input, apply, load=None):
self.open_pipe()
algo = ADKTest(apply, load)
sys.stdin = input
algo.init()
output = self.read_in()
return output
def execute_without_load(self, input, apply):
self.open_pipe()
algo = ADKTest(apply)
sys.stdin = input
algo.init()
output = self.read_in()
return output
def execute_manifest_example(self, input, apply, load, manifest_path):
client = Algorithmia.client()
self.open_pipe()
algo = ADKTest(apply, load, manifest_path=manifest_path, client=client)
sys.stdin = input
algo.init()
output = self.read_in()
return output
# ----- Tests ----- #
def test_basic(self):
input = {'content_type': 'json', 'data': 'Algorithmia'}
expected_output = {"metadata":
{
"content_type": "text"
},
"result": "hello Algorithmia"
}
input = [str(json.dumps(input))]
actual_output = self.execute_example(input, apply_input_or_context)
self.assertEqual(expected_output, actual_output)
def test_basic_2(self):
input = {'content_type': 'json', 'data': 'Algorithmia'}
expected_output = {"metadata":
{
"content_type": "text"
},
"result": "hello Algorithmia"
}
input = [str(json.dumps(input))]
actual_output = self.execute_without_load(input, apply_basic)
self.assertEqual(expected_output, actual_output)
def test_algorithm_loading_basic(self):
input = {'content_type': 'json', 'data': 'ignore me'}
expected_output = {'metadata':
{
'content_type': 'json'
},
'result': {'message': 'This message was loaded prior to runtime'}
}
input = [str(json.dumps(input))]
actual_output = self.execute_example(input, apply_input_or_context, loading_text)
self.assertEqual(expected_output, actual_output)
def test_algorithm_loading_algorithmia(self):
input = {'content_type': 'json', 'data': 'ignore me'}
expected_output = {'metadata':
{
'content_type': 'json'
},
'result': {
'data_url': 'data://demo/collection/somefile.json',
'data': {'foo': 'bar'}
}
}
input = [str(json.dumps(input))]
actual_output = self.execute_example(input, apply_input_or_context, loading_file_from_algorithmia)
self.assertEqual(expected_output, actual_output)
def test_error_loading(self):
input = {'content_type': 'json', 'data': 'Algorithmia'}
expected_output = {'error':
{'message': 'This exception was thrown in loading',
'error_type': 'LoadingError',
'stacktrace': ''
}
}
input = [str(json.dumps(input))]
actual_output = self.execute_example(input, apply_input_or_context, loading_exception)
# beacuse the stacktrace is local path specific,
# we're going to assume it's setup correctly and remove it from our equality check
actual_output["error"]["stacktrace"] = ''
self.assertEqual(expected_output, actual_output)
def test_error_binary_data(self):
input = {"content_type": "binary", "data": "cGF5bG9hZA=="}
expected_output = {'error':
{'message': 'can only concatenate str (not "bytes") to str',
'error_type': 'AlgorithmError',
'stacktrace': ''
}
}
input = [str(json.dumps(input))]
actual_output = self.execute_without_load(input, apply_basic)
actual_output["error"]["stacktrace"] = ''
self.assertEqual(expected_output, actual_output)
def test_binary_data(self):
input = {"content_type": "binary", "data": "cGF5bG9hZA=="}
expected_output = {'metadata':
{
'content_type': 'binary'
},
'result': "aGVsbG8gcGF5bG9hZA=="
}
input = [str(json.dumps(input))]
actual_output = self.execute_without_load(input, apply_binary)
self.assertEqual(expected_output, actual_output)
def test_manifest_file_success(self):
input = {'content_type': 'json', 'data': 'Algorithmia'}
expected_output = {'metadata':
{
'content_type': 'text'
},
'result': "all model files were successfully loaded"
}
input = [str(json.dumps(input))]
actual_output = self.execute_manifest_example(input, apply_successful_manifest_parsing,
loading_with_manifest,
manifest_path="tests/manifests/good_model_manifest"
".json")
self.assertEqual(expected_output, actual_output)
def run_test():
unittest.main()