Merge branch '2800-python-global-state' into 2800-pgs
[arvados.git] / sdk / python / tests / test_api.py
index cd7b1e9611f763ef06c3dedca920202a6478c5a7..4917d004ea706302785b9d0d5498c297cfb83c78 100644 (file)
@@ -1,10 +1,13 @@
 #!/usr/bin/env python
 
+import apiclient.errors
 import arvados
 import httplib2
 import json
 import mimetypes
 import unittest
+import os
+import run_test_server
 
 from apiclient.http import RequestMockBuilder
 from httplib import responses as HTTP_RESPONSES
@@ -13,35 +16,57 @@ if not mimetypes.inited:
     mimetypes.init()
 
 class ArvadosApiClientTest(unittest.TestCase):
+    @classmethod
+    def response_from_code(cls, code):
+        return httplib2.Response(
+            {'status': code,
+             'reason': HTTP_RESPONSES.get(code, "Unknown Response"),
+             'Content-Type': mimetypes.types_map['.json']})
+
+    @classmethod
+    def api_error_response(cls, code, *errors):
+        return (cls.response_from_code(code),
+                json.dumps({'errors': errors,
+                            'error_token': '1234567890+12345678'}))
+
     @classmethod
     def setUpClass(cls):
-        # The apiclient library has support for mocking requests for
-        # testing, but it doesn't extend to the discovery document
-        # itself.  Point it at a known stable discovery document for now.
-        # FIXME: Figure out a better way to stub this out.
-        cls.orig_api_host = arvados.config.get('ARVADOS_API_HOST')
-        arvados.config.settings()['ARVADOS_API_HOST'] = 'qr1hi.arvadosapi.com'
+        run_test_server.run()
         mock_responses = {
+            'arvados.humans.delete': (cls.response_from_code(500), ""),
+            'arvados.humans.get': cls.api_error_response(
+                422, "Bad UUID format", "Bad output format"),
             'arvados.humans.list': (None, json.dumps(
                     {'items_available': 0, 'items': []})),
             }
         req_builder = RequestMockBuilder(mock_responses)
-        cls.api = arvados.api('v1', False, requestBuilder=req_builder)
+        cls.api = arvados.api('v1', cache=False,
+                              host=os.environ['ARVADOS_API_HOST'],
+                              token='discovery-doc-only-no-token-needed',
+                              insecure=True,
+                              requestBuilder=req_builder)
 
     @classmethod
     def tearDownClass(cls):
-        if cls.orig_api_host is None:
-            del arvados.config.settings()['ARVADOS_API_HOST']
-        else:
-            arvados.config.settings()['ARVADOS_API_HOST'] = cls.orig_api_host
-        # Prevent other tests from using our mocked API client.
-        arvados.uncache_api('v1')
+        run_test_server.stop()
 
     def test_basic_list(self):
         answer = self.api.humans().list(
             filters=[['uuid', 'is', None]]).execute()
         self.assertEqual(answer['items_available'], len(answer['items']))
 
+    def test_exceptions_include_errors(self):
+        with self.assertRaises(apiclient.errors.HttpError) as err_ctx:
+            self.api.humans().get(uuid='xyz-xyz-abcdef').execute()
+        err_s = str(err_ctx.exception)
+        for msg in ["Bad UUID format", "Bad output format"]:
+            self.assertIn(msg, err_s)
+
+    def test_exceptions_without_errors_have_basic_info(self):
+        with self.assertRaises(apiclient.errors.HttpError) as err_ctx:
+            self.api.humans().delete(uuid='xyz-xyz-abcdef').execute()
+        self.assertIn("500", str(err_ctx.exception))
+
 
 if __name__ == '__main__':
     unittest.main()