Skip to content

Commit ab2051d

Browse files
authored
fix: use project_id property from service account credentials (#120)
* fix: use `project_id` property from service account credentials * fix typo in test * more test typos
1 parent 29c7fb0 commit ab2051d

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

pybigquery/_helpers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ def create_bigquery_client(
3838
credentials_path
3939
)
4040
credentials = credentials.with_scopes(SCOPES)
41-
default_project = credentials.project
41+
default_project = credentials.project_id
4242
elif credentials_info:
4343
credentials = service_account.Credentials.from_service_account_info(
4444
credentials_info
4545
)
4646
credentials = credentials.with_scopes(SCOPES)
47-
default_project = credentials.project
47+
default_project = credentials.project_id
4848
else:
4949
credentials, default_project = google.auth.default(scopes=SCOPES)
5050

tests/system/test_helpers.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright 2021 The PyBigQuery Authors
2+
#
3+
# Use of this source code is governed by an MIT-style
4+
# license that can be found in the LICENSE file or at
5+
# https://opensource.org/licenses/MIT.
6+
7+
import os
8+
import json
9+
10+
import pytest
11+
12+
13+
@pytest.fixture(scope="session")
14+
def module_under_test():
15+
from pybigquery import _helpers
16+
17+
return _helpers
18+
19+
20+
@pytest.fixture
21+
def credentials_path():
22+
if "GOOGLE_APPLICATION_CREDENTIALS" not in os.environ:
23+
pytest.skip("GOOGLE_APPLICATION_CREDENTIALS must be set")
24+
return os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
25+
26+
27+
@pytest.fixture
28+
def credentials_info(credentials_path):
29+
with open(credentials_path) as credentials_file:
30+
return json.load(credentials_file)
31+
32+
33+
def test_create_bigquery_client_with_credentials_path(
34+
module_under_test, credentials_path, credentials_info
35+
):
36+
bqclient = module_under_test.create_bigquery_client(
37+
credentials_path=credentials_path
38+
)
39+
assert bqclient.project == credentials_info["project_id"]
40+
41+
42+
def test_create_bigquery_client_with_credentials_path_respects_project(
43+
module_under_test, credentials_path
44+
):
45+
"""Test that project_id is used, even when there is a default project.
46+
47+
https://github.com/googleapis/python-bigquery-sqlalchemy/issues/48
48+
"""
49+
bqclient = module_under_test.create_bigquery_client(
50+
credentials_path=credentials_path, project_id="connection-url-project",
51+
)
52+
assert bqclient.project == "connection-url-project"
53+
54+
55+
def test_create_bigquery_client_with_credentials_info(
56+
module_under_test, credentials_info
57+
):
58+
bqclient = module_under_test.create_bigquery_client(
59+
credentials_info=credentials_info
60+
)
61+
assert bqclient.project == credentials_info["project_id"]
62+
63+
64+
def test_create_bigquery_client_with_credentials_info_respects_project(
65+
module_under_test, credentials_info
66+
):
67+
"""Test that project_id is used, even when there is a default project.
68+
69+
https://github.com/googleapis/python-bigquery-sqlalchemy/issues/48
70+
"""
71+
bqclient = module_under_test.create_bigquery_client(
72+
credentials_info=credentials_info, project_id="connection-url-project",
73+
)
74+
assert bqclient.project == "connection-url-project"

tests/unit/test_helpers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class AnonymousCredentialsWithProject(google.auth.credentials.AnonymousCredentia
1717

1818
def __init__(self, project):
1919
super().__init__()
20-
self.project = project
20+
self.project_id = project
2121

2222
def with_scopes(self, scopes):
2323
return self

0 commit comments

Comments
 (0)