@@ -50,14 +50,14 @@ def _make_one(self, *args, **kw):
50
50
def test_unpickleable (self ):
51
51
import pickle
52
52
53
- CREDENTIALS = _make_credentials ()
53
+ credentials = _make_credentials ()
54
54
HTTP = object ()
55
55
56
- client_obj = self ._make_one (credentials = CREDENTIALS , _http = HTTP )
56
+ client_obj = self ._make_one (credentials = credentials , _http = HTTP )
57
57
with self .assertRaises (pickle .PicklingError ):
58
58
pickle .dumps (client_obj )
59
59
60
- def test_constructor_defaults (self ):
60
+ def test_ctor_defaults (self ):
61
61
credentials = _make_credentials ()
62
62
63
63
patch = mock .patch ("google.auth.default" , return_value = (credentials , None ))
@@ -66,22 +66,81 @@ def test_constructor_defaults(self):
66
66
67
67
self .assertIs (client_obj ._credentials , credentials )
68
68
self .assertIsNone (client_obj ._http_internal )
69
- default .assert_called_once_with ()
69
+ default .assert_called_once_with (scopes = None )
70
70
71
- def test_constructor_explicit (self ):
71
+ def test_ctor_explicit (self ):
72
72
credentials = _make_credentials ()
73
73
http = mock .sentinel .http
74
74
client_obj = self ._make_one (credentials = credentials , _http = http )
75
75
76
76
self .assertIs (client_obj ._credentials , credentials )
77
77
self .assertIs (client_obj ._http_internal , http )
78
78
79
- def test_constructor_bad_credentials (self ):
79
+ def test_ctor_client_options_w_conflicting_creds (self ):
80
+ from google .api_core .exceptions import DuplicateCredentialArgs
81
+
82
+ credentials = _make_credentials ()
83
+ client_options = {'credentials_file' : '/path/to/creds.json' }
84
+ with self .assertRaises (DuplicateCredentialArgs ):
85
+ self ._make_one (credentials = credentials , client_options = client_options )
86
+
87
+ def test_ctor_bad_credentials (self ):
80
88
credentials = mock .sentinel .credentials
81
89
82
90
with self .assertRaises (ValueError ):
83
91
self ._make_one (credentials = credentials )
84
92
93
+ def test_ctor_client_options_w_creds_file_scopes (self ):
94
+ credentials = _make_credentials ()
95
+ credentials_file = '/path/to/creds.json'
96
+ scopes = ['SCOPE1' , 'SCOPE2' ]
97
+ client_options = {'credentials_file' : credentials_file , 'scopes' : scopes }
98
+
99
+ patch = mock .patch ("google.auth.load_credentials_from_file" , return_value = (credentials , None ))
100
+ with patch as load_credentials_from_file :
101
+ client_obj = self ._make_one (client_options = client_options )
102
+
103
+ self .assertIs (client_obj ._credentials , credentials )
104
+ self .assertIsNone (client_obj ._http_internal )
105
+ load_credentials_from_file .assert_called_once_with (credentials_file , scopes = scopes )
106
+
107
+ def test_ctor_client_options_w_quota_project (self ):
108
+ credentials = _make_credentials ()
109
+ quota_project_id = 'quota-project-123'
110
+ client_options = {'quota_project_id' : quota_project_id }
111
+
112
+ client_obj = self ._make_one (credentials = credentials , client_options = client_options )
113
+
114
+ self .assertIs (client_obj ._credentials , credentials .with_quota_project .return_value )
115
+ credentials .with_quota_project .assert_called_once_with (quota_project_id )
116
+
117
+ def test_ctor__http_property_existing (self ):
118
+ credentials = _make_credentials ()
119
+ http = object ()
120
+ client = self ._make_one (credentials = credentials , _http = http )
121
+ self .assertIs (client ._http_internal , http )
122
+ self .assertIs (client ._http , http )
123
+
124
+ def test_ctor__http_property_new (self ):
125
+ from google .cloud .client import _CREDENTIALS_REFRESH_TIMEOUT
126
+
127
+ credentials = _make_credentials ()
128
+ client = self ._make_one (credentials = credentials )
129
+ self .assertIsNone (client ._http_internal )
130
+
131
+ authorized_session_patch = mock .patch (
132
+ "google.auth.transport.requests.AuthorizedSession" ,
133
+ return_value = mock .sentinel .http ,
134
+ )
135
+ with authorized_session_patch as AuthorizedSession :
136
+ self .assertIs (client ._http , mock .sentinel .http )
137
+ # Check the mock.
138
+ AuthorizedSession .assert_called_once_with (credentials , refresh_timeout = _CREDENTIALS_REFRESH_TIMEOUT )
139
+ # Make sure the cached value is used on subsequent access.
140
+ self .assertIs (client ._http_internal , mock .sentinel .http )
141
+ self .assertIs (client ._http , mock .sentinel .http )
142
+ self .assertEqual (AuthorizedSession .call_count , 1 )
143
+
85
144
def test_from_service_account_json (self ):
86
145
from google .cloud import _helpers
87
146
@@ -114,32 +173,6 @@ def test_from_service_account_json_bad_args(self):
114
173
mock .sentinel .filename , credentials = mock .sentinel .credentials
115
174
)
116
175
117
- def test__http_property_existing (self ):
118
- credentials = _make_credentials ()
119
- http = object ()
120
- client = self ._make_one (credentials = credentials , _http = http )
121
- self .assertIs (client ._http_internal , http )
122
- self .assertIs (client ._http , http )
123
-
124
- def test__http_property_new (self ):
125
- from google .cloud .client import _CREDENTIALS_REFRESH_TIMEOUT
126
- credentials = _make_credentials ()
127
- client = self ._make_one (credentials = credentials )
128
- self .assertIsNone (client ._http_internal )
129
-
130
- authorized_session_patch = mock .patch (
131
- "google.auth.transport.requests.AuthorizedSession" ,
132
- return_value = mock .sentinel .http ,
133
- )
134
- with authorized_session_patch as AuthorizedSession :
135
- self .assertIs (client ._http , mock .sentinel .http )
136
- # Check the mock.
137
- AuthorizedSession .assert_called_once_with (credentials , refresh_timeout = _CREDENTIALS_REFRESH_TIMEOUT )
138
- # Make sure the cached value is used on subsequent access.
139
- self .assertIs (client ._http_internal , mock .sentinel .http )
140
- self .assertIs (client ._http , mock .sentinel .http )
141
- self .assertEqual (AuthorizedSession .call_count , 1 )
142
-
143
176
144
177
class TestClientWithProject (unittest .TestCase ):
145
178
@staticmethod
@@ -167,7 +200,7 @@ def test_constructor_defaults(self):
167
200
self .assertEqual (client_obj .project , project )
168
201
self .assertIs (client_obj ._credentials , credentials )
169
202
self .assertIsNone (client_obj ._http_internal )
170
- default .assert_called_once_with ()
203
+ default .assert_called_once_with (scopes = None )
171
204
_determine_default_project .assert_called_once_with (None )
172
205
173
206
def test_constructor_missing_project (self ):
0 commit comments