12 from apiclient import errors as apiclient_errors
13 from apiclient import http as apiclient_http
14 from arvados.api import OrderedJsonModel
16 from arvados_testutil import fake_httplib2_response
18 if not mimetypes.inited:
21 class ArvadosApiClientTest(unittest.TestCase):
22 ERROR_HEADERS = {'Content-Type': mimetypes.types_map['.json']}
25 def api_error_response(cls, code, *errors):
26 return (fake_httplib2_response(code, **cls.ERROR_HEADERS),
27 json.dumps({'errors': errors,
28 'error_token': '1234567890+12345678'}))
32 # The apiclient library has support for mocking requests for
33 # testing, but it doesn't extend to the discovery document
34 # itself. For now, bring up an API server that will serve
35 # a discovery document.
36 # FIXME: Figure out a better way to stub this out.
39 'arvados.humans.delete': (
40 fake_httplib2_response(500, **cls.ERROR_HEADERS),
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 = apiclient_http.RequestMockBuilder(mock_responses)
48 cls.api = arvados.api('v1',
49 host=os.environ['ARVADOS_API_HOST'],
50 token='discovery-doc-only-no-token-needed',
52 requestBuilder=req_builder)
55 run_test_server.reset()
57 def test_new_api_objects_with_cache(self):
58 clients = [arvados.api('v1', cache=True,
59 host=os.environ['ARVADOS_API_HOST'],
60 token='discovery-doc-only-no-token-needed',
63 self.assertIsNot(*clients)
65 def test_empty_list(self):
66 answer = arvados.api('v1').humans().list(
67 filters=[['uuid', 'is', None]]).execute()
68 self.assertEqual(answer['items_available'], len(answer['items']))
70 def test_nonempty_list(self):
71 answer = arvados.api('v1').collections().list().execute()
72 self.assertNotEqual(0, answer['items_available'])
73 self.assertNotEqual(0, len(answer['items']))
75 def test_timestamp_inequality_filter(self):
76 api = arvados.api('v1')
77 new_item = api.specimens().create(body={}).execute()
78 for operator, should_include in [
79 ['<', False], ['>', False],
80 ['<=', True], ['>=', True], ['=', True]]:
81 response = api.specimens().list(filters=[
82 ['created_at', operator, new_item['created_at']],
83 # Also filter by uuid to ensure (if it matches) it's on page 0
84 ['uuid', '=', new_item['uuid']]]).execute()
85 uuids = [item['uuid'] for item in response['items']]
86 did_include = new_item['uuid'] in uuids
88 did_include, should_include,
89 "'%s %s' filter should%s have matched '%s'" % (
90 operator, new_item['created_at'],
91 ('' if should_include else ' not'),
92 new_item['created_at']))
94 def test_exceptions_include_errors(self):
95 with self.assertRaises(apiclient_errors.HttpError) as err_ctx:
96 self.api.humans().get(uuid='xyz-xyz-abcdef').execute()
97 err_s = str(err_ctx.exception)
98 for msg in ["Bad UUID format", "Bad output format"]:
99 self.assertIn(msg, err_s)
101 def test_exceptions_without_errors_have_basic_info(self):
102 with self.assertRaises(apiclient_errors.HttpError) as err_ctx:
103 self.api.humans().delete(uuid='xyz-xyz-abcdef').execute()
104 self.assertIn("500", str(err_ctx.exception))
106 def test_request_too_large(self):
107 api = arvados.api('v1')
108 maxsize = api._rootDesc.get('maxRequestSize', 0)
109 with self.assertRaises(apiclient_errors.MediaUploadSizeError):
111 arvados.api('v1').collections().create(body={"manifest_text": text}).execute()
113 def test_ordered_json_model(self):
115 'arvados.humans.get': (None, json.dumps(collections.OrderedDict(
116 (c, int(c, 16)) for c in string.hexdigits))),
118 req_builder = apiclient_http.RequestMockBuilder(mock_responses)
119 api = arvados.api('v1',
120 host=os.environ['ARVADOS_API_HOST'],
121 token='discovery-doc-only-no-token-needed',
123 requestBuilder=req_builder,
124 model=OrderedJsonModel())
125 result = api.humans().get(uuid='test').execute()
126 self.assertEqual(string.hexdigits, ''.join(result.keys()))
129 if __name__ == '__main__':