forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
213 lines (157 loc) · 6.95 KB
/
Copy pathtest_client.py
File metadata and controls
213 lines (157 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Copyright 2015 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import mock
def _make_credentials():
import google.auth.credentials
return mock.Mock(spec=google.auth.credentials.Credentials)
class Test_ClientFactoryMixin(unittest.TestCase):
@staticmethod
def _get_target_class():
from google.cloud.client import _ClientFactoryMixin
return _ClientFactoryMixin
def test_virtual(self):
klass = self._get_target_class()
self.assertFalse('__init__' in klass.__dict__)
class TestClient(unittest.TestCase):
@staticmethod
def _get_target_class():
from google.cloud.client import Client
return Client
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)
def test_ctor_defaults(self):
from google.cloud._testing import _Monkey
from google.cloud import client
CREDENTIALS = _make_credentials()
FUNC_CALLS = []
def mock_get_credentials():
FUNC_CALLS.append('get_credentials')
return CREDENTIALS
with _Monkey(client, get_credentials=mock_get_credentials):
client_obj = self._make_one()
self.assertIs(client_obj._credentials, CREDENTIALS)
self.assertIsNone(client_obj._http_internal)
self.assertEqual(FUNC_CALLS, ['get_credentials'])
def test_ctor_explicit(self):
CREDENTIALS = _make_credentials()
HTTP = object()
client_obj = self._make_one(credentials=CREDENTIALS, http=HTTP)
self.assertIs(client_obj._credentials, CREDENTIALS)
self.assertIs(client_obj._http_internal, HTTP)
def test_ctor_bad_credentials(self):
CREDENTIALS = object()
with self.assertRaises(ValueError):
self._make_one(credentials=CREDENTIALS)
def test_from_service_account_json(self):
KLASS = self._get_target_class()
constructor_patch = mock.patch(
'google.oauth2.service_account.Credentials.'
'from_service_account_file',
return_value=_make_credentials())
with constructor_patch as constructor:
client_obj = KLASS.from_service_account_json(
mock.sentinel.filename)
self.assertIs(
client_obj._credentials, constructor.return_value)
self.assertIsNone(client_obj._http_internal)
constructor.assert_called_once_with(mock.sentinel.filename)
def test_from_service_account_json_bad_args(self):
KLASS = self._get_target_class()
with self.assertRaises(TypeError):
KLASS.from_service_account_json(
mock.sentinel.filename, credentials=mock.sentinel.credentials)
def test__http_property_existing(self):
credentials = _make_credentials()
http = object()
client = self._make_one(credentials=credentials, http=http)
self.assertIs(client._http_internal, http)
self.assertIs(client._http, http)
def test__http_property_new(self):
credentials = _make_credentials()
client = self._make_one(credentials=credentials)
self.assertIsNone(client._http_internal)
patch = mock.patch('google_auth_httplib2.AuthorizedHttp',
return_value=mock.sentinel.http)
with patch as mocked:
self.assertIs(client._http, mock.sentinel.http)
# Check the mock.
mocked.assert_called_once_with(credentials)
self.assertEqual(mocked.call_count, 1)
# Make sure the cached value is used on subsequent access.
self.assertIs(client._http_internal, mock.sentinel.http)
self.assertIs(client._http, mock.sentinel.http)
self.assertEqual(mocked.call_count, 1)
class TestClientWithProject(unittest.TestCase):
@staticmethod
def _get_target_class():
from google.cloud.client import ClientWithProject
return ClientWithProject
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)
def test_ctor_defaults(self):
from google.cloud._testing import _Monkey
from google.cloud import client
PROJECT = 'PROJECT'
CREDENTIALS = _make_credentials()
FUNC_CALLS = []
def mock_determine_proj(project):
FUNC_CALLS.append((project, '_determine_default_project'))
return PROJECT
def mock_get_credentials():
FUNC_CALLS.append('get_credentials')
return CREDENTIALS
with _Monkey(client, get_credentials=mock_get_credentials,
_determine_default_project=mock_determine_proj):
client_obj = self._make_one()
self.assertEqual(client_obj.project, PROJECT)
self.assertIs(client_obj._credentials, CREDENTIALS)
self.assertIsNone(client_obj._http_internal)
self.assertEqual(
FUNC_CALLS,
[(None, '_determine_default_project'), 'get_credentials'])
def test_ctor_missing_project(self):
from google.cloud._testing import _Monkey
from google.cloud import client
FUNC_CALLS = []
def mock_determine_proj(project):
FUNC_CALLS.append((project, '_determine_default_project'))
return None
with _Monkey(client, _determine_default_project=mock_determine_proj):
self.assertRaises(EnvironmentError, self._make_one)
self.assertEqual(FUNC_CALLS, [(None, '_determine_default_project')])
def test_ctor_w_invalid_project(self):
CREDENTIALS = _make_credentials()
HTTP = object()
with self.assertRaises(ValueError):
self._make_one(project=object(), credentials=CREDENTIALS,
http=HTTP)
def _explicit_ctor_helper(self, project):
import six
CREDENTIALS = _make_credentials()
HTTP = object()
client_obj = self._make_one(project=project, credentials=CREDENTIALS,
http=HTTP)
if isinstance(project, six.binary_type):
self.assertEqual(client_obj.project, project.decode('utf-8'))
else:
self.assertEqual(client_obj.project, project)
self.assertIs(client_obj._credentials, CREDENTIALS)
self.assertIs(client_obj._http_internal, HTTP)
def test_ctor_explicit_bytes(self):
PROJECT = b'PROJECT'
self._explicit_ctor_helper(PROJECT)
def test_ctor_explicit_unicode(self):
PROJECT = u'PROJECT'
self._explicit_ctor_helper(PROJECT)