def test_timetuple(self): for i in range(7): # January 2, 1956 is a Monday (0) d = cpy_date(1956, 1, 2 + i) t = d.timetuple() d2 = cpython_date(1956, 1, 2 + i) t2 = d2.timetuple() self.assertEqual(t, t2) # February 1, 1956 is a Wednesday (2) d = cpy_date(1956, 2, 1 + i) t = d.timetuple() d2 = cpython_date(1956, 2, 1 + i) t2 = d2.timetuple() self.assertEqual(t, t2) # March 1, 1956 is a Thursday (3), and is the 31+29+1 = 61st day # of the year. d = cpy_date(1956, 3, 1 + i) t = d.timetuple() d2 = cpython_date(1956, 3, 1 + i) t2 = d2.timetuple() self.assertEqual(t, t2) self.assertEqual(t.tm_year, t2.tm_year) self.assertEqual(t.tm_mon, t2.tm_mon) self.assertEqual(t.tm_mday, t2.tm_mday) self.assertEqual(t.tm_hour, t2.tm_hour) self.assertEqual(t.tm_min, t2.tm_min) self.assertEqual(t.tm_sec, t2.tm_sec) self.assertEqual(t.tm_wday, t2.tm_wday) self.assertEqual(t.tm_yday, t2.tm_yday) self.assertEqual(t.tm_isdst, t2.tm_isdst)
def test_weekday(self): for i in range(7): # March 4, 2002 is a Monday self.assertEqual( cpy_date(2002, 3, 4 + i).weekday(), cpython_date(2002, 3, 4 + i).weekday()) self.assertEqual( cpy_date(2002, 3, 4 + i).isoweekday(), cpython_date(2002, 3, 4 + i).isoweekday()) # January 2, 1956 is a Monday self.assertEqual( cpy_date(1956, 1, 2 + i).weekday(), cpython_date(1956, 1, 2 + i).weekday()) self.assertEqual( cpy_date(1956, 1, 2 + i).isoweekday(), cpython_date(1956, 1, 2 + i).isoweekday())
def test_mixed_compare(self): our = cpy_date(2000, 4, 5) our2 = cpython_date(2000, 4, 5) # Our class can be compared for equality to other classes self.assertEqual(our == 1, our2 == 1) self.assertEqual(1 == our, 1 == our2) self.assertEqual(our != 1, our2 != 1) self.assertEqual(1 != our, 1 != our2) # But the ordering is undefined self.assertRaises(TypeError, lambda: our < 1) self.assertRaises(TypeError, lambda: 1 < our) # Repeat those tests with a different class class SomeClass: pass their = SomeClass() self.assertEqual(our == their, False) self.assertEqual(their == our, False) self.assertEqual(our != their, True) self.assertEqual(their != our, True) self.assertRaises(TypeError, lambda: our < their) self.assertRaises(TypeError, lambda: their < our) # However, if the other class explicitly defines ordering # relative to our class, it is allowed to do so class LargerThanAnything: def __lt__(self, other): return False def __le__(self, other): return isinstance(other, LargerThanAnything) def __eq__(self, other): return isinstance(other, LargerThanAnything) def __ne__(self, other): return not isinstance(other, LargerThanAnything) def __gt__(self, other): return not isinstance(other, LargerThanAnything) def __ge__(self, other): return True their = LargerThanAnything() self.assertEqual(our == their, False) self.assertEqual(their == our, False) self.assertEqual(our != their, True) self.assertEqual(their != our, True) self.assertEqual(our < their, True) self.assertEqual(their < our, False)
def test_basic_attributes(self): dt = cpy_date(2002, 3, 1) dt_2 = cpython_date(2002, 3, 1) self.assertEqual(dt.year, dt_2.year) self.assertEqual(dt.month, dt_2.month) self.assertEqual(dt.day, dt_2.day)
def test_isoformat(self): # test isoformat against expected and cpython equiv. t = cpy_date(2, 3, 2) t2 = cpython_date(2, 3, 2) self.assertEqual(t.isoformat(), "0002-03-02") self.assertEqual(t.isoformat(), t2.isoformat())