Commit e1756a5d authored by Jérome Perrin's avatar Jérome Perrin

tests: fix DateTime.isPast() under pinDateTime

When under a pinDateTime context, d.isPast() was comparing d with the
actual current date, not the fake date from the pinned context.

Also, make the methods class methods, so that they can be used as
external methods in Zelenium tests.
parent d4974e48
Pipeline #35651 failed with stage
in 0 seconds
......@@ -208,6 +208,7 @@ class TestPinDateTime(ERP5TypeTestCase):
self.pinDateTime(datetime)
self.assertEqual(DateTime(), datetime)
self.assertEqual(DateTime(None), datetime)
self.assertEqual(DateTime('2002/02/02 02:02:02 GMT+2'), fixed_date_with_timezone)
self.unpinDateTime()
......@@ -221,6 +222,24 @@ class TestPinDateTime(ERP5TypeTestCase):
self.assertEqual(DateTime(), datetime)
self.assertGreaterEqual(DateTime(), actual_begin_date)
def test_pinDateTime_date_time_methods(self):
with self.pinDateTime(DateTime('2001/01/01 01:01:01')):
self.assertTrue(DateTime('2000').isPast())
self.assertTrue(DateTime('2002').isFuture())
self.assertTrue(DateTime('2001').isCurrentYear())
self.assertTrue(DateTime('2001/01/02 01:01:01').isCurrentMonth())
self.assertTrue(DateTime('2001/01/01 02:01:01').isCurrentDay())
self.assertTrue(DateTime().strftime('%Y'), '2001')
self.assertTrue(DateTime('2002').strftime('%Y'), '2001')
def test_pinDateTime_timezone(self):
with self.pinDateTime(DateTime('2001/01/01 01:01:01 GMT+9')):
self.assertEqual(DateTime().timezone(), 'GMT+9')
self.assertEqual(DateTime('2001/01/01 01:01:01 GMT+4').timezone(), 'GMT+4')
with self.pinDateTime(DateTime('2001/01/01 01:01:01 Europe/Paris')):
self.assertEqual(DateTime().timezone(), 'Europe/Paris')
self.assertEqual(DateTime('2001/01/01 01:01:01 GMT+4').timezone(), 'GMT+4')
class TestTimeZoneContext(ERP5TypeTestCase):
def afterSetUp(self):
......
......@@ -189,6 +189,7 @@ def profile_if_environ(environment_var_name):
assert getattr(DateTime, '_original_parse_args', None) is None
DateTime._original_parse_args = DateTime._parse_args
_datetime_system_time_patcher = None
_pinned_date_time = None
def _parse_args(self, *args, **kw):
......@@ -368,26 +369,38 @@ class ERP5TypeTestCaseMixin(ProcessingNodeTestCase, PortalTestCase, functional.F
if not uf.getUserById(user_name):
uf._doAddUser(user_name, self.newPassword(), ['Member'], [])
def pinDateTime(self, date_time):
@classmethod
def pinDateTime(cls, date_time):
# pretend time has stopped at a certain date (i.e. the test runs
# infinitely fast), for example to avoid errors on tests that are started
# just before midnight.
# This can be used as a context manager, otherwise use unpinDateTime to
# This is best used as a context manager, otherwise use unpinDateTime to
# reset.
global _pinned_date_time
global _pinned_date_time, _datetime_system_time_patcher
assert date_time is None or isinstance(date_time, DateTime)
_pinned_date_time = date_time
unpinDateTime = self.unpinDateTime
if _datetime_system_time_patcher is not None:
_datetime_system_time_patcher.stop()
if date_time is not None:
_datetime_system_time_patcher = mock.patch.object(
sys.modules['DateTime.DateTime'],
'_system_time',
return_value=date_time.timeTime())
_datetime_system_time_patcher.start()
unpinDateTime = cls.unpinDateTime
class UnpinContextManager(object):
def __enter__(self):
return self
def __exit__(self, *args):
unpinDateTime()
_datetime_system_time_patcher.stop()
return UnpinContextManager()
def unpinDateTime(self):
self.pinDateTime(None)
@classmethod
def unpinDateTime(cls):
cls.pinDateTime(None)
def setTimeZoneToUTC(self):
# Deprecated, prefer using `timeZoneContext` context manager instead.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment