diff --git a/docs/pubsub_snippets.py b/docs/pubsub_snippets.py index b3eec76f8f22..f21a35538853 100644 --- a/docs/pubsub_snippets.py +++ b/docs/pubsub_snippets.py @@ -93,11 +93,7 @@ def topic_create(client, to_delete): def topic_exists(client, to_delete): """Test existence of a topic.""" TOPIC_NAME = 'topic_exists-%d' % (_millis(),) - - # [START client_topic] topic = client.topic(TOPIC_NAME) - # [END client_topic] - to_delete.append(topic) # [START topic_exists] diff --git a/gcloud/bigquery/__init__.py b/gcloud/bigquery/__init__.py index cca30b80e91f..9007b9fab0fd 100644 --- a/gcloud/bigquery/__init__.py +++ b/gcloud/bigquery/__init__.py @@ -23,6 +23,7 @@ from gcloud.bigquery.client import Client from gcloud.bigquery.connection import Connection +from gcloud.bigquery.dataset import AccessGrant from gcloud.bigquery.dataset import Dataset from gcloud.bigquery.table import SchemaField from gcloud.bigquery.table import Table diff --git a/gcloud/bigquery/dataset.py b/gcloud/bigquery/dataset.py index 64944be03fd3..397dbe244353 100644 --- a/gcloud/bigquery/dataset.py +++ b/gcloud/bigquery/dataset.py @@ -74,6 +74,12 @@ def __init__(self, role, entity_type, entity_id): self.entity_type = entity_type self.entity_id = entity_id + def __eq__(self, other): + return ( + self.role == other.role and + self.entity_type == other.entity_type and + self.entity_id == other.entity_id) + def __repr__(self): return '' % ( self.role, self.entity_type, self.entity_id) @@ -362,6 +368,9 @@ def _set_properties(self, api_response): cleaned['creationTime'] = float(cleaned['creationTime']) if 'lastModifiedTime' in cleaned: cleaned['lastModifiedTime'] = float(cleaned['lastModifiedTime']) + if 'defaultTableExpirationMs' in cleaned: + cleaned['defaultTableExpirationMs'] = int( + cleaned['defaultTableExpirationMs']) self._properties.update(cleaned) def _build_access_resource(self): diff --git a/gcloud/bigquery/table.py b/gcloud/bigquery/table.py index df774c9f89ec..7bd7f818ee8d 100644 --- a/gcloud/bigquery/table.py +++ b/gcloud/bigquery/table.py @@ -62,6 +62,14 @@ def __init__(self, name, field_type, mode='NULLABLE', description=None, self.description = description self.fields = fields + def __eq__(self, other): + return ( + self.name == other.name and + self.field_type.lower() == other.field_type.lower() and + self.mode == other.mode and + self.description == other.description and + self.fields == other.fields) + class Table(object): """Tables represent a set of rows whose values correspond to a schema. @@ -626,6 +634,8 @@ def fetch_data(self, max_results=None, page_token=None, client=None): path='%s/data' % self.path, query_params=params) total_rows = response.get('totalRows') + if total_rows is not None: + total_rows = int(total_rows) page_token = response.get('pageToken') rows_data = _rows_from_json(response.get('rows', ()), self._schema) diff --git a/gcloud/bigquery/test_dataset.py b/gcloud/bigquery/test_dataset.py index 6e950208bff7..e1caa1f81dff 100644 --- a/gcloud/bigquery/test_dataset.py +++ b/gcloud/bigquery/test_dataset.py @@ -55,6 +55,26 @@ def test_ctor_nonview_without_role(self): with self.assertRaises(ValueError): self._makeOne(role, entity_type, None) + def test___eq___role_mismatch(self): + grant = self._makeOne('OWNER', 'userByEmail', 'phred@example.com') + other = self._makeOne('WRITER', 'userByEmail', 'phred@example.com') + self.assertNotEqual(grant, other) + + def test___eq___entity_type_mismatch(self): + grant = self._makeOne('OWNER', 'userByEmail', 'phred@example.com') + other = self._makeOne('OWNER', 'groupByEmail', 'phred@example.com') + self.assertNotEqual(grant, other) + + def test___eq___entity_id_mismatch(self): + grant = self._makeOne('OWNER', 'userByEmail', 'phred@example.com') + other = self._makeOne('OWNER', 'userByEmail', 'bharney@example.com') + self.assertNotEqual(grant, other) + + def test___eq___hit(self): + grant = self._makeOne('OWNER', 'userByEmail', 'phred@example.com') + other = self._makeOne('OWNER', 'userByEmail', 'phred@example.com') + self.assertEqual(grant, other) + class TestDataset(unittest2.TestCase): PROJECT = 'project' @@ -138,8 +158,11 @@ def _verifyResourceProperties(self, dataset, resource): self._verifyReadonlyResourceProperties(dataset, resource) - self.assertEqual(dataset.default_table_expiration_ms, - resource.get('defaultTableExpirationMs')) + if 'defaultTableExpirationMs' in resource: + self.assertEqual(dataset.default_table_expiration_ms, + int(resource.get('defaultTableExpirationMs'))) + else: + self.assertEqual(dataset.default_table_expiration_ms, None) self.assertEqual(dataset.description, resource.get('description')) self.assertEqual(dataset.friendly_name, resource.get('friendlyName')) self.assertEqual(dataset.location, resource.get('location')) @@ -500,7 +523,7 @@ def test_patch_w_alternate_client(self): DEF_TABLE_EXP = 12345 LOCATION = 'EU' RESOURCE = self._makeResource() - RESOURCE['defaultTableExpirationMs'] = DEF_TABLE_EXP + RESOURCE['defaultTableExpirationMs'] = str(DEF_TABLE_EXP) RESOURCE['location'] = LOCATION conn1 = _Connection() CLIENT1 = _Client(project=self.PROJECT, connection=conn1) diff --git a/gcloud/bigquery/test_table.py b/gcloud/bigquery/test_table.py index 10895952d92d..a0edf9242824 100644 --- a/gcloud/bigquery/test_table.py +++ b/gcloud/bigquery/test_table.py @@ -61,6 +61,54 @@ def test_ctor_subfields(self): self.assertEqual(field.fields[1].description, None) self.assertEqual(field.fields[1].fields, None) + def test___eq___name_mismatch(self): + field = self._makeOne('test', 'STRING') + other = self._makeOne('other', 'STRING') + self.assertNotEqual(field, other) + + def test___eq___field_type_mismatch(self): + field = self._makeOne('test', 'STRING') + other = self._makeOne('test', 'INTEGER') + self.assertNotEqual(field, other) + + def test___eq___mode_mismatch(self): + field = self._makeOne('test', 'STRING', mode='REQUIRED') + other = self._makeOne('test', 'STRING', mode='NULLABLE') + self.assertNotEqual(field, other) + + def test___eq___description_mismatch(self): + field = self._makeOne('test', 'STRING', description='Testing') + other = self._makeOne('test', 'STRING', description='Other') + self.assertNotEqual(field, other) + + def test___eq___fields_mismatch(self): + sub1 = self._makeOne('sub1', 'STRING') + sub2 = self._makeOne('sub2', 'STRING') + field = self._makeOne('test', 'RECORD', fields=[sub1]) + other = self._makeOne('test', 'RECORD', fields=[sub2]) + self.assertNotEqual(field, other) + + def test___eq___hit(self): + field = self._makeOne('test', 'STRING', mode='REQUIRED', + description='Testing') + other = self._makeOne('test', 'STRING', mode='REQUIRED', + description='Testing') + self.assertEqual(field, other) + + def test___eq___hit_case_diff_on_type(self): + field = self._makeOne('test', 'STRING', mode='REQUIRED', + description='Testing') + other = self._makeOne('test', 'string', mode='REQUIRED', + description='Testing') + self.assertEqual(field, other) + + def test___eq___hit_w_fields(self): + sub1 = self._makeOne('sub1', 'STRING') + sub2 = self._makeOne('sub2', 'STRING') + field = self._makeOne('test', 'RECORD', fields=[sub1, sub2]) + other = self._makeOne('test', 'RECORD', fields=[sub1, sub2]) + self.assertEqual(field, other) + class _SchemaBase(object): @@ -884,7 +932,7 @@ def _bigquery_timestamp_float_repr(ts_float): return '%0.15E' % (ts_float,) DATA = { - 'totalRows': ROWS, + 'totalRows': str(ROWS), 'pageToken': TOKEN, 'rows': [ {'f': [ @@ -939,10 +987,8 @@ def test_fetch_data_w_alternate_client(self): PATH = 'projects/%s/datasets/%s/tables/%s/data' % ( self.PROJECT, self.DS_NAME, self.TABLE_NAME) MAX = 10 - ROWS = 1234 TOKEN = 'TOKEN' DATA = { - 'totalRows': ROWS, 'rows': [ {'f': [ {'v': 'Phred Phlyntstone'}, @@ -991,7 +1037,7 @@ def test_fetch_data_w_alternate_client(self): self.assertEqual(rows[1], ('Bharney Rhubble', 33, False, 1.414)) self.assertEqual(rows[2], ('Wylma Phlyntstone', 29, True, 2.71828)) self.assertEqual(rows[3], ('Bhettye Rhubble', 27, None, None)) - self.assertEqual(total_rows, ROWS) + self.assertEqual(total_rows, None) self.assertEqual(page_token, None) self.assertEqual(len(conn1._requested), 0)