| 1 | # -*- coding: utf-8 -*- |
| 2 | # Tests for the different contrib/localflavor/ form fields. |
| 3 | |
| 4 | localflavor_tests = r""" |
| 5 | >>> from django.contrib.localflavor.generic.forms import * |
| 6 | |
| 7 | |
| 8 | ## Generic DateField ########################################################## |
| 9 | |
| 10 | A DateField that uses generic dd/mm/yy dates instead of mm/dd/yy where |
| 11 | appropriate. |
| 12 | |
| 13 | >>> import datetime |
| 14 | >>> f = DateField() |
| 15 | >>> f.clean(datetime.date(2006, 10, 25)) |
| 16 | datetime.date(2006, 10, 25) |
| 17 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) |
| 18 | datetime.date(2006, 10, 25) |
| 19 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59)) |
| 20 | datetime.date(2006, 10, 25) |
| 21 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200)) |
| 22 | datetime.date(2006, 10, 25) |
| 23 | >>> f.clean('2006-10-25') |
| 24 | datetime.date(2006, 10, 25) |
| 25 | >>> f.clean('25/10/2006') |
| 26 | datetime.date(2006, 10, 25) |
| 27 | >>> f.clean('25/10/06') |
| 28 | datetime.date(2006, 10, 25) |
| 29 | >>> f.clean('Oct 25 2006') |
| 30 | datetime.date(2006, 10, 25) |
| 31 | >>> f.clean('October 25 2006') |
| 32 | datetime.date(2006, 10, 25) |
| 33 | >>> f.clean('October 25, 2006') |
| 34 | datetime.date(2006, 10, 25) |
| 35 | >>> f.clean('25 October 2006') |
| 36 | datetime.date(2006, 10, 25) |
| 37 | >>> f.clean('25 October, 2006') |
| 38 | datetime.date(2006, 10, 25) |
| 39 | >>> f.clean('2006-4-31') |
| 40 | Traceback (most recent call last): |
| 41 | ... |
| 42 | ValidationError: [u'Enter a valid date.'] |
| 43 | >>> f.clean('200a-10-25') |
| 44 | Traceback (most recent call last): |
| 45 | ... |
| 46 | ValidationError: [u'Enter a valid date.'] |
| 47 | >>> f.clean('10/25/06') |
| 48 | Traceback (most recent call last): |
| 49 | ... |
| 50 | ValidationError: [u'Enter a valid date.'] |
| 51 | >>> f.clean(None) |
| 52 | Traceback (most recent call last): |
| 53 | ... |
| 54 | ValidationError: [u'This field is required.'] |
| 55 | |
| 56 | >>> f = DateField(required=False) |
| 57 | >>> f.clean(None) |
| 58 | >>> repr(f.clean(None)) |
| 59 | 'None' |
| 60 | >>> f.clean('') |
| 61 | >>> repr(f.clean('')) |
| 62 | 'None' |
| 63 | |
| 64 | DateField accepts an optional input_formats parameter: |
| 65 | >>> f = DateField(input_formats=['%Y %m %d']) |
| 66 | >>> f.clean(datetime.date(2006, 10, 25)) |
| 67 | datetime.date(2006, 10, 25) |
| 68 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) |
| 69 | datetime.date(2006, 10, 25) |
| 70 | >>> f.clean('2006 10 25') |
| 71 | datetime.date(2006, 10, 25) |
| 72 | |
| 73 | The input_formats parameter overrides all default input formats, |
| 74 | so the default formats won't work unless you specify them: |
| 75 | >>> f.clean('2006-10-25') |
| 76 | Traceback (most recent call last): |
| 77 | ... |
| 78 | ValidationError: [u'Enter a valid date.'] |
| 79 | >>> f.clean('25/10/2006') |
| 80 | Traceback (most recent call last): |
| 81 | ... |
| 82 | ValidationError: [u'Enter a valid date.'] |
| 83 | >>> f.clean('25/10/06') |
| 84 | Traceback (most recent call last): |
| 85 | ... |
| 86 | ValidationError: [u'Enter a valid date.'] |
| 87 | |
| 88 | ## Generic DateTimeField ###################################################### |
| 89 | |
| 90 | A DateField that uses generic dd/mm/yy dates instead of mm/dd/yy where |
| 91 | appropriate. |
| 92 | |
| 93 | >>> import datetime |
| 94 | >>> f = DateTimeField() |
| 95 | >>> f.clean(datetime.date(2006, 10, 25)) |
| 96 | datetime.datetime(2006, 10, 25, 0, 0) |
| 97 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) |
| 98 | datetime.datetime(2006, 10, 25, 14, 30) |
| 99 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59)) |
| 100 | datetime.datetime(2006, 10, 25, 14, 30, 59) |
| 101 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200)) |
| 102 | datetime.datetime(2006, 10, 25, 14, 30, 59, 200) |
| 103 | >>> f.clean('2006-10-25 14:30:45') |
| 104 | datetime.datetime(2006, 10, 25, 14, 30, 45) |
| 105 | >>> f.clean('2006-10-25 14:30:00') |
| 106 | datetime.datetime(2006, 10, 25, 14, 30) |
| 107 | >>> f.clean('2006-10-25 14:30') |
| 108 | datetime.datetime(2006, 10, 25, 14, 30) |
| 109 | >>> f.clean('2006-10-25') |
| 110 | datetime.datetime(2006, 10, 25, 0, 0) |
| 111 | >>> f.clean('25/10/2006 14:30:45') |
| 112 | datetime.datetime(2006, 10, 25, 14, 30, 45) |
| 113 | >>> f.clean('25/10/2006 14:30:00') |
| 114 | datetime.datetime(2006, 10, 25, 14, 30) |
| 115 | >>> f.clean('25/10/2006 14:30') |
| 116 | datetime.datetime(2006, 10, 25, 14, 30) |
| 117 | >>> f.clean('25/10/2006') |
| 118 | datetime.datetime(2006, 10, 25, 0, 0) |
| 119 | >>> f.clean('25/10/06 14:30:45') |
| 120 | datetime.datetime(2006, 10, 25, 14, 30, 45) |
| 121 | >>> f.clean('25/10/06 14:30:00') |
| 122 | datetime.datetime(2006, 10, 25, 14, 30) |
| 123 | >>> f.clean('25/10/06 14:30') |
| 124 | datetime.datetime(2006, 10, 25, 14, 30) |
| 125 | >>> f.clean('25/10/06') |
| 126 | datetime.datetime(2006, 10, 25, 0, 0) |
| 127 | >>> f.clean('hello') |
| 128 | Traceback (most recent call last): |
| 129 | ... |
| 130 | ValidationError: [u'Enter a valid date/time.'] |
| 131 | >>> f.clean('2006-10-25 4:30 p.m.') |
| 132 | Traceback (most recent call last): |
| 133 | ... |
| 134 | ValidationError: [u'Enter a valid date/time.'] |
| 135 | |
| 136 | DateField accepts an optional input_formats parameter: |
| 137 | >>> f = DateTimeField(input_formats=['%Y %m %d %I:%M %p']) |
| 138 | >>> f.clean(datetime.date(2006, 10, 25)) |
| 139 | datetime.datetime(2006, 10, 25, 0, 0) |
| 140 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) |
| 141 | datetime.datetime(2006, 10, 25, 14, 30) |
| 142 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59)) |
| 143 | datetime.datetime(2006, 10, 25, 14, 30, 59) |
| 144 | >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200)) |
| 145 | datetime.datetime(2006, 10, 25, 14, 30, 59, 200) |
| 146 | >>> f.clean('2006 10 25 2:30 PM') |
| 147 | datetime.datetime(2006, 10, 25, 14, 30) |
| 148 | |
| 149 | The input_formats parameter overrides all default input formats, |
| 150 | so the default formats won't work unless you specify them: |
| 151 | >>> f.clean('2006-10-25 14:30:45') |
| 152 | Traceback (most recent call last): |
| 153 | ... |
| 154 | ValidationError: [u'Enter a valid date/time.'] |
| 155 | |
| 156 | >>> f = DateTimeField(required=False) |
| 157 | >>> f.clean(None) |
| 158 | >>> repr(f.clean(None)) |
| 159 | 'None' |
| 160 | >>> f.clean('') |
| 161 | >>> repr(f.clean('')) |
| 162 | 'None' |
| 163 | |
| 164 | """ |