10 from apiclient import errors as apiclient_errors
11 from apiclient import http as apiclient_http
13 from arvados_testutil import fake_httplib2_response
15 if not mimetypes.inited:
18 class ArvadosApiClientTest(unittest.TestCase):
19 ERROR_HEADERS = {'Content-Type': mimetypes.types_map['.json']}
22 def api_error_response(cls, code, *errors):
23 return (fake_httplib2_response(code, **cls.ERROR_HEADERS),
24 json.dumps({'errors': errors,
25 'error_token': '1234567890+12345678'}))
29 # The apiclient library has support for mocking requests for
30 # testing, but it doesn't extend to the discovery document
31 # itself. For now, bring up an API server that will serve
32 # a discovery document.
33 # FIXME: Figure out a better way to stub this out.
36 'arvados.humans.delete': (
37 fake_httplib2_response(500, **cls.ERROR_HEADERS),
39 'arvados.humans.get': cls.api_error_response(
40 422, "Bad UUID format", "Bad output format"),
41 'arvados.humans.list': (None, json.dumps(
42 {'items_available': 0, 'items': []})),
44 req_builder = apiclient_http.RequestMockBuilder(mock_responses)
45 cls.api = arvados.api('v1',
46 host=os.environ['ARVADOS_API_HOST'],
47 token='discovery-doc-only-no-token-needed',
49 requestBuilder=req_builder)
52 run_test_server.reset()
54 def test_new_api_objects_with_cache(self):
55 clients = [arvados.api('v1', cache=True,
56 host=os.environ['ARVADOS_API_HOST'],
57 token='discovery-doc-only-no-token-needed',
60 self.assertIsNot(*clients)
62 def test_empty_list(self):
63 answer = arvados.api('v1').humans().list(
64 filters=[['uuid', 'is', None]]).execute()
65 self.assertEqual(answer['items_available'], len(answer['items']))
67 def test_nonempty_list(self):
68 answer = arvados.api('v1').collections().list().execute()
69 self.assertNotEqual(0, answer['items_available'])
70 self.assertNotEqual(0, len(answer['items']))
72 def test_timestamp_inequality_filter(self):
73 api = arvados.api('v1')
74 new_item = api.specimens().create(body={}).execute()
75 for operator, should_include in [
76 ['<', False], ['>', False],
77 ['<=', True], ['>=', True], ['=', True]]:
78 response = api.specimens().list(filters=[
79 ['created_at', operator, new_item['created_at']],
80 # Also filter by uuid to ensure (if it matches) it's on page 0
81 ['uuid', '=', new_item['uuid']]]).execute()
82 uuids = [item['uuid'] for item in response['items']]
83 did_include = new_item['uuid'] in uuids
85 did_include, should_include,
86 "'%s %s' filter should%s have matched '%s'" % (
87 operator, new_item['created_at'],
88 ('' if should_include else ' not'),
89 new_item['created_at']))
91 def test_exceptions_include_errors(self):
92 with self.assertRaises(apiclient_errors.HttpError) as err_ctx:
93 self.api.humans().get(uuid='xyz-xyz-abcdef').execute()
94 err_s = str(err_ctx.exception)
95 for msg in ["Bad UUID format", "Bad output format"]:
96 self.assertIn(msg, err_s)
98 def test_exceptions_without_errors_have_basic_info(self):
99 with self.assertRaises(apiclient_errors.HttpError) as err_ctx:
100 self.api.humans().delete(uuid='xyz-xyz-abcdef').execute()
101 self.assertIn("500", str(err_ctx.exception))
103 def test_request_too_large(self):
104 api = arvados.api('v1')
105 maxsize = api._rootDesc.get('maxRequestSize', 0)
106 with self.assertRaises(apiclient_errors.MediaUploadSizeError):
108 arvados.api('v1').collections().create(body={"manifest_text": text}).execute()
111 if __name__ == '__main__':