generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathtest_encode_decode.py
More file actions
269 lines (192 loc) · 9.11 KB
/
test_encode_decode.py
File metadata and controls
269 lines (192 loc) · 9.11 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
260
261
262
263
264
265
266
267
268
269
import unittest
import csv
import numpy as np
from io import StringIO
from unittest.mock import Mock, patch
from djl_python.inputs import Input
from djl_python.outputs import Output
from djl_python.encode_decode import decode, encode, decode_csv, encode_csv
class TestEncodeDecode(unittest.TestCase):
def setUp(self):
self.mock_input = Mock(spec=Input)
self.mock_output = Mock(spec=Output)
self.mock_output.add_as_string = Mock()
self.mock_output.add_as_json = Mock()
self.mock_output.add_as_numpy = Mock()
self.mock_output.add_as_npz = Mock()
self.mock_output.add_property = Mock()
def test_decode_csv_valid_inputs_header(self):
csv_content = "inputs,other\ntest input 1,value1\ntest input 2,value2"
self.mock_input.get_as_string.return_value = csv_content
result = decode_csv(self.mock_input)
expected = {"inputs": ["test input 1", "test input 2"]}
self.assertEqual(result, expected)
def test_decode_csv_valid_question_header(self):
csv_content = "question,context\nWhat is AI?,Technology context\nWhat is ML?,Machine learning context"
self.mock_input.get_as_string.return_value = csv_content
result = decode_csv(self.mock_input)
expected = {
"inputs": [{
"question": "What is AI?",
"context": "Technology context"
}, {
"question": "What is ML?",
"context": "Machine learning context"
}]
}
self.assertEqual(result, expected)
def test_decode_csv_invalid_header(self):
csv_content = "invalid,header\nvalue1,value2"
self.mock_input.get_as_string.return_value = csv_content
with self.assertRaises(ValueError) as context:
decode_csv(self.mock_input)
self.assertIn("correct CSV with Header columns",
str(context.exception))
def test_encode_csv_list_of_dicts(self):
content = [{
"name": "Axl",
"age": "30"
}, {
"name": "Fiona",
"age": "25"
}]
result = encode_csv(content)
reader = csv.DictReader(StringIO(result))
rows = list(reader)
self.assertEqual(len(rows), 2)
self.assertEqual(rows[0]["name"], "Axl")
self.assertEqual(rows[1]["name"], "Fiona")
def test_encode_csv_single_dict(self):
content = [{"name": "Axl", "age": "30"}]
result = encode_csv(content)
reader = csv.DictReader(StringIO(result))
rows = list(reader)
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0]["name"], "Axl")
def test_decode_no_content_type(self):
self.mock_input.get_as_bytes.return_value = None
result = decode(self.mock_input, None)
expected = {"inputs": ""}
self.assertEqual(result, expected)
def test_decode_no_content_type_with_json(self):
test_data = {"test": "data"}
self.mock_input.get_as_bytes.return_value = b'{"test": "data"}'
self.mock_input.get_as_json.return_value = test_data
result = decode(self.mock_input, None)
self.assertEqual(result, test_data)
def test_decode_application_json(self):
test_data = {"message": "hello"}
self.mock_input.get_as_json.return_value = test_data
result = decode(self.mock_input, "application/json")
self.assertEqual(result, test_data)
def test_decode_text_csv(self):
csv_content = "inputs\ntest input"
self.mock_input.get_as_string.return_value = csv_content
with patch('djl_python.encode_decode.decode_csv') as mock_decode_csv:
mock_decode_csv.return_value = {"inputs": ["test input"]}
result = decode(self.mock_input, "text/csv")
mock_decode_csv.assert_called_once_with(self.mock_input,
require_headers=True)
self.assertEqual(result, {"inputs": ["test input"]})
def test_decode_text_plain(self):
text_content = "Hello world"
self.mock_input.get_as_string.return_value = text_content
result = decode(self.mock_input, "text/plain")
expected = {"inputs": ["Hello world"]}
self.assertEqual(result, expected)
def test_decode_image_content_type(self):
image_data = b"fake_image_data"
self.mock_input.get_as_image.return_value = image_data
result = decode(self.mock_input, "image/jpeg")
expected = {"inputs": image_data}
self.assertEqual(result, expected)
def test_decode_audio_content_type(self):
audio_data = b"fake_audio_data"
self.mock_input.get_as_bytes.return_value = audio_data
result = decode(self.mock_input, "audio/wav")
expected = {"inputs": audio_data}
self.assertEqual(result, expected)
def test_decode_tensor_npz(self):
tensor_data = [np.array([1, 2, 3])]
self.mock_input.get_as_npz.return_value = tensor_data
result = decode(self.mock_input, "tensor/npz")
expected = {"inputs": tensor_data}
self.assertEqual(result, expected)
def test_decode_tensor_ndlist(self):
tensor_data = [np.array([1, 2, 3])]
self.mock_input.get_as_numpy.return_value = tensor_data
result = decode(self.mock_input, "tensor/ndlist")
expected = {"inputs": tensor_data}
self.assertEqual(result, expected)
def test_decode_application_x_npy(self):
tensor_data = [np.array([1, 2, 3])]
self.mock_input.get_as_numpy.return_value = tensor_data
result = decode(self.mock_input, "application/x-npy")
expected = {"inputs": tensor_data}
self.assertEqual(result, expected)
def test_decode_form_urlencoded(self):
form_data = "key1=value1&key2=value2"
self.mock_input.get_as_string.return_value = form_data
result = decode(self.mock_input, "application/x-www-form-urlencoded")
expected = {"inputs": form_data}
self.assertEqual(result, expected)
def test_decode_octet_stream(self):
binary_data = b"binary_data"
self.mock_input.get_as_bytes.return_value = binary_data
result = decode(self.mock_input, "application/octet-stream")
expected = {"inputs": binary_data}
self.assertEqual(result, expected)
def test_decode_with_key(self):
test_data = {"test": "data"}
self.mock_input.get_as_json.return_value = test_data
result = decode(self.mock_input, "application/json", key="test_key")
self.mock_input.get_as_json.assert_called_with(key="test_key")
self.assertEqual(result, test_data)
def test_encode_default_json(self):
prediction = {"result": "success"}
encode(self.mock_output, prediction, None)
self.mock_output.add_as_json.assert_called_once_with(prediction,
key=None)
self.mock_output.add_property.assert_called_once_with(
"Content-Type", "application/json")
def test_encode_application_json(self):
prediction = {"result": "success"}
encode(self.mock_output, prediction, "application/json")
self.mock_output.add_as_json.assert_called_once_with(prediction,
key=None)
self.mock_output.add_property.assert_called_once_with(
"Content-Type", "application/json")
def test_encode_text_csv(self):
prediction = [{"name": "Axl", "age": "30"}]
with patch('djl_python.encode_decode.encode_csv') as mock_encode_csv:
mock_encode_csv.return_value = "name,age\nAxl,30\n"
encode(self.mock_output, prediction, "text/csv")
mock_encode_csv.assert_called_once_with(prediction)
self.mock_output.add_as_string.assert_called_once_with(
"name,age\nAxl,30\n", key=None)
self.mock_output.add_property.assert_called_once_with(
"Content-Type", "text/csv")
def test_encode_tensor_npz(self):
prediction = [np.array([1, 2, 3])]
encode(self.mock_output, prediction, "tensor/npz")
self.mock_output.add_as_npz.assert_called_once_with(prediction,
key=None)
self.mock_output.add_property.assert_called_once_with(
"Content-Type", "tensor/npz")
def test_encode_other_content_type(self):
prediction = [np.array([1, 2, 3])]
encode(self.mock_output, prediction, "custom/type")
self.mock_output.add_as_numpy.assert_called_once_with(prediction,
key=None)
self.mock_output.add_property.assert_called_once_with(
"Content-Type", "tensor/ndlist")
def test_encode_with_key(self):
prediction = {"result": "success"}
encode(self.mock_output,
prediction,
"application/json",
key="test_key")
self.mock_output.add_as_json.assert_called_once_with(prediction,
key="test_key")
if __name__ == '__main__':
unittest.main()