Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
test.datetimetester.TestDate Class Reference
Inheritance diagram for test.datetimetester.TestDate:
Inheritance graph
[legend]
Collaboration diagram for test.datetimetester.TestDate:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_basic_attributes
def test_roundtrip
def test_ordinal_conversions
def test_extreme_ordinals
def test_bad_constructor_arguments
def test_hash_equality
def test_computations
def test_overflow
def test_fromtimestamp
def test_insane_fromtimestamp
def test_today
def test_weekday
def test_isocalendar
def test_iso_long_years
def test_isoformat
def test_ctime
def test_strftime
def test_format
def test_resolution_info
def test_extreme_timedelta
def test_timetuple
def test_pickling
def test_compare
def test_mixed_compare
def test_bool
def test_strftime_out_of_range
def test_replace
def test_subclass_date
def test_pickling_subclass_date
def test_backdoor_resistance
def test_harmless_mixed_comparison
def test_harmful_mixed_comparison

Static Public Attributes

 theclass = date

Detailed Description

Definition at line 754 of file datetimetester.py.


Member Function Documentation

Definition at line 1355 of file datetimetester.py.

01355 
01356     def test_backdoor_resistance(self):
01357         # For fast unpickling, the constructor accepts a pickle byte string.
01358         # This is a low-overhead backdoor.  A user can (by intent or
01359         # mistake) pass a string directly, which (if it's the right length)
01360         # will get treated like a pickle, and bypass the normal sanity
01361         # checks in the constructor.  This can create insane objects.
01362         # The constructor doesn't want to burn the time to validate all
01363         # fields, but does check the month field.  This stops, e.g.,
01364         # datetime.datetime('1995-03-25') from yielding an insane object.
01365         base = b'1995-03-25'
01366         if not issubclass(self.theclass, datetime):
01367             base = base[:4]
01368         for month_byte in b'9', b'\0', b'\r', b'\xff':
01369             self.assertRaises(TypeError, self.theclass,
01370                                          base[:2] + month_byte + base[3:])
01371         # Good bytes, but bad tzinfo:
01372         self.assertRaises(TypeError, self.theclass,
01373                           bytes([1] * len(base)), 'EST')
01374 
01375         for ord_byte in range(1, 13):
01376             # This shouldn't blow up because of the month byte alone.  If
01377             # the implementation changes to do more-careful checking, it may
01378             # blow up because other fields are insane.
01379             self.theclass(base[:2] + bytes([ord_byte]) + base[3:])

Here is the call graph for this function:

Reimplemented in test.datetimetester.TestDateTime.

Definition at line 852 of file datetimetester.py.

00852 
00853     def test_bad_constructor_arguments(self):
00854         # bad years
00855         self.theclass(MINYEAR, 1, 1)  # no exception
00856         self.theclass(MAXYEAR, 1, 1)  # no exception
00857         self.assertRaises(ValueError, self.theclass, MINYEAR-1, 1, 1)
00858         self.assertRaises(ValueError, self.theclass, MAXYEAR+1, 1, 1)
00859         # bad months
00860         self.theclass(2000, 1, 1)    # no exception
00861         self.theclass(2000, 12, 1)   # no exception
00862         self.assertRaises(ValueError, self.theclass, 2000, 0, 1)
00863         self.assertRaises(ValueError, self.theclass, 2000, 13, 1)
00864         # bad days
00865         self.theclass(2000, 2, 29)   # no exception
00866         self.theclass(2004, 2, 29)   # no exception
00867         self.theclass(2400, 2, 29)   # no exception
00868         self.assertRaises(ValueError, self.theclass, 2000, 2, 30)
00869         self.assertRaises(ValueError, self.theclass, 2001, 2, 29)
00870         self.assertRaises(ValueError, self.theclass, 2100, 2, 29)
00871         self.assertRaises(ValueError, self.theclass, 1900, 2, 29)
00872         self.assertRaises(ValueError, self.theclass, 2000, 1, 0)
00873         self.assertRaises(ValueError, self.theclass, 2000, 1, 32)

Here is the call graph for this function:

Reimplemented in test.datetimetester.TestDateTime.

Definition at line 760 of file datetimetester.py.

