|
| 1 | +# Copyright 2018 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 4 | +# in compliance with the License. You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed under the License |
| 9 | +# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 10 | +# or implied. See the License for the specific language governing permissions and limitations under |
| 11 | +# the License. |
| 12 | + |
| 13 | +import unittest |
| 14 | +import mock |
| 15 | + |
| 16 | +import google.auth |
| 17 | +import google.datalab.utils |
| 18 | +from google.datalab.contrib.pipeline.composer._api import Api |
| 19 | + |
| 20 | + |
| 21 | +class TestCases(unittest.TestCase): |
| 22 | + |
| 23 | + TEST_PROJECT_ID = 'test_project' |
| 24 | + |
| 25 | + def validate(self, mock_http_request, expected_url, expected_args=None, expected_data=None, |
| 26 | + expected_headers=None, expected_method=None): |
| 27 | + url = mock_http_request.call_args[0][0] |
| 28 | + kwargs = mock_http_request.call_args[1] |
| 29 | + self.assertEquals(expected_url, url) |
| 30 | + if expected_args is not None: |
| 31 | + self.assertEquals(expected_args, kwargs['args']) |
| 32 | + else: |
| 33 | + self.assertNotIn('args', kwargs) |
| 34 | + if expected_data is not None: |
| 35 | + self.assertEquals(expected_data, kwargs['data']) |
| 36 | + else: |
| 37 | + self.assertNotIn('data', kwargs) |
| 38 | + if expected_headers is not None: |
| 39 | + self.assertEquals(expected_headers, kwargs['headers']) |
| 40 | + else: |
| 41 | + self.assertNotIn('headers', kwargs) |
| 42 | + if expected_method is not None: |
| 43 | + self.assertEquals(expected_method, kwargs['method']) |
| 44 | + else: |
| 45 | + self.assertNotIn('method', kwargs) |
| 46 | + |
| 47 | + @mock.patch('google.datalab.Context.default') |
| 48 | + @mock.patch('google.datalab.utils.Http.request') |
| 49 | + def test_environment_details_get(self, mock_http_request, mock_context_default): |
| 50 | + mock_context_default.return_value = TestCases._create_context() |
| 51 | + Api.get_environment_details('ZONE', 'ENVIRONMENT') |
| 52 | + self.validate(mock_http_request, |
| 53 | + 'https://composer.googleapis.com/v1alpha1/projects/test_project/locations/ZONE/' |
| 54 | + 'environments/ENVIRONMENT') |
| 55 | + |
| 56 | + @staticmethod |
| 57 | + def _create_context(): |
| 58 | + project_id = TestCases.TEST_PROJECT_ID |
| 59 | + creds = mock.Mock(spec=google.auth.credentials.Credentials) |
| 60 | + return google.datalab.Context(project_id, creds) |
0 commit comments