Exemple #1
0
def test_auth_default(project_id, credentials, monkeypatch):
    mock_calls = []

    def mock_default(*args, **kwargs):
        mock_calls.append((args, kwargs))
        return credentials, project_id

    monkeypatch.setattr(pydata_google_auth, "default", mock_default)

    ibis_bigquery.connect(
        project_id=project_id,
        dataset_id='bigquery-public-data.stackoverflow',
    )

    assert len(mock_calls) == 1
    args, kwargs = mock_calls[0]
    assert len(args) == 1
    scopes = args[0]
    assert scopes == ibis_bigquery.SCOPES
    auth_local_webserver = kwargs["use_local_webserver"]
    auth_cache = kwargs["credentials_cache"]
    assert not auth_local_webserver
    assert isinstance(
        auth_cache,
        pydata_google_auth.cache.ReadWriteCredentialsCache,
    )
Exemple #2
0
def test_auth_cache_unknown(project_id):
    with pytest.raises(ValueError, match="unexpected value for auth_cache"):
        ibis_bigquery.connect(
            project_id=project_id,
            dataset_id="bigquery-public-data.stackoverflow",
            auth_cache="not_a_real_cache",
        )
Exemple #3
0
def test_application_name_sets_user_agent(project_id, credentials,
                                          monkeypatch):
    mock_client = mock.create_autospec(bq.Client)
    monkeypatch.setattr(bq, 'Client', mock_client)
    ibis_bigquery.connect(
        project_id=project_id,
        dataset_id='bigquery-public-data.stackoverflow',
        application_name='my-great-app/0.7.0',
        credentials=credentials,
    )
    info = mock_client.call_args[1]['client_info']
    user_agent = info.to_user_agent()
    assert ' ibis/{}'.format(ibis.__version__) in user_agent
    assert 'my-great-app/0.7.0 ' in user_agent
Exemple #4
0
def test_application_name_sets_user_agent(project_id, credentials,
                                          monkeypatch):
    mock_client = mock.create_autospec(bq.Client)
    monkeypatch.setattr(bq, "Client", mock_client)
    ibis_bigquery.connect(
        project_id=project_id,
        dataset_id="bigquery-public-data.stackoverflow",
        application_name="my-great-app/0.7.0",
        credentials=credentials,
    )
    info = mock_client.call_args[1]["client_info"]
    user_agent = info.to_user_agent()
    assert " ibis/{}".format(ibis.__version__) in user_agent
    assert "my-great-app/0.7.0 " in user_agent
Exemple #5
0
def test_repeated_project_name(project_id, credentials):
    con = ibis_bigquery.connect(
        project_id=project_id,
        dataset_id='{}.testing'.format(project_id),
        credentials=credentials,
    )
    assert 'functional_alltypes' in con.list_tables()
Exemple #6
0
def test_without_dataset(project_id, credentials):
    con = ibis_bigquery.connect(
        project_id=project_id,
        credentials=credentials,
    )
    with pytest.raises(ValueError, match="Unable to determine BigQuery"):
        con.list_tables()
Exemple #7
0
def test_project_id_different_from_default_credentials(monkeypatch):
    creds = mock.create_autospec(auth.Credentials)

    def mock_credentials(*args, **kwargs):
        return creds, 'default-project-id'

    monkeypatch.setattr(pydata_google_auth, 'default', mock_credentials)
    con = ibis_bigquery.connect(project_id='explicit-project-id', )
    assert con.billing_project == 'explicit-project-id'
Exemple #8
0
def test_auth_local_webserver(project_id, credentials, monkeypatch):
    mock_calls = []

    def mock_default(*args, **kwargs):
        mock_calls.append((args, kwargs))
        return credentials, project_id

    monkeypatch.setattr(pydata_google_auth, "default", mock_default)

    ibis_bigquery.connect(
        project_id=project_id,
        dataset_id='bigquery-public-data.stackoverflow',
        auth_local_webserver=True,
    )

    assert len(mock_calls) == 1
    _, kwargs = mock_calls[0]
    auth_local_webserver = kwargs["use_local_webserver"]
    assert auth_local_webserver
Exemple #9
0
def test_auth_cache_none(project_id, credentials, monkeypatch):
    mock_calls = []

    def mock_default(*args, **kwargs):
        mock_calls.append((args, kwargs))
        return credentials, project_id

    monkeypatch.setattr(pydata_google_auth, "default", mock_default)

    ibis_bigquery.connect(
        project_id=project_id,
        dataset_id="bigquery-public-data.stackoverflow",
        auth_cache="none",
    )

    assert len(mock_calls) == 1
    _, kwargs = mock_calls[0]
    auth_cache = kwargs["credentials_cache"]
    assert auth_cache is pydata_google_auth.cache.NOOP
Exemple #10
0
def test_auth_external_data(project_id, credentials, monkeypatch):
    mock_calls = []

    def mock_default(*args, **kwargs):
        mock_calls.append((args, kwargs))
        return credentials, project_id

    monkeypatch.setattr(pydata_google_auth, "default", mock_default)

    ibis_bigquery.connect(
        project_id=project_id,
        dataset_id='bigquery-public-data.stackoverflow',
        auth_external_data=True,
    )

    assert len(mock_calls) == 1
    args, _ = mock_calls[0]
    assert len(args) == 1
    scopes = args[0]
    assert scopes == ibis_bigquery.EXTERNAL_DATA_SCOPES
def get_bigquery_client(project_id, dataset_id=None, credentials=None):
    info = client_info.get_http_client_info()
    google_client = bigquery.Client(project=project_id,
                                    client_info=info,
                                    credentials=credentials)
    ibis_client = ibis_bigquery.connect(project_id,
                                        dataset_id=dataset_id,
                                        credentials=credentials)

    # Override the BigQuery client object to ensure the correct user agent is
    # included.
    ibis_client.client = google_client
    return ibis_client
    def connect(data_directory: pathlib.Path) -> ibis_bigquery.Backend:
        """Connect to the test database."""
        credentials, default_project_id = google.auth.default(
            scopes=ibis_bigquery.EXTERNAL_DATA_SCOPES)

        project_id = os.getenv(PROJECT_ID_ENV_VAR)
        if project_id is None:
            project_id = default_project_id
        if project_id is None:
            project_id = DEFAULT_PROJECT_ID

        return ibis_bigquery.connect(
            project_id=project_id,
            dataset_id=f"{project_id}.{DATASET_ID}",
            credentials=credentials,
        )
Exemple #13
0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

YOUR_PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT']

# [START bigquery_ibis_connect]
import ibis
import ibis_bigquery

conn = ibis_bigquery.connect(project_id=YOUR_PROJECT_ID,
                             dataset_id='bigquery-public-data.stackoverflow')
# [END bigquery_ibis_connect]

# [START bigquery_ibis_table]
table = conn.table('posts_questions')
print(table)
# BigQueryTable[table]
# name: bigquery-public-data.stackoverflow.posts_questions
# schema:
#   id : int64
#   title : string
#   body : string
#   accepted_answer_id : int64
#   answer_count : int64
#   comment_count : int64
#   community_owned_date : timestamp
Exemple #14
0
def bigquery_client():
    return ibis_bigquery.connect()