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

Allow strings or references in create_dataset and create_table #6199

Merged
merged 1 commit into from
Oct 12, 2018
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
39 changes: 33 additions & 6 deletions bigquery/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,14 @@ def create_dataset(self, dataset):
https://cloud.google.com/bigquery/docs/reference/rest/v2/tables/insert

Args:
dataset (google.cloud.bigquery.dataset.Dataset):
A ``Dataset`` populated with the desired initial state.
dataset (Union[ \
:class:`~google.cloud.bigquery.dataset.Dataset`, \
:class:`~google.cloud.bigquery.dataset.DatasetReference`, \
str, \
]):
A :class:`~google.cloud.bigquery.dataset.Dataset` to create.
If ``dataset`` is a reference, an empty dataset is created
with the specified ID and client's default location.

Returns:
google.cloud.bigquery.dataset.Dataset:
Expand All @@ -300,6 +306,12 @@ def create_dataset(self, dataset):
>>> dataset = client.create_dataset(dataset)

"""
if isinstance(dataset, str):
dataset = DatasetReference.from_string(
dataset, default_project=self.project)
if isinstance(dataset, DatasetReference):
dataset = Dataset(dataset)

path = '/projects/%s/datasets' % (dataset.project,)

data = dataset.to_api_repr()
Expand All @@ -317,12 +329,27 @@ def create_table(self, table):
See
https://cloud.google.com/bigquery/docs/reference/rest/v2/tables/insert

:type table: :class:`~google.cloud.bigquery.table.Table`
:param table: A ``Table`` populated with the desired initial state.
Args:
table (Union[ \
:class:`~google.cloud.bigquery.table.Table`, \
:class:`~google.cloud.bigquery.table.TableReference`, \
str, \
]):
A :class:`~google.cloud.bigquery.table.Table` to create.
If ``table`` is a reference, an empty table is created
with the specified ID. The dataset that the table belongs to
must already exist.

:rtype: ":class:`~google.cloud.bigquery.table.Table`"
:returns: a new ``Table`` returned from the service.
Returns:
google.cloud.bigquery.table.Table:
A new ``Table`` returned from the service.
"""
if isinstance(table, str):
table = TableReference.from_string(
table, default_project=self.project)
if isinstance(table, TableReference):
table = Table(table)

path = '/projects/%s/datasets/%s/tables' % (
table.project, table.dataset_id)
api_response = self._connection.api_request(
Expand Down
194 changes: 194 additions & 0 deletions bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,109 @@ def test_create_dataset_w_client_location_w_dataset_location(self):
'location': OTHER_LOCATION,
})

def test_create_dataset_w_reference(self):
path = '/projects/%s/datasets' % self.PROJECT
resource = {
'datasetReference':
{'projectId': self.PROJECT, 'datasetId': self.DS_ID},
'etag': "etag",
'id': "%s:%s" % (self.PROJECT, self.DS_ID),
'location': self.LOCATION,
}
creds = _make_credentials()
client = self._make_one(
project=self.PROJECT, credentials=creds, location=self.LOCATION)
conn = client._connection = _make_connection(resource)

dataset = client.create_dataset(client.dataset(self.DS_ID))

self.assertEqual(dataset.dataset_id, self.DS_ID)
self.assertEqual(dataset.project, self.PROJECT)
self.assertEqual(dataset.etag, resource['etag'])
self.assertEqual(dataset.full_dataset_id, resource['id'])
self.assertEqual(dataset.location, self.LOCATION)

conn.api_request.assert_called_once_with(
method='POST',
path=path,
data={
'datasetReference': {
'projectId': self.PROJECT,
'datasetId': self.DS_ID,
},
'labels': {},
'location': self.LOCATION,
})

def test_create_dataset_w_fully_qualified_string(self):
path = '/projects/%s/datasets' % self.PROJECT
resource = {
'datasetReference':
{'projectId': self.PROJECT, 'datasetId': self.DS_ID},
'etag': "etag",
'id': "%s:%s" % (self.PROJECT, self.DS_ID),
'location': self.LOCATION,
}
creds = _make_credentials()
client = self._make_one(
project=self.PROJECT, credentials=creds, location=self.LOCATION)
conn = client._connection = _make_connection(resource)

dataset = client.create_dataset(
'{}.{}'.format(self.PROJECT, self.DS_ID))

self.assertEqual(dataset.dataset_id, self.DS_ID)
self.assertEqual(dataset.project, self.PROJECT)
self.assertEqual(dataset.etag, resource['etag'])
self.assertEqual(dataset.full_dataset_id, resource['id'])
self.assertEqual(dataset.location, self.LOCATION)

