Skip to content

Commit d8735e1

Browse files
committed
Merge pull request #1831 from tseaver/bigquery-testable_snippets_prep
Prepare for making snippets testable
2 parents 0fe6657 + 03218dd commit d8735e1

File tree

6 files changed

+96
-11
lines changed

6 files changed

+96
-11
lines changed

docs/pubsub_snippets.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ def topic_create(client, to_delete):
9393
def topic_exists(client, to_delete):
9494
"""Test existence of a topic."""
9595
TOPIC_NAME = 'topic_exists-%d' % (_millis(),)
96-
97-
# [START client_topic]
9896
topic = client.topic(TOPIC_NAME)
99-
# [END client_topic]
100-
10197
to_delete.append(topic)
10298

10399
# [START topic_exists]

gcloud/bigquery/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from gcloud.bigquery.client import Client
2525
from gcloud.bigquery.connection import Connection
26+
from gcloud.bigquery.dataset import AccessGrant
2627
from gcloud.bigquery.dataset import Dataset
2728
from gcloud.bigquery.table import SchemaField
2829
from gcloud.bigquery.table import Table

gcloud/bigquery/dataset.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ def __init__(self, role, entity_type, entity_id):
7474
self.entity_type = entity_type
7575
self.entity_id = entity_id
7676

77+
def __eq__(self, other):
78+
return (
79+
self.role == other.role and
80+
self.entity_type == other.entity_type and
81+
self.entity_id == other.entity_id)
82+
7783
def __repr__(self):
7884
return '<AccessGrant: role=%s, %s=%s>' % (
7985
self.role, self.entity_type, self.entity_id)
@@ -362,6 +368,9 @@ def _set_properties(self, api_response):
362368
cleaned['creationTime'] = float(cleaned['creationTime'])
363369
if 'lastModifiedTime' in cleaned:
364370
cleaned['lastModifiedTime'] = float(cleaned['lastModifiedTime'])
371+
if 'defaultTableExpirationMs' in cleaned:
372+
cleaned['defaultTableExpirationMs'] = int(
373+
cleaned['defaultTableExpirationMs'])
365374
self._properties.update(cleaned)
366375

367376
def _build_access_resource(self):

