예제 #1
0
 def setUp(self):
     self.field = KungfuTimeField()
예제 #2
0
class TestTimeParsing(unittest.TestCase):

    def setUp(self):
        self.field = KungfuTimeField()

    def test24HourFormats(self):
        """
        Tests a variety of 24 hour formats.
        """
        twofour_hour_tests = (
            ("8:30", dt.time(8, 30)),
            ("14:30", dt.time(14, 30)),
            ("8", dt.time(8)),
            ("16", dt.time(16)),
            ("1742", dt.time(17, 42)),
            ("856", dt.time(8, 56)),
            ("101", dt.time(1, 01)),
            ("1 01", dt.time(1, 01)),
            ("13 01", dt.time(13, 01)),
            ("01 01", dt.time(1, 1)),
            (" 13     01 ", dt.time(13, 01)),
        )

        for (input, expected) in twofour_hour_tests:
            self.assertTimeEquals(input, expected)

    def test12HourFormats(self):
        """
        Tests a variety of 12 hour formats.
        """
        twelve_hour_tests = (
            ("12:30pm", dt.time(12, 30)),
            ("12:30PM", dt.time(12, 30)),
            ("12:30P.m.", dt.time(12, 30)),
            ("12:30 pm  ", dt.time(12, 30)),
            ("12:30 P m", dt.time(12, 30)),
            ("12:30 p .  M  .", dt.time(12, 30)),
            ("12:30p", dt.time(12, 30)),
            ("12:30  p", dt.time(12, 30)),
            ("12 30pm", dt.time(12, 30)),
            ("12  30 pm", dt.time(12, 30)),
            ("1230 p m", dt.time(12, 30)),
            ("  1230 p m", dt.time(12, 30)),
            ("12  30 p .  m  .", dt.time(12, 30)),
            ("1:30am", dt.time(1, 30)),
            ("1:30 am  ", dt.time(1, 30)),
            ("1:30 a m", dt.time(1, 30)),
            ("1:30 a .  m  .", dt.time(1, 30)),
            ("1:30a", dt.time(1, 30)),
            ("1:30  a", dt.time(1, 30)),
            ("1 30am", dt.time(1, 30)),
            ("  1  30 am", dt.time(1, 30)),
            ("130 a m", dt.time(1, 30)),
            ("3:30 p m", dt.time(15, 30)),
            ("     1  30 a .  m  ", dt.time(1, 30)),
        )

        for (input, expected) in twelve_hour_tests:
            self.assertTimeEquals(input, expected)

    def testBadTimes(self):
        """
        Tests a variety of times that should bork and die, not necessarily in
        that order.
        """
        bad_inputs = (
            "",
            " aa ",
            "12345",
            "1340am",
            "030pm",
            "25:23",
            "24:10",
            "this ain't nothing like a time",
            "-2:30",
        )

        for bad_input in bad_inputs:
            try:
                self.field.clean(bad_input)
                self.fail("Bad input %s validated." % bad_input)
            except AssertionError, e:
                raise e
            except ValidationError, e:
                pass
            except Exception, e:
                self.fail("Validator threw unexpected exception %s" % str(e))