[Zope3-checkins] SVN: Zope3/branches/stub-pytzpickle/src/pytz/tests/test_ Update pytz tests

Stuart Bishop stuart at stuartbishop.net
Sun Aug 14 04:29:24 EDT 2005


Log message for revision 37926:
  Update pytz tests

Changed:
  U   Zope3/branches/stub-pytzpickle/src/pytz/tests/test_docs.py
  U   Zope3/branches/stub-pytzpickle/src/pytz/tests/test_tzinfo.py

-=-
Modified: Zope3/branches/stub-pytzpickle/src/pytz/tests/test_docs.py
===================================================================
--- Zope3/branches/stub-pytzpickle/src/pytz/tests/test_docs.py	2005-08-14 03:58:36 UTC (rev 37925)
+++ Zope3/branches/stub-pytzpickle/src/pytz/tests/test_docs.py	2005-08-14 08:28:53 UTC (rev 37926)
@@ -1,15 +1,35 @@
-#!/usr/bin/env python
 # -*- coding: ascii -*-
 
 import unittest, os, os.path, sys
-from zope.testing.doctest import DocFileSuite
-sys.path.insert(0, os.path.join(os.pardir, os.pardir))
+from doctest import DocTestSuite
 
-README = DocFileSuite('../README.txt')
+# We test the documentation this way instead of using DocFileSuite so
+# we can run the tests under Python 2.3
+def test_README():
+    pass
 
+this_dir = os.path.dirname(__file__)
+locs = [
+    os.path.join(this_dir, os.pardir, 'README.txt'),
+    os.path.join(this_dir, os.pardir, os.pardir, 'README.txt'),
+    ]
+for loc in locs:
+    if os.path.exists(loc):
+        test_README.__doc__ = open(loc).read()
+        break
+if test_README.__doc__ is None:
+    raise RuntimeError('README.txt not found')
+
+README = DocTestSuite()
+
 def test_suite():
+    "For the Z3 test runner"
     return README
 
 if __name__ == '__main__':
+    sys.path.insert(0, os.path.normpath(os.path.join(
+        this_dir, os.pardir, os.pardir
+        )))
     unittest.main(defaultTest='README')
 
+

Modified: Zope3/branches/stub-pytzpickle/src/pytz/tests/test_tzinfo.py
===================================================================
--- Zope3/branches/stub-pytzpickle/src/pytz/tests/test_tzinfo.py	2005-08-14 03:58:36 UTC (rev 37925)
+++ Zope3/branches/stub-pytzpickle/src/pytz/tests/test_tzinfo.py	2005-08-14 08:28:53 UTC (rev 37926)
@@ -1,17 +1,15 @@
-#!/usr/bin/env python
 # -*- coding: ascii -*-
-'''
-$Id: test_tzinfo.py,v 1.9 2004/10/25 04:14:00 zenzen Exp $
-'''
 
-__rcs_id__  = '$Id: test_tzinfo.py,v 1.9 2004/10/25 04:14:00 zenzen Exp $'
-__version__ = '$Revision: 1.9 $'[11:-2]
-
 import sys, os, os.path
-sys.path.insert(0, os.path.join(os.pardir, os.pardir))
-
 import unittest, doctest
+import cPickle as pickle
 from datetime import datetime, tzinfo, timedelta
+
+if __name__ == '__main__':
+    # Only munge path if invoked as a script. Testrunners should have setup
+    # the paths already
+    sys.path.insert(0, os.path.join(os.pardir, os.pardir))
+
 import pytz
 from pytz import reference
 
@@ -26,6 +24,12 @@
 
 class BasicTest(unittest.TestCase):
 
+    def testVersion(self):
+        # Ensuring the correct version of pytz has been loaded
+        self.failUnlessEqual('2005k', pytz.__version__,
+                'Incorrect pytz version loaded. Import path is stuffed.'
+                )
+
     def testGMT(self):
         now = datetime.now(tz=GMT)
         self.failUnless(now.utcoffset() == NOTIME)
@@ -40,6 +44,60 @@
         self.failUnless(now.timetuple() == now.utctimetuple())
 
 
