|
1 import datetime |
|
2 |
|
3 from django.contrib.localflavor.generic.forms import DateField, DateTimeField |
|
4 |
|
5 from utils import LocalFlavorTestCase |
|
6 |
|
7 |
|
8 class GenericLocalFlavorTests(LocalFlavorTestCase): |
|
9 def test_GenericDateField(self): |
|
10 error_invalid = [u'Enter a valid date.'] |
|
11 valid = { |
|
12 datetime.date(2006, 10, 25): datetime.date(2006, 10, 25), |
|
13 datetime.datetime(2006, 10, 25, 14, 30): datetime.date(2006, 10, 25), |
|
14 datetime.datetime(2006, 10, 25, 14, 30, 59): datetime.date(2006, 10, 25), |
|
15 datetime.datetime(2006, 10, 25, 14, 30, 59, 200): datetime.date(2006, 10, 25), |
|
16 '2006-10-25': datetime.date(2006, 10, 25), |
|
17 '25/10/2006': datetime.date(2006, 10, 25), |
|
18 '25/10/06': datetime.date(2006, 10, 25), |
|
19 'Oct 25 2006': datetime.date(2006, 10, 25), |
|
20 'October 25 2006': datetime.date(2006, 10, 25), |
|
21 'October 25, 2006': datetime.date(2006, 10, 25), |
|
22 '25 October 2006': datetime.date(2006, 10, 25), |
|
23 '25 October, 2006': datetime.date(2006, 10, 25), |
|
24 } |
|
25 invalid = { |
|
26 '2006-4-31': error_invalid, |
|
27 '200a-10-25': error_invalid, |
|
28 '10/25/06': error_invalid, |
|
29 } |
|
30 self.assertFieldOutput(DateField, valid, invalid, empty_value=None) |
|
31 |
|
32 # DateField with optional input_formats parameter |
|
33 valid = { |
|
34 datetime.date(2006, 10, 25): datetime.date(2006, 10, 25), |
|
35 datetime.datetime(2006, 10, 25, 14, 30): datetime.date(2006, 10, 25), |
|
36 '2006 10 25': datetime.date(2006, 10, 25), |
|
37 } |
|
38 invalid = { |
|
39 '2006-10-25': error_invalid, |
|
40 '25/10/2006': error_invalid, |
|
41 '25/10/06': error_invalid, |
|
42 } |
|
43 kwargs = {'input_formats':['%Y %m %d'],} |
|
44 self.assertFieldOutput(DateField, |
|
45 valid, invalid, field_kwargs=kwargs, empty_value=None |
|
46 ) |
|
47 |
|
48 def test_GenericDateTimeField(self): |
|
49 error_invalid = [u'Enter a valid date/time.'] |
|
50 valid = { |
|
51 datetime.date(2006, 10, 25): datetime.datetime(2006, 10, 25, 0, 0), |
|
52 datetime.datetime(2006, 10, 25, 14, 30): datetime.datetime(2006, 10, 25, 14, 30), |
|
53 datetime.datetime(2006, 10, 25, 14, 30, 59): datetime.datetime(2006, 10, 25, 14, 30, 59), |
|
54 datetime.datetime(2006, 10, 25, 14, 30, 59, 200): datetime.datetime(2006, 10, 25, 14, 30, 59, 200), |
|
55 '2006-10-25 14:30:45': datetime.datetime(2006, 10, 25, 14, 30, 45), |
|
56 '2006-10-25 14:30:00': datetime.datetime(2006, 10, 25, 14, 30), |
|
57 '2006-10-25 14:30': datetime.datetime(2006, 10, 25, 14, 30), |
|
58 '2006-10-25': datetime.datetime(2006, 10, 25, 0, 0), |
|
59 '25/10/2006 14:30:45': datetime.datetime(2006, 10, 25, 14, 30, 45), |
|
60 '25/10/2006 14:30:00': datetime.datetime(2006, 10, 25, 14, 30), |
|
61 '25/10/2006 14:30': datetime.datetime(2006, 10, 25, 14, 30), |
|
62 '25/10/2006': datetime.datetime(2006, 10, 25, 0, 0), |
|
63 '25/10/06 14:30:45': datetime.datetime(2006, 10, 25, 14, 30, 45), |
|
64 '25/10/06 14:30:00': datetime.datetime(2006, 10, 25, 14, 30), |
|
65 '25/10/06 14:30': datetime.datetime(2006, 10, 25, 14, 30), |
|
66 '25/10/06': datetime.datetime(2006, 10, 25, 0, 0), |
|
67 } |
|
68 invalid = { |
|
69 'hello': error_invalid, |
|
70 '2006-10-25 4:30 p.m.': error_invalid, |
|
71 } |
|
72 self.assertFieldOutput(DateTimeField, valid, invalid, empty_value=None) |
|
73 |
|
74 # DateTimeField with optional input_formats paramter |
|
75 valid = { |
|
76 datetime.date(2006, 10, 25): datetime.datetime(2006, 10, 25, 0, 0), |
|
77 datetime.datetime(2006, 10, 25, 14, 30): datetime.datetime(2006, 10, 25, 14, 30), |
|
78 datetime.datetime(2006, 10, 25, 14, 30, 59): datetime.datetime(2006, 10, 25, 14, 30, 59), |
|
79 datetime.datetime(2006, 10, 25, 14, 30, 59, 200): datetime.datetime(2006, 10, 25, 14, 30, 59, 200), |
|
80 '2006 10 25 2:30 PM': datetime.datetime(2006, 10, 25, 14, 30), |
|
81 } |
|
82 invalid = { |
|
83 '2006-10-25 14:30:45': error_invalid, |
|
84 } |
|
85 kwargs = {'input_formats':['%Y %m %d %I:%M %p'],} |
|
86 self.assertFieldOutput(DateTimeField, |
|
87 valid, invalid, field_kwargs=kwargs, empty_value=None |
|
88 ) |