Skip to content

Commit 07ad219

Browse files
lbristol88tswast
authored andcommitted
Adding creationTime and expirationTime properties to TableListItem (googleapis#7684)
* Added properties created and expires to TableListItem
1 parent f74ee83 commit 07ad219

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

bigquery/google/cloud/bigquery/table.py

+24
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,30 @@ def __init__(self, resource):
906906

907907
self._properties = resource
908908

909+
@property
910+
def created(self):
911+
"""Union[datetime.datetime, None]: Datetime at which the table was
912+
created (:data:`None` until set from the server).
913+
"""
914+
creation_time = self._properties.get("creationTime")
915+
if creation_time is not None:
916+
# creation_time will be in milliseconds.
917+
return google.cloud._helpers._datetime_from_microseconds(
918+
1000.0 * float(creation_time)
919+
)
920+
921+
@property
922+
def expires(self):
923+
"""Union[datetime.datetime, None]: Datetime at which the table will be
924+
deleted.
925+
"""
926+
expiration_time = self._properties.get("expirationTime")
927+
if expiration_time is not None:
928+
# expiration_time will be in milliseconds.
929+
return google.cloud._helpers._datetime_from_microseconds(
930+
1000.0 * float(expiration_time)
931+
)
932+
909933
@property
910934
def project(self):
911935
"""str: Project bound to the table."""

bigquery/tests/unit/test_table.py

+18
Original file line numberDiff line numberDiff line change
@@ -1115,11 +1115,24 @@ def _get_target_class():
11151115
def _make_one(self, *args, **kw):
11161116
return self._get_target_class()(*args, **kw)
11171117

1118+
def _setUpConstants(self):
1119+
import datetime
1120+
from google.cloud._helpers import UTC
1121+
1122+
self.WHEN_TS = 1437767599.125
1123+
self.WHEN = datetime.datetime.utcfromtimestamp(self.WHEN_TS).replace(tzinfo=UTC)
1124+
self.EXP_TIME = datetime.datetime(2015, 8, 1, 23, 59, 59, tzinfo=UTC)
1125+
11181126
def test_ctor(self):
1127+
from google.cloud._helpers import _millis
1128+
1129+
self._setUpConstants()
11191130
project = "test-project"
11201131
dataset_id = "test_dataset"
11211132
table_id = "coffee_table"
11221133
resource = {
1134+
"creationTime": self.WHEN_TS * 1000,
1135+
"expirationTime": _millis(self.EXP_TIME),
11231136
"kind": "bigquery#table",
11241137
"id": "{}:{}.{}".format(project, dataset_id, table_id),
11251138
"tableReference": {
@@ -1138,6 +1151,9 @@ def test_ctor(self):
11381151
}
11391152

11401153
table = self._make_one(resource)
1154+
1155+
self.assertEqual(table.created, self.WHEN)
1156+
self.assertEqual(table.expires, self.EXP_TIME)
11411157
self.assertEqual(table.project, project)
11421158
self.assertEqual(table.dataset_id, dataset_id)
11431159
self.assertEqual(table.table_id, table_id)
@@ -1204,6 +1220,8 @@ def test_ctor_missing_properties(self):
12041220
self.assertEqual(table.project, "testproject")
12051221
self.assertEqual(table.dataset_id, "testdataset")
12061222
self.assertEqual(table.table_id, "testtable")
1223+
self.assertIsNone(table.created)
1224+
self.assertIsNone(table.expires)
12071225
self.assertIsNone(table.full_table_id)
12081226
self.assertIsNone(table.friendly_name)
12091227
self.assertIsNone(table.table_type)

0 commit comments

Comments
 (0)