Fix timezone tests

This commit is contained in:
Jon Pentland 2022-10-21 17:11:19 +02:00
parent f0cd076fd7
commit 9944796627
2 changed files with 22 additions and 17 deletions

View File

@ -9,6 +9,8 @@ from Acquisition import aq_parent
from datetime import datetime from datetime import datetime
from datetime import timedelta from datetime import timedelta
from datetime import timezone from datetime import timezone
from dateutil import tz
from plone.app.event.base import default_timezone
from plone.app.testing import setRoles from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID from plone.app.testing import TEST_USER_ID
from plone.app.vocabularies.types import BAD_TYPES from plone.app.vocabularies.types import BAD_TYPES
@ -72,7 +74,7 @@ class ConversationTest(unittest.TestCase):
self.assertEqual(conversation.total_comments(), 1) self.assertEqual(conversation.total_comments(), 1)
self.assertTrue( self.assertTrue(
conversation.last_comment_date conversation.last_comment_date
- datetime.now().astimezone(timezone.utc) - datetime.now().astimezone(tz.gettz(default_timezone()))
< timedelta(seconds=1), < timedelta(seconds=1),
) )
@ -99,7 +101,7 @@ class ConversationTest(unittest.TestCase):
# Check that the date is still correct # Check that the date is still correct
self.assertTrue( self.assertTrue(
conversation.last_comment_date conversation.last_comment_date
- datetime.now().astimezone(timezone.utc) - datetime.now().astimezone(tz.gettz(default_timezone()))
< timedelta(seconds=1), < timedelta(seconds=1),
) )
# Check that comments still have timezones # Check that comments still have timezones
@ -522,30 +524,30 @@ class ConversationTest(unittest.TestCase):
comment1 = createObject("plone.Comment") comment1 = createObject("plone.Comment")
comment1.text = "Comment text" comment1.text = "Comment text"
comment1.creation_date =\ comment1.creation_date =\
datetime.now().astimezone(timezone.utc) - timedelta(4) datetime.now().astimezone(tz.gettz(default_timezone())) - timedelta(4)
conversation.addComment(comment1) conversation.addComment(comment1)
comment2 = createObject("plone.Comment") comment2 = createObject("plone.Comment")
comment2.text = "Comment text" comment2.text = "Comment text"
comment2.creation_date =\ comment2.creation_date =\
datetime.now().astimezone(timezone.utc) - timedelta(2) datetime.now().astimezone(tz.gettz(default_timezone())) - timedelta(2)
new_comment2_id = conversation.addComment(comment2) new_comment2_id = conversation.addComment(comment2)
comment3 = createObject("plone.Comment") comment3 = createObject("plone.Comment")
comment3.text = "Comment text" comment3.text = "Comment text"
comment3.creation_date =\ comment3.creation_date =\
datetime.now().astimezone(timezone.utc) - timedelta(1) datetime.now().astimezone(tz.gettz(default_timezone())) - timedelta(1)
new_comment3_id = conversation.addComment(comment3) new_comment3_id = conversation.addComment(comment3)
# check if the latest comment is exactly one day old # check if the latest comment is exactly one day old
self.assertTrue( self.assertTrue(
conversation.last_comment_date conversation.last_comment_date
< datetime.now().astimezone(timezone.utc) < datetime.now().astimezone(tz.gettz(default_timezone()))
- timedelta(hours=23, minutes=59, seconds=59), - timedelta(hours=23, minutes=59, seconds=59),
) )
self.assertTrue( self.assertTrue(
conversation.last_comment_date conversation.last_comment_date
> datetime.now().astimezone(timezone.utc) > datetime.now().astimezone(tz.gettz(default_timezone()))
- timedelta(days=1, seconds=1), - timedelta(days=1, seconds=1),
) )
@ -556,12 +558,12 @@ class ConversationTest(unittest.TestCase):
# the latest comment should be exactly two days old # the latest comment should be exactly two days old
self.assertTrue( self.assertTrue(
conversation.last_comment_date conversation.last_comment_date
< datetime.now().astimezone(timezone.utc) < datetime.now().astimezone(tz.gettz(default_timezone()))
- timedelta(days=1, hours=23, minutes=59, seconds=59), - timedelta(days=1, hours=23, minutes=59, seconds=59),
) )
self.assertTrue( self.assertTrue(
conversation.last_comment_date conversation.last_comment_date
> datetime.now().astimezone(timezone.utc) > datetime.now().astimezone(tz.gettz(default_timezone()))
- timedelta(days=2, seconds=1), - timedelta(days=2, seconds=1),
) )
@ -572,12 +574,12 @@ class ConversationTest(unittest.TestCase):
# the latest comment should be exactly four days old # the latest comment should be exactly four days old
self.assertTrue( self.assertTrue(
conversation.last_comment_date conversation.last_comment_date
< datetime.now().astimezone(timezone.utc) < datetime.now().astimezone(tz.gettz(default_timezone()))
- timedelta(days=3, hours=23, minutes=59, seconds=59), - timedelta(days=3, hours=23, minutes=59, seconds=59),
) )
self.assertTrue( self.assertTrue(
conversation.last_comment_date conversation.last_comment_date
> datetime.now().astimezone(timezone.utc) > datetime.now().astimezone(tz.gettz(default_timezone()))
- timedelta(days=4, seconds=2), - timedelta(days=4, seconds=2),
) )