gcloud/bigquery/table.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ def __init__(self, name, field_type, mode='NULLABLE', description=None,
6262
self.description = description
6363
self.fields = fields
6464

65+
def __eq__(self, other):
66+
return (
67+
self.name == other.name and
68+
self.field_type.lower() == other.field_type.lower() and
69+
self.mode == other.mode and
70+
self.description == other.description and
71+
self.fields == other.fields)
72+
6573

6674
class Table(object):
6775
"""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):
626634
path='%s/data' % self.path,
627635
query_params=params)
628636
total_rows = response.get('totalRows')
637+
if total_rows is not None:
638+
total_rows = int(total_rows)
629639
page_token = response.get('pageToken')
630640
rows_data = _rows_from_json(response.get('rows', ()), self._schema)
631641

gcloud/bigquery/test_dataset.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ def test_ctor_nonview_without_role(self):
5555
with self.assertRaises(ValueError):
5656
self._makeOne(role, entity_type, None)
5757

58+
def test___eq___role_mismatch(self):
59+
grant = self._makeOne('OWNER', 'userByEmail', 'phred@example.com')
60+
other = self._makeOne('WRITER', 'userByEmail', 'phred@example.com')
61+
self.assertNotEqual(grant, other)
62+
63+
def test___eq___entity_type_mismatch(self):
64+
grant = self._makeOne('OWNER', 'userByEmail', 'phred@example.com')
65+
other = self._makeOne('OWNER', 'groupByEmail', 'phred@example.com')
66+
self.assertNotEqual(grant, other)
67+
68+
def test___eq___entity_id_mismatch(self):
69+
grant = self._makeOne('OWNER', 'userByEmail', 'phred@example.com')
70+
other = self._makeOne('OWNER', 'userByEmail', 'bharney@example.com')
71+
self.assertNotEqual(grant, other)
72+
73+
def test___eq___hit(self):
74+
grant = self._makeOne('OWNER', 'userByEmail', 'phred@example.com')
75+
other = self._makeOne('OWNER', 'userByEmail', 'phred@example.com')
76+
self.assertEqual(grant, other)
77+
5878

5979
class TestDataset(unittest2.TestCase):
6080
PROJECT = 'project'
@@ -138,8 +158,11 @@ def _verifyResourceProperties(self, dataset, resource):
138158

139159
self._verifyReadonlyResourceProperties(dataset, resource)
140160

141-
self.assertEqual(dataset.default_table_expiration_ms,
142-
resource.get('defaultTableExpirationMs'))
161+
if 'defaultTableExpirationMs' in resource:
162+
self.assertEqual(dataset.default_table_expiration_ms,
163+
int(resource.get('defaultTableExpirationMs')))
164+
else:
165+
self.assertEqual(dataset.default_table_expiration_ms, None)
143166
self.assertEqual(dataset.description, resource.get('description'))
144167
self.assertEqual(dataset.friendly_name, resource.get('friendlyName'))
145168
self.assertEqual(dataset.location, resource.get('location'))
@@ -500,7 +523,7 @@ def test_patch_w_alternate_client(self):
500523
DEF_TABLE_EXP = 12345
501524
LOCATION = 'EU'
502525
RESOURCE = self._makeResource()
503-
RESOURCE['defaultTableExpirationMs'] = DEF_TABLE_EXP
526+
RESOURCE['defaultTableExpirationMs'] = str(DEF_TABLE_EXP)
504527
RESOURCE['location'] = LOCATION
505528
conn1 = _Connection()
506529
CLIENT1 = _Client(project=self.PROJECT, connection=conn1)

gcloud/bigquery/test_table.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,54 @@ def test_ctor_subfields(self):
6161
self.assertEqual(field.fields[1].description, None)
6262
self.assertEqual(field.fields[1].fields, None)
6363

64+
def test___eq___name_mismatch(self):
65+
field = self._makeOne('test', 'STRING')
66+
other = self._makeOne('other', 'STRING')
67+
self.assertNotEqual(field, other)
68+
69+
def test___eq___field_type_mismatch(self):
70+
field = self._makeOne('test', 'STRING')
71+
other = self._makeOne('test', 'INTEGER')
72+
self.assertNotEqual(field, other)
73+
74+
def test___eq___mode_mismatch(self):
75+
field = self._makeOne('test', 'STRING', mode='REQUIRED')
76+
other = self._makeOne('test', 'STRING', mode='NULLABLE')
77+
self.assertNotEqual(field, other)
78+
79+
def test___eq___description_mismatch(self):
80+
field = self._makeOne('test', 'STRING', description='Testing')
81+
other = self._makeOne('test', 'STRING', description='Other')
82+
self.assertNotEqual(field, other)
83+
84+
def test___eq___fields_mismatch(self):
85+
sub1 = self._makeOne('sub1', 'STRING')
86+
sub2 = self._makeOne('sub2', 'STRING')
87+
field = self._makeOne('test', 'RECORD', fields=[sub1])
88+
other = self._makeOne('test', 'RECORD', fields=[sub2])
89+
self.assertNotEqual(field, other)
90+
91+
def test___eq___hit(self):
92+
field = self._makeOne('test', 'STRING', mode='REQUIRED',
93+
description='Testing')
94+
other = self._makeOne('test', 'STRING', mode='REQUIRED',
95+
description='Testing')
96+
self.assertEqual(field, other)
97+
98+
def test___eq___hit_case_diff_on_type(self):
99+
field = self._makeOne('test', 'STRING', mode='REQUIRED',
100+
description='Testing')
101+
other = self._makeOne('test', 'string', mode='REQUIRED',
102+
description='Testing')
103+
self.assertEqual(field, other)
104+
105+
def test___eq___hit_w_fields(self):
106+
sub1 = self._makeOne('sub1', 'STRING')
107+
sub2 = self._makeOne('sub2', 'STRING')
108+
field = self._makeOne('test', 'RECORD', fields=[sub1, sub2])
109+
other = self._makeOne('test', 'RECORD', fields=[sub1, sub2])
110+
self.assertEqual(field, other)
111+
64112

65113
class _SchemaBase(object):
66114

@@ -884,7 +932,7 @@ def _bigquery_timestamp_float_repr(ts_float):
884932
return '%0.15E' % (ts_float,)
885933

886934
DATA = {
887-
'totalRows': ROWS,
935+
'totalRows': str(ROWS),
888936
'pageToken': TOKEN,
889937
'rows': [
890938
{'f': [
@@ -939,10 +987,8 @@ def test_fetch_data_w_alternate_client(self):
939987
PATH = 'projects/%s/datasets/%s/tables/%s/data' % (
940988
self.PROJECT, self.DS_NAME, self.TABLE_NAME)
941989
MAX = 10
942-
ROWS = 1234
943990
TOKEN = 'TOKEN'
944991
DATA = {
945-
'totalRows': ROWS,
946992
'rows': [
947993
{'f': [
948994
{'v': 'Phred Phlyntstone'},
@@ -991,7 +1037,7 @@ def test_fetch_data_w_alternate_client(self):
9911037
self.assertEqual(rows[1], ('Bharney Rhubble', 33, False, 1.414))
9921038
self.assertEqual(rows[2], ('Wylma Phlyntstone', 29, True, 2.71828))
9931039
self.assertEqual(rows[3], ('Bhettye Rhubble', 27, None, None))
994-
self.assertEqual(total_rows, ROWS)
1040+
self.assertEqual(total_rows, None)
9951041
self.assertEqual(page_token, None)
9961042

9971043
self.assertEqual(len(conn1._requested), 0)

0 commit comments

Comments
 (0)