|
| 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" |
0 commit comments