<commit_before>from unittest import mock

import ibis
import pydata_google_auth
import pytest
from google.auth import credentials as auth
from google.cloud import bigquery as bq

import ibis_bigquery

pytestmark = pytest.mark.bigquery

bq_backend = ibis_bigquery.Backend()


def test_repeated_project_name(project_id, credentials):
    con = bq_backend.connect(
        project_id=project_id,
        dataset_id='{}.testing'.format(project_id),
        credentials=credentials,
    )
    assert 'functional_alltypes' in con.list_tables()


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 = bq_backend.connect(project_id='explicit-project-id',)
    assert con.billing_project == 'explicit-project-id'


def test_without_dataset(project_id, credentials):
    con = bq_backend.connect(
        project_id=project_id, dataset_id=None, credentials=credentials,
    )
    with pytest.raises(ValueError, match="Unable to determine BigQuery"):
        con.list_tables()


def test_application_name_sets_user_agent(
    project_id, credentials, monkeypatch
):
    mock_client = mock.create_autospec(bq.Client)
    monkeypatch.setattr(bq, 'Client', mock_client)
    bq_backend.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


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)

    bq_backend.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 == bq_backend.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,
    )


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)

    bq_backend.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


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)

    bq_backend.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 == bq_backend.EXTERNAL_DATA_SCOPES


def test_auth_cache_reauth(project_id, credentials, monkeypatch):
    mock_calls = []

    def mock_default(*args, **kwargs):
        mock_calls.append((args, kwargs))<commit_msg>test: fix `AttributeError` in `test_connect.py` (#23)<commit_after>from unittest import mock

import ibis
import pydata_google_auth
import pytest
from google.auth import credentials as auth
from google.cloud import bigquery as bq

import ibis_bigquery

pytestmark = pytest.mark.bigquery

bq_backend = ibis_bigquery.Backend()


def test_repeated_project_name(project_id, credentials):
    con = bq_backend.connect(
        project_id=project_id,
        dataset_id='{}.testing'.format(project_id),
        credentials=credentials,
    )
    assert 'functional_alltypes' in con.list_tables()


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 = bq_backend.connect(project_id='explicit-project-id',)
    assert con.billing_project == 'explicit-project-id'


def test_without_dataset(project_id, credentials):
    con = bq_backend.connect(
        project_id=project_id, dataset_id=None, credentials=credentials,
    )
    with pytest.raises(ValueError, match="Unable to determine BigQuery"):
        con.list_tables()


def test_application_name_sets_user_agent(
    project_id, credentials, monkeypatch
):
    mock_client = mock.create_autospec(bq.Client)
    monkeypatch.setattr(bq, 'Client', mock_client)
    bq_backend.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


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)

    bq_backend.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,
    )


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)

    bq_backend.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


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)

    bq_backend.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 test_auth_cache_reauth(project_id, credentials, monkeypatch):
    mock_calls = []

    def mock_default(*args, **kwargs):
        mock_calls.append((args, kwargs))
{'fraction_non_alphanumeric': 0.09964547986832109, 'fraction_numerical': 0.004558115978728792, 'mean_word_length': 3.9962049335863377, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3924039', 'n_tokens_mistral': 2598, 'n_tokens_neox': 2562, 'n_words': 436}