Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept a string in Table and Dataset constructors. #7483

Merged
merged 1 commit into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions bigquery/google/cloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,13 @@ class Dataset(object):
https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets

Args:
dataset_ref (google.cloud.bigquery.dataset.DatasetReference):
a pointer to a dataset
dataset_ref (Union[ \
:class:`~google.cloud.bigquery.dataset.DatasetReference`, \
str, \
]):
A pointer to a dataset. If ``dataset_ref`` is a string, it must
include both the project ID and the dataset ID, separated by
``.``.
"""

_PROPERTY_TO_API_FIELD = {
Expand All @@ -318,6 +323,8 @@ class Dataset(object):
}

def __init__(self, dataset_ref):
if isinstance(dataset_ref, six.string_types):
dataset_ref = DatasetReference.from_string(dataset_ref)
self._properties = {"datasetReference": dataset_ref.to_api_repr(), "labels": {}}

@property
Expand Down
11 changes: 9 additions & 2 deletions bigquery/google/cloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,13 @@ class Table(object):
https://cloud.google.com/bigquery/docs/reference/rest/v2/tables

Args:
table_ref (google.cloud.bigquery.table.TableReference):
A pointer to a table
table_ref (Union[ \
:class:`~google.cloud.bigquery.table.TableReference`, \
str, \
]):
A pointer to a table. If ``table_ref`` is a string, it must
included a project ID, dataset ID, and table ID, each separated
by ``.``.
schema (List[google.cloud.bigquery.schema.SchemaField]):
The table's schema
"""
Expand All @@ -367,6 +372,8 @@ class Table(object):
}

def __init__(self, table_ref, schema=None):
if isinstance(table_ref, six.string_types):
table_ref = TableReference.from_string(table_ref)
self._properties = {"tableReference": table_ref.to_api_repr(), "labels": {}}
# Let the @property do validation.
if schema is not None:
Expand Down
11 changes: 11 additions & 0 deletions bigquery/tests/unit/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import unittest

import mock
import pytest


class TestAccessEntry(unittest.TestCase):
Expand Down Expand Up @@ -364,6 +365,16 @@ def test_ctor_defaults(self):
self.assertIsNone(dataset.friendly_name)
self.assertIsNone(dataset.location)

def test_ctor_string(self):
dataset = self._make_one("some-project.some_dset")
self.assertEqual(dataset.project, "some-project")
self.assertEqual(dataset.dataset_id, "some_dset")

def test_ctor_string_wo_project_id(self):
with pytest.raises(ValueError):
# Project ID is missing.
self._make_one("some_dset")

def test_ctor_explicit(self):
from google.cloud.bigquery.dataset import DatasetReference, AccessEntry

Expand Down
11 changes: 11 additions & 0 deletions bigquery/tests/unit/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,17 @@ def test_ctor_w_schema(self):

self.assertEqual(table.schema, [full_name, age])

def test_ctor_string(self):
table = self._make_one("some-project.some_dset.some_tbl")
self.assertEqual(table.project, "some-project")
self.assertEqual(table.dataset_id, "some_dset")
self.assertEqual(table.table_id, "some_tbl")

def test_ctor_string_wo_project_id(self):
with pytest.raises(ValueError):
# Project ID is missing.
self._make_one("some_dset.some_tbl")

def test_num_bytes_getter(self):
dataset = DatasetReference(self.PROJECT, self.DS_ID)
table_ref = dataset.table(self.TABLE_NAME)
Expand Down