summaryrefslogtreecommitdiff
path: root/tests/modeltests/validation/models.py
blob: 57811f25a591f6dd9940b617f279c1464bfe68a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
30. Validation

This is an experimental feature!

Each model instance has a validate() method that returns a dictionary of
validation errors in the instance's fields. This method has a side effect
of converting each field to its appropriate Python data type.
"""

from django.db import models

class Person(models.Model):
    is_child = models.BooleanField()
    name = models.CharField(maxlength=20)
    birthdate = models.DateField()
    favorite_moment = models.DateTimeField()
    email = models.EmailField()

    def __str__(self):
        return self.name

API_TESTS = """

>>> import datetime
>>> valid_params = {
...     'is_child': True,
...     'name': 'John',
...     'birthdate': datetime.date(2000, 5, 3),
...     'favorite_moment': datetime.datetime(2002, 4, 3, 13, 23),
...     'email': 'john@example.com'
... }
>>> p = Person(**valid_params)
>>> p.validate()
{}

>>> p = Person(**dict(valid_params, id='23'))
>>> p.validate()
{}
>>> p.id
23

>>> p = Person(**dict(valid_params, id='foo'))
>>> p.validate()
{'id': ['This value must be an integer.']}

>>> p = Person(**dict(valid_params, id=None))
>>> p.validate()
{}
>>> repr(p.id)
'None'

>>> p = Person(**dict(valid_params, is_child='t'))
>>> p.validate()
{}
>>> p.is_child
True

>>> p = Person(**dict(valid_params, is_child='f'))
>>> p.validate()
{}
>>> p.is_child
False

>>> p = Person(**dict(valid_params, is_child=True))
>>> p.validate()
{}
>>> p.is_child
True

>>> p = Person(**dict(valid_params, is_child=False))
>>> p.validate()
{}
>>> p.is_child
False

>>> p = Person(**dict(valid_params, is_child='foo'))
>>> p.validate()
{'is_child': ['This value must be either True or False.']}

>>> p = Person(**dict(valid_params, name=u'Jose'))
>>> p.validate()
{}
>>> p.name
u'Jose'

>>> p = Person(**dict(valid_params, name=227))
>>> p.validate()
{}
>>> p.name
'227'

>>> p = Person(**dict(valid_params, birthdate=datetime.date(2000, 5, 3)))
>>> p.validate()
{}
>>> p.birthdate
datetime.date(2000, 5, 3)

>>> p = Person(**dict(valid_params, birthdate=datetime.datetime(2000, 5, 3)))
>>> p.validate()
{}
>>> p.birthdate
datetime.date(2000, 5, 3)

>>> p = Person(**dict(valid_params, birthdate='2000-05-03'))
>>> p.validate()
{}
>>> p.birthdate
datetime.date(2000, 5, 3)

>>> p = Person(**dict(valid_params, birthdate='2000-5-3'))
>>> p.validate()
{}
>>> p.birthdate
datetime.date(2000, 5, 3)

>>> p = Person(**dict(valid_params, birthdate='foo'))
>>> p.validate()
{'birthdate': ['Enter a valid date in YYYY-MM-DD format.']}

>>> p = Person(**dict(valid_params, favorite_moment=datetime.datetime(2002, 4, 3, 13, 23)))
>>> p.validate()
{}
>>> p.favorite_moment
datetime.datetime(2002, 4, 3, 13, 23)

>>> p = Person(**dict(valid_params, favorite_moment=datetime.datetime(2002, 4, 3)))
>>> p.validate()
{}
>>> p.favorite_moment
datetime.datetime(2002, 4, 3, 0, 0)

>>> p = Person(**dict(valid_params, email='john@example.com'))
>>> p.validate()
{}
>>> p.email
'john@example.com'

>>> p = Person(**dict(valid_params, email=u'john@example.com'))
>>> p.validate()
{}
>>> p.email
u'john@example.com'

>>> p = Person(**dict(valid_params, email=22))
>>> p.validate()
{'email': ['Enter a valid e-mail address.']}

"""