diff --git a/gcloud/bigquery/dataset.py b/gcloud/bigquery/dataset.py index 38007b2d08bf..64944be03fd3 100644 --- a/gcloud/bigquery/dataset.py +++ b/gcloud/bigquery/dataset.py @@ -550,7 +550,7 @@ def list_tables(self, max_results=None, page_token=None): resp = connection.api_request(method='GET', path=path, query_params=params) tables = [Table.from_api_repr(resource, self) - for resource in resp['tables']] + for resource in resp.get('tables', ())] return tables, resp.get('nextPageToken') def table(self, name, schema=()): diff --git a/gcloud/bigquery/test_dataset.py b/gcloud/bigquery/test_dataset.py index d88b5f8c3bd3..6e950208bff7 100644 --- a/gcloud/bigquery/test_dataset.py +++ b/gcloud/bigquery/test_dataset.py @@ -612,9 +612,24 @@ def test_delete_w_alternate_client(self): self.assertEqual(req['method'], 'DELETE') self.assertEqual(req['path'], '/%s' % PATH) - def test_list_tables_defaults(self): + def test_list_tables_empty(self): from gcloud.bigquery.table import Table + conn = _Connection({}) + client = _Client(project=self.PROJECT, connection=conn) + dataset = self._makeOne(self.DS_NAME, client=client) + tables, token = dataset.list_tables() + self.assertEqual(tables, []) + self.assertEqual(token, None) + self.assertEqual(len(conn._requested), 1) + req = conn._requested[0] + self.assertEqual(req['method'], 'GET') + PATH = 'projects/%s/datasets/%s/tables' % (self.PROJECT, self.DS_NAME) + self.assertEqual(req['path'], '/%s' % PATH) + + def test_list_tables_defaults(self): + from gcloud.bigquery.table import Table + TABLE_1 = 'table_one' TABLE_2 = 'table_two' PATH = 'projects/%s/datasets/%s/tables' % (self.PROJECT, self.DS_NAME) @@ -657,7 +672,7 @@ def test_list_tables_defaults(self): 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) diff --git a/system_tests/bigquery.py b/system_tests/bigquery.py index 723e7ff42eaa..dee49786cac1 100644 --- a/system_tests/bigquery.py +++ b/system_tests/bigquery.py @@ -135,6 +135,13 @@ def test_list_tables(self): self.assertFalse(dataset.exists()) dataset.create() self.to_delete.append(dataset) + + # Retrieve tables before any are created for the dataset. + all_tables, token = dataset.list_tables() + self.assertEqual(all_tables, []) + self.assertEqual(token, None) + + # Insert some tables to be listed. tables_to_create = [ 'new%d' % (1000 * time.time(),), 'newer%d' % (1000 * time.time(),),