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

Adding creationTime and expirationTime properties to TableListItem #7684

Merged
merged 5 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 24 additions & 0 deletions bigquery/google/cloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,30 @@ def __init__(self, resource):

self._properties = resource

@property
def created(self):
"""Union[datetime.datetime, None]: Datetime at which the table was
created (:data:`None` until set from the server).
"""
creation_time = self._properties.get("creationTime")
if creation_time is not None:
# creation_time will be in milliseconds.
return google.cloud._helpers._datetime_from_microseconds(
1000.0 * float(creation_time)
)

@property
def expires(self):
"""Union[datetime.datetime, None]: Datetime at which the table will be
deleted.
"""
expiration_time = self._properties.get("expirationTime")
if expiration_time is not None:
# expiration_time will be in milliseconds.
return google.cloud._helpers._datetime_from_microseconds(
1000.0 * float(expiration_time)
)

@property
def project(self):
"""str: Project bound to the table."""
Expand Down
17 changes: 17 additions & 0 deletions bigquery/tests/unit/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1115,11 +1115,24 @@ def _get_target_class():
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def _setUpConstants(self):
import datetime
from google.cloud._helpers import UTC

self.WHEN_TS = 1437767599.125
self.WHEN = datetime.datetime.utcfromtimestamp(self.WHEN_TS).replace(tzinfo=UTC)
self.EXP_TIME = datetime.datetime(2015, 8, 1, 23, 59, 59, tzinfo=UTC)

def test_ctor(self):
from google.cloud._helpers import _millis

self._setUpConstants()
project = "test-project"
dataset_id = "test_dataset"
table_id = "coffee_table"
resource = {
"creationTime": self.WHEN_TS * 1000,
"expirationTime": _millis(self.EXP_TIME),
"kind": "bigquery#table",
"id": "{}:{}.{}".format(project, dataset_id, table_id),
"tableReference": {
Expand All @@ -1138,6 +1151,9 @@ def test_ctor(self):
}

table = self._make_one(resource)

self.assertEqual(table.created, self.WHEN)
self.assertEqual(table.expires, self.EXP_TIME)
self.assertEqual(table.project, project)
self.assertEqual(table.dataset_id, dataset_id)
self.assertEqual(table.table_id, table_id)
Expand All @@ -1154,6 +1170,7 @@ def test_ctor(self):
self.assertEqual(table.time_partitioning.field, "mycolumn")
self.assertEqual(table.labels["some-stuff"], "this-is-a-label")
self.assertIsNone(table.view_use_legacy_sql)
# self.assertIsNone(table.expires)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove, don't comment out.

Actually, could you move this assertIsNone for expires and add one for created to test_ctor_missing_properties?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for removing. Could you add assertions for None values to the test_ctor_missing_properties test, too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added assertions for created and expires under the test_ctor_missing_properties test.


with warnings.catch_warnings(record=True) as warned:
self.assertEqual(table.partitioning_type, "DAY")
Expand Down