Skip to content

Commit

Permalink
Make python tests use timezone, py3.12 deprecated tz-less utcnow()
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
smemsh committed Aug 24, 2024
1 parent 0afc3ab commit 3a1729f
Show file tree
Hide file tree
Showing 22 changed files with 220 additions and 207 deletions.
23 changes: 12 additions & 11 deletions test/annotate.t
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
import os
import sys
import unittest
from datetime import datetime, timedelta
from datetime import datetime, timezone, timedelta
from dateutil import tz

# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -44,7 +45,7 @@ class TestAnnotate(TestCase):

def test_add_annotation_to_open_interval(self):
"""Add an annotation to an open interval"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
one_hour_before_utc = now_utc - timedelta(hours=1)

self.t("start {:%Y-%m-%dT%H:%M:%S}Z".format(one_hour_before_utc))
Expand All @@ -58,7 +59,7 @@ class TestAnnotate(TestCase):

def test_should_use_default_on_missing_id_and_active_time_tracking(self):
"""Use open interval when adding annotation with missing id and active time tracking"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
one_hour_before_utc = now_utc - timedelta(hours=1)
two_hours_before_utc = now_utc - timedelta(hours=2)

Expand All @@ -81,7 +82,7 @@ class TestAnnotate(TestCase):

def test_should_fail_on_missing_id_and_inactive_time_tracking(self):
"""Adding annotation with missing id on inactive time tracking is an error"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
one_hour_before_utc = now_utc - timedelta(hours=1)

self.t("track {:%Y-%m-%dT%H:%M:%S}Z - {:%Y-%m-%dT%H:%M:%S}Z".format(one_hour_before_utc, now_utc))
Expand All @@ -92,7 +93,7 @@ class TestAnnotate(TestCase):

def test_remove_annotation_from_interval(self):
"""Calling 'annotate' without annotation removes annotation"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
one_hour_before_utc = now_utc - timedelta(hours=1)

self.t("track {:%Y-%m-%dT%H:%M:%S}Z - {:%Y-%m-%dT%H:%M:%S}Z".format(one_hour_before_utc, now_utc))
Expand All @@ -106,7 +107,7 @@ class TestAnnotate(TestCase):

def test_add_annotation_to_closed_interval(self):
"""Add an annotation to a closed interval"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
one_hour_before_utc = now_utc - timedelta(hours=1)

self.t("track {:%Y-%m-%dT%H:%M:%S}Z - {:%Y-%m-%dT%H:%M:%S}Z".format(one_hour_before_utc, now_utc))
Expand All @@ -120,7 +121,7 @@ class TestAnnotate(TestCase):

def test_add_annotation_to_multiple_intervals(self):
"""Add an annotation to multiple intervals"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
one_hour_before_utc = now_utc - timedelta(hours=1)
two_hours_before_utc = now_utc - timedelta(hours=2)

Expand All @@ -141,7 +142,7 @@ class TestAnnotate(TestCase):
three_hours_before = now - timedelta(hours=3)
four_hours_before = now - timedelta(hours=4)

now_utc = now.utcnow()
now_utc = now.replace(tzinfo=tz.tzlocal()).astimezone(timezone.utc)
three_hours_before_utc = now_utc - timedelta(hours=3)
four_hours_before_utc = now_utc - timedelta(hours=4)
five_hours_before_utc = now_utc - timedelta(hours=5)
Expand All @@ -166,7 +167,7 @@ class TestAnnotate(TestCase):

def test_annotate_with_identical_ids(self):
"""Call 'annotate' with identical ids"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
one_hour_before_utc = now_utc - timedelta(hours=1)

self.t("track {:%Y-%m-%dT%H:%M:%S}Z - {:%Y-%m-%dT%H:%M:%S}Z".format(one_hour_before_utc, now_utc))
Expand All @@ -179,7 +180,7 @@ class TestAnnotate(TestCase):

def test_annotate_with_embedded_quotes(self):
"""Call 'annotate' with embedded quotes"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
one_hour_before_utc = now_utc - timedelta(hours=1)

self.t("track {:%Y-%m-%dT%H:%M:%S}Z - {:%Y-%m-%dT%H:%M:%S}Z".format(one_hour_before_utc, now_utc))
Expand All @@ -198,7 +199,7 @@ class TestAnnotate(TestCase):
three_hours_before = now - timedelta(hours=3)
four_hours_before = now - timedelta(hours=4)

