Make python tests use timezone, py3.12 deprecated tz-less utcnow()

Python upstream is trying to eliminate tz-naive date functions that
imply anything to do with a timezone, even UTC.  They have deprecated
datetime.datetime.utcnow() in Python 3.12 and thus running tests emits
many warnings for us.

We switch to instantiate datetime objects taht are intended to reflect
UTC to have a timezone, or if we instantiate naive ones and then later
convert, we do the full conversion.

After the changes, the tests still work on Python 3.9, but now also on
Python 3.12 without warnings.

See PR description for #632 for more details.

Signed-off-by: Scott Mcdermott <scott@smemsh.net>
This commit is contained in:
Scott Mcdermott 2024-08-20 19:06:13 -07:00 committed by Thomas Lauf
parent 05b72bba6f
commit 05724a9d21
22 changed files with 220 additions and 207 deletions

View file

@ -29,7 +29,7 @@
import os
import sys
import unittest
from datetime import datetime, timedelta, time
from datetime import datetime, timezone, timedelta, time
# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
@ -53,7 +53,7 @@ class TestStart(TestCase):
def test_timed_start_past(self):
"""Test timed start past"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
one_hour_before_utc = now_utc - timedelta(hours=1)
@ -68,7 +68,7 @@ class TestStart(TestCase):
def test_timed_start_future(self):
"""Test timed start future"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
one_hour_after_utc = now_utc + timedelta(hours=1)
@ -131,7 +131,7 @@ class TestStart(TestCase):
def test_start_with_same_tags_as_current_tracking(self):
"""Test 'start' with same tags as current tracking should not start new tracking"""
utc_now = datetime.now().utcnow()
utc_now = datetime.now(timezone.utc)
one_hour_ago_utc = utc_now - timedelta(hours=1)
self.t("start {:%Y-%m-%dT%H:%M:%S}Z bar foo".format(one_hour_ago_utc))
@ -225,7 +225,7 @@ class TestStart(TestCase):
def test_start_with_new_tag(self):
"""Call 'start' with new tag"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
two_hours_before_utc = now_utc - timedelta(hours=2)
one_hour_before_utc = now_utc - timedelta(hours=1)
@ -238,7 +238,7 @@ class TestStart(TestCase):
def test_start_with_previous_tag(self):
"""Call 'start' with previous tag"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
three_hours_before_utc = now_utc - timedelta(hours=3)
two_hours_before_utc = now_utc - timedelta(hours=2)