def test_encode(content_type): encoder = Mock() with patch.dict(_encoders._encoders_map, {content_type: encoder}, clear=True): _encoders.encode(42, content_type) encoder.assert_called_once_with(42)
def test_request(content_type_header): headers = { content_type_header: _content_types.JSON, "Accept": _content_types.CSV } request = _worker.Request(test.environ(data="42", headers=headers)) assert request.content_type == _content_types.JSON assert request.accept == _content_types.CSV assert request.content == "42" headers = { content_type_header: _content_types.NPY, "Accept": _content_types.CSV } request = _worker.Request( test.environ(data=_encoders.encode([6, 9.3], _content_types.NPY), headers=headers)) assert request.content_type == _content_types.NPY assert request.accept == _content_types.CSV result = _encoders.decode(request.data, _content_types.NPY) np.testing.assert_array_equal(result, np.array([6, 9.3]))
def default_output_fn(prediction, accept): """Function responsible to serialize the prediction for the response. Args: prediction (obj): prediction returned by predict_fn . accept (str): accept content-type expected by the client. Returns: (worker.Response): a Flask response object with the following args: * Args: response: the serialized data to return accept: the content-type that the data was transformed to. """ return _worker.Response(_encoders.encode(prediction, accept), accept)
def test_request(): request = test.request(data='42') assert request.content_type == _content_types.JSON assert request.accept == _content_types.JSON assert request.content == '42' request = test.request(data=_encoders.encode([6, 9.3], _content_types.NPY), content_type=_content_types.NPY, accept=_content_types.CSV) assert request.content_type == _content_types.NPY assert request.accept == _content_types.CSV result = _encoders.decode(request.data, _content_types.NPY) np.testing.assert_array_equal(result, np.array([6, 9.3]))
def test_encode_error(): with pytest.raises(_errors.UnsupportedFormatError): _encoders.encode(42, _content_types.OCTET_STREAM)