Skip to content

Commit

Permalink
bigquery: remove dataset.exists (#3996)
Browse files Browse the repository at this point in the history
* bigquery: remove dataset.exists

Dataset won't be able to support this method when we remove its client.

Don't add client.dataset_exists; the user can use client.get_dataset and
catch NotFound.

* fix lint

* fix lint agian

* fix more lint
  • Loading branch information
jba authored Sep 18, 2017
1 parent 771768e commit 8847511
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 71 deletions.
25 changes: 0 additions & 25 deletions bigquery/google/cloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from google.api.core import page_iterator
from google.cloud._helpers import _datetime_from_microseconds
from google.cloud.exceptions import NotFound
from google.cloud.bigquery.table import Table
from google.cloud.bigquery.table import TableReference

Expand Down Expand Up @@ -486,30 +485,6 @@ def _build_resource(self):

return resource

def exists(self, client=None):
"""API call: test for the existence of the dataset via a GET request
See
https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets/get
: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: bool
:returns: Boolean indicating existence of the dataset.
"""
client = self._require_client(client)

try:
client._connection.api_request(method='GET', path=self.path,
query_params={'fields': 'id'})
except NotFound:
return False
else:
return True

def patch(self, client=None, **kw):
"""API call: update individual dataset properties via a PATCH request.
Expand Down
19 changes: 13 additions & 6 deletions bigquery/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import six

from google.cloud import bigquery
from google.cloud.bigquery.dataset import Dataset
from google.cloud.bigquery.dataset import Dataset, DatasetReference
from google.cloud._helpers import UTC
from google.cloud.bigquery import dbapi
from google.cloud.exceptions import Forbidden
from google.cloud.exceptions import Forbidden, NotFound

from test_utils.retry import RetryErrors
from test_utils.retry import RetryInstanceState
Expand Down Expand Up @@ -91,7 +91,6 @@ def setUp(self):
self.to_delete = []

def tearDown(self):
from google.cloud.bigquery.dataset import Dataset
from google.cloud.storage import Bucket
from google.cloud.exceptions import BadRequest
from google.cloud.exceptions import Conflict
Expand All @@ -115,7 +114,7 @@ def test_create_dataset(self):
dataset = retry_403(Config.CLIENT.create_dataset)(Dataset(DATASET_ID))
self.to_delete.append(dataset)

self.assertTrue(dataset.exists())
self.assertTrue(_dataset_exists(dataset))
self.assertEqual(dataset.dataset_id, DATASET_ID)

def test_get_dataset(self):
Expand All @@ -138,7 +137,7 @@ def test_patch_dataset(self):
Dataset(_make_dataset_id('patch_dataset')))
self.to_delete.append(dataset)

self.assertTrue(dataset.exists())
self.assertTrue(_dataset_exists(dataset))
self.assertIsNone(dataset.friendly_name)
self.assertIsNone(dataset.description)
dataset.patch(friendly_name='Friendly', description='Description')
Expand All @@ -150,7 +149,7 @@ def test_update_dataset(self):
Dataset(_make_dataset_id('update_dataset')))
self.to_delete.append(dataset)

self.assertTrue(dataset.exists())
self.assertTrue(_dataset_exists(dataset))
after = [entry for entry in dataset.access_entries
if entry.entity_id != 'projectWriters']
dataset.access_entries = after
Expand Down Expand Up @@ -1196,3 +1195,11 @@ def test_create_table_insert_fetch_nested_schema(self):

def _job_done(instance):
return instance.state.lower() == 'done'


def _dataset_exists(ds):
try:
Config.CLIENT.get_dataset(DatasetReference(ds.project, ds.dataset_id))
return True
except NotFound:
return False
42 changes: 2 additions & 40 deletions bigquery/tests/unit/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,37 +386,6 @@ def test__parse_access_entries_w_extra_keys(self):
with self.assertRaises(ValueError):
dataset._parse_access_entries(ACCESS)

def test_exists_miss_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)

self.assertFalse(dataset.exists())

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'], {'fields': 'id'})

def test_exists_hit_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)

self.assertTrue(dataset.exists(client=CLIENT2))

self.assertEqual(len(conn1._requested), 0)
self.assertEqual(len(conn2._requested), 1)
req = conn2._requested[0]
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/%s' % PATH)
self.assertEqual(req['query_params'], {'fields': 'id'})

def test_patch_w_invalid_expiration(self):
RESOURCE = self._makeResource()
conn = _Connection(RESOURCE)
Expand Down Expand Up @@ -727,13 +696,6 @@ def __init__(self, *responses):
self._requested = []

def api_request(self, **kw):
from google.cloud.exceptions import NotFound

self._requested.append(kw)

try:
response, self._responses = self._responses[0], self._responses[1:]
except IndexError:
raise NotFound('miss')
else:
return response
response, self._responses = self._responses[0], self._responses[1:]
return response

0 comments on commit 8847511

Please sign in to comment.