Skip to content

Commit

Permalink
Merge pull request #1831 from tseaver/bigquery-testable_snippets_prep
Browse files Browse the repository at this point in the history
Prepare for making snippets testable
  • Loading branch information
tseaver committed May 28, 2016
2 parents 0fe6657 + 03218dd commit d8735e1
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 11 deletions.
4 changes: 0 additions & 4 deletions docs/pubsub_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions gcloud/bigquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions gcloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<AccessGrant: role=%s, %s=%s>' % (
self.role, self.entity_type, self.entity_id)
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions gcloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down
29 changes: 26 additions & 3 deletions gcloud/bigquery/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '[email protected]')
other = self._makeOne('WRITER', 'userByEmail', '[email protected]')
self.assertNotEqual(grant, other)

def test___eq___entity_type_mismatch(self):
grant = self._makeOne('OWNER', 'userByEmail', '[email protected]')
other = self._makeOne('OWNER', 'groupByEmail', '[email protected]')
self.assertNotEqual(grant, other)

def test___eq___entity_id_mismatch(self):
grant = self._makeOne('OWNER', 'userByEmail', '[email protected]')
other = self._makeOne('OWNER', 'userByEmail', '[email protected]')
self.assertNotEqual(grant, other)

def test___eq___hit(self):
grant = self._makeOne('OWNER', 'userByEmail', '[email protected]')
other = self._makeOne('OWNER', 'userByEmail', '[email protected]')
self.assertEqual(grant, other)


class TestDataset(unittest2.TestCase):
PROJECT = 'project'
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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)
Expand Down
54 changes: 50 additions & 4 deletions gcloud/bigquery/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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': [
Expand Down Expand Up @@ -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'},
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d8735e1

Please sign in to comment.