conn.api_request.assert_called_once_with(
method='POST',
path=path,
data={
'datasetReference': {
'projectId': self.PROJECT,
'datasetId': self.DS_ID,
},
'labels': {},
'location': self.LOCATION,
})

def test_create_dataset_w_string(self):
path = '/projects/%s/datasets' % self.PROJECT
resource = {
'datasetReference':
{'projectId': self.PROJECT, 'datasetId': self.DS_ID},
'etag': "etag",
'id': "%s:%s" % (self.PROJECT, self.DS_ID),
'location': self.LOCATION,
}
creds = _make_credentials()
client = self._make_one(
project=self.PROJECT, credentials=creds, location=self.LOCATION)
conn = client._connection = _make_connection(resource)

dataset = client.create_dataset(self.DS_ID)

self.assertEqual(dataset.dataset_id, self.DS_ID)
self.assertEqual(dataset.project, self.PROJECT)
self.assertEqual(dataset.etag, resource['etag'])
self.assertEqual(dataset.full_dataset_id, resource['id'])
self.assertEqual(dataset.location, self.LOCATION)

conn.api_request.assert_called_once_with(
method='POST',
path=path,
data={
'datasetReference': {
'projectId': self.PROJECT,
'datasetId': self.DS_ID,
},
'labels': {},
'location': self.LOCATION,
})

def test_create_table_w_day_partition(self):
from google.cloud.bigquery.table import Table
from google.cloud.bigquery.table import TimePartitioning
Expand Down Expand Up @@ -991,6 +1094,97 @@ def test_create_table_w_external(self):
SourceFormat.CSV)
self.assertEqual(got.external_data_configuration.autodetect, True)

def test_create_table_w_reference(self):
path = 'projects/%s/datasets/%s/tables' % (
self.PROJECT, self.DS_ID)
creds = _make_credentials()
client = self._make_one(project=self.PROJECT, credentials=creds)
resource = {
'id': '%s:%s:%s' % (self.PROJECT, self.DS_ID, self.TABLE_ID),
'tableReference': {
'projectId': self.PROJECT,
'datasetId': self.DS_ID,
'tableId': self.TABLE_ID
},
}
conn = client._connection = _make_connection(resource)

got = client.create_table(self.TABLE_REF)

conn.api_request.assert_called_once_with(
method='POST',
path='/%s' % path,
data={
'tableReference': {
'projectId': self.PROJECT,
'datasetId': self.DS_ID,
'tableId': self.TABLE_ID
},
'labels': {},
})
self.assertEqual(got.table_id, self.TABLE_ID)

def test_create_table_w_fully_qualified_string(self):
path = 'projects/%s/datasets/%s/tables' % (
self.PROJECT, self.DS_ID)
creds = _make_credentials()
client = self._make_one(project=self.PROJECT, credentials=creds)
resource = {
'id': '%s:%s:%s' % (self.PROJECT, self.DS_ID, self.TABLE_ID),
'tableReference': {
'projectId': self.PROJECT,
'datasetId': self.DS_ID,
'tableId': self.TABLE_ID
},
}
conn = client._connection = _make_connection(resource)

got = client.create_table(
'{}.{}.{}'.format(self.PROJECT, self.DS_ID, self.TABLE_ID))

conn.api_request.assert_called_once_with(
method='POST',
path='/%s' % path,
data={
'tableReference': {
'projectId': self.PROJECT,
'datasetId': self.DS_ID,
'tableId': self.TABLE_ID
},
'labels': {},
})
self.assertEqual(got.table_id, self.TABLE_ID)

def test_create_table_w_string(self):
path = 'projects/%s/datasets/%s/tables' % (
self.PROJECT, self.DS_ID)
creds = _make_credentials()
client = self._make_one(project=self.PROJECT, credentials=creds)
resource = {
'id': '%s:%s:%s' % (self.PROJECT, self.DS_ID, self.TABLE_ID),
'tableReference': {
'projectId': self.PROJECT,
'datasetId': self.DS_ID,
'tableId': self.TABLE_ID
},
}
conn = client._connection = _make_connection(resource)

got = client.create_table('{}.{}'.format(self.DS_ID, self.TABLE_ID))

conn.api_request.assert_called_once_with(
method='POST',
path='/%s' % path,
data={
'tableReference': {
'projectId': self.PROJECT,
'datasetId': self.DS_ID,
'tableId': self.TABLE_ID
},
'labels': {},
})
self.assertEqual(got.table_id, self.TABLE_ID)

def test_get_table(self):
path = 'projects/%s/datasets/%s/tables/%s' % (
self.PROJECT, self.DS_ID, self.TABLE_ID)
Expand Down