Commit 32e04c8a authored by Jérome Perrin's avatar Jérome Perrin

ERP5TypeTestCase: support using pinDateTime as a context manager

exiting the context manager restores the original DateTime behavior
parent 2f359c8d
......@@ -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