示例#1
0
    def test_constructors(self):
        w = CalWeek(2011,1)
        self.assertTrue(w)
        self.assertEqual(str(w), "2011W01")

        w = CalWeek(2011,52)
        self.assertEqual(str(w), "2011W52")

        w = CalWeek(2009,51)
        self.assertEqual(str(w), "2009W51")
        w = CalWeek(2009,52)
        self.assertEqual(str(w), "2009W52")
        w = CalWeek(2009,53)
        self.assertEqual(str(w), "2009W53")

        w = CalWeek.thisweek()
        self.assertTrue(w)

        w = CalWeek.fromordinal(1)
        self.assertEqual(str(w), "0001W01")
        w = CalWeek.fromordinal(2)
        self.assertEqual(str(w), "0001W02")
        w = CalWeek.fromordinal(521723)
        self.assertEqual(str(w), "9999W53")

        w = CalWeek.fromstring("2011W01")
        self.assertEqual(str(w), "2011W01")
        w = CalWeek.fromstring("2011-W01")
        self.assertEqual(str(w), "2011W01")

        from datetime import date
        w = CalWeek.withdate(date(2011, 5, 17))
        self.assertEqual(str(w), "2011W21")

        weeks = list(CalWeek.weeks_of_year(2009))
        self.assertEqual(len(weeks), 53)
        self.assertEqual(weeks[0], CalWeek(2009,1))
        self.assertEqual(weeks[-1], CalWeek(2009,53))

        weeks = list(CalWeek.weeks_of_year(2011))
        self.assertEqual(len(weeks), 53)
        self.assertEqual(weeks[0], CalWeek(2011,1))
        self.assertEqual(weeks[-1], CalWeek(2011,53))

        self.assertEqual(CalWeek.last_week_of_year(2009), CalWeek(2009, 53))
        self.assertEqual(CalWeek.last_week_of_year(2010), CalWeek(2010, 53))
        self.assertEqual(CalWeek.last_week_of_year(2011), CalWeek(2011, 53))
        self.assertEqual(CalWeek.last_week_of_year(9999), CalWeek(9999, 53))

        self.assertRaises(ValueError, lambda: CalWeek(0, 0))
        self.assertRaises(ValueError, lambda: CalWeek.fromstring("0000W00"))
        self.assertRaises(ValueError, lambda: CalWeek.fromstring("foo"))
        self.assertRaises(ValueError, lambda: CalWeek.fromordinal(-1))
        self.assertRaises(ValueError, lambda: CalWeek.fromordinal(0))
        self.assertRaises(ValueError, lambda: CalWeek.fromordinal(521724))
        self.assertRaises(ValueError, lambda: CalWeek.last_week_of_year(0))
        self.assertRaises(ValueError, lambda: CalWeek.last_week_of_year(10000))
示例#2
0
def test():

    all_dates = list(sorted(week_by_date_maps[0].keys()))

    for i in range(7):
        for dt in all_dates:
            assert weeknum(dt, 11 + i) == week_by_date_maps[i][dt].week

    for dt in all_dates:
        assert week_by_date_maps[6][dt].week == CalWeek.withdate(dt).week

    for a in range(7):

        if a == 6:
            MyWeek = CalWeek
        else:
            class MyWeek(CalWeek):
                first_dow = a

        for y in range(1998, 2010):
            assert MyWeek.days_in_first_week(y) == len(week_by_number_maps[a][(y, 1)].days)

        w_prev = None

        checked = set()
        for dt in all_dates:

            w = MyWeek.withdate(dt)
            if w in checked:
                continue

            if w.week == 1 and w_prev is not None:
                assert w_prev == MyWeek.last_week_of_year(w_prev.year)

            w_prev = w

            checked.add(w)

            info = week_by_date_maps[a][dt]
            assert w.day(0) == info.days[0]

            assert w.days() == info.days