Merge branch 'master' into 4823-python-sdk-writable-collection-api
[arvados.git] / sdk / python / tests / test_api.py
1 #!/usr/bin/env python
2
3 import arvados
4 import httplib2
5 import json
6 import mimetypes
7 import os
8 import run_test_server
9 import unittest
10 from apiclient import errors as apiclient_errors
11 from apiclient import http as apiclient_http
12
13 from arvados_testutil import fake_httplib2_response
14
15 if not mimetypes.inited:
16     mimetypes.init()
17
18 class ArvadosApiClientTest(unittest.TestCase):
19     ERROR_HEADERS = {'Content-Type': mimetypes.types_map['.json']}
20
21     @classmethod
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'}))
26
27     @classmethod
28     def setUpClass(cls):
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.
34         run_test_server.run()
35         mock_responses = {
36             'arvados.humans.delete': (
37                 fake_httplib2_response(500, **cls.ERROR_HEADERS),
38                 ""),
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': []})),
43             }
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',
48                               insecure=True,
49                               requestBuilder=req_builder)
50
51     @classmethod
52     def tearDownClass(cls):
53         run_test_server.stop()
54
55     def test_new_api_objects_with_cache(self):
56         clients = [arvados.api('v1', cache=True,
57                                host=os.environ['ARVADOS_API_HOST'],
58                                token='discovery-doc-only-no-token-needed',
59                                insecure=True)
60                    for index in [0, 1]]
61         self.assertIsNot(*clients)
62
63     def test_basic_list(self):
64         answer = self.api.humans().list(
65             filters=[['uuid', 'is', None]]).execute()
66         self.assertEqual(answer['items_available'], len(answer['items']))
67
68     def test_exceptions_include_errors(self):
69         with self.assertRaises(apiclient_errors.HttpError) as err_ctx:
70             self.api.humans().get(uuid='xyz-xyz-abcdef').execute()
71         err_s = str(err_ctx.exception)
72         for msg in ["Bad UUID format", "Bad output format"]:
73             self.assertIn(msg, err_s)
74
75     def test_exceptions_without_errors_have_basic_info(self):
76         with self.assertRaises(apiclient_errors.HttpError) as err_ctx:
77             self.api.humans().delete(uuid='xyz-xyz-abcdef').execute()
78         self.assertIn("500", str(err_ctx.exception))
79
80
81 if __name__ == '__main__':
82     unittest.main()