Skip to content
Prev Previous commit
Next Next commit
Add 'Dataset.list_tables' API method.
  • Loading branch information
tseaver committed Aug 7, 2015
commit e409492842094bf541777b455ad27305b78696b8
37 changes: 37 additions & 0 deletions gcloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,43 @@ def delete(self, client=None):
client = self._require_client(client)
client.connection.api_request(method='DELETE', path=self.path)

def list_tables(self, max_results=None, page_token=None):
"""List tables for the project associated with this client.

See:
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/tables/list

This comment was marked as spam.

This comment was marked as spam.

:type max_results: int
:param max_results: maximum number of tables to return, If not
passed, defaults to a value set by the API.

:type page_token: string
:param page_token: opaque marker for the next "page" of datasets. If
not passed, the API will return the first page of
datasets.

:rtype: tuple, (list, str)
:returns: list of :class:`gcloud.pubsub.table.Table`, plus a

This comment was marked as spam.

This comment was marked as spam.

"next page token" string: if not None, indicates that
more tables can be retrieved with another call (pass that
value as ``page_token``).
"""
params = {}

if max_results is not None:
params['maxResults'] = max_results

if page_token is not None:
params['pageToken'] = page_token

path = '/projects/%s/datasets/%s/tables' % (self.project, self.name)
connection = self._client.connection
resp = connection.api_request(method='GET', path=path,
query_params=params)
tables = [Table.from_api_repr(resource, self)
for resource in resp['tables']]
return tables, resp.get('nextPageToken')

def table(self, name, schema=()):
"""Construct a table bound to this dataset.

Expand Down
87 changes: 87 additions & 0 deletions gcloud/bigquery/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,93 @@ def test_delete_w_alternate_client(self):
self.assertEqual(req['method'], 'DELETE')
self.assertEqual(req['path'], '/%s' % PATH)

def test_list_tables_defaults(self):
from gcloud.bigquery.table import Table
conn = _Connection({})
TABLE_1 = 'table_one'
TABLE_2 = 'table_two'
PATH = 'projects/%s/datasets/%s/tables' % (self.PROJECT, self.DS_NAME)
TOKEN = 'TOKEN'
DATA = {
'nextPageToken': TOKEN,
'tables': [
{'kind': 'bigquery#table',
'id': '%s:%s.%s' % (self.PROJECT, self.DS_NAME, TABLE_1),
'tableReference': {'tableId': TABLE_1,
'datasetId': self.DS_NAME,
'projectId': self.PROJECT},
'type': 'TABLE'},
{'kind': 'bigquery#table',
'id': '%s:%s.%s' % (self.PROJECT, self.DS_NAME, TABLE_2),
'tableReference': {'tableId': TABLE_2,
'datasetId': self.DS_NAME,
'projectId': self.PROJECT},
'type': 'TABLE'},
]
}

conn = _Connection(DATA)
client = _Client(project=self.PROJECT, connection=conn)
dataset = self._makeOne(self.DS_NAME, client=client)

tables, token = dataset.list_tables()

self.assertEqual(len(tables), len(DATA['tables']))
for found, expected in zip(tables, DATA['tables']):
self.assertTrue(isinstance(found, Table))
self.assertEqual(found.table_id, expected['id'])
self.assertEqual(found.table_type, expected['type'])
self.assertEqual(token, TOKEN)

self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/%s' % PATH)

def test_list_tables_explicit(self):
from gcloud.bigquery.table import Table
conn = _Connection({})
TABLE_1 = 'table_one'
TABLE_2 = 'table_two'
PATH = 'projects/%s/datasets/%s/tables' % (self.PROJECT, self.DS_NAME)
TOKEN = 'TOKEN'
DATA = {
'tables': [
{'kind': 'bigquery#dataset',
'id': '%s:%s.%s' % (self.PROJECT, self.DS_NAME, TABLE_1),
'tableReference': {'tableId': TABLE_1,
'datasetId': self.DS_NAME,
'projectId': self.PROJECT},
'type': 'TABLE'},
{'kind': 'bigquery#dataset',
'id': '%s:%s.%s' % (self.PROJECT, self.DS_NAME, TABLE_2),
'tableReference': {'tableId': TABLE_2,
'datasetId': self.DS_NAME,
'projectId': self.PROJECT},
'type': 'TABLE'},
]
}

conn = _Connection(DATA)
client = _Client(project=self.PROJECT, connection=conn)
dataset = self._makeOne(self.DS_NAME, client=client)

tables, token = dataset.list_tables(max_results=3, page_token=TOKEN)

self.assertEqual(len(tables), len(DATA['tables']))
for found, expected in zip(tables, DATA['tables']):
self.assertTrue(isinstance(found, Table))
self.assertEqual(found.table_id, expected['id'])
self.assertEqual(found.table_type, expected['type'])
self.assertEqual(token, None)

self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/%s' % PATH)
self.assertEqual(req['query_params'],
{'maxResults': 3, 'pageToken': TOKEN})

def test_table_wo_schema(self):
from gcloud.bigquery.table import Table
conn = _Connection({})
Expand Down