View File

@ -15,8 +15,9 @@ from plone.indexer.delegate import DelegatingIndexerFactory
from plone.registry.interfaces import IRegistry from plone.registry.interfaces import IRegistry
from zope.component import createObject from zope.component import createObject
from zope.component import getUtility from zope.component import getUtility
import os
import time
import os
import unittest import unittest
@ -44,7 +45,8 @@ class ConversationIndexersTest(unittest.TestCase):
workflow.doActionFor(self.portal.doc1, "publish") workflow.doActionFor(self.portal.doc1, "publish")
# Change the timezone to europe to test timezones properly # Change the timezone to europe to test timezones properly
os.environ['TZ'] = 'UTC' os.environ['TZ'] = 'Europe/Berlin'
time.tzset()
reg_key = "plone.portal_timezone" reg_key = "plone.portal_timezone"
registry = getUtility(IRegistry) registry = getUtility(IRegistry)
registry[reg_key] = "Europe/Berlin" registry[reg_key] = "Europe/Berlin"
@ -139,7 +141,8 @@ class CommentIndexersTest(unittest.TestCase):
# factory to allow different factories to be swapped in # factory to allow different factories to be swapped in
# Change the timezone to europe to test timezones properly # Change the timezone to europe to test timezones properly
os.environ['TZ'] = 'UTC' os.environ['TZ'] = 'Europe/Berlin'
time.tzset()
reg_key = "plone.portal_timezone" reg_key = "plone.portal_timezone"
registry = getUtility(IRegistry) registry = getUtility(IRegistry)
registry[reg_key] = "Europe/Berlin" registry[reg_key] = "Europe/Berlin"
@ -189,15 +192,15 @@ class CommentIndexersTest(unittest.TestCase):
# Test if created, modified, effective etc. are set correctly # Test if created, modified, effective etc. are set correctly
self.assertEqual( self.assertEqual(
catalog.created(self.comment)(), catalog.created(self.comment)(),
DateTime(2006, 9, 17, 16, 18, 12, "GMT+2"), DateTime(2006, 9, 17, 14, 18, 12, "GMT+2"),
) )
self.assertEqual( self.assertEqual(
catalog.effective(self.comment)(), catalog.effective(self.comment)(),
DateTime(2006, 9, 17, 16, 18, 12, "GMT+2"), DateTime(2006, 9, 17, 14, 18, 12, "GMT+2"),
) )
self.assertEqual( self.assertEqual(
catalog.modified(self.comment)(), catalog.modified(self.comment)(),
DateTime(2008, 3, 12, 8, 32, 52, "GMT+1"), DateTime(2008, 3, 12, 7, 32, 52, "GMT+1"),
) )
def test_searchable_text(self): def test_searchable_text(self):