Skip to content

Commit e9c0d92

Browse files
committed
CLN: share _test_imports between main module and tests
1 parent c690d6a commit e9c0d92

File tree

2 files changed

+29
-105
lines changed

2 files changed

+29
-105
lines changed

pandas_gbq/gbq.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ def _check_google_client_version():
3030

3131
if (StrictVersion(_GOOGLE_API_CLIENT_VERSION) <
3232
StrictVersion(google_api_minimum_version)):
33-
raise ImportError("pandas requires google-api-python-client >= {0} "
34-
"for Google BigQuery support, "
35-
"current version {1}"
33+
raise ImportError('pandas requires google-api-python-client >= {0} '
34+
'for Google BigQuery support, '
35+
'current version {1}'
3636
.format(google_api_minimum_version,
3737
_GOOGLE_API_CLIENT_VERSION))
3838

@@ -41,26 +41,40 @@ def _test_google_api_imports():
4141

4242
try:
4343
import httplib2 # noqa
44+
except ImportError as ex:
45+
raise ImportError(
46+
'pandas requires httplib2 for Google BigQuery support: '
47+
'{0}'.format(ex))
48+
49+
try:
4450
from google_auth_oauthlib.flow import InstalledAppFlow # noqa
45-
import google_auth_httplib2 # noqa
4651
except ImportError as ex:
47-
raise ImportError("Missing module required for Google BigQuery "
48-
"support: {0}".format(str(ex)))
52+
raise ImportError(
53+
'pandas requires google-auth-oauthlib for Google BigQuery '
54+
'support: {0}'.format(ex))
55+
56+
try:
57+
from google_auth_httplib2 import AuthorizedHttp # noqa
58+
from google_auth_httplib2 import Request # noqa
59+
except ImportError:
60+
raise ImportError(
61+
'pandas requires google-auth-httplib2 for Google BigQuery '
62+
'support: {0}'.format(ex))
4963

5064
try:
5165
from googleapiclient.discovery import build # noqa
5266
from googleapiclient.errors import HttpError # noqa
5367
except ImportError as ex:
5468
raise ImportError(
55-
"pandas requires google-api-python-client for Google BigQuery "
56-
"support: {0}".format(str(ex)))
69+
"pandas requires google-api-python-client for Google BigQuery "
70+
"support: {0}".format(ex))
5771

5872
try:
5973
import google.auth # noqa
6074
except ImportError as ex:
6175
raise ImportError(
62-
"pandas requires google-auth for Google BigQuery support: "
63-
"{0}".format(str(ex)))
76+
"pandas requires google-auth for Google BigQuery support: "
77+
"{0}".format(ex))
6478

6579
_check_google_client_version()
6680

@@ -76,8 +90,8 @@ def _try_credentials(project_id, credentials):
7690

7791
http = httplib2.Http()
7892
try:
79-
http = AuthorizedHttp(credentials, http=http)
80-
bigquery_service = build('bigquery', 'v2', http=http)
93+
authed_http = AuthorizedHttp(credentials, http=http)
94+
bigquery_service = build('bigquery', 'v2', http=authed_http)
8195
# Check if the application has rights to the BigQuery project
8296
jobs = bigquery_service.jobs()
8397
job_data = {'configuration': {'query': {'query': 'SELECT 1'}}}
@@ -413,9 +427,9 @@ def get_service(self):
413427
from googleapiclient.discovery import build
414428

