Repository: googleapis/google-cloud-python

Issue:
BigQuery dataset.list_tables() fails when the dataset is empty
Good day.

Getting this exception when the BigQuery dataset contains no tables:

``` python
  File "/Library/Python/2.7/site-packages/gcloud/bigquery/dataset.py", line 507, in list_tables
    for resource in resp['tables']]
KeyError: 'tables'
```

Mac OS X El Capitan. gcloud 0.8.0



Discussion:
Thanks @evmin!

I reproduced with

``` python
>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> dataset_name = 'foo_bar_baz'
>>> dataset = client.dataset(dataset_name)
>>> if dataset.exists():
...     raise ValueError('Expected a new dataset')
...
>>> dataset.create()
>>> dataset.exists()
True
>>> all_tables, token = dataset.list_tables()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "gcloud/bigquery/dataset.py", line 553, in list_tables
    for resource in resp['tables']]
KeyError: 'tables'
>>> dataset.delete()
>>>
```

at 2323e1a110d240545bf2950e18e5ccefb75d01f1


Resolving patch:
diff --git a/gcloud/bigquery/dataset.py b/gcloud/bigquery/dataset.py
--- 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=()):


Test patch:
diff --git a/gcloud/bigquery/test_dataset.py b/gcloud/bigquery/test_dataset.py
--- 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
--- 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(),),