Exemplo n.º 1
0
    def test_when_has_normalize(self):
        class tzz(tzinfo):
            raises = None

            def normalize(self, dt, **kwargs):
                self.normalized = True
                if self.raises and kwargs and kwargs.get('is_dst') is None:
                    self.raised = True
                    raise self.raises
                return 1  # needed by min() in Python 3 (None not hashable)

        tz = tzz()
        localize(make_aware(datetime.utcnow(), tz), tz)
        self.assertTrue(tz.normalized)

        tz2 = tzz()
        tz2.raises = AmbiguousTimeError()
        localize(make_aware(datetime.utcnow(), tz2), tz2)
        self.assertTrue(tz2.normalized)
        self.assertTrue(tz2.raised)

        tz3 = tzz()
        tz3.raises = TypeError()
        localize(make_aware(datetime.utcnow(), tz3), tz3)
        self.assertTrue(tz3.normalized)
        self.assertTrue(tz3.raised)
Exemplo n.º 2
0
 def localize(self, dt, is_dst=None):
     self.localized = True
     if self.raises and is_dst is None:
         self.raised = True
         raise AmbiguousTimeError()
     return 1  # needed by min() in Python 3 (None not hashable)