Beispiel #1
0
class TestDay(TestCase):
    def setUp(self):
        self.day = Day(events=Event.objects.all(),
                       date=datetime.datetime(2008,
                                              2,
                                              7,
                                              9,
                                              0,
                                              tzinfo=pytz.utc))

    def test_day_setup(self):
        self.assertEqual(self.day.start,
                         datetime.datetime(2008, 2, 7, 0, 0, tzinfo=pytz.utc))
        self.assertEqual(self.day.end,
                         datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_day_convenience_functions(self):
        self.assertEqual(self.day.prev_day().start,
                         datetime.datetime(2008, 2, 6, 0, 0, tzinfo=pytz.utc))
        self.assertEqual(self.day.next_day().start,
                         datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_time_slot(self):
        slot_start = datetime.datetime(2008, 2, 7, 13, 30, tzinfo=pytz.utc)
        slot_end = datetime.datetime(2008, 2, 7, 15, 0, tzinfo=pytz.utc)
        period = self.day.get_time_slot(slot_start, slot_end)
        self.assertEqual(period.start, slot_start)
        self.assertEqual(period.end, slot_end)

    def test_time_slot_with_dst(self):
        tzinfo = pytz.timezone('America/Vancouver')
        slot_start = datetime.datetime(2016, 3, 13, 0, 0, tzinfo=tzinfo)
        slot_end = datetime.datetime(2016, 3, 14, 0, 0, tzinfo=tzinfo)
        period = self.day.get_time_slot(slot_start, slot_end)
        self.assertEqual(period.start, slot_start)
        self.assertEqual(period.end, slot_end)

    def test_get_day_range(self):
        # This test exercises the case where a Day object is initiatized with
        # no date, which causes the Day constructor to call timezone.now(),
        # which always uses UTC.  This can cause a problem if the desired TZ
        # is not UTC, because the _get_day_range method typecasts the
        # tz-aware datetime to a naive datetime.

        # To simulate this case, we will create a NY tz date, localize that
        # date to UTC, then create a Day object with the UTC date and NY TZ

        NY = pytz.timezone('America/New_York')
        user_wall_time = datetime.datetime(2015, 11, 4, 21, 30, tzinfo=NY)
        timezone_now = user_wall_time.astimezone(pytz.utc)

        test_day = Day(events=Event.objects.all(),
                       date=timezone_now,
                       tzinfo=NY)

        expected_start = datetime.datetime(2015, 11, 4, 5, 00, tzinfo=pytz.utc)
        expected_end = datetime.datetime(2015, 11, 5, 5, 00, tzinfo=pytz.utc)

        self.assertEqual(test_day.start, expected_start)
        self.assertEqual(test_day.end, expected_end)
Beispiel #2
0
class TestDay(TestCase):
    def setUp(self):
        self.day = Day(events=Event.objects.all(),
                       date=datetime.datetime(2008,
                                              2,
                                              7,
                                              9,
                                              0,
                                              tzinfo=pytz.utc))

    def test_day_setup(self):
        self.assertEqual(self.day.start,
                         datetime.datetime(2008, 2, 7, 0, 0, tzinfo=pytz.utc))
        self.assertEqual(self.day.end,
                         datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_day_convenience_functions(self):
        self.assertEqual(self.day.prev_day().start,
                         datetime.datetime(2008, 2, 6, 0, 0, tzinfo=pytz.utc))
        self.assertEqual(self.day.next_day().start,
                         datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_time_slot(self):
        slot_start = datetime.datetime(2008, 2, 7, 13, 30, tzinfo=pytz.utc)
        slot_end = datetime.datetime(2008, 2, 7, 15, 0, tzinfo=pytz.utc)
        period = self.day.get_time_slot(slot_start, slot_end)
        self.assertEqual(period.start, slot_start)
        self.assertEqual(period.end, slot_end)
class TestDay(TestCase):
    def setUp(self):
        self.day = Day(events=Event.objects.all(),
                           date=datetime.datetime(2008, 2, 7, 9, 0, tzinfo=pytz.utc))

    def test_day_setup(self):
        self.assertEqual( self.day.start, datetime.datetime(2008, 2, 7, 0, 0, tzinfo=pytz.utc))
        self.assertEqual( self.day.end, datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_day_convenience_functions(self):
        self.assertEqual( self.day.prev_day().start, datetime.datetime(2008, 2, 6, 0, 0, tzinfo=pytz.utc))
        self.assertEqual( self.day.next_day().start, datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_time_slot(self):
        slot_start = datetime.datetime(2008, 2, 7, 13, 30, tzinfo=pytz.utc)
        slot_end = datetime.datetime(2008, 2, 7, 15, 0, tzinfo=pytz.utc)
        period = self.day.get_time_slot( slot_start, slot_end )
        self.assertEqual( period.start, slot_start )
        self.assertEqual( period.end, slot_end )

    def test_time_slot_with_dst(self):
        tzinfo = pytz.timezone('America/Vancouver')
        slot_start = datetime.datetime(2016, 3, 13, 0, 0, tzinfo=tzinfo)
        slot_end = datetime.datetime(2016, 3, 14, 0, 0, tzinfo=tzinfo)
        period = self.day.get_time_slot(slot_start, slot_end)
        self.assertEqual(period.start, slot_start)
        self.assertEqual(period.end, slot_end)
class TestDay(TestCase):
    def setUp(self):
        self.day = Day(
            events=Event.objects.all(),
            date=datetime.datetime(2008, 2, 7, 9, 0, tzinfo=pytz.utc))

    def test_day_setup(self):
        self.assertEqual(self.day.start, datetime.datetime(2008, 2, 7, 0, 0, tzinfo=pytz.utc))
        self.assertEqual(self.day.end, datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_day_convenience_functions(self):
        self.assertEqual(self.day.prev_day().start, datetime.datetime(2008, 2, 6, 0, 0, tzinfo=pytz.utc))
        self.assertEqual(self.day.next_day().start, datetime.datetime(2008, 2, 8, 0, 0, tzinfo=pytz.utc))

    def test_time_slot(self):
        slot_start = datetime.datetime(2008, 2, 7, 13, 30, tzinfo=pytz.utc)
        slot_end = datetime.datetime(2008, 2, 7, 15, 0, tzinfo=pytz.utc)
        period = self.day.get_time_slot(slot_start, slot_end)
        self.assertEqual(period.start, slot_start)
        self.assertEqual(period.end, slot_end)

    def test_time_slot_with_dst(self):
        tzinfo = pytz.timezone('America/Vancouver')
        slot_start = datetime.datetime(2016, 3, 13, 0, 0, tzinfo=tzinfo)
        slot_end = datetime.datetime(2016, 3, 14, 0, 0, tzinfo=tzinfo)
        period = self.day.get_time_slot(slot_start, slot_end)
        self.assertEqual(period.start, slot_start)
        self.assertEqual(period.end, slot_end)

    def test_get_day_range(self):
        # This test exercises the case where a Day object is initiatized with
        # no date, which causes the Day constructor to call timezone.now(),
        # which always uses UTC.  This can cause a problem if the desired TZ
        # is not UTC, because the _get_day_range method typecasts the
        # tz-aware datetime to a naive datetime.

        # To simulate this case, we will create a NY tz date, localize that
        # date to UTC, then create a Day object with the UTC date and NY TZ

        NY = pytz.timezone('America/New_York')
        user_wall_time = datetime.datetime(2015, 11, 4, 21, 30, tzinfo=NY)
        timezone_now = user_wall_time.astimezone(pytz.utc)

        test_day = Day(
            events=Event.objects.all(),
            date=timezone_now,
            tzinfo=NY)

        expected_start = datetime.datetime(2015, 11, 4, 5, 00, tzinfo=pytz.utc)
        expected_end = datetime.datetime(2015, 11, 5, 5, 00, tzinfo=pytz.utc)

        self.assertEqual(test_day.start, expected_start)
        self.assertEqual(test_day.end, expected_end)
class TestDay(TestCase):
    def setUp(self):
        self.day = Day(reservations=Reservation.objects.all(), date=datetime.datetime(2008, 2, 7, 9, 0))

    def test_day_setup(self):
        self.assertEqual(self.day.start, datetime.datetime(2008, 2, 7, 0, 0))
        self.assertEqual(self.day.end, datetime.datetime(2008, 2, 8, 0, 0))

    def test_day_convenience_functions(self):
        self.assertEqual(self.day.prev_day().start, datetime.datetime(2008, 2, 6, 0, 0))
        self.assertEqual(self.day.next_day().start, datetime.datetime(2008, 2, 8, 0, 0))

    def test_time_slot(self):
        slot_start = datetime.datetime(2008, 2, 7, 13, 30)
        slot_end = datetime.datetime(2008, 2, 7, 15, 0)
        period = self.day.get_time_slot(slot_start, slot_end)
        self.assertEqual(period.start, slot_start)
        self.assertEqual(period.end, slot_end)