00760 
00761     def test_basic_attributes(self):
00762         dt = self.theclass(2002, 3, 1)
00763         self.assertEqual(dt.year, 2002)
00764         self.assertEqual(dt.month, 3)
00765         self.assertEqual(dt.day, 1)

Here is the call graph for this function:

Definition at line 1287 of file datetimetester.py.

01287 
01288     def test_bool(self):
01289         # All dates are considered true.
01290         self.assertTrue(self.theclass.min)
01291         self.assertTrue(self.theclass.max)

Here is the call graph for this function:

Definition at line 1197 of file datetimetester.py.

01197 
01198     def test_compare(self):
01199         t1 = self.theclass(2, 3, 4)
01200         t2 = self.theclass(2, 3, 4)
01201         self.assertEqual(t1, t2)
01202         self.assertTrue(t1 <= t2)
01203         self.assertTrue(t1 >= t2)
01204         self.assertTrue(not t1 != t2)
01205         self.assertTrue(not t1 < t2)
01206         self.assertTrue(not t1 > t2)
01207 
01208         for args in (3, 3, 3), (2, 4, 4), (2, 3, 5):
01209             t2 = self.theclass(*args)   # this is larger than t1
01210             self.assertTrue(t1 < t2)
01211             self.assertTrue(t2 > t1)
01212             self.assertTrue(t1 <= t2)
01213             self.assertTrue(t2 >= t1)
01214             self.assertTrue(t1 != t2)
01215             self.assertTrue(t2 != t1)
01216             self.assertTrue(not t1 == t2)
01217             self.assertTrue(not t2 == t1)
01218             self.assertTrue(not t1 > t2)
01219             self.assertTrue(not t2 < t1)
01220             self.assertTrue(not t1 >= t2)
01221             self.assertTrue(not t2 <= t1)
01222 
01223         for badarg in OTHERSTUFF:
01224             self.assertEqual(t1 == badarg, False)
01225             self.assertEqual(t1 != badarg, True)
01226             self.assertEqual(badarg == t1, False)
01227             self.assertEqual(badarg != t1, True)
01228 
01229             self.assertRaises(TypeError, lambda: t1 < badarg)
01230             self.assertRaises(TypeError, lambda: t1 > badarg)
01231             self.assertRaises(TypeError, lambda: t1 >= badarg)
01232             self.assertRaises(TypeError, lambda: badarg <= t1)
01233             self.assertRaises(TypeError, lambda: badarg < t1)
01234             self.assertRaises(TypeError, lambda: badarg > t1)
01235             self.assertRaises(TypeError, lambda: badarg >= t1)

Here is the call graph for this function:

Reimplemented in test.datetimetester.TestDateTime.

Definition at line 899 of file datetimetester.py.

