Ticket #2266: validators.py

File validators.py, 3.1 KB (added by alang@…, 9 years ago)

tests/othertests/validators.py

Line 
1r"""
2Here are some validator tests - there are plenty more to do!
3
4RequiredIfOtherFieldsNotGiven
5-----------------------------
6
7>>> check(v.RequiredIfOtherFieldsNotGiven(['full1']), '')
8'OK'
9
10>>> check(v.RequiredIfOtherFieldsNotGiven(['full1']), 'something')
11'OK'
12
13>>> check(v.RequiredIfOtherFieldsNotGiven(['empty1']), '')
14'Fail'
15
16>>> check(v.RequiredIfOtherFieldsNotGiven(['empty1']), 'something')
17'OK'
18
19>>> check(v.RequiredIfOtherFieldsNotGiven(['full1', 'full2']), '')
20'OK'
21
22>>> check(v.RequiredIfOtherFieldsNotGiven(['empty1', 'full2']), '')
23'OK'
24
25>>> check(v.RequiredIfOtherFieldsNotGiven(['full1', 'empty2']), '')
26'OK'
27
28>>> check(v.RequiredIfOtherFieldsNotGiven(['empty1', 'empty2']), '')
29'Fail'
30
31>>> check(v.RequiredIfOtherFieldsNotGiven(['full1', 'full2']), 'something')
32'OK'
33
34>>> check(v.RequiredIfOtherFieldsNotGiven(['empty1', 'full2']), 'something')
35'OK'
36
37>>> check(v.RequiredIfOtherFieldsNotGiven(['full1', 'empty2']), 'something')
38'OK'
39
40>>> check(v.RequiredIfOtherFieldsNotGiven(['empty1', 'empty2']), 'something')
41'OK'
42
43
44RequiredIfOtherFieldNotGiven
45----------------------------
46
47Should work the same as RequiredIfOtherFieldsNotGiven, but with only one
48field name.
49
50>>> check(v.RequiredIfOtherFieldNotGiven('full1'), '')
51'OK'
52
53>>> check(v.RequiredIfOtherFieldNotGiven('full1'), 'something')
54'OK'
55
56>>> check(v.RequiredIfOtherFieldNotGiven('empty1'), '')
57'Fail'
58
59>>> check(v.RequiredIfOtherFieldNotGiven('empty1'), 'something')
60'OK'
61
62
63RequiredIfOtherFieldsGiven
64--------------------------
65
66>>> check(v.RequiredIfOtherFieldsGiven(['full1']), '')
67'Fail'
68
69>>> check(v.RequiredIfOtherFieldsGiven(['full1']), 'something')
70'OK'
71
72>>> check(v.RequiredIfOtherFieldsGiven(['empty1']), '')
73'OK'
74
75>>> check(v.RequiredIfOtherFieldsGiven(['empty1']), 'something')
76'OK'
77
78>>> check(v.RequiredIfOtherFieldsGiven(['full1', 'full2']), '')
79'Fail'
80
81>>> check(v.RequiredIfOtherFieldsGiven(['empty1', 'full2']), '')
82'Fail'
83
84>>> check(v.RequiredIfOtherFieldsGiven(['full1', 'empty2']), '')
85'Fail'
86
87>>> check(v.RequiredIfOtherFieldsGiven(['empty1', 'empty2']), '')
88'OK'
89
90>>> check(v.RequiredIfOtherFieldsGiven(['full1', 'full2']), 'something')
91'OK'
92
93>>> check(v.RequiredIfOtherFieldsGiven(['empty1', 'full2']), 'something')
94'OK'
95
96>>> check(v.RequiredIfOtherFieldsGiven(['full1', 'empty2']), 'something')
97'OK'
98
99>>> check(v.RequiredIfOtherFieldsGiven(['empty1', 'empty2']), 'something')
100'OK'
101
102
103RequiredIfOtherFieldGiven
104-------------------------
105
106Just like RequiredIfOtherFieldsGiven, but only works for a single field.
107
108>>> check(v.RequiredIfOtherFieldGiven('full1'), '')
109'Fail'
110
111>>> check(v.RequiredIfOtherFieldGiven('full1'), 'something')
112'OK'
113
114>>> check(v.RequiredIfOtherFieldGiven('empty1'), '')
115'OK'
116
117>>> check(v.RequiredIfOtherFieldGiven('empty1'), 'something')
118'OK'
119
120"""
121
122from django.core import validators as v
123ALL_DATA = {
124        'empty1': '',
125        'empty2': '',
126        'full1': 'X',
127        'full2': 'x',
128} 
129
130def check(validator, field_data):
131    try:
132        validator(field_data, ALL_DATA)
133        return 'OK'
134    except v.ValidationError:
135        return 'Fail'
136
Back to Top