Skip to content

Commit

Permalink
BigQuery: Add TestReference class. Add table function to DatasetRefer…
Browse files Browse the repository at this point in the history
…ence (#3942)
  • Loading branch information
alixhami authored and tswast committed Oct 12, 2017
1 parent f17bb9c commit 10f5a2d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
9 changes: 9 additions & 0 deletions bigquery/google/cloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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


class AccessEntry(object):
Expand Down Expand Up @@ -126,6 +127,14 @@ def dataset_id(self):
"""
return self._dataset_id

def table(self, table_id):
"""Constructs a TableReference.
:rtype: :class:`google.cloud.bigquery.table.TableReference`
:returns: a TableReference for a table in this dataset.
"""
return TableReference(self, table_id)


class Dataset(object):
"""Datasets are containers for tables.
Expand Down
36 changes: 36 additions & 0 deletions bigquery/google/cloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@
_DEFAULT_NUM_RETRIES = 6


class TableReference(object):
"""TableReferences are pointers to tables.
See
https://cloud.google.com/bigquery/docs/reference/rest/v2/tables
:type dataset_ref: :class:`google.cloud.bigquery.dataset.DatasetReference`
:param dataset_ref: a pointer to the dataset
:type table_id: str
:param table_id: the ID of the table
"""

def __init__(self, dataset_ref, table_id):
self._dataset_ref = dataset_ref
self._table_id = table_id

@property
def dataset_ref(self):
"""Pointer to the dataset.
:rtype: :class:`google.cloud.bigquery.dataset.DatasetReference`
:returns: a pointer to the dataset.
"""
return self._dataset_ref

@property
def table_id(self):
"""Table ID.
:rtype: str
:returns: the table ID.
"""
return self._table_id


class Table(object):
"""Tables represent a set of rows whose values correspond to a schema.
Expand Down
6 changes: 6 additions & 0 deletions bigquery/tests/unit/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ def test_ctor_defaults(self):
self.assertEqual(dataset_ref.project_id, 'some-project-1')
self.assertEqual(dataset_ref.dataset_id, 'dataset_1')

def test_table(self):
dataset_ref = self._make_one('some-project-1', 'dataset_1')
table_ref = dataset_ref.table('table_1')
self.assertIs(table_ref.dataset_ref, dataset_ref)
self.assertEqual(table_ref.table_id, 'table_1')


class TestDataset(unittest.TestCase):
PROJECT = 'project'
Expand Down
20 changes: 20 additions & 0 deletions bigquery/tests/unit/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ def _verifySchema(self, schema, resource):
self._verify_field(field, r_field)


class TestTableReference(unittest.TestCase):

@staticmethod
def _get_target_class():
from google.cloud.bigquery.table import TableReference

return TableReference

def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_ctor_defaults(self):
from google.cloud.bigquery.dataset import DatasetReference
dataset_ref = DatasetReference('project_1', 'dataset_1')

table_ref = self._make_one(dataset_ref, 'table_1')
self.assertIs(table_ref.dataset_ref, dataset_ref)
self.assertEqual(table_ref.table_id, 'table_1')


class TestTable(unittest.TestCase, _SchemaBase):

PROJECT = 'prahj-ekt'
Expand Down

0 comments on commit 10f5a2d

Please sign in to comment.