415429
http = httplib2.Http()
416-
http = AuthorizedHttp(
430+
authed_http = AuthorizedHttp(
417431
self.credentials, http=http)
418-
bigquery_service = build('bigquery', 'v2', http=http)
432+
bigquery_service = build('bigquery', 'v2', http=authed_http)
419433

420434
return bigquery_service
421435

pandas_gbq/tests/test_gbq.py

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@
2323
TABLE_ID = 'new_test'
2424

2525

26-
_IMPORTS = False
27-
_GOOGLE_API_CLIENT_INSTALLED = False
28-
_GOOGLE_API_CLIENT_VALID_VERSION = False
29-
_HTTPLIB2_INSTALLED = False
30-
_GOOGLE_AUTH_INSTALLED = False
31-
_GOOGLE_AUTH_HTTPLIB2_INSTALLED = False
32-
_GOOGLE_AUTH_OAUTHLIB_INSTALLED = False
33-
_SETUPTOOLS_INSTALLED = False
34-
35-
3626
def _skip_if_no_project_id():
3727
if not _get_project_id():
3828
pytest.skip(
@@ -87,92 +77,12 @@ def _get_private_key_contents():
8777

8878

8979
def _test_imports():
90-
global _GOOGLE_API_CLIENT_INSTALLED, _GOOGLE_API_CLIENT_VALID_VERSION, \
91-
_GOOGLE_AUTH_INSTALLED, _GOOGLE_AUTH_HTTPLIB2_INSTALLED, \
92-
_GOOGLE_AUTH_OAUTHLIB_INSTALLED, _HTTPLIB2_INSTALLED, \
93-
_SETUPTOOLS_INSTALLED
94-
9580
try:
9681
import pkg_resources
97-
_SETUPTOOLS_INSTALLED = True
9882
except ImportError:
99-
_SETUPTOOLS_INSTALLED = False
100-
101-
google_api_minimum_version = '1.6.0'
102-
103-
if _SETUPTOOLS_INSTALLED:
104-
try:
105-
from googleapiclient.discovery import build # noqa
106-
from googleapiclient.errors import HttpError # noqa
107-
108-
_GOOGLE_API_CLIENT_INSTALLED = True
109-
_GOOGLE_API_CLIENT_VERSION = pkg_resources.get_distribution(
110-
'google-api-python-client').version
111-
112-
if (StrictVersion(_GOOGLE_API_CLIENT_VERSION) >=
113-
StrictVersion(google_api_minimum_version)):
114-
_GOOGLE_API_CLIENT_VALID_VERSION = True
115-
116-
except ImportError:
117-
_GOOGLE_API_CLIENT_INSTALLED = False
118-
119-
try:
120-
from google.auth import default # noqa
121-
from google.auth.exceptions import DefaultCredentialsError # noqa
122-
from google.oauth2.credentials import Credentials # noqa
123-
from google.oauth2.service_account import Credentials # noqa
124-
_GOOGLE_AUTH_INSTALLED = True
125-
except ImportError:
126-
_GOOGLE_AUTH_INSTALLED = False
127-
128-
try:
129-
from google_auth_httplib2 import AuthorizedHttp # noqa
130-
from google_auth_httplib2 import Request # noqa
131-
_GOOGLE_AUTH_HTTPLIB2_INSTALLED = True
132-
except ImportError:
133-
_GOOGLE_AUTH_HTTPLIB2_INSTALLED = False
134-
135-
try:
136-
from google_auth_oauthlib.flow import InstalledAppFlow # noqa
137-
from oauthlib.oauth2.rfc6749.errors import OAuth2Error # noqa
138-
_GOOGLE_AUTH_OAUTHLIB_INSTALLED = True
139-
except ImportError:
140-
_GOOGLE_AUTH_OAUTHLIB_INSTALLED = False
141-
142-
try:
143-
import httplib2 # noqa
144-
_HTTPLIB2_INSTALLED = True
145-
except ImportError:
146-
_HTTPLIB2_INSTALLED = False
147-
148-
if not _SETUPTOOLS_INSTALLED:
14983
raise ImportError('Could not import pkg_resources (setuptools).')
15084

151-
if not _GOOGLE_API_CLIENT_INSTALLED:
152-
raise ImportError('Could not import Google API Client.')
153-
154-
if not _GOOGLE_API_CLIENT_VALID_VERSION:
155-
raise ImportError('pandas requires google-api-python-client >= {0} '
156-
'for Google BigQuery support, '
157-
'current version {1}'
158-
.format(google_api_minimum_version,
159-
_GOOGLE_API_CLIENT_VERSION))
160-
161-
if not _GOOGLE_AUTH_INSTALLED:
162-
raise ImportError(
163-
'pandas requires google-auth for Google BigQuery support')
164-
165-
if not _GOOGLE_AUTH_HTTPLIB2_INSTALLED:
166-
raise ImportError(
167-
'pandas requires google-auth-httplib2 for Google BigQuery support')
168-
169-
if not _GOOGLE_AUTH_OAUTHLIB_INSTALLED:
170-
raise ImportError(
171-
'pandas requires google-auth-oauthlib for Google BigQuery support')
172-
173-
if not _HTTPLIB2_INSTALLED:
174-
raise ImportError(
175-
'pandas requires httplib2 for Google BigQuery support')
85+
gbq._test_google_api_imports()
17686

17787

17888
def _setup_common():

0 commit comments

Comments
 (0)