예제 #1
0
    def test_mix_max(self):
        self.assertEqual(CalWeek.min, CalWeek(1, 1))
        self.assertEqual(CalWeek.max, CalWeek(9999, 53))
        self.assertEqual(CalWeek.resolution.days, 7)

        self.assertRaises(ValueError, lambda: CalWeek.min - 1)
        self.assertRaises(ValueError, lambda: CalWeek.max + 1)
예제 #2
0
 def test_replace(self):
     w = CalWeek(2011, 20)
     self.assertEqual(w.replace(), w)
     self.assertEqual(w.replace(year=2010), CalWeek(2010, 20))
     self.assertEqual(w.replace(week=2), CalWeek(2011, 2))
     #self.assertEqual(w.replace(week=99), CalWeek(2012, 47))
     self.assertEqual(w.replace(year=1, week=1), CalWeek(1, 1))
예제 #3
0
    def test_arithmetics(self):
        w = CalWeek(2011, 20)
        self.assertEqual(str(w + 0),   "2011W20")
        self.assertEqual(str(w + 1),   "2011W21")
        self.assertEqual(str(w - 1),   "2011W19")
        self.assertEqual(str(w + 52),  "2012W19")
        self.assertEqual(str(w - 104), "2009W20")

        self.assertEqual(w - w, 0)
        self.assertEqual(w - CalWeek(2011, 1), 19)
        self.assertEqual(CalWeek(2011, 1) - w, -19)

        self.assertEqual(str(w + CalWeek.resolution), "2011W21")
        self.assertEqual(str(w - CalWeek.resolution), "2011W19")
예제 #4
0
    def test_days(self):
        w = CalWeek(2011, 20)
        #self.assertEqual(w.monday().isoformat(),    ...)
        #self.assertEqual(w.tuesday().isoformat(),   ...)
        #self.assertEqual(w.wednesday().isoformat(), ...)
        #self.assertEqual(w.thursday().isoformat(),  ...)
        #self.assertEqual(w.friday().isoformat(),    ...)
        #self.assertEqual(w.saturday().isoformat(),  ...)
        self.assertEqual(w.sunday().isoformat(),    "2011-05-08")

        self.assertEqual(w.day(0).isoformat(),  "2011-05-08")

        days = w.days()
        self.assertEqual(len(days), 7)
        self.assertEqual(days[0].isoformat(), "2011-05-08")
        self.assertEqual(days[-1].isoformat(), "2011-05-14")

        from datetime import date
        self.assertFalse(w.contains(date(2011,5,7)))
        self.assertTrue(w.contains(date(2011,5,8)))
        self.assertTrue(w.contains(date(2011,5,14)))
        self.assertFalse(w.contains(date(2011,5,15)))
예제 #5
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
예제 #6
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))
예제 #7
0
 def test_stringification(self):
     w = CalWeek(2011, 20)
     self.assertEqual(str(w), "2011W20")
     #self.assertEqual(w.isoformat(), "2011W20")
     self.assertEqual(repr(w), "calweek.CalWeek(2011, 20)")
예제 #8
0
    def test_year_boundary_days(self):
        w = CalWeek(2020, 53)
        self.assertEqual(w.last_day().isoformat(), '2020-12-31')

        w = CalWeek(2021, 1)
        self.assertEqual(w.last_day().isoformat(), '2021-01-02')