Skip to content

Commit b8ca74f

Browse files
committed
ENH: Adding schema to GBQConnector
ENH: Schema outputs a list of dicts Fixing Flake8
1 parent eb22b17 commit b8ca74f

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

pandas_gbq/gbq.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,9 @@ def load_data(self, dataframe, dataset_id, table_id, chunksize):
558558

559559
self._print("\n")
560560

561-
def verify_schema(self, dataset_id, table_id, schema):
561+
def schema(self, dataset_id, table_id):
562+
"""Retrieve the schema of the table"""
563+
562564
try:
563565
from googleapiclient.errors import HttpError
564566
except:
@@ -574,15 +576,18 @@ def verify_schema(self, dataset_id, table_id, schema):
574576
'type': field_remote['type']}
575577
for field_remote in remote_schema['fields']]
576578

577-
fields_remote = set([json.dumps(field_remote)
578-
for field_remote in remote_fields])
579-
fields_local = set(json.dumps(field_local)
580-
for field_local in schema['fields'])
581-
582-
return fields_remote == fields_local
579+
return remote_fields
583580
except HttpError as ex:
584581
self.process_http_error(ex)
585582

583+
def verify_schema(self, dataset_id, table_id, schema):
584+
fields_remote = set([json.dumps(field)
585+
for field in self.schema(dataset_id, table_id)])
586+
fields_local = set(json.dumps(field_local)
587+
for field_local in schema['fields'])
588+
589+
return fields_remote == fields_local
590+
586591
def delete_and_recreate_table(self, dataset_id, table_id, table_schema):
587592
delay = 0
588593

pandas_gbq/tests/test_gbq.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
import pandas.util.testing as tm
2020
from pandas.compat.numpy import np_datetime64_compat
2121

22-
PROJECT_ID = None
23-
PRIVATE_KEY_JSON_PATH = None
24-
PRIVATE_KEY_JSON_CONTENTS = None
22+
PROJECT_ID = os.environ.get('PROJECT_ID')
23+
PRIVATE_KEY_JSON_PATH = 'client_secrets.json'
24+
PRIVATE_KEY_JSON_CONTENTS = os.environ.get('SERVICE_ACCOUNT_KEY')
25+
# PROJECT_ID = None
26+
# PRIVATE_KEY_JSON_PATH = None
27+
# PRIVATE_KEY_JSON_CONTENTS = None
2528

2629
TABLE_ID = 'new_test'
2730

@@ -309,6 +312,7 @@ def test_get_application_default_credentials_returns_credentials(self):
309312

310313

311314
class TestGBQConnectorIntegrationWithServiceAccountKeyPath(tm.TestCase):
315+
312316
def setUp(self):
313317
_setup_common()
314318

@@ -340,6 +344,7 @@ def test_should_be_able_to_get_results_from_query(self):
340344

341345

342346
class TestGBQConnectorIntegrationWithServiceAccountKeyContents(tm.TestCase):
347+
343348
def setUp(self):
344349
_setup_common()
345350

@@ -1275,6 +1280,18 @@ def test_verify_schema_ignores_field_mode(self):
12751280
self.dataset_prefix + "1", TABLE_ID + test_id, test_schema_2),
12761281
'Expected schema to match')
12771282

1283+
def test_retrieve_schema(self):
1284+
test_id = "15"
1285+
test_schema = {'fields': [{'name': 'A', 'type': 'FLOAT'},
1286+
{'name': 'B', 'type': 'FLOAT'},
1287+
{'name': 'C', 'type': 'STRING'},
1288+
{'name': 'D', 'type': 'TIMESTAMP'}]}
1289+
1290+
self.table.create(TABLE_ID + test_id, test_schema)
1291+
actual = self.sut.schema(self.dataset_prefix + "1", TABLE_ID + test_id)
1292+
expected = test_schema['fields']
1293+
assert expected == actual, 'Expected schema used to create table'
1294+
12781295
def test_list_dataset(self):
12791296
dataset_id = self.dataset_prefix + "1"
12801297
self.assertTrue(dataset_id in self.dataset.datasets(),

0 commit comments

Comments
 (0)