Skip to content

Commit 0f5a1df

Browse files
committed
Merge pull request tylertreat#62 from hagino3000/add_table_expiration_time
Add expiration_time option for create_table
2 parents 508b3ed + f615350 commit 0f5a1df

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

bigquery/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,14 @@ def get_table(self, dataset, table):
384384

385385
return table
386386

387-
def create_table(self, dataset, table, schema):
387+
def create_table(self, dataset, table, schema, expiration_time=None):
388388
"""Create a new table in the dataset.
389389
390390
Args:
391391
dataset: the dataset to create the table in.
392392
table: the name of table to create.
393393
schema: table schema dict.
394+
expiration_time: the expiry time in milliseconds since the epoch.
394395
395396
Returns:
396397
bool indicating if the table was successfully created or not,
@@ -406,6 +407,9 @@ def create_table(self, dataset, table, schema):
406407
}
407408
}
408409

410+
if expiration_time is not None:
411+
body['expirationTime'] = expiration_time
412+
409413
try:
410414
table = self.bigquery.tables().insert(
411415
projectId=self.project_id,

bigquery/tests/test_client.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_initialize_readonly(self, mock_build, mock_return_cred):
6565
mock_return_cred.assert_called_once_with()
6666
mock_cred.assert_called_once_with(service_account, key,
6767
scope=BIGQUERY_SCOPE_READ_ONLY)
68-
mock_cred.authorize.assert_called_once()
68+
self.assertTrue(mock_cred.return_value.authorize.called)
6969
mock_build.assert_called_once_with('bigquery', 'v2', http=mock_http)
7070
self.assertEquals(mock_bq, bq_client.bigquery)
7171
self.assertEquals(project_id, bq_client.project_id)
@@ -95,7 +95,7 @@ def test_initialize_read_write(self, mock_build, mock_return_cred):
9595
mock_return_cred.assert_called_once_with()
9696
mock_cred.assert_called_once_with(service_account, key,
9797
scope=BIGQUERY_SCOPE)
98-
mock_cred.authorize.assert_called_once()
98+
self.assertTrue(mock_cred.return_value.authorize.called)
9999
mock_build.assert_called_once_with('bigquery', 'v2', http=mock_http)
100100
self.assertEquals(mock_bq, bq_client.bigquery)
101101
self.assertEquals(project_id, bq_client.project_id)
@@ -130,7 +130,7 @@ def test_initialize_key_file(self, mock_open, mock_build,
130130
mock_return_cred.assert_called_once_with()
131131
mock_cred.assert_called_once_with(service_account, key,
132132
scope=BIGQUERY_SCOPE)
133-
mock_cred.authorize.assert_called_once()
133+
self.assertTrue(mock_cred.return_value.authorize.called)
134134
mock_build.assert_called_once_with('bigquery', 'v2', http=mock_http)
135135
self.assertEquals(mock_bq, bq_client.bigquery)
136136
self.assertEquals(project_id, bq_client.project_id)
@@ -378,7 +378,7 @@ def test_get_response(self):
378378
projectId=self.project_id, jobId=job_id, startIndex=offset,
379379
maxResults=limit, pageToken=page_token, timeoutMs=1000)
380380

381-
mock_query_job.execute.assert_called_once()
381+
mock_query_job.execute.assert_called_once_with()
382382
self.assertEquals(actual, mock_query_reply)
383383

384384

@@ -1482,6 +1482,7 @@ def setUp(self):
14821482
'tableId': self.table, 'projectId': self.project,
14831483
'datasetId': self.dataset}
14841484
}
1485+
self.expiration_time = 1437513693000
14851486

14861487
def test_table_create_failed(self):
14871488
"""Ensure that if creating the table fails, False is returned,
@@ -1535,6 +1536,26 @@ def test_table_create_success(self):
15351536

15361537
self.mock_tables.insert.return_value.execute.assert_called_with()
15371538

1539+
def test_table_create_body_with_expiration_time(self):
1540+
"""Ensure that if expiration_time has specified,
1541+
it passed to the body."""
1542+
1543+
self.mock_tables.insert.return_value.execute.side_effect = [{
1544+
'status': 'foo'}, {'status': 'bar'}]
1545+
1546+
self.client.create_table(self.dataset, self.table,
1547+
self.schema, self.expiration_time)
1548+
1549+
body = self.body.copy()
1550+
body.update({
1551+
'expirationTime': self.expiration_time
1552+
})
1553+
1554+
self.mock_tables.insert.assert_called_with(
1555+
projectId=self.project, datasetId=self.dataset, body=body)
1556+
1557+
self.mock_tables.insert.return_value.execute.assert_called_with()
1558+
15381559

15391560
class TestCreateView(unittest.TestCase):
15401561

0 commit comments

Comments
 (0)