PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 12ms RepoModel.GetById 28ms app.codeStats 0ms

/tests/regressiontests/db_typecasts/tests.py

https://code.google.com/p/mango-py/
Python | 62 lines | 59 code | 2 blank | 1 comment | 0 complexity | eca3ac31cfcbd2ca39d38f03b1e0a9ca MD5 | raw file
 1# Unit tests for typecast functions in django.db.backends.util
 2
 3from django.db.backends import util as typecasts
 4import datetime, unittest
 5
 6TEST_CASES = {
 7    'typecast_date': (
 8        ('', None),
 9        (None, None),
10        ('2005-08-11', datetime.date(2005, 8, 11)),
11        ('1990-01-01', datetime.date(1990, 1, 1)),
12    ),
13    'typecast_time': (
14        ('', None),
15        (None, None),
16        ('0:00:00', datetime.time(0, 0)),
17        ('0:30:00', datetime.time(0, 30)),
18        ('8:50:00', datetime.time(8, 50)),
19        ('08:50:00', datetime.time(8, 50)),
20        ('12:00:00', datetime.time(12, 00)),
21        ('12:30:00', datetime.time(12, 30)),
22        ('13:00:00', datetime.time(13, 00)),
23        ('23:59:00', datetime.time(23, 59)),
24        ('00:00:12', datetime.time(0, 0, 12)),
25        ('00:00:12.5', datetime.time(0, 0, 12, 500000)),
26        ('7:22:13.312', datetime.time(7, 22, 13, 312000)),
27    ),
28    'typecast_timestamp': (
29        ('', None),
30        (None, None),
31        ('2005-08-11 0:00:00', datetime.datetime(2005, 8, 11)),
32        ('2005-08-11 0:30:00', datetime.datetime(2005, 8, 11, 0, 30)),
33        ('2005-08-11 8:50:30', datetime.datetime(2005, 8, 11, 8, 50, 30)),
34        ('2005-08-11 8:50:30.123', datetime.datetime(2005, 8, 11, 8, 50, 30, 123000)),
35        ('2005-08-11 8:50:30.9', datetime.datetime(2005, 8, 11, 8, 50, 30, 900000)),
36        ('2005-08-11 8:50:30.312-05', datetime.datetime(2005, 8, 11, 8, 50, 30, 312000)),
37        ('2005-08-11 8:50:30.312+02', datetime.datetime(2005, 8, 11, 8, 50, 30, 312000)),
38        # ticket 14453
39        ('2010-10-12 15:29:22.063202', datetime.datetime(2010, 10, 12, 15, 29, 22, 63202)),
40        ('2010-10-12 15:29:22.063202-03', datetime.datetime(2010, 10, 12, 15, 29, 22, 63202)),
41        ('2010-10-12 15:29:22.063202+04', datetime.datetime(2010, 10, 12, 15, 29, 22, 63202)),
42        ('2010-10-12 15:29:22.0632021', datetime.datetime(2010, 10, 12, 15, 29, 22, 63202)),
43        ('2010-10-12 15:29:22.0632029', datetime.datetime(2010, 10, 12, 15, 29, 22, 63202)),
44    ),
45    'typecast_boolean': (
46        (None, None),
47        ('', False),
48        ('t', True),
49        ('f', False),
50        ('x', False),
51    ),
52}
53
54class DBTypeCasts(unittest.TestCase):
55    def test_typeCasts(self):
56        for k, v in TEST_CASES.items():
57            for inpt, expected in v:
58                got = getattr(typecasts, k)(inpt)
59                assert got == expected, "In %s: %r doesn't match %r. Got %r instead." % (k, inpt, expected, got)
60
61if __name__ == '__main__':
62    unittest.main()