now_utc = now.utcnow()
now_utc = now.replace(tzinfo=tz.tzlocal()).astimezone(timezone.utc)
three_hours_before_utc = now_utc - timedelta(hours=3)
four_hours_before_utc = now_utc - timedelta(hours=4)
five_hours_before_utc = now_utc - timedelta(hours=5)
Expand Down
6 changes: 3 additions & 3 deletions test/cancel.t
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import os
import sys
import unittest
from datetime import datetime, timedelta
from datetime import datetime, timezone, timedelta

# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -44,7 +44,7 @@ class TestCancel(TestCase):

def test_cancel_inactive_time_tracking(self):
"""Verify cancelling inactive time tracking"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)

one_hour_before_utc = now_utc - timedelta(hours=1)

Expand Down Expand Up @@ -72,7 +72,7 @@ class TestCancel(TestCase):

def test_cancel_active_time_tracking(self):
"""Verify cancelling active time tracking"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)

one_hour_before_utc = now_utc - timedelta(hours=1)

Expand Down
4 changes: 2 additions & 2 deletions test/cli.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import os
import shutil
import sys
import unittest
from datetime import datetime, timedelta
from datetime import datetime, timezone, timedelta

# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
Expand Down Expand Up @@ -60,7 +60,7 @@ class TestCLI(TestCase):

def test_tag_database_is_recreated(self):
"""Verify that calling 'timew' recreates tag database"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)

four_hours_before_utc = now_utc - timedelta(hours=4)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand Down
24 changes: 12 additions & 12 deletions test/continue.t
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import os
import sys
import unittest
from datetime import datetime, timedelta
from datetime import datetime, timezone, timedelta

# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
Expand Down Expand Up @@ -136,7 +136,7 @@ class TestContinue(TestCase):

def test_continue_with_tag_without_active_tracking(self):
"""Verify that continuing a specified interval works"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)

two_hours_before_utc = now_utc - timedelta(hours=2)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand Down Expand Up @@ -174,7 +174,7 @@ class TestContinue(TestCase):

def test_continue_with_tag_with_active_tracking(self):
"""Verify that continuing a specified interval stops active tracking"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)

two_hours_before_utc = now_utc - timedelta(hours=2)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand Down Expand Up @@ -218,7 +218,7 @@ class TestContinue(TestCase):

def test_continue_with_tag_and_date(self):
"""Verify that continuing an interval specified by tag with date continues at given date"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)

two_hours_before_utc = now_utc - timedelta(hours=2)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand Down Expand Up @@ -259,7 +259,7 @@ class TestContinue(TestCase):

def test_continue_with_tag_and_range(self):
"""Verify that continue an interval specified by tag with a range adds a copy with same tags at given range"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)

one_hour_before_utc = now_utc - timedelta(hours=1)
two_hours_before_utc = now_utc - timedelta(hours=2)
Expand Down Expand Up @@ -308,7 +308,7 @@ class TestContinue(TestCase):

def test_continue_with_id_and_date(self):
"""Verify that continuing a specified interval with date continues at given date"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)

two_hours_before_utc = now_utc - timedelta(hours=2)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand Down Expand Up @@ -349,7 +349,7 @@ class TestContinue(TestCase):

def test_continue_without_adjust_hint(self):
"""Verify that continuing without the :adjust hint fails to overwrite"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)

two_hours_before_utc = now_utc - timedelta(hours=2)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand All @@ -363,7 +363,7 @@ class TestContinue(TestCase):

def test_continue_with_adjust_hint(self):
"""Verify that continuing with the :adjust hint works"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)

two_hours_before_utc = now_utc - timedelta(hours=2)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand Down Expand Up @@ -395,7 +395,7 @@ class TestContinue(TestCase):

def test_continue_with_id_is_idempotent(self):
"""Verify that continuing with id is idempotent"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)

two_hours_before_utc = now_utc - timedelta(hours=2)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand Down Expand Up @@ -427,7 +427,7 @@ class TestContinue(TestCase):

def test_continue_with_tag_is_idempotent(self):
"""Verify that continuing with id is idempotent"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)

two_hours_before_utc = now_utc - timedelta(hours=2)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand Down Expand Up @@ -459,7 +459,7 @@ class TestContinue(TestCase):

def test_continue_with_id_and_range(self):
"""Verify that continue with a range adds a copy with same tags"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)

one_hour_before_utc = now_utc - timedelta(hours=1)
two_hours_before_utc = now_utc - timedelta(hours=2)
Expand Down Expand Up @@ -503,7 +503,7 @@ class TestContinue(TestCase):

def test_continue_with_future_time(self):
"""Verify that continue fails with time in the future"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)

one_hour_before_utc = now_utc - timedelta(hours=1)
two_hours_before_utc = now_utc - timedelta(hours=2)
Expand Down
11 changes: 6 additions & 5 deletions test/delete.t
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
import os
import sys
import unittest
from datetime import datetime, timedelta
from datetime import datetime, timezone, timedelta
from dateutil import tz

# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -44,7 +45,7 @@ class TestDelete(TestCase):

def test_delete_open(self):
"""Delete a single open interval"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
five_hours_before_utc = now_utc - timedelta(hours=5)
four_hours_before_utc = now_utc - timedelta(hours=4)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand All @@ -68,7 +69,7 @@ class TestDelete(TestCase):

def test_delete_closed(self):
"""Delete a single closed interval"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
five_hours_before_utc = now_utc - timedelta(hours=5)
four_hours_before_utc = now_utc - timedelta(hours=4)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand All @@ -92,7 +93,7 @@ class TestDelete(TestCase):

def test_delete_multiple(self):
"""Delete a mix of open/closed intervals"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
five_hours_before_utc = now_utc - timedelta(hours=5)
four_hours_before_utc = now_utc - timedelta(hours=4)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand All @@ -113,7 +114,7 @@ class TestDelete(TestCase):
three_hours_before = now - timedelta(hours=3)
four_hours_before = now - timedelta(hours=4)

now_utc = now.utcnow()
now_utc = now.replace(tzinfo=tz.tzlocal()).astimezone(timezone.utc)
four_hours_before_utc = now_utc - timedelta(hours=4)
five_hours_before_utc = now_utc - timedelta(hours=5)

Expand Down
18 changes: 9 additions & 9 deletions test/dom.t
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import os
import sys
import unittest

from datetime import datetime, timedelta
from datetime import datetime, timezone, timedelta

# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
Expand Down Expand Up @@ -174,7 +174,7 @@ class TestDOMTracked(TestCase):

def test_dom_tracked_tags_with_no_tags(self):
"""Test 'dom.tracked.tags' with no tags"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
four_hours_before_utc = now_utc - timedelta(hours=4)
five_hours_before_utc = now_utc - timedelta(hours=5)

Expand All @@ -185,7 +185,7 @@ class TestDOMTracked(TestCase):

def test_dom_tracked_tags_with_tags(self):
"""Test 'dom.tracked.tags' with tags"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
two_hours_before_utc = now_utc - timedelta(hours=2)
three_hours_before_utc = now_utc - timedelta(hours=3)
four_hours_before_utc = now_utc - timedelta(hours=4)
Expand All @@ -199,7 +199,7 @@ class TestDOMTracked(TestCase):

def test_dom_tracked_tags_with_quoted_tag(self):
"""Test 'dom.tracked.tags' with a tag with quotes"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
two_hours_before_utc = now_utc - timedelta(hours=2)
three_hours_before_utc = now_utc - timedelta(hours=3)
four_hours_before_utc = now_utc - timedelta(hours=4)
Expand All @@ -213,7 +213,7 @@ class TestDOMTracked(TestCase):

def test_dom_tracked_tags_filtered_by_time(self):
"""Test 'dom.tracked.tags' with tags filtered by time"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
one_hour_before_utc = now_utc - timedelta(hours=1)
two_hours_before_utc = now_utc - timedelta(hours=2)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand All @@ -229,7 +229,7 @@ class TestDOMTracked(TestCase):

def test_dom_tracked_tags_filtered_by_tag(self):
"""Test 'dom.tracked.tags' with tags filtered by tag"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
one_hour_before_utc = now_utc - timedelta(hours=1)
two_hours_before_utc = now_utc - timedelta(hours=2)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand All @@ -250,7 +250,7 @@ class TestDOMTracked(TestCase):

def test_dom_tracked_ids(self):
"""Test 'dom.tracked.ids'"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
four_hours_before_utc = now_utc - timedelta(hours=4)
five_hours_before_utc = now_utc - timedelta(hours=5)

Expand All @@ -261,7 +261,7 @@ class TestDOMTracked(TestCase):

def test_dom_tracked_ids_filtered_by_time(self):
"""Test 'dom.tracked.ids' filtered by time"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
one_hour_before_utc = now_utc - timedelta(hours=1)
two_hours_before_utc = now_utc - timedelta(hours=2)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand All @@ -277,7 +277,7 @@ class TestDOMTracked(TestCase):

def test_dom_tracked_ids_filtered_by_tag(self):
"""Test 'dom.tracked.ids' filtered by tag"""
now_utc = datetime.now().utcnow()
now_utc = datetime.now(timezone.utc)
one_hour_before_utc = now_utc - timedelta(hours=1)
two_hours_before_utc = now_utc - timedelta(hours=2)
three_hours_before_utc = now_utc - timedelta(hours=3)
Expand Down
Loading

0 comments on commit 3a1729f

Please sign in to comment.