|
| 1 | +# |
| 2 | +# Tests addapted from the test_pywintypes.py of pywin32 to test the |
| 3 | +# win32ctypes implementation |
| 4 | +# |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import unittest |
| 8 | +import contextlib |
| 9 | +import tempfile |
| 10 | +import shutil |
| 11 | +import faulthandler |
| 12 | +import datetime |
| 13 | +import time |
| 14 | + |
| 15 | +from win32ctypes import pywin32 |
| 16 | +from win32ctypes.pywin32.pywintypes import error |
| 17 | + |
| 18 | +class TestPyWINTypes(unittest.TestCase): |
| 19 | + |
| 20 | + # the pywin32ctypes implementation |
| 21 | + module = pywin32.pywintypes |
| 22 | + |
| 23 | + def testPyTimeFormat(self): |
| 24 | + struct_current = time.localtime() |
| 25 | + pytime_current = self.module.Time(struct_current) |
| 26 | + # try and test all the standard parts of the format |
| 27 | + # Note we used to include '%Z' testing, but that was pretty useless as |
| 28 | + # it always returned the local timezone. |
| 29 | + format_strings = "%a %A %b %B %c %d %H %I %j %m %M %p %S %U %w %W %x %X %y %Y" |
| 30 | + for fmt in format_strings.split(): |
| 31 | + v1 = pytime_current.Format(fmt) |
| 32 | + v2 = time.strftime(fmt, struct_current) |
| 33 | + self.assertEqual(v1, v2, "format %s failed - %r != %r" % (fmt, v1, v2)) |
| 34 | + |
| 35 | + def testPyTimePrint(self): |
| 36 | + # This used to crash with an invalid, or too early time. |
| 37 | + # We don't really want to check that it does cause a ValueError |
| 38 | + # (as hopefully this wont be true forever). So either working, or |
| 39 | + # ValueError is OK. |
| 40 | + try: |
| 41 | + t = self.module.Time(-2) |
| 42 | + t.Format() |
| 43 | + except ValueError: |
| 44 | + return |
| 45 | + |
| 46 | + def testTimeInDict(self): |
| 47 | + d = {} |
| 48 | + d["t1"] = self.module.Time(1) |
| 49 | + self.assertEqual(d["t1"], self.module.Time(1)) |
| 50 | + |
| 51 | + def testPyTimeCompare(self): |
| 52 | + t1 = self.module.Time(100) |
| 53 | + t1_2 = self.module.Time(100) |
| 54 | + t2 = self.module.Time(101) |
| 55 | + |
| 56 | + self.assertEqual(t1, t1_2) |
| 57 | + self.assertTrue(t1 <= t1_2) |
| 58 | + self.assertTrue(t1_2 >= t1) |
| 59 | + |
| 60 | + self.assertNotEqual(t1, t2) |
| 61 | + self.assertTrue(t1 < t2) |
| 62 | + self.assertTrue(t2 > t1) |
| 63 | + |
| 64 | + def testPyTimeCompareOther(self): |
| 65 | + t1 = self.module.Time(100) |
| 66 | + t2 = None |
| 67 | + self.assertNotEqual(t1, t2) |
| 68 | + |
| 69 | + def testTimeTuple(self): |
| 70 | + now = datetime.datetime.now() # has usec... |
| 71 | + # timetuple() lost usec - pt must be <=... |
| 72 | + pt = self.module.Time(now.timetuple()) |
| 73 | + # *sob* - only if we have a datetime object can we compare like this. |
| 74 | + if isinstance(pt, datetime.datetime): |
| 75 | + self.assertTrue(pt <= now) |
| 76 | + |
| 77 | + def testTimeTuplems(self): |
| 78 | + now = datetime.datetime.now() # has usec... |
| 79 | + tt = now.timetuple() + (now.microsecond // 1000,) |
| 80 | + pt = self.module.Time(tt) |
| 81 | + # we can't compare if using the old type, as it loses all sub-second res. |
| 82 | + if isinstance(pt, datetime.datetime): |
| 83 | + # but even with datetime, we lose sub-millisecond. |
| 84 | + expectedDelta = datetime.timedelta(milliseconds=1) |
| 85 | + self.assertTrue(-expectedDelta < (now - pt) < expectedDelta) |
| 86 | + |
| 87 | + def testPyTimeFromTime(self): |
| 88 | + t1 = self.module.Time(time.time()) |
| 89 | + self.assertTrue(self.module.Time(t1) is t1) |
0 commit comments