Don't convert time when adding timezone in getter. Refactor time comparison in tests to correct direction (avoid comparing against negative deltas)

This commit is contained in:
Jon Pentland 2022-10-23 10:24:59 +02:00
parent 2df8ff4ea7
commit 0d643964a2
2 changed files with 7 additions and 9 deletions

View File

@ -142,7 +142,7 @@ class Comment(
old_date = super(Comment, self).__getattribute__(attr) old_date = super(Comment, self).__getattribute__(attr)
if old_date.tzinfo is None: if old_date.tzinfo is None:
# Naive dates were always stored utc # Naive dates were always stored utc
return old_date.astimezone(timezone.utc) return old_date.replace(tzinfo=timezone.utc)
return old_date return old_date
return super(Comment, self).__getattribute__(attr) return super(Comment, self).__getattribute__(attr)

View File

@ -79,9 +79,9 @@ class ConversationTest(unittest.TestCase):
self.assertEqual(len(tuple(conversation.getThreads())), 1) self.assertEqual(len(tuple(conversation.getThreads())), 1)
self.assertEqual(conversation.total_comments(), 1) self.assertEqual(conversation.total_comments(), 1)
self.assertTrue( self.assertTrue(
conversation.last_comment_date datetime.now().astimezone(tz.gettz(default_timezone()))
- datetime.now().astimezone(tz.gettz(default_timezone())) - conversation.last_comment_date
< timedelta(seconds=1), >= timedelta(seconds=0) <= timedelta(seconds=1),
) )
def test_timezone_naive_comment(self): def test_timezone_naive_comment(self):
@ -105,15 +105,13 @@ class ConversationTest(unittest.TestCase):
# Remove the timezone from the comment dates # Remove the timezone from the comment dates
comment.creation_date = datetime.utcnow() comment.creation_date = datetime.utcnow()
comment.modification_date = datetime.utcnow() comment.modification_date = datetime.utcnow()
comment.reindexObject()
# Check that the timezone naive date is converted to UTC # Check that the timezone naive date is converted to UTC
# See https://github.com/plone/plone.app.discussion/pull/204 # See https://github.com/plone/plone.app.discussion/pull/204
self.assertTrue( self.assertTrue(
conversation.last_comment_date datetime.utcnow().replace(tzinfo=timezone.utc)
- datetime.utcnow().replace(tzinfo=timezone.utc) - conversation.last_comment_date
< timedelta(seconds=10), >= timedelta(seconds=0) <= timedelta(seconds=1),
) )
self.assertTrue(comment.creation_date.tzinfo, timezone.utc) self.assertTrue(comment.creation_date.tzinfo, timezone.utc)
self.assertTrue(comment.modification_date.tzinfo, timezone.utc) self.assertTrue(comment.modification_date.tzinfo, timezone.utc)