3 import apiclient.errors
12 from apiclient.http import RequestMockBuilder
13 from httplib import responses as HTTP_RESPONSES
15 if not mimetypes.inited:
18 class ArvadosApiClientTest(unittest.TestCase):
20 def response_from_code(cls, code):
21 return httplib2.Response(
23 'reason': HTTP_RESPONSES.get(code, "Unknown Response"),
24 'Content-Type': mimetypes.types_map['.json']})
27 def api_error_response(cls, code, *errors):
28 return (cls.response_from_code(code),
29 json.dumps({'errors': errors,
30 'error_token': '1234567890+12345678'}))
34 # The apiclient library has support for mocking requests for
35 # testing, but it doesn't extend to the discovery document
36 # itself. For now, bring up an API server that will serve
37 # a discovery document.
38 # FIXME: Figure out a better way to stub this out.
41 'arvados.humans.delete': (cls.response_from_code(500), ""),
42 'arvados.humans.get': cls.api_error_response(
43 422, "Bad UUID format", "Bad output format"),
44 'arvados.humans.list': (None, json.dumps(
45 {'items_available': 0, 'items': []})),
47 req_builder = RequestMockBuilder(mock_responses)
48 cls.api = arvados.api('v1', cache=False,
49 host=os.environ['ARVADOS_API_HOST'],
50 token='discovery-doc-only-no-token-needed',
52 requestBuilder=req_builder)
55 def tearDownClass(cls):
56 run_test_server.stop()
58 def test_basic_list(self):
59 answer = self.api.humans().list(
60 filters=[['uuid', 'is', None]]).execute()
61 self.assertEqual(answer['items_available'], len(answer['items']))
63 def test_exceptions_include_errors(self):
64 with self.assertRaises(apiclient.errors.HttpError) as err_ctx:
65 self.api.humans().get(uuid='xyz-xyz-abcdef').execute()
66 err_s = str(err_ctx.exception)
67 for msg in ["Bad UUID format", "Bad output format"]:
68 self.assertIn(msg, err_s)
70 def test_exceptions_without_errors_have_basic_info(self):
71 with self.assertRaises(apiclient.errors.HttpError) as err_ctx:
72 self.api.humans().delete(uuid='xyz-xyz-abcdef').execute()
73 self.assertIn("500", str(err_ctx.exception))
76 if __name__ == '__main__':