def test_send_message_json_request_error(mock_send): """RequestError exception from send_message is handled.""" svc = OnapService() mock_send.side_effect = RequestError with pytest.raises(RequestError) as exc: svc.send_message_json("GET", 'test get', 'http://my.url/') assert exc.type is RequestError mock_send.assert_called_once()
def test_send_message_json_resource_not_found(mock_send): """ResourceNotFound exception from send_message is handled.""" svc = OnapService() mock_send.side_effect = ResourceNotFound with pytest.raises(ResourceNotFound) as exc: svc.send_message_json("GET", 'test get', 'http://my.url/') assert exc.type is ResourceNotFound mock_send.assert_called_once()
def test_send_message_json_api_error(mock_send): """APIError (error codes) from send_message is handled.""" svc = OnapService() mock_send.side_effect = APIError with pytest.raises(APIError) as exc: svc.send_message_json("GET", 'test get', 'http://my.url/') assert exc.type is APIError mock_send.assert_called_once()
def test_send_message_json_connection_failed(mock_send): """ConnectionFailed from send_message is handled.""" svc = OnapService() mock_send.side_effect = ConnectionFailed with pytest.raises(ConnectionFailed) as exc: svc.send_message_json("GET", 'test get', 'http://my.url/') assert exc.type is ConnectionFailed mock_send.assert_called_once()
def test_send_message_json_invalid_response(mock_send): """Raises InvalidResponse if response is not JSON.""" svc = OnapService() mocked_response = Response() mocked_response._content = b'{yolo}' mocked_response.encoding = "UTF-8" mocked_response.status_code = 200 mock_send.return_value = mocked_response with pytest.raises(InvalidResponse) as exc: svc.send_message_json("GET", 'test get', 'http://my.url/') assert exc.type is InvalidResponse mock_send.assert_called_once()
def test_send_message_json_OK(mock_send): svc = OnapService() mocked_response = Response() mocked_response._content = b'{"yolo": "yala"}' mocked_response.encoding = "UTF-8" mocked_response.status_code = 200 mock_send.return_value = mocked_response response = svc.send_message_json("GET", 'test get', 'http://my.url/') mock_send.assert_called_once_with("GET", 'test get', 'http://my.url/') assert response['yolo'] == 'yala'
def test_send_message_json_bad_json_no_exception(mock_send): svc = OnapService() mocked_response = Response() mocked_response._content = b'yolo' mocked_response.encoding = "UTF-8" mocked_response.status_code = 200 mock_send.return_value = mocked_response response = svc.send_message_json("GET", 'test get', 'http://my.url/') mock_send.assert_called_once_with("GET", 'test get', 'http://my.url/') assert response == {}
def test_send_message_json_bad_send_exception(mock_send): with pytest.raises(Timeout): svc = OnapService() mock_send.side_effect = Timeout response = svc.send_message_json("GET", 'test get', 'http://my.url/', exception=Timeout) mock_send.assert_called_once_with("GET", 'test get', 'http://my.url/', exception=Timeout)
def test_send_message_json_bad_send_no_exception(mock_send): svc = OnapService() mock_send.return_value = None response = svc.send_message_json("GET", 'test get', 'http://my.url/') mock_send.assert_called_once_with("GET", 'test get', 'http://my.url/') assert response == {}