+class PicklingTest(unittest.TestCase):
+
+    def _roundtrip_tzinfo(self, tz):
+        p = pickle.dumps(tz)
+        unpickled_tz = pickle.loads(p)
+        self.failUnless(tz is unpickled_tz, '%s did not roundtrip' % tz.zone)
+
+    def _roundtrip_datetime(self, dt):
+        # Ensure that the tzinfo attached to a datetime instance
+        # is identical to the one returned. This is important for
+        # DST timezones, as some state is stored in the tzinfo.
+        tz = dt.tzinfo
+        p = pickle.dumps(dt)
+        unpickled_dt = pickle.loads(p)
+        unpickled_tz = unpickled_dt.tzinfo
+        self.failUnless(tz is unpickled_tz, '%s did not roundtrip' % tz.zone)
+
+    def testDst(self):
+        tz = pytz.timezone('Europe/Amsterdam')
+        dt = datetime(2004, 2, 1, 0, 0, 0)
+
+        for localized_tz in tz._tzinfos.values():
+            self._roundtrip_tzinfo(localized_tz)
+            self._roundtrip_datetime(dt.replace(tzinfo=localized_tz))
+
+    def testRoundtrip(self):
+        dt = datetime(2004, 2, 1, 0, 0, 0)
+        for zone in pytz.all_timezones:
+            tz = pytz.timezone(zone)
+            self._roundtrip_tzinfo(tz)
+
+    def testDatabaseFixes(self):
+        # Hack the pickle to make it refer to a timezone abbreviation
+        # that does not match anything. The unpickler should be able
+        # to repair this case
+        tz = pytz.timezone('Australia/Melbourne')
+        p = pickle.dumps(tz)
+        tzname = tz._tzname
+        hacked_p = p.replace(tzname, '???')
+        self.failIfEqual(p, hacked_p)
+        unpickled_tz = pickle.loads(hacked_p)
+        self.failUnless(tz is unpickled_tz)
+
+        # Simulate a database correction. In this case, the incorrect
+        # data will continue to be used.
+        p = pickle.dumps(tz)
+        new_utcoffset = tz._utcoffset.seconds + 42
+        hacked_p = p.replace(str(tz._utcoffset.seconds), str(new_utcoffset))
+        self.failIfEqual(p, hacked_p)
+        unpickled_tz = pickle.loads(hacked_p)
+        self.failUnlessEqual(unpickled_tz._utcoffset.seconds, new_utcoffset)
+        self.failUnless(tz is not unpickled_tz)
+
+
 class USEasternDSTStartTestCase(unittest.TestCase):
     tzinfo = pytz.timezone('US/Eastern')
 
@@ -65,10 +123,11 @@
         }
 
     def _test_tzname(self, utc_dt, wanted):
+        tzname = wanted['tzname']
         dt = utc_dt.astimezone(self.tzinfo)
-        self.failUnlessEqual(dt.tzname(),wanted['tzname'],
+        self.failUnlessEqual(dt.tzname(), tzname,
             'Expected %s as tzname for %s. Got %s' % (
-                wanted['tzname'],str(utc_dt),dt.tzname()
+                tzname, str(utc_dt), dt.tzname()
                 )
             )
 
@@ -76,26 +135,18 @@
         utcoffset = wanted['utcoffset']
         dt = utc_dt.astimezone(self.tzinfo)
         self.failUnlessEqual(
-                dt.utcoffset(),utcoffset,
+                dt.utcoffset(), wanted['utcoffset'],
                 'Expected %s as utcoffset for %s. Got %s' % (
-                    utcoffset,utc_dt,dt.utcoffset()
+                    utcoffset, utc_dt, dt.utcoffset()
                     )
                 )
-        return
-        dt_wanted = utc_dt.replace(tzinfo=None) + utcoffset
-        dt_got = dt.replace(tzinfo=None)
-        self.failUnlessEqual(
-                dt_wanted,
-                dt_got,
-                'Got %s. Wanted %s' % (str(dt_got),str(dt_wanted))
-                )
 
     def _test_dst(self, utc_dt, wanted):
         dst = wanted['dst']
         dt = utc_dt.astimezone(self.tzinfo)
         self.failUnlessEqual(dt.dst(),dst,
             'Expected %s as dst for %s. Got %s' % (
-                dst,utc_dt,dt.dst()
+                dst, utc_dt, dt.dst()
                 )
             )
 
@@ -391,18 +442,12 @@
     suite = unittest.TestSuite()
     suite.addTest(doctest.DocTestSuite('pytz'))
     suite.addTest(doctest.DocTestSuite('pytz.tzinfo'))
-    suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(
-        __import__('__main__')
-        ))
+    import test_tzinfo
+    suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_tzinfo))
     return suite
 
+DEFAULT = test_suite()
+
 if __name__ == '__main__':
-    suite = test_suite()
-    if '-v' in sys.argv:
-        runner = unittest.TextTestRunner(verbosity=2)
-    else:
-        runner = unittest.TextTestRunner()
-    runner.run(suite)
+    unittest.main(defaultTest='DEFAULT')
 
-# vim: set filetype=python ts=4 sw=4 et
-



More information about the Zope3-Checkins mailing list