Skip to content

Commit

Permalink
chore: drop use of 'pytz' in tests / examples (googleapis#534)
Browse files Browse the repository at this point in the history
Current (pre-)releases of 'google-api-core' and 'google-cloud-core' no
longer use or depend on 'pytz'.

Closes googleapis#533.
  • Loading branch information
tseaver authored and cojenco committed Oct 13, 2021
1 parent 274ddd4 commit 4907e96
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
4 changes: 2 additions & 2 deletions google/cloud/storage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1613,10 +1613,10 @@ def generate_signed_post_policy_v4(
Example:
Generate signed POST policy and upload a file.
>>> import datetime
>>> from google.cloud import storage
>>> import pytz
>>> client = storage.Client()
>>> tz = pytz.timezone('America/New_York')
>>> tz = datetime.timezone(datetime.timedelta(hours=1), 'CET')
>>> policy = client.generate_signed_post_policy_v4(
"bucket-name",
"blob-name",
Expand Down
33 changes: 20 additions & 13 deletions tests/unit/test__signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ def _utc_seconds(when):
return int(calendar.timegm(when.timetuple()))


def _make_cet_timezone():
try:
from datetime import timezone

except ImportError: # Python 2.7
from google.cloud._helpers import _UTC

class CET(_UTC):
_tzname = "CET"
_utcoffset = datetime.timedelta(hours=1)

return CET()
else:
from datetime import timedelta

return timezone(timedelta(hours=1), name="CET")


class Test_get_expiration_seconds_v2(unittest.TestCase):
@staticmethod
def _call_fut(expiration):
Expand Down Expand Up @@ -81,13 +99,7 @@ def test_w_expiration_utc_datetime(self):
self.assertEqual(self._call_fut(expiration_utc), utc_seconds)

def test_w_expiration_other_zone_datetime(self):
from google.cloud._helpers import _UTC

class CET(_UTC):
_tzname = "CET"
_utcoffset = datetime.timedelta(hours=1)

zone = CET()
zone = _make_cet_timezone()
expiration_other = datetime.datetime(2004, 8, 19, 0, 0, 0, 0, zone)
utc_seconds = _utc_seconds(expiration_other)
cet_seconds = utc_seconds - (60 * 60) # CET one hour earlier than UTC
Expand Down Expand Up @@ -198,13 +210,8 @@ def test_w_expiration_utc_datetime(self):

def test_w_expiration_other_zone_datetime(self):
from google.cloud._helpers import UTC
from google.cloud._helpers import _UTC

class CET(_UTC):
_tzname = "CET"
_utcoffset = datetime.timedelta(hours=1)

zone = CET()
zone = _make_cet_timezone()
fake_utcnow = datetime.datetime(2004, 8, 19, 0, 0, 0, 0, UTC)
fake_cetnow = fake_utcnow.astimezone(zone)
delta = datetime.timedelta(seconds=10)
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,10 @@ def test_ctor_defaults(self):

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

bucket = self._make_bucket()
now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
now = datetime.datetime.utcnow().replace(tzinfo=UTC)

config = self._make_one(
bucket,
Expand Down Expand Up @@ -403,10 +403,10 @@ def test_ctor_explicit_pap(self):

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

bucket = self._make_bucket()
now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
now = datetime.datetime.utcnow().replace(tzinfo=UTC)

config = pytest.deprecated_call(
self._make_one,
Expand All @@ -433,10 +433,10 @@ def test_ctor_ubla_and_bpo_enabled(self):

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

bucket = self._make_bucket()
now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
now = datetime.datetime.utcnow().replace(tzinfo=UTC)

with self.assertRaises(ValueError):
self._make_one(
Expand Down Expand Up @@ -481,12 +481,12 @@ def test_from_api_repr_w_disabled(self):

def test_from_api_repr_w_enabled(self):
import datetime
import pytz
from google.cloud._helpers import UTC
from google.cloud._helpers import _datetime_to_rfc3339

klass = self._get_target_class()
bucket = self._make_bucket()
now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
now = datetime.datetime.utcnow().replace(tzinfo=UTC)
resource = {
"uniformBucketLevelAccess": {
"enabled": True,
Expand Down Expand Up @@ -2174,11 +2174,11 @@ def test_iam_configuration_policy_missing(self):

def test_iam_configuration_policy_w_entry(self):
import datetime
import pytz
from google.cloud._helpers import UTC
from google.cloud._helpers import _datetime_to_rfc3339
from google.cloud.storage.bucket import IAMConfiguration

now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
now = datetime.datetime.utcnow().replace(tzinfo=UTC)
NAME = "name"
properties = {
"iamConfiguration": {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,7 @@ def _create_hmac_key_helper(
self, explicit_project=None, user_project=None, timeout=None, retry=None,
):
import datetime
from pytz import UTC
from google.cloud._helpers import UTC
from google.cloud.storage.hmac_key import HMACKeyMetadata

project = "PROJECT"
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_hmac_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def test_state_setter_active(self):

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

metadata = self._make_one()
now = datetime.datetime.utcnow()
Expand All @@ -183,7 +183,7 @@ def test_time_created_getter(self):

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

metadata = self._make_one()
now = datetime.datetime.utcnow()
Expand Down

0 comments on commit 4907e96

Please sign in to comment.