Skip to content

Commit 6cc14c5

Browse files
committed
BigQuery user creds sample: add tests.
Mocks out user credentials using the Application Default Credentials, but uses the same scopes.
1 parent 5e1d96c commit 6cc14c5

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

bigquery/cloud-client/client_secrets.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

bigquery/cloud-client/user_credentials.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import time
2525
import uuid
2626

27-
from google_auth_oauthlib import flow
2827
from google.cloud import bigquery
28+
from google_auth_oauthlib import flow
2929

3030

3131
def wait_for_job(job):
@@ -62,10 +62,10 @@ def run_query(credentials, project, query):
6262
break
6363

6464

65-
def auth_query(project, query, launch_browser=True):
65+
def authenticate_and_query(project, query, launch_browser=True):
6666
appflow = flow.InstalledAppFlow.from_client_secrets_file(
67-
'client_secrets.json',
68-
scopes=['https://www.googleapis.com/auth/bigquery'])
67+
'client_secrets.json',
68+
scopes=['https://www.googleapis.com/auth/bigquery'])
6969

7070
if launch_browser:
7171
appflow.run_local_server()
@@ -88,5 +88,5 @@ def auth_query(project, query, launch_browser=True):
8888

8989
args = parser.parse_args()
9090

91-
auth_query(args.project, args.query, launch_browser=args.launch_browser)
92-
91+
authenticate_and_query(
92+
args.project, args.query, launch_browser=args.launch_browser)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2017 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import google.auth
18+
import mock
19+
import pytest
20+
21+
from user_credentials import authenticate_and_query
22+
23+
24+
PROJECT = os.environ['GCLOUD_PROJECT']
25+
26+
27+
@pytest.fixture
28+
def mock_flow():
29+
flow_patch = mock.patch(
30+
'google_auth_oauthlib.flow.InstalledAppFlow', autospec=True)
31+
32+
with flow_patch as flow_mock:
33+
flow_mock.from_client_secrets_file.return_value = flow_mock
34+
flow_mock.credentials = google.auth.default()[0]
35+
yield flow_mock
36+
37+
38+
def test_auth_query_console(mock_flow, capsys):
39+
authenticate_and_query(PROJECT, 'SELECT 1+1;', launch_browser=False)
40+
out, _ = capsys.readouterr()
41+
assert '2' in out

0 commit comments

Comments
 (0)