Skip to content

Commit

Permalink
bigquery: add client.delete_dataset (#4012)
Browse files Browse the repository at this point in the history
* bigquery: add client.delete_dataset

* support Dataset as well as DatasetReference

* fix lint
  • Loading branch information
jba authored and tswast committed Oct 16, 2017
1 parent ca10fee commit 6aab47a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 61 deletions.
18 changes: 16 additions & 2 deletions bigquery/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,6 @@ def update_dataset(self, dataset, fields):
:rtype: :class:`google.cloud.bigquery.dataset.Dataset`
:returns: the modified ``Dataset`` instance
:raises: ValueError for fields that cannot be updated.
"""
if dataset.project is None:
dataset._project = self.project
Expand All @@ -266,6 +264,22 @@ def update_dataset(self, dataset, fields):
method='PATCH', path=path, data=partial, headers=headers)
return Dataset.from_api_repr(api_response, self)

def delete_dataset(self, dataset):
"""Delete a dataset.
See
https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets/delete
:type dataset: One of:
:class:`~google.cloud.bigquery.dataset.Dataset`
:class:`~google.cloud.bigquery.dataset.DatasetReference`
:param dataset: the dataset to delete, or a reference to it.
"""
if not isinstance(dataset, (Dataset, DatasetReference)):
raise TypeError('dataset must be a Dataset or a DatasetReference')
self._connection.api_request(method='DELETE', path=dataset.path)

def _get_query_results(self, job_id, project=None, timeout_ms=None):
"""Get the query results object for a query job.
Expand Down
29 changes: 0 additions & 29 deletions bigquery/google/cloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,21 +394,6 @@ def from_api_repr(cls, resource, client):
dataset._set_properties(resource)
return dataset

def _require_client(self, client):
"""Check client or verify over-ride.
:type client: :class:`~google.cloud.bigquery.client.Client` or
``NoneType``
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current dataset.
:rtype: :class:`google.cloud.bigquery.client.Client`
:returns: The client passed in or the currently bound client.
"""
if client is None:
client = self._client
return client

@staticmethod
def _parse_access_entries(access):
"""Parse a resource fragment into a set of access entries.
Expand Down Expand Up @@ -488,20 +473,6 @@ def _build_resource(self):

return resource

def delete(self, client=None):
"""API call: delete the dataset via a DELETE request.
See
https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets/delete
:type client: :class:`~google.cloud.bigquery.client.Client` or
``NoneType``
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current dataset.
"""
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.
Expand Down
2 changes: 1 addition & 1 deletion bigquery/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _still_in_use(bad_request):
if isinstance(doomed, Bucket):
retry_409(doomed.delete)(force=True)
elif isinstance(doomed, Dataset):
retry_in_use(doomed.delete)()
retry_in_use(Config.CLIENT.delete_dataset)(doomed)
else:
doomed.delete()

Expand Down
23 changes: 23 additions & 0 deletions bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,29 @@ def test_update_dataset(self):
req = conn._requested[1]
self.assertEqual(req['headers']['If-Match'], 'etag')

def test_delete_dataset(self):
from google.cloud.bigquery.dataset import Dataset

PROJECT = 'PROJECT'
DS_ID = 'DATASET_ID'
PATH = 'projects/%s/datasets/%s' % (PROJECT, DS_ID)
creds = _make_credentials()
client = self._make_one(project=PROJECT, credentials=creds)
conn = client._connection = _Connection({}, {})
for arg in (client.dataset(DS_ID), Dataset(DS_ID, project=PROJECT)):
client.delete_dataset(arg)
req = conn._requested[0]
self.assertEqual(req['method'], 'DELETE')
self.assertEqual(req['path'], '/%s' % PATH)

def test_delete_dataset_wrong_type(self):
PROJECT = 'PROJECT'
DS_ID = 'DATASET_ID'
creds = _make_credentials()
client = self._make_one(project=PROJECT, credentials=creds)
with self.assertRaises(TypeError):
client.delete_dataset(client.dataset(DS_ID).table("foo"))

def test_job_from_resource_unknown_type(self):
PROJECT = 'PROJECT'
creds = _make_credentials()
Expand Down
29 changes: 0 additions & 29 deletions bigquery/tests/unit/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,35 +388,6 @@ def test__parse_access_entries_w_extra_keys(self):
with self.assertRaises(ValueError):
dataset._parse_access_entries(ACCESS)

def test_delete_w_bound_client(self):
PATH = 'projects/%s/datasets/%s' % (self.PROJECT, self.DS_ID)
conn = _Connection({})
client = _Client(project=self.PROJECT, connection=conn)
dataset = self._make_one(self.DS_ID, client=client)

dataset.delete()

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

def test_delete_w_alternate_client(self):
PATH = 'projects/%s/datasets/%s' % (self.PROJECT, self.DS_ID)
conn1 = _Connection()
CLIENT1 = _Client(project=self.PROJECT, connection=conn1)
conn2 = _Connection({})
CLIENT2 = _Client(project=self.PROJECT, connection=conn2)
dataset = self._make_one(self.DS_ID, client=CLIENT1)

dataset.delete(client=CLIENT2)

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

def test_list_tables_empty(self):
import six

Expand Down

0 comments on commit 6aab47a

Please sign in to comment.