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 2 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
36 changes: 36 additions & 0 deletions bigquery/google/cloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,42 @@ 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.

Raises:
Copy link
Contributor

Choose a reason for hiding this comment

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

The Raises section can be removed when the setter is removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the raises comment since setter was removed!

ValueError: For invalid value types.
"""
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)
)

@expires.setter
Copy link
Contributor

Choose a reason for hiding this comment

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

TableListItem is read-only, so a setter is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the setter.

def expires(self, value):
if not isinstance(value, datetime.datetime) and value is not None:
raise ValueError("Pass a datetime, or None")
value_ms = google.cloud._helpers._millis_from_datetime(value)
self._properties["tableReference"]["expirationTime"] = _helpers._str_or_none(
value_ms
)

@property
def project(self):
"""str: Project bound to the table."""
Expand Down
21 changes: 21 additions & 0 deletions bigquery/tests/unit/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1115,11 +1115,20 @@ 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.006
Copy link
Contributor

Choose a reason for hiding this comment

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

.006 is not accurately representable in base 2, so you may encounter rounding issues with this value. Choose a fraction such as .125 instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made change to .125 as recommended.

self.WHEN = datetime.datetime.utcfromtimestamp(self.WHEN_TS).replace(tzinfo=UTC)

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

table = self._make_one(resource)

if "creationTime" in resource:
Copy link
Contributor

Choose a reason for hiding this comment

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

creationTime is in resource, as you are setting it above. This if statement is not needed. Just assert on the expected value. Same applies for expirationTime.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the If statements for creation an expiration, added asserts on expected value.

self.assertEqual(table.created, self.WHEN)
else:
self.assertIsNone(table.created)

if "expirationTime" in resource:
self.assertEqual(table.expires, self.EXP_TIME)
else:
self.assertIsNone(table.expires)

self.assertEqual(table.project, project)
self.assertEqual(table.dataset_id, dataset_id)
self.assertEqual(table.table_id, table_id)
Expand All @@ -1154,6 +1174,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)

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