2
2
3
3
import mock
4
4
import six
5
- from nose .tools import raises
6
-
7
- from apiclient .errors import HttpError
8
5
from bigquery import client
9
6
from bigquery .errors import (
10
7
JobInsertException , JobExecutingException ,
11
8
BigQueryTimeoutException
12
9
)
10
+ from googleapiclient .errors import HttpError
11
+ from nose .tools import raises
13
12
14
13
15
14
class HttpResponse (object ):
16
-
17
15
def __init__ (self , status , reason = 'There was an error' ):
18
16
"""
19
17
Args:
@@ -24,7 +22,6 @@ def __init__(self, status, reason='There was an error'):
24
22
25
23
26
24
class TestGetClient (unittest .TestCase ):
27
-
28
25
def setUp (self ):
29
26
client ._bq_client = None
30
27
@@ -51,7 +48,7 @@ def test_initialize_readonly(self, mock_build, mock_return_cred):
51
48
mock_cred = mock .Mock ()
52
49
mock_http = mock .Mock ()
53
50
mock_service_url = mock .Mock ()
54
- mock_cred .return_value .authorize .return_value = mock_http
51
+ mock_cred .from_p12_keyfile_buffer . return_value .authorize .return_value = mock_http
55
52
mock_bq = mock .Mock ()
56
53
mock_build .return_value = mock_bq
57
54
key = 'key'
@@ -65,9 +62,11 @@ def test_initialize_readonly(self, mock_build, mock_return_cred):
65
62
readonly = True )
66
63
67
64
mock_return_cred .assert_called_once_with ()
68
- mock_cred .assert_called_once_with (service_account , key ,
69
- scope = BIGQUERY_SCOPE_READ_ONLY )
70
- self .assertTrue (mock_cred .return_value .authorize .called )
65
+ mock_cred .from_p12_keyfile_buffer .assert_called_once_with (
66
+ service_account , mock .ANY ,
67
+ scopes = BIGQUERY_SCOPE_READ_ONLY )
68
+ self .assertTrue (
69
+ mock_cred .from_p12_keyfile_buffer .return_value .authorize .called )
71
70
mock_build .assert_called_once_with ('bigquery' , 'v2' , http = mock_http ,
72
71
discoveryServiceUrl = mock_service_url )
73
72
self .assertEquals (mock_bq , bq_client .bigquery )
@@ -84,7 +83,7 @@ def test_initialize_read_write(self, mock_build, mock_return_cred):
84
83
mock_cred = mock .Mock ()
85
84
mock_http = mock .Mock ()
86
85
mock_service_url = mock .Mock ()
87
- mock_cred .return_value .authorize .return_value = mock_http
86
+ mock_cred .from_p12_keyfile_buffer . return_value .authorize .return_value = mock_http
88
87
mock_bq = mock .Mock ()
89
88
mock_build .return_value = mock_bq
90
89
key = 'key'
@@ -98,19 +97,18 @@ def test_initialize_read_write(self, mock_build, mock_return_cred):
98
97
readonly = False )
99
98
100
99
mock_return_cred .assert_called_once_with ()
101
- mock_cred .assert_called_once_with (service_account , key ,
102
- scope = BIGQUERY_SCOPE )
103
- self .assertTrue (mock_cred .return_value .authorize .called )
100
+ mock_cred .from_p12_keyfile_buffer .assert_called_once_with (
101
+ service_account , mock .ANY , scopes = BIGQUERY_SCOPE )
102
+ self .assertTrue (
103
+ mock_cred .from_p12_keyfile_buffer .return_value .authorize .called )
104
104
mock_build .assert_called_once_with ('bigquery' , 'v2' , http = mock_http ,
105
105
discoveryServiceUrl = mock_service_url )
106
106
self .assertEquals (mock_bq , bq_client .bigquery )
107
107
self .assertEquals (project_id , bq_client .project_id )
108
108
109
109
@mock .patch ('bigquery.client._credentials' )
110
110
@mock .patch ('bigquery.client.build' )
111
- @mock .patch ('__builtin__.open' if six .PY2 else 'builtins.open' )
112
- def test_initialize_key_file (self , mock_open , mock_build ,
113
- mock_return_cred ):
111
+ def test_initialize_key_file (self , mock_build , mock_return_cred ):
114
112
"""Ensure that a BigQueryClient is initialized and returned with
115
113
read/write permissions using a private key file.
116
114
"""
@@ -119,12 +117,10 @@ def test_initialize_key_file(self, mock_open, mock_build,
119
117
mock_cred = mock .Mock ()
120
118
mock_http = mock .Mock ()
121
119
mock_service_url = mock .Mock ()
122
- mock_cred .return_value .authorize .return_value = mock_http
120
+ mock_cred .from_p12_keyfile . return_value .authorize .return_value = mock_http
123
121
mock_bq = mock .Mock ()
124
122
mock_build .return_value = mock_bq
125
123
key_file = 'key.pem'
126
- key = 'key'
127
- mock_open .return_value .__enter__ .return_value .read .return_value = key
128
124
service_account = 'account'
129
125
project_id = 'project'
130
126
mock_return_cred .return_value = mock_cred
@@ -134,46 +130,46 @@ def test_initialize_key_file(self, mock_open, mock_build,
134
130
service_account = service_account ,
135
131
private_key_file = key_file , readonly = False )
136
132
137
- mock_open .assert_called_once_with (key_file , 'rb' )
138
133
mock_return_cred .assert_called_once_with ()
139
- mock_cred .assert_called_once_with (service_account , key ,
140
- scope = BIGQUERY_SCOPE )
141
- self .assertTrue (mock_cred .return_value .authorize .called )
134
+ mock_cred .from_p12_keyfile .assert_called_once_with (service_account ,
135
+ key_file ,
136
+ scopes = BIGQUERY_SCOPE )
137
+ self .assertTrue (
138
+ mock_cred .from_p12_keyfile .return_value .authorize .called )
142
139
mock_build .assert_called_once_with ('bigquery' , 'v2' , http = mock_http ,
143
140
discoveryServiceUrl = mock_service_url )
144
141
self .assertEquals (mock_bq , bq_client .bigquery )
145
142
self .assertEquals (project_id , bq_client .project_id )
146
143
147
144
@mock .patch ('bigquery.client._credentials' )
148
145
@mock .patch ('bigquery.client.build' )
149
- @mock .patch ('__builtin__.open' if six .PY2 else 'builtins.open' )
150
- def test_initialize_json_key_file (self , mock_open , mock_build , mock_return_cred ):
146
+ def test_initialize_json_key_file (self , mock_build , mock_return_cred ):
151
147
"""Ensure that a BigQueryClient is initialized and returned with
152
148
read/write permissions using a JSON key file.
153
149
"""
154
150
from bigquery .client import BIGQUERY_SCOPE
155
- import json
156
151
157
152
mock_cred = mock .Mock ()
158
153
mock_http = mock .Mock ()
159
154
mock_service_url = mock .Mock ()
160
- mock_cred .return_value .authorize .return_value = mock_http
155
+ mock_cred .from_json_keyfile_name . return_value .authorize .return_value = mock_http
161
156
mock_bq = mock .Mock ()
162
157
mock_build .return_value = mock_bq
163
158
json_key_file = 'key.json'
164
- json_key = {'client_email' : 'mail' , 'private_key' : 'pkey' }
165
- mock_open .return_value .__enter__ .return_value .read .return_value = json .dumps (json_key )
166
159
project_id = 'project'
167
160
mock_return_cred .return_value = mock_cred
168
161
169
162
bq_client = client .get_client (
170
- project_id , service_url = mock_service_url , json_key_file = json_key_file , readonly = False )
163
+ project_id , service_url = mock_service_url ,
164
+ json_key_file = json_key_file , readonly = False )
171
165
172
- mock_open .assert_called_once_with (json_key_file , 'r' )
173
166
mock_return_cred .assert_called_once_with ()
174
- mock_cred .assert_called_once_with (json_key ['client_email' ], json_key ['private_key' ], scope = BIGQUERY_SCOPE )
175
- self .assertTrue (mock_cred .return_value .authorize .called )
176
- mock_build .assert_called_once_with ('bigquery' , 'v2' , http = mock_http , discoveryServiceUrl = mock_service_url )
167
+ mock_cred .from_json_keyfile_name .assert_called_once_with (json_key_file ,
168
+ scopes = BIGQUERY_SCOPE )
169
+ self .assertTrue (
170
+ mock_cred .from_json_keyfile_name .return_value .authorize .called )
171
+ mock_build .assert_called_once_with ('bigquery' , 'v2' , http = mock_http ,
172
+ discoveryServiceUrl = mock_service_url )
177
173
self .assertEquals (mock_bq , bq_client .bigquery )
178
174
self .assertEquals (project_id , bq_client .project_id )
179
175
0 commit comments