00899 
00900     def test_computations(self):
00901         a = self.theclass(2002, 1, 31)
00902         b = self.theclass(1956, 1, 31)
00903         c = self.theclass(2001,2,1)
00904 
00905         diff = a-b
00906         self.assertEqual(diff.days, 46*365 + len(range(1956, 2002, 4)))
00907         self.assertEqual(diff.seconds, 0)
00908         self.assertEqual(diff.microseconds, 0)
00909 
00910         day = timedelta(1)
00911         week = timedelta(7)
00912         a = self.theclass(2002, 3, 2)
00913         self.assertEqual(a + day, self.theclass(2002, 3, 3))
00914         self.assertEqual(day + a, self.theclass(2002, 3, 3))
00915         self.assertEqual(a - day, self.theclass(2002, 3, 1))
00916         self.assertEqual(-day + a, self.theclass(2002, 3, 1))
00917         self.assertEqual(a + week, self.theclass(2002, 3, 9))
00918         self.assertEqual(a - week, self.theclass(2002, 2, 23))
00919         self.assertEqual(a + 52*week, self.theclass(2003, 3, 1))
00920         self.assertEqual(a - 52*week, self.theclass(2001, 3, 3))
00921         self.assertEqual((a + week) - a, week)
00922         self.assertEqual((a + day) - a, day)
00923         self.assertEqual((a - week) - a, -week)
00924         self.assertEqual((a - day) - a, -day)
00925         self.assertEqual(a - (a + week), -week)
00926         self.assertEqual(a - (a + day), -day)
00927         self.assertEqual(a - (a - week), week)
00928         self.assertEqual(a - (a - day), day)
00929         self.assertEqual(c - (c - day), day)
00930 
00931         # Add/sub ints or floats should be illegal
00932         for i in 1, 1.0:
00933             self.assertRaises(TypeError, lambda: a+i)
00934             self.assertRaises(TypeError, lambda: a-i)
00935             self.assertRaises(TypeError, lambda: i+a)
00936             self.assertRaises(TypeError, lambda: i-a)
00937 
00938         # delta - date is senseless.
00939         self.assertRaises(TypeError, lambda: day - a)
00940         # mixing date and (delta or date) via * or // is senseless
00941         self.assertRaises(TypeError, lambda: day * a)
00942         self.assertRaises(TypeError, lambda: a * day)
00943         self.assertRaises(TypeError, lambda: day // a)
00944         self.assertRaises(TypeError, lambda: a // day)
00945         self.assertRaises(TypeError, lambda: a * a)
00946         self.assertRaises(TypeError, lambda: a // a)
00947         # date + date is senseless
00948         self.assertRaises(TypeError, lambda: a + a)

Here is the call graph for this function:

Definition at line 1079 of file datetimetester.py.

01079 
01080     def test_ctime(self):
01081         t = self.theclass(2002, 3, 2)
01082         self.assertEqual(t.ctime(), "Sat Mar  2 00:00:00 2002")

Here is the call graph for this function:

Definition at line 827 of file datetimetester.py.

00827 
00828     def test_extreme_ordinals(self):
00829         a = self.theclass.min
00830         a = self.theclass(a.year, a.month, a.day)  # get rid of time parts
00831         aord = a.toordinal()
00832         b = a.fromordinal(aord)
00833         self.assertEqual(a, b)
00834 
00835         self.assertRaises(ValueError, lambda: a.fromordinal(aord - 1))
00836 
00837         b = a + timedelta(days=1)
00838         self.assertEqual(b.toordinal(), aord + 1)
00839         self.assertEqual(b, self.theclass.fromordinal(aord + 1))
00840 
00841         a = self.theclass.max
00842         a = self.theclass(a.year, a.month, a.day)  # get rid of time parts
00843         aord = a.toordinal()
00844         b = a.fromordinal(aord)
00845         self.assertEqual(a, b)
00846 
00847         self.assertRaises(ValueError, lambda: a.fromordinal(aord + 1))
00848 
00849         b = a - timedelta(days=1)
00850         self.assertEqual(b.toordinal(), aord - 1)
00851         self.assertEqual(b, self.theclass.fromordinal(aord - 1))

Here is the call graph for this function:

Definition at line 1154 of file datetimetester.py.

01154 
01155     def test_extreme_timedelta(self):
01156         big = self.theclass.max - self.theclass.min
01157         # 3652058 days, 23 hours, 59 minutes, 59 seconds, 999999 microseconds
01158         n = (big.days*24*3600 + big.seconds)*1000000 + big.microseconds
01159         # n == 315537897599999999 ~= 2**58.13
01160         justasbig = timedelta(0, 0, n)
01161         self.assertEqual(big, justasbig)
01162         self.assertEqual(self.theclass.min + big, self.theclass.max)
01163         self.assertEqual(self.theclass.max - big, self.theclass.min)

Here is the call graph for this function:

Reimplemented in test.datetimetester.TestDateTime.

Definition at line 1117 of file datetimetester.py.

01117 
01118     def test_format(self):
01119         dt = self.theclass(2007, 9, 10)
01120         self.assertEqual(dt.__format__(''), str(dt))
01121 
01122         # check that a derived class's __str__() gets called
01123         class A(self.theclass):
01124             def __str__(self):
01125                 return 'A'
01126         a = A(2007, 9, 10)
01127         self.assertEqual(a.__format__(''), 'A')
01128 
01129         # check that a derived class's strftime gets called
01130         class B(self.theclass):
01131             def strftime(self, format_spec):
01132                 return 'B'
01133         b = B(2007, 9, 10)
01134         self.assertEqual(b.__format__(''), str(dt))
01135 
01136         for fmt in ["m:%m d:%d y:%y",
01137                     "m:%m d:%d y:%y H:%H M:%M S:%S",
01138                     "%z %Z",
01139                     ]:
01140             self.assertEqual(dt.__format__(fmt), dt.strftime(fmt))
01141             self.assertEqual(a.__format__(fmt), dt.strftime(fmt))
01142             self.assertEqual(b.__format__(fmt), 'B')

Here is the call graph for this function:

Reimplemented in test.datetimetester.TestDateTime.

Definition at line 963 of file datetimetester.py.

00963 
00964     def test_fromtimestamp(self):
00965         import time
00966 
00967         # Try an arbitrary fixed value.
00968         year, month, day = 1999, 9, 19
00969         ts = time.mktime((year, month, day, 0, 0, 0, 0, 0, -1))
00970         d = self.theclass.fromtimestamp(ts)
00971         self.assertEqual(d.year, year)
00972         self.assertEqual(d.month, month)
00973         self.assertEqual(d.day, day)

Here is the call graph for this function:

Definition at line 271 of file datetimetester.py.

00271 
00272     def test_harmful_mixed_comparison(self):
00273         me = self.theclass(1, 1, 1)
00274 
00275         self.assertRaises(TypeError, lambda: me < ())
00276         self.assertRaises(TypeError, lambda: me <= ())
00277         self.assertRaises(TypeError, lambda: me > ())
00278         self.assertRaises(TypeError, lambda: me >= ())
00279 
00280         self.assertRaises(TypeError, lambda: () < me)
00281         self.assertRaises(TypeError, lambda: () <= me)
00282         self.assertRaises(TypeError, lambda: () > me)
00283         self.assertRaises(TypeError, lambda: () >= me)

Here is the call graph for this function:

Definition at line 260 of file datetimetester.py.

00260 
00261     def test_harmless_mixed_comparison(self):
00262         me = self.theclass(1, 1, 1)
00263 
00264         self.assertFalse(me == ())
00265         self.assertTrue(me != ())
00266         self.assertFalse(() == me)
00267         self.assertTrue(() != me)
00268 
00269         self.assertIn(me, [1, 20, [], me])
00270         self.assertIn([], [me, 1, 20, []])

Here is the call graph for this function:

Reimplemented in test.datetimetester.TestDateTime.

Definition at line 874 of file datetimetester.py.

00874 
00875     def test_hash_equality(self):
00876         d = self.theclass(2000, 12, 31)
00877         # same thing
00878         e = self.theclass(2000, 12, 31)
00879         self.assertEqual(d, e)
00880         self.assertEqual(hash(d), hash(e))
00881 
00882         dic = {d: 1}
00883         dic[e] = 2
00884         self.assertEqual(len(dic), 1)
00885         self.assertEqual(dic[d], 2)
00886         self.assertEqual(dic[e], 2)
00887 
00888         d = self.theclass(2001,  1,  1)
00889         # same thing
00890         e = self.theclass(2001,  1,  1)
00891         self.assertEqual(d, e)
00892         self.assertEqual(hash(d), hash(e))
00893 
00894         dic = {d: 1}
00895         dic[e] = 2
00896         self.assertEqual(len(dic), 1)
00897         self.assertEqual(dic[d], 2)
00898         self.assertEqual(dic[e], 2)

Here is the call graph for this function:

Reimplemented in test.datetimetester.TestDateTime.

Definition at line 974 of file datetimetester.py.

00974 
00975     def test_insane_fromtimestamp(self):
00976         # It's possible that some platform maps time_t to double,
00977         # and that this test will fail there.  This test should
00978         # exempt such platforms (provided they return reasonable
00979         # results!).
00980         for insane in -1e200, 1e200:
00981             self.assertRaises(ValueError, self.theclass.fromtimestamp,
00982                               insane)

Here is the call graph for this function:

Definition at line 1037 of file datetimetester.py.

01037 
01038     def test_iso_long_years(self):
01039         # Calculate long ISO years and compare to table from
01040         # http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm
01041         ISO_LONG_YEARS_TABLE = """
01042               4   32   60   88
01043               9   37   65   93
01044              15   43   71   99
01045              20   48   76
01046              26   54   82
01047 
01048             105  133  161  189
01049             111  139  167  195
01050             116  144  172
01051             122  150  178
01052             128  156  184
01053 
01054             201  229  257  285
01055             207  235  263  291
01056             212  240  268  296
01057             218  246  274
01058             224  252  280
01059 
01060             303  331  359  387
01061             308  336  364  392
01062             314  342  370  398
01063             320  348  376
01064             325  353  381
01065         """
01066         iso_long_years = sorted(map(int, ISO_LONG_YEARS_TABLE.split()))
01067         L = []
01068         for i in range(400):
01069             d = self.theclass(2000+i, 12, 31)
01070             d1 = self.theclass(1600+i, 12, 31)
01071             self.assertEqual(d.isocalendar()[1:], d1.isocalendar()[1:])
01072             if d.isocalendar()[1] == 53:
01073                 L.append(i)
01074         self.assertEqual(L, iso_long_years)

Here is the call graph for this function:

Definition at line 1020 of file datetimetester.py.

01020 
01021     def test_isocalendar(self):
01022         # Check examples from
01023         # http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm
01024         for i in range(7):
01025             d = self.theclass(2003, 12, 22+i)
01026             self.assertEqual(d.isocalendar(), (2003, 52, i+1))
01027             d = self.theclass(2003, 12, 29) + timedelta(i)
01028             self.assertEqual(d.isocalendar(), (2004, 1, i+1))
01029             d = self.theclass(2004, 1, 5+i)
01030             self.assertEqual(d.isocalendar(), (2004, 2, i+1))
01031             d = self.theclass(2009, 12, 21+i)
01032             self.assertEqual(d.isocalendar(), (2009, 52, i+1))
01033             d = self.theclass(2009, 12, 28) + timedelta(i)
01034             self.assertEqual(d.isocalendar(), (2009, 53, i+1))
01035             d = self.theclass(2010, 1, 4+i)
01036             self.assertEqual(d.isocalendar(), (2010, 1, i+1))

Here is the call graph for this function:

Reimplemented in test.datetimetester.TestDateTime.

Definition at line 1075 of file datetimetester.py.

01075 
01076     def test_isoformat(self):
01077         t = self.theclass(2, 3, 2)
01078         self.assertEqual(t.isoformat(), "0002-03-02")

Here is the call graph for this function:

Reimplemented in test.datetimetester.TestDateTimeTZ.

Definition at line 1236 of file datetimetester.py.

01236 
01237     def test_mixed_compare(self):
01238         our = self.theclass(2000, 4, 5)
01239 
01240         # Our class can be compared for equality to other classes
01241         self.assertEqual(our == 1, False)
01242         self.assertEqual(1 == our, False)
01243         self.assertEqual(our != 1, True)
01244         self.assertEqual(1 != our, True)
01245 
01246         # But the ordering is undefined
01247         self.assertRaises(TypeError, lambda: our < 1)
01248         self.assertRaises(TypeError, lambda: 1 < our)
01249 
01250         # Repeat those tests with a different class
01251 
01252         class SomeClass:
01253             pass
01254 
01255         their = SomeClass()
01256         self.assertEqual(our == their, False)
01257         self.assertEqual(their == our, False)
01258         self.assertEqual(our != their, True)
01259         self.assertEqual(their != our, True)
01260         self.assertRaises(TypeError, lambda: our < their)
01261         self.assertRaises(TypeError, lambda: their < our)
01262 
01263         # However, if the other class explicitly defines ordering
01264         # relative to our class, it is allowed to do so
01265 
01266         class LargerThanAnything:
01267             def __lt__(self, other):
01268                 return False
01269             def __le__(self, other):
01270                 return isinstance(other, LargerThanAnything)
01271             def __eq__(self, other):
01272                 return isinstance(other, LargerThanAnything)
01273             def __ne__(self, other):
01274                 return not isinstance(other, LargerThanAnything)
01275             def __gt__(self, other):
01276                 return not isinstance(other, LargerThanAnything)
01277             def __ge__(self, other):
01278                 return True
01279 
01280         their = LargerThanAnything()
01281         self.assertEqual(our == their, False)
01282         self.assertEqual(their == our, False)
01283         self.assertEqual(our != their, True)
01284         self.assertEqual(their != our, True)
01285         self.assertEqual(our < their, True)
01286         self.assertEqual(their < our, False)

Here is the call graph for this function:

Definition at line 780 of file datetimetester.py.

00780 
00781     def test_ordinal_conversions(self):
00782         # Check some fixed values.
00783         for y, m, d, n in [(1, 1, 1, 1),      # calendar origin
00784                            (1, 12, 31, 365),
00785                            (2, 1, 1, 366),
00786                            # first example from "Calendrical Calculations"
00787                            (1945, 11, 12, 710347)]:
00788             d = self.theclass(y, m, d)
00789             self.assertEqual(n, d.toordinal())
00790             fromord = self.theclass.fromordinal(n)
00791             self.assertEqual(d, fromord)
00792             if hasattr(fromord, "hour"):
00793             # if we're checking something fancier than a date, verify
00794             # the extra fields have been zeroed out
00795                 self.assertEqual(fromord.hour, 0)
00796                 self.assertEqual(fromord.minute, 0)
00797                 self.assertEqual(fromord.second, 0)
00798                 self.assertEqual(fromord.microsecond, 0)
00799 
00800         # Check first and last days of year spottily across the whole
00801         # range of years supported.
00802         for year in range(MINYEAR, MAXYEAR+1, 7):
00803             # Verify (year, 1, 1) -> ordinal -> y, m, d is identity.
00804             d = self.theclass(year, 1, 1)
00805             n = d.toordinal()
00806             d2 = self.theclass.fromordinal(n)
00807             self.assertEqual(d, d2)
00808             # Verify that moving back a day gets to the end of year-1.
00809             if year > 1:
00810                 d = self.theclass.fromordinal(n-1)
00811                 d2 = self.theclass(year-1, 12, 31)
00812                 self.assertEqual(d, d2)
00813                 self.assertEqual(d2.toordinal(), n-1)
00814 
00815         # Test every day in a leap-year and a non-leap year.
00816         dim = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
00817         for year, isleap in (2000, True), (2002, False):
00818             n = self.theclass(year, 1, 1).toordinal()
00819             for month, maxday in zip(range(1, 13), dim):
00820                 if month == 2 and isleap:
00821                     maxday += 1
00822                 for day in range(1, maxday+1):
00823                     d = self.theclass(year, month, day)
00824                     self.assertEqual(d.toordinal(), n)
00825                     self.assertEqual(d, self.theclass.fromordinal(n))
00826                     n += 1

Here is the call graph for this function:

Definition at line 949 of file datetimetester.py.

00949 
00950     def test_overflow(self):
00951         tiny = self.theclass.resolution
00952 
00953         for delta in [tiny, timedelta(1), timedelta(2)]:
00954             dt = self.theclass.min + delta
00955             dt -= delta  # no problem
00956             self.assertRaises(OverflowError, dt.__sub__, delta)
00957             self.assertRaises(OverflowError, dt.__add__, -delta)
00958 
00959             dt = self.theclass.max - delta
00960             dt += delta  # no problem
00961             self.assertRaises(OverflowError, dt.__add__, delta)
00962             self.assertRaises(OverflowError, dt.__sub__, -delta)

Here is the call graph for this function:

Reimplemented in test.datetimetester.TestDateTimeTZ, and test.datetimetester.TestDateTime.

Definition at line 1189 of file datetimetester.py.

01189 
01190     def test_pickling(self):
01191         args = 6, 7, 23
01192         orig = self.theclass(*args)
01193         for pickler, unpickler, proto in pickle_choices:
01194             green = pickler.dumps(orig, proto)
01195             derived = unpickler.loads(green)
01196             self.assertEqual(orig, derived)

Here is the call graph for this function:

Definition at line 1346 of file datetimetester.py.

01346 
01347     def test_pickling_subclass_date(self):
01348 
01349         args = 6, 7, 23
01350         orig = SubclassDate(*args)
01351         for pickler, unpickler, proto in pickle_choices:
01352             green = pickler.dumps(orig, proto)
01353             derived = unpickler.loads(green)
01354             self.assertEqual(orig, derived)

Here is the call graph for this function:

Reimplemented in test.datetimetester.TestDateTimeTZ, and test.datetimetester.TestDateTime.

Definition at line 1299 of file datetimetester.py.

01299 
01300     def test_replace(self):
01301         cls = self.theclass
01302         args = [1, 2, 3]
01303         base = cls(*args)
01304         self.assertEqual(base, base.replace())
01305 
01306         i = 0
01307         for name, newval in (("year", 2),
01308                              ("month", 3),
01309                              ("day", 4)):
01310             newargs = args[:]
01311             newargs[i] = newval
01312             expected = cls(*newargs)
01313             got = base.replace(**{name: newval})
01314             self.assertEqual(expected, got)
01315             i += 1
01316 
01317         # Out of bounds.
01318         base = cls(2000, 2, 29)
01319         self.assertRaises(ValueError, base.replace, year=2001)

Here is the call graph for this function:

Definition at line 1143 of file datetimetester.py.

01143 
01144     def test_resolution_info(self):
01145         # XXX: Should min and max respect subclassing?
01146         if issubclass(self.theclass, datetime):
01147             expected_class = datetime
01148         else:
01149             expected_class = date
01150         self.assertIsInstance(self.theclass.min, expected_class)
01151         self.assertIsInstance(self.theclass.max, expected_class)
01152         self.assertIsInstance(self.theclass.resolution, timedelta)
01153         self.assertTrue(self.theclass.max > self.theclass.min)

Here is the call graph for this function:

Reimplemented in test.datetimetester.TestSubclassDateTime, and test.datetimetester.TestDateTime.

Definition at line 766 of file datetimetester.py.

00766 
00767     def test_roundtrip(self):
00768         for dt in (self.theclass(1, 2, 3),
00769                    self.theclass.today()):
00770             # Verify dt -> string -> date identity.
00771             s = repr(dt)
00772             self.assertTrue(s.startswith('datetime.'))
00773             s = s[9:]
00774             dt2 = eval(s)
00775             self.assertEqual(dt, dt2)
00776 
00777             # Verify identity via reconstructing from pieces.
00778             dt2 = self.theclass(dt.year, dt.month, dt.day)
00779             self.assertEqual(dt, dt2)

Here is the call graph for this function:

Definition at line 1083 of file datetimetester.py.

01083 
01084     def test_strftime(self):
01085         t = self.theclass(2005, 3, 2)
01086         self.assertEqual(t.strftime("m:%m d:%d y:%y"), "m:03 d:02 y:05")
01087         self.assertEqual(t.strftime(""), "") # SF bug #761337
01088         self.assertEqual(t.strftime('x'*1000), 'x'*1000) # SF bug #1556784
01089 
01090         self.assertRaises(TypeError, t.strftime) # needs an arg
01091         self.assertRaises(TypeError, t.strftime, "one", "two") # too many args
01092         self.assertRaises(TypeError, t.strftime, 42) # arg wrong type
01093 
01094         # test that unicode input is allowed (issue 2782)
01095         self.assertEqual(t.strftime("%m"), "03")
01096 
01097         # A naive object replaces %z and %Z w/ empty strings.
01098         self.assertEqual(t.strftime("'%z' '%Z'"), "'' ''")
01099 
01100         #make sure that invalid format specifiers are handled correctly
01101         #self.assertRaises(ValueError, t.strftime, "%e")
01102         #self.assertRaises(ValueError, t.strftime, "%")
01103         #self.assertRaises(ValueError, t.strftime, "%#")
01104 
01105         #oh well, some systems just ignore those invalid ones.
01106         #at least, excercise them to make sure that no crashes
01107         #are generated
01108         for f in ["%e", "%", "%#"]:
01109             try:
01110                 t.strftime(f)
01111             except ValueError:
01112                 pass
01113 
01114         #check that this standard extension works
01115         t.strftime("%f")
01116 

Here is the call graph for this function:

Definition at line 1292 of file datetimetester.py.

01292 
01293     def test_strftime_out_of_range(self):
01294         # For nasty technical reasons, we can't handle years before 1000.
01295         cls = self.theclass
01296         self.assertEqual(cls(1000, 1, 1).strftime("%Y"), "1000")
01297         for y in 1, 49, 51, 99, 100, 999:
01298             self.assertRaises(ValueError, cls(y, 1, 1).strftime, "%Y")

Here is the call graph for this function:

Definition at line 1320 of file datetimetester.py.

01320 
01321     def test_subclass_date(self):
01322 
01323         class C(self.theclass):
01324             theAnswer = 42
01325 
01326             def __new__(cls, *args, **kws):
01327                 temp = kws.copy()
01328                 extra = temp.pop('extra')
01329                 result = self.theclass.__new__(cls, *args, **temp)
01330                 result.extra = extra
01331                 return result
01332 
01333             def newmeth(self, start):
01334                 return start + self.year + self.month
01335 
01336         args = 2003, 4, 14
01337 
01338         dt1 = self.theclass(*args)
01339         dt2 = C(*args, **{'extra': 7})
01340 
01341         self.assertEqual(dt2.__class__, C)
01342         self.assertEqual(dt2.theAnswer, 42)
01343         self.assertEqual(dt2.extra, 7)
01344         self.assertEqual(dt1.toordinal(), dt2.toordinal())
01345         self.assertEqual(dt2.newmeth(-7), dt1.year + dt1.month - 7)

Here is the call graph for this function:

Definition at line 1164 of file datetimetester.py.

01164 
01165     def test_timetuple(self):
01166         for i in range(7):
01167             # January 2, 1956 is a Monday (0)
01168             d = self.theclass(1956, 1, 2+i)
01169             t = d.timetuple()
01170             self.assertEqual(t, (1956, 1, 2+i, 0, 0, 0, i, 2+i, -1))
01171             # February 1, 1956 is a Wednesday (2)
01172             d = self.theclass(1956, 2, 1+i)
01173             t = d.timetuple()
01174             self.assertEqual(t, (1956, 2, 1+i, 0, 0, 0, (2+i)%7, 32+i, -1))
01175             # March 1, 1956 is a Thursday (3), and is the 31+29+1 = 61st day
01176             # of the year.
01177             d = self.theclass(1956, 3, 1+i)
01178             t = d.timetuple()
01179             self.assertEqual(t, (1956, 3, 1+i, 0, 0, 0, (3+i)%7, 61+i, -1))
01180             self.assertEqual(t.tm_year, 1956)
01181             self.assertEqual(t.tm_mon, 3)
01182             self.assertEqual(t.tm_mday, 1+i)
01183             self.assertEqual(t.tm_hour, 0)
01184             self.assertEqual(t.tm_min, 0)
01185             self.assertEqual(t.tm_sec, 0)
01186             self.assertEqual(t.tm_wday, (3+i)%7)
01187             self.assertEqual(t.tm_yday, 61+i)
01188             self.assertEqual(t.tm_isdst, -1)

Here is the call graph for this function:

Definition at line 983 of file datetimetester.py.

00983 
00984     def test_today(self):
00985         import time
00986 
00987         # We claim that today() is like fromtimestamp(time.time()), so
00988         # prove it.
00989         for dummy in range(3):
00990             today = self.theclass.today()
00991             ts = time.time()
00992             todayagain = self.theclass.fromtimestamp(ts)
00993             if today == todayagain:
00994                 break
00995             # There are several legit reasons that could fail:
00996             # 1. It recently became midnight, between the today() and the
00997             #    time() calls.
00998             # 2. The platform time() has such fine resolution that we'll
00999             #    never get the same value twice.
01000             # 3. The platform time() has poor resolution, and we just
01001             #    happened to call today() right before a resolution quantum
01002             #    boundary.
01003             # 4. The system clock got fiddled between calls.
01004             # In any case, wait a little while and try again.
01005             time.sleep(0.1)
01006 
01007         # It worked or it didn't.  If it didn't, assume it's reason #2, and
01008         # let the test pass if they're within half a second of each other.
01009         self.assertTrue(today == todayagain or
01010                         abs(todayagain - today) < timedelta(seconds=0.5))

Here is the call graph for this function:

Definition at line 1011 of file datetimetester.py.

01011 
01012     def test_weekday(self):
01013         for i in range(7):
01014             # March 4, 2002 is a Monday
01015             self.assertEqual(self.theclass(2002, 3, 4+i).weekday(), i)
01016             self.assertEqual(self.theclass(2002, 3, 4+i).isoweekday(), i+1)
01017             # January 2, 1956 is a Monday
01018             self.assertEqual(self.theclass(1956, 1, 2+i).weekday(), i)
01019             self.assertEqual(self.theclass(1956, 1, 2+i).isoweekday(), i+1)

Here is the call graph for this function:


Member Data Documentation


The documentation for this class was generated from the following file: