Commit 4644c8f4 authored by Jérome Perrin's avatar Jérome Perrin

ERP5TypeTestCase: support using unpinDateTime as a context manager

exiting the context manager restores the original DateTime behavior
parent cc96b47a
Pipeline #20361 running with stage
in 0 seconds
......@@ -190,6 +190,14 @@ class TestPinDateTime(ERP5TypeTestCase):
self.unpinDateTime()
self.assertGreaterEqual(DateTime(), actual_begin_date)
def test_pinDateTime_context_manager(self):
actual_begin_date = DateTime()
datetime = DateTime('2001/01/01 01:01:01')
with self.pinDateTime(datetime):
self.assertEqual(DateTime(), datetime)
self.assertGreaterEqual(DateTime(), actual_begin_date)
def test_suite():
suite = unittest.TestSuite()
......
......@@ -372,12 +372,25 @@ class ERP5TypeTestCaseMixin(ProcessingNodeTestCase, PortalTestCase):
def pinDateTime(self, date_time):
# pretend time has stopped at a certain date (i.e. the test runs
# infinitely fast), to avoid errors on tests that are started
# 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
# reset.
global _pinned_date_time
assert date_time is None or isinstance(date_time, DateTime)
_pinned_date_time = date_time
unpinDateTime = self.unpinDateTime
class UnpinContextManager(object):
def __enter__(self):
return self
def __exit__(self, *args):
unpinDateTime()
return UnpinContextManager()
def unpinDateTime(self):
self.pinDateTime(None)
def setTimeZoneToUTC(self):
# Make sure tests runs with UTC timezone. Some tests are checking values
# based on now, and this could give unexpected results:
......@@ -389,9 +402,6 @@ class ERP5TypeTestCaseMixin(ProcessingNodeTestCase, PortalTestCase):
DateTime._isDST = False
DateTime._localzone = DateTime._localzone0 = DateTime._localzone1 = "UTC"
def unpinDateTime(self):
self.pinDateTime(None)
def getDefaultSystemPreference(self):
id = 'default_system_preference'
tool = self.getPreferenceTool()
......
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