Commit 64e9f5d8 authored by Jérome Perrin's avatar Jérome Perrin

tests: target DateTime 3 and use mock to monkey patch

mock has several advantages, the main one here is that it errors when
patching does not replace an existing attribute, which happens when we
are not patching the right place.

These attributes are not the same place in DateTime 2 and 3, this code
when using DateTime 2 was replacing the attributes, but in DateTime 3 it
was just creating new attributes that were never used. Update the code
to patch the DateTime 3 location
parent 50a7580f
...@@ -26,17 +26,13 @@ ...@@ -26,17 +26,13 @@
############################################################################## ##############################################################################
import unittest import unittest
import os import os
import sys
import time import time
import mock
from unittest import expectedFailure from unittest import expectedFailure
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from DateTime import DateTime from DateTime import DateTime
# explicitly set Europe/Paris timezone
os.environ['TZ']='Europe/Paris'
time.tzset()
DateTime._localzone0 = 'GMT+1'
DateTime._localzone1 = 'GMT+2'
DateTime._multipleZones = True
class TestOpenOrder(ERP5TypeTestCase): class TestOpenOrder(ERP5TypeTestCase):
""" """
...@@ -47,6 +43,19 @@ class TestOpenOrder(ERP5TypeTestCase): ...@@ -47,6 +43,19 @@ class TestOpenOrder(ERP5TypeTestCase):
return 'Test Open Order' return 'Test Open Order'
def afterSetUp(self): def afterSetUp(self):
# explicitly set Europe/Paris timezone
# We use mock, to make sure we patch in the right place, but the stopping
# the patcher does not really work as we also have to set TZ
os.environ['TZ'] = 'Europe/Paris'
time.tzset()
for patcher in (
mock.patch.object(sys.modules['DateTime.DateTime'], '_localzone0', new='GMT+1'),
mock.patch.object(sys.modules['DateTime.DateTime'], '_localzone1', new='GMT+2'),
mock.patch.object(sys.modules['DateTime.DateTime'], '_multipleZones', new=True),
):
patcher.start()
self.addCleanup(patcher.stop)
if getattr(self.portal, '_run_after_setup', None) is not None: if getattr(self.portal, '_run_after_setup', None) is not None:
return return
......
...@@ -28,6 +28,7 @@ from hashlib import md5 ...@@ -28,6 +28,7 @@ from hashlib import md5
from warnings import warn from warnings import warn
from ExtensionClass import pmc_init_of from ExtensionClass import pmc_init_of
from DateTime import DateTime from DateTime import DateTime
import mock
import Products.ZMySQLDA.DA import Products.ZMySQLDA.DA
from Products.ZMySQLDA.DA import Connection as ZMySQLDA_Connection from Products.ZMySQLDA.DA import Connection as ZMySQLDA_Connection
...@@ -386,8 +387,11 @@ class ERP5TypeTestCaseMixin(ProcessingNodeTestCase, PortalTestCase): ...@@ -386,8 +387,11 @@ class ERP5TypeTestCaseMixin(ProcessingNodeTestCase, PortalTestCase):
# UTC # UTC
os.environ['TZ'] = "UTC" os.environ['TZ'] = "UTC"
time.tzset() time.tzset()
DateTime._isDST = False
DateTime._localzone = DateTime._localzone0 = DateTime._localzone1 = "UTC" mock.patch.object(sys.modules['DateTime.DateTime'], '_localzone0', new='UTC').start()
mock.patch.object(sys.modules['DateTime.DateTime'], '_localzone1', new='UTC').start()
mock.patch.object(sys.modules['DateTime.DateTime'], '_multipleZones', new=False).start()
def unpinDateTime(self): def unpinDateTime(self):
self.pinDateTime(None) self.pinDateTime(None)
......
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