Skip to content

Commit

Permalink
Client: Fix tests for python 3.12 (#12697)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 2716e3ff146205d66f2aec419e81b82938587c11
  • Loading branch information
stephencpope authored and Descartes Labs Build committed Oct 8, 2024
1 parent 98fc65a commit 7bb227c
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 24 deletions.
3 changes: 2 additions & 1 deletion descarteslabs/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,8 @@ def _token_expired(self, payload, leeway=0):

if exp is not None:
now = (
datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)
datetime.datetime.now(datetime.timezone.utc)
- datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
).total_seconds()

return now + leeway > exp
Expand Down
3 changes: 2 additions & 1 deletion descarteslabs/auth/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ def test_token_in_leeway_autherror(self, _get_token):
client_id="ZOBAi4UROl5gKZIpxxlwOEfx8KpqXf2c",
)
exp = (
datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)
datetime.datetime.now(datetime.timezone.utc)
- datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
).total_seconds() + auth.leeway / 2
token = b".".join(
(
Expand Down
3 changes: 1 addition & 2 deletions descarteslabs/core/catalog/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,9 +983,8 @@ def test_ndarray_geocontext(self, mock_raster):
image = Image.get("landsat:LC08:PRE:TOAR:meta_LC80270312016188_v1")
arr, info = image.ndarray("red", resolution=1000, raster_info=True)

assert mock_raster.called_once()
mock_raster.assert_called_once()
assert image.geocontext.geometry is not None
print(mock_raster.call_args)
assert mock_raster.call_args[1]["cutline"] is None

@patch.object(Image, "get", _image_get)
Expand Down
30 changes: 16 additions & 14 deletions descarteslabs/core/common/client/tests/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class MyDocument(Document):
name: str = Attribute(str)
local: str = Attribute(str, default="local", sticky=True)
once: int = Attribute(int, mutable=False)
default: datetime = DatetimeAttribute(default=lambda: datetime.utcnow())
default: datetime = DatetimeAttribute(
default=lambda: datetime.now(timezone.utc).replace(tzinfo=None)
)
created_at: datetime = DatetimeAttribute(readonly=True)


Expand Down Expand Up @@ -94,7 +96,7 @@ def test_attribute_readonly(self):
assert "Unable to set readonly attribute 'id'" == str(ctx.exception)

def test_init_from_server(self):
now = datetime.utcnow()
now = datetime.now(timezone.utc)
# 2000-01-01, if set to 0 astimezone on windows in python 3.8 will error
timestamp = 946710000
data = {
Expand All @@ -113,12 +115,12 @@ def test_init_from_server(self):
assert doc.local == "local"
assert doc.once == 2
assert doc.default == datetime.fromtimestamp(timestamp, tz=timezone.utc)
assert doc.created_at == now.replace(tzinfo=timezone.utc)
assert doc.created_at == now
with self.assertRaises(AttributeError):
doc.extra

def test_set_from_server(self):
now = datetime.utcnow()
now = datetime.now(timezone.utc)
doc = MyDocument(name="local", once="1", default=now)
# 2000-01-01, if set to 0 astimezone on windows in python 3.8 will error
timestamp = 946710000
Expand All @@ -138,7 +140,7 @@ def test_set_from_server(self):
assert doc.local == "local"
assert doc.once == 2
assert doc.default == datetime.fromtimestamp(timestamp, tz=timezone.utc)
assert doc.created_at == now.replace(tzinfo=timezone.utc)
assert doc.created_at == now

def test_to_dict(self):
doc = MyDocument(name="local", once="1")
Expand All @@ -165,30 +167,30 @@ def test_local_time(self):
class TzTest(Document):
date: datetime = DatetimeAttribute(timezone=ZoneInfo("MST"))

now = datetime.utcnow()
now = datetime.now(timezone.utc)
doc = TzTest(date=now.isoformat())
assert doc.date.tzinfo == ZoneInfo("MST")
assert doc.date.astimezone(tz=timezone.utc) == now.replace(tzinfo=timezone.utc)

assert doc.to_dict()["date"] == now.replace(tzinfo=timezone.utc).isoformat()
assert doc.to_dict()["date"] == now.isoformat()

def test_trailing_z(self):
class TrailingTest(Document):
date: datetime = DatetimeAttribute()

now = datetime.utcnow()
doc = TrailingTest(date=now.isoformat() + "Z")
doc.date == now.replace(tzinfo=timezone.utc)
now = datetime.now(timezone.utc)
doc = TrailingTest(date=now.isoformat()[:-6] + "Z")
assert doc.date == now

def test_assign_instance(self):
tz = ZoneInfo("MST")

class InstanceTest(Document):
date: datetime = DatetimeAttribute(timezone=tz)

now = datetime.utcnow()
now = datetime.now(timezone.utc)
doc = InstanceTest(date=now)
assert doc.date == now.replace(tzinfo=timezone.utc).astimezone(tz=tz)
assert doc.date == now.astimezone(tz=tz)

def test_validation(self):
class ValidationTest(Document):
Expand All @@ -200,9 +202,9 @@ class ValidationTest(Document):
ctx.exception
)

now = datetime.utcnow()
now = datetime.now(timezone.utc)
doc = ValidationTest(date=now.timestamp())
assert doc.date == now.replace(tzinfo=timezone.utc)
assert doc.date == now

def test_serialize_filter(self):
with self.assertRaises(ValueError) as ctx:
Expand Down
8 changes: 6 additions & 2 deletions descarteslabs/core/common/retry/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ def _handle_exception(self, exception, previous_exceptions):

def _check_retries(self, retries, name, deadline, previous_exceptions):
# Raise RetryError if deadline exceeded
if deadline is not None and deadline <= datetime.datetime.utcnow():
if deadline is not None and deadline <= datetime.datetime.now(
datetime.timezone.utc
).replace(tzinfo=None):
raise RetryError(
"Deadline of {:.1f}s exceeded while calling {}".format(deadline, name),
previous_exceptions,
Expand All @@ -204,7 +206,9 @@ def _deadline_datetime(deadline):
if deadline is None:
return None

return datetime.datetime.utcnow() + datetime.timedelta(seconds=deadline)
return datetime.datetime.now(datetime.timezone.utc).replace(
tzinfo=None
) + datetime.timedelta(seconds=deadline)


class RetryError(Exception):
Expand Down
9 changes: 7 additions & 2 deletions descarteslabs/core/compute/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import time
import urllib.parse
import uuid
from datetime import datetime
from datetime import datetime, timezone
from unittest import TestCase

import responses
Expand Down Expand Up @@ -40,7 +40,7 @@ def setUp(self):
del os.environ[envvar]

responses.mock.assert_all_requests_are_fired = True
self.now = datetime.utcnow()
self.now = datetime.now(timezone.utc).replace(tzinfo=None)

payload = (
base64.b64encode(
Expand Down Expand Up @@ -200,3 +200,8 @@ def assert_url_called(self, uri, times=1, json=None, body=None, params=None):
msg += "\n\nParams:\n" + "\n".join(calls_with_params)

assert count == times, msg

# this was removed from python 3.12 unittest.TestCase
def assertDictContainsSubset(self, subset, dictionary):
for key, value in subset.items():
assert key in dictionary and dictionary[key] == value
4 changes: 2 additions & 2 deletions descarteslabs/core/vector/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time
import urllib.parse
import uuid
from datetime import datetime
from datetime import datetime, timezone
from unittest import TestCase

import geopandas as gpd
Expand Down Expand Up @@ -61,7 +61,7 @@ class BaseTestCase(TestCase):

def setUp(self):
responses.mock.assert_all_requests_are_fired = True
self.now = datetime.utcnow()
self.now = datetime.now(timezone.utc).replace(tzinfo=None)

payload = (
base64.b64encode(
Expand Down

0 comments on commit 7bb227c

Please sign in to comment.