|
2 | 2 |
|
3 | 3 | See http://www.zope.org/Members/fdrake/DateTimeWiki/TestCases |
4 | 4 | """ |
| 5 | +import io |
5 | 6 | import itertools |
6 | 7 | import bisect |
7 | 8 | import copy |
@@ -1355,19 +1356,43 @@ def test_weekday(self): |
1355 | 1356 | def test_isocalendar(self): |
1356 | 1357 | # Check examples from |
1357 | 1358 | # http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm |
1358 | | - for i in range(7): |
1359 | | - d = self.theclass(2003, 12, 22+i) |
1360 | | - self.assertEqual(d.isocalendar(), (2003, 52, i+1)) |
1361 | | - d = self.theclass(2003, 12, 29) + timedelta(i) |
1362 | | - self.assertEqual(d.isocalendar(), (2004, 1, i+1)) |
1363 | | - d = self.theclass(2004, 1, 5+i) |
1364 | | - self.assertEqual(d.isocalendar(), (2004, 2, i+1)) |
1365 | | - d = self.theclass(2009, 12, 21+i) |
1366 | | - self.assertEqual(d.isocalendar(), (2009, 52, i+1)) |
1367 | | - d = self.theclass(2009, 12, 28) + timedelta(i) |
1368 | | - self.assertEqual(d.isocalendar(), (2009, 53, i+1)) |
1369 | | - d = self.theclass(2010, 1, 4+i) |
1370 | | - self.assertEqual(d.isocalendar(), (2010, 1, i+1)) |
| 1359 | + week_mondays = [ |
| 1360 | + ((2003, 12, 22), (2003, 52, 1)), |
| 1361 | + ((2003, 12, 29), (2004, 1, 1)), |
| 1362 | + ((2004, 1, 5), (2004, 2, 1)), |
| 1363 | + ((2009, 12, 21), (2009, 52, 1)), |
| 1364 | + ((2009, 12, 28), (2009, 53, 1)), |
| 1365 | + ((2010, 1, 4), (2010, 1, 1)), |
| 1366 | + ] |
| 1367 | + |
| 1368 | + test_cases = [] |
| 1369 | + for cal_date, iso_date in week_mondays: |
| 1370 | + base_date = self.theclass(*cal_date) |
| 1371 | + # Adds one test case for every day of the specified weeks |
| 1372 | + for i in range(7): |
| 1373 | + new_date = base_date + timedelta(i) |
| 1374 | + new_iso = iso_date[0:2] + (iso_date[2] + i,) |
| 1375 | + test_cases.append((new_date, new_iso)) |
| 1376 | + |
| 1377 | + for d, exp_iso in test_cases: |
| 1378 | + with self.subTest(d=d, comparison="tuple"): |
| 1379 | + self.assertEqual(d.isocalendar(), exp_iso) |
| 1380 | + |
| 1381 | + # Check that the tuple contents are accessible by field name |
| 1382 | + with self.subTest(d=d, comparison="fields"): |
| 1383 | + t = d.isocalendar() |
| 1384 | + self.assertEqual((t.year, t.week, t.weekday), exp_iso) |
| 1385 | + |
| 1386 | + def test_isocalendar_pickling(self): |
| 1387 | + """Test that the result of datetime.isocalendar() can be pickled. |
| 1388 | +
|
| 1389 | + The result of a round trip should be a plain tuple. |
| 1390 | + """ |
| 1391 | + d = self.theclass(2019, 1, 1) |
| 1392 | + p = pickle.dumps(d.isocalendar()) |
| 1393 | + res = pickle.loads(p) |
| 1394 | + self.assertEqual(type(res), tuple) |
| 1395 | + self.assertEqual(res, (2019, 1, 2)) |
1371 | 1396 |
|
1372 | 1397 | def test_iso_long_years(self): |
1373 | 1398 | # Calculate long ISO years and compare to table from |
|
0 commit comments