Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1808)

Delta Between Two Patch Sets: Lib/test/datetimetester.py

Issue 17267: datetime.time support for '+' and 'now'
Left Patch Set: Created 6 years, 5 months ago
Right Patch Set: Created 6 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
LEFTRIGHT
(no file at all)
1 """Test date/time type. 1 """Test date/time type.
2 2
3 See http://www.zope.org/Members/fdrake/DateTimeWiki/TestCases 3 See http://www.zope.org/Members/fdrake/DateTimeWiki/TestCases
4 """ 4 """
5 5
6 import sys 6 import sys
7 import pickle 7 import pickle
8 import unittest 8 import unittest
9 9
10 from operator import lt, le, gt, ge, eq, ne, truediv, floordiv, mod 10 from operator import lt, le, gt, ge, eq, ne, truediv, floordiv, mod
(...skipping 2023 matching lines...) Expand 10 before | Expand all | Expand 10 after
2034 2034
2035 theclass = time 2035 theclass = time
2036 2036
2037 def test_basic_attributes(self): 2037 def test_basic_attributes(self):
2038 t = self.theclass(12, 0) 2038 t = self.theclass(12, 0)
2039 self.assertEqual(t.hour, 12) 2039 self.assertEqual(t.hour, 12)
2040 self.assertEqual(t.minute, 0) 2040 self.assertEqual(t.minute, 0)
2041 self.assertEqual(t.second, 0) 2041 self.assertEqual(t.second, 0)
2042 self.assertEqual(t.microsecond, 0) 2042 self.assertEqual(t.microsecond, 0)
2043 2043
2044 def test_computations(self):
2045 a = self.theclass(0, 0, 0)
2046 b = self.theclass(23, 59)
2047
2048 minute = timedelta(minutes=1)
2049 hour = timedelta(hours=1)
2050 week = timedelta(days=7)
2051 self.assertEqual(a + minute, self.theclass(0, 1, 0))
2052 self.assertEqual(b + minute + minute, self.theclass(0, 1))
2053 self.assertEqual(b + minute, self.theclass(0, 0))
2054 self.assertEqual(a + week, self.theclass(0))
2055 self.assertEqual(a + week, self.theclass(0))
2056 self.assertEqual(b + hour * 12, self.theclass(11, 59))
2057
2058 # Subtracting
2059 self.assertEqual(a - minute, self.theclass(23, 59))
2060 self.assertEqual(a - minute * 2, self.theclass(23, 58))
2061 self.assertEqual(b - hour * 24, self.theclass(23, 59))
2062 self.assertEqual(b - week, self.theclass(23, 59))
2063 self.assertEqual(b - week - hour, self.theclass(22, 59))
2064
2044 def test_basic_attributes_nonzero(self): 2065 def test_basic_attributes_nonzero(self):
2045 # Make sure all attributes are non-zero so bugs in 2066 # Make sure all attributes are non-zero so bugs in
2046 # bit-shifting access show up. 2067 # bit-shifting access show up.
2047 t = self.theclass(12, 59, 59, 8000) 2068 t = self.theclass(12, 59, 59, 8000)
2048 self.assertEqual(t.hour, 12) 2069 self.assertEqual(t.hour, 12)
2049 self.assertEqual(t.minute, 59) 2070 self.assertEqual(t.minute, 59)
2050 self.assertEqual(t.second, 59) 2071 self.assertEqual(t.second, 59)
2051 self.assertEqual(t.microsecond, 8000) 2072 self.assertEqual(t.microsecond, 8000)
2052 2073
2053 def test_roundtrip(self): 2074 def test_roundtrip(self):
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
2323 self.assertEqual(dt1.isoformat(), dt2.isoformat()) 2344 self.assertEqual(dt1.isoformat(), dt2.isoformat())
2324 self.assertEqual(dt2.newmeth(-7), dt1.hour + dt1.second - 7) 2345 self.assertEqual(dt2.newmeth(-7), dt1.hour + dt1.second - 7)
2325 2346
2326 def test_backdoor_resistance(self): 2347 def test_backdoor_resistance(self):
2327 # see TestDate.test_backdoor_resistance(). 2348 # see TestDate.test_backdoor_resistance().
2328 base = '2:59.0' 2349 base = '2:59.0'
2329 for hour_byte in ' ', '9', chr(24), '\xff': 2350 for hour_byte in ' ', '9', chr(24), '\xff':
2330 self.assertRaises(TypeError, self.theclass, 2351 self.assertRaises(TypeError, self.theclass,
2331 hour_byte + base[1:]) 2352 hour_byte + base[1:])
2332 2353
2354
2333 # A mixin for classes with a tzinfo= argument. Subclasses must define 2355 # A mixin for classes with a tzinfo= argument. Subclasses must define
2334 # theclass as a class atribute, and theclass(1, 1, 1, tzinfo=whatever) 2356 # theclass as a class atribute, and theclass(1, 1, 1, tzinfo=whatever)
2335 # must be legit (which is true for time and datetime). 2357 # must be legit (which is true for time and datetime).
2336 class TZInfoBase: 2358 class TZInfoBase:
2337 2359
2338 def test_argument_passing(self): 2360 def test_argument_passing(self):
2339 cls = self.theclass 2361 cls = self.theclass
2340 # A datetime passes itself on, a time passes None. 2362 # A datetime passes itself on, a time passes None.
2341 class introspective(tzinfo): 2363 class introspective(tzinfo):
2342 def tzname(self, dt): return dt and "real" or "none" 2364 def tzname(self, dt): return dt and "real" or "none"
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
2489 expected = -1 2511 expected = -1
2490 else: 2512 else:
2491 assert y is d2 2513 assert y is d2
2492 expected = 1 2514 expected = 1
2493 self.assertEqual(got, expected) 2515 self.assertEqual(got, expected)
2494 2516
2495 2517
2496 # Testing time objects with a non-None tzinfo. 2518 # Testing time objects with a non-None tzinfo.
2497 class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase): 2519 class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
2498 theclass = time 2520 theclass = time
2521
2522 def test_computations(self):
2523 est = FixedOffset(-300, "EST", 1)
2524 utc = FixedOffset(0, "UTC", -2)
2525 met = FixedOffset(60, "MET", 3)
2526
2527 minute = timedelta(minutes=1)
2528 hour = timedelta(hours=1)
2529 week = timedelta(days=7)
2530
2531 for tz in [est, utc, met]:
2532 a = self.theclass(0, 0, 0, tzinfo=tz)
2533 b = self.theclass(23, 59, tzinfo=tz)
2534 self.assertEqual(a + minute, self.theclass(0, 1, 0, tzinfo=tz))
2535 self.assertEqual(b + minute + minute, self.theclass(0, 1, tzinfo=tz) )
2536 self.assertEqual(b + minute, self.theclass(0, 0, tzinfo=tz))
2537 self.assertEqual(a + week, self.theclass(0, tzinfo=tz))
2538 self.assertEqual(a + week, self.theclass(0, tzinfo=tz))
2539 self.assertEqual(b + hour * 12, self.theclass(11, 59, tzinfo=tz))
2540
2541 c = self.theclass(0, 0, 0, tzinfo=est)
2542 d = self.theclass(23, 59, 0, tzinfo=met)
2543
2544 self.assertEqual(c + hour * 5, self.theclass(10, 0, tzinfo=utc))
2545 self.assertEqual(d + hour + minute, self.theclass(0, 0, tzinfo=utc))
2546
2547 # Subtracting
2548 for tz in [est, utc, met]:
2549 a = self.theclass(0, 0, 0, tzinfo=tz)
2550 b = self.theclass(23, 59, tzinfo=tz)
2551
2552 self.assertEqual(a - minute, self.theclass(23, 59, tzinfo=tz))
2553 self.assertEqual(a - minute * 2, self.theclass(23, 58, tzinfo=tz))
2554 self.assertEqual(b - hour * 24, self.theclass(23, 59, tzinfo=tz))
2555 self.assertEqual(b - week, self.theclass(23, 59, tzinfo=tz))
2556 self.assertEqual(b - week - hour, self.theclass(22, 59, tzinfo=tz))
2557
2558 e = self.theclass(0, tzinfo=est)
2559
2560 self.assertEqual(e - hour * 5, self.theclass(0, tzinfo=utc))
2499 2561
2500 def test_empty(self): 2562 def test_empty(self):
2501 t = self.theclass() 2563 t = self.theclass()
2502 self.assertEqual(t.hour, 0) 2564 self.assertEqual(t.hour, 0)
2503 self.assertEqual(t.minute, 0) 2565 self.assertEqual(t.minute, 0)
2504 self.assertEqual(t.second, 0) 2566 self.assertEqual(t.second, 0)
2505 self.assertEqual(t.microsecond, 0) 2567 self.assertEqual(t.microsecond, 0)
2506 self.assertTrue(t.tzinfo is None) 2568 self.assertTrue(t.tzinfo is None)
2507 2569
2508 def test_zones(self): 2570 def test_zones(self):
(...skipping 1251 matching lines...) Expand 10 before | Expand all | Expand 10 after
3760 datetime_sc = SubclassDatetime(as_datetime.year, as_datetime.month, 3822 datetime_sc = SubclassDatetime(as_datetime.year, as_datetime.month,
3761 as_date.day, 0, 0, 0) 3823 as_date.day, 0, 0, 0)
3762 self.assertEqual(as_datetime, datetime_sc) 3824 self.assertEqual(as_datetime, datetime_sc)
3763 self.assertEqual(datetime_sc, as_datetime) 3825 self.assertEqual(datetime_sc, as_datetime)
3764 3826
3765 def test_main(): 3827 def test_main():
3766 support.run_unittest(__name__) 3828 support.run_unittest(__name__)
3767 3829
3768 if __name__ == "__main__": 3830 if __name__ == "__main__":
3769 test_main() 3831 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+