2 # -*- coding: iso-8859-15 -*-
3 import wrpylib.wrvalidators
7 def test_NoneValidator():
8 v = wrpylib.wrvalidators.NoneValidator(wrpylib.wrvalidators.Unicode())
9 assert v.to_python(u'') == None
10 assert v.from_python(None) == u''
25 def test_UnsignedNone():
26 v = wrpylib.wrvalidators.UnsignedNone()
27 assert v.to_python(u'42') == 42
28 assert v.to_python(u'') == None
29 assert v.from_python(42) == u'42'
30 assert v.from_python(None) == u''
33 # test_UnsignedNeinNone
45 def test_GermanTristateTuple():
46 v = wrpylib.wrvalidators.GermanTristateTuple()
47 assert v.to_python(u'') == (None, None)
48 assert v.to_python(u'Ja') == (True, False)
49 assert v.to_python(u'Nein') == (False, True)
50 assert v.to_python(u'Teilweise') == (True, True)
51 assert v.from_python((None, None)) == u''
52 assert v.from_python((False, True)) == u'Nein'
53 assert v.from_python((True, False)) == u'Ja'
54 assert v.from_python((True, True)) == u'Teilweise'
57 def test_GermanTristateFloat():
58 v = wrpylib.wrvalidators.GermanTristateFloat()
59 assert v.to_python(u'') == None
60 assert v.to_python(u'Ja') == 1.0
61 assert v.to_python(u'Nein') == 0.0
62 assert v.to_python(u'Teilweise') == 0.5
63 assert v.from_python(None) == u''
64 assert v.from_python(0.0) == u'Nein'
65 assert v.from_python(1.0) == u'Ja'
66 assert v.from_python(0.5) == u'Teilweise'
75 def test_ValueCommentList():
76 v = wrpylib.wrvalidators.ValueCommentList()
77 assert v.to_python(u'abc') == [(u'abc', None)]
78 assert v.to_python(u'abc def') == [(u'abc def', None)]
79 assert v.to_python(u'value (comment)') == [(u'value', u'comment')]
80 assert v.to_python(u'value (comment)') == [(u'value', u'comment')]
81 assert v.to_python(u'value1 (comment); value2') == [(u'value1', u'comment'), (u'value2', None)]
82 assert v.to_python(u'value1 (comment1); value2; value3 (comment3)') == [(u'value1', u'comment1'), (u'value2', None), ('value3', 'comment3')]
83 assert v.to_python(u'value1 (comment1); [[link (linkcomment)]] (not easy)') == [(u'value1', u'comment1'), (u'[[link (linkcomment)]]', u'not easy')]
86 # test_GenericDateTime
99 coord = u'47.076207 N 11.453553 E'
100 v = wrpylib.wrvalidators.GeoNone()
101 (lat, lon) = v.to_python(coord)
102 assert lat == 47.076207
103 assert lon == 11.453553
104 assert v.to_python(u'') == (None, None)
106 assert v.from_python((lat, lon)) == coord
107 assert v.from_python((None, None)) == u''
113 # test_AustrianPhoneNumber
116 # test_AustrianPhoneNumberNone
119 # test_AustrianPhoneNumberCommentLoop
122 # test_GermanDifficulty
125 # test_GermanAvalanches
128 def test_GermanPublicTransport():
129 v = wrpylib.wrvalidators.GermanPublicTransport()
130 assert v.to_python(u'') is None
131 assert v.to_python(u'Sehr gut') == 1
132 assert v.to_python(u'Gut') == 2
133 assert v.to_python(u'Mittelmäßig') == 3
134 assert v.to_python(u'Schlecht') == 4
135 assert v.to_python(u'Nein') == 5
136 assert v.to_python(u'Ja') == 6
138 assert v.from_python(None) == u''
139 assert v.from_python(1) == u'Sehr gut'
140 assert v.from_python(2) == u'Gut'
141 assert v.from_python(3) == u'Mittelmäßig'
142 assert v.from_python(4) == u'Schlecht'
143 assert v.from_python(5) == u'Nein'
144 assert v.from_python(6) == u'Ja'
145 assert v.from_python(1l) == u'Sehr gut'
148 # test_GermanTristateFloatComment
151 # test_UnsignedCommentNone
160 def test_UrlNeinNone():
161 v = wrpylib.wrvalidators.UrlNeinNone()
162 assert v.to_python(u'') == None
163 assert v.to_python(u'Nein') == u'Nein'
164 assert v.to_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at'
165 assert v.from_python(None) == u''
166 assert v.from_python(u'Nein') == u'Nein'
167 assert v.from_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at'
170 def test_ValueCommentListNeinLoopNone():
171 v = wrpylib.wrvalidators.ValueCommentListNeinLoopNone()
172 assert v.to_python(u'') == None
173 assert v.to_python(u'Nein') == u'Nein'
174 assert v.to_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1'
175 assert v.from_python(None) == u''
176 assert v.from_python(u'Nein') == u'Nein'
177 assert v.from_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1'
183 def test_PhoneCommentListNeinLoopNone():
184 v = wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True)
185 assert v.to_python(u'') == None
186 assert v.to_python(u'Nein') == u'Nein'
187 assert v.to_python(u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456'
188 assert v.from_python(None) == u''
189 assert v.from_python(u'Nein') == u'Nein'
190 assert v.from_python(u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456'
193 def test_MaskedEmail():
194 v = wrpylib.wrvalidators.MaskedEmail()
195 assert v.to_python(u'') == (None, None)
196 assert v.to_python(u'abc.def@example.com') == (u'abc.def@example.com', False)
197 assert v.to_python(u'abc.def(at)example.com') == (u'abc.def@example.com', True)
198 assert v.from_python((None, None)) == u''
199 assert v.from_python((u'abc.def@example.com', False)) == u'abc.def@example.com'
200 assert v.from_python((u'abc.def@example.com', True)) == u'abc.def(at)example.com'
203 def test_EmailCommentListNeinLoopNone():
204 v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone()
205 assert v.to_python(u'') == None
206 assert v.to_python(u'Nein') == u'Nein'
207 assert v.to_python(u'first@example.com') == u'first@example.com'
208 assert v.to_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com'
209 assert v.from_python(None) == u''
210 assert v.from_python(u'Nein') == u'Nein'
211 assert v.from_python(u'first@example.com') == u'first@example.com'
212 assert v.from_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com'
213 testvalue = u'abc.def(at)example.com (comment)'
215 v.to_python(testvalue)
217 except formencode.Invalid:
220 v.from_python(testvalue)
222 except formencode.Invalid:
224 v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True)
225 assert v.to_python(testvalue) == testvalue
226 assert v.from_python(testvalue) == testvalue
235 def test_WikiPageListLoopNone():
236 v = wrpylib.wrvalidators.WikiPageListLoopNone()
237 assert v.to_python(u'') == None
238 assert v.to_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]'
239 assert v.from_python(None) == u''
240 assert v.from_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]'
243 # test_TupleSecondValidator
246 def test_BoolUnicodeTupleValidator():
247 v = wrpylib.wrvalidators.BoolUnicodeTupleValidator()
248 assert v.to_python(u'') == (None, None)
249 assert v.to_python(u'Nein') == (False, None)
250 assert v.to_python(u'any text') == (True, u'any text')
251 assert v.from_python((None, None)) == u''
252 assert v.from_python((False, None)) == u'Nein'
253 assert v.from_python((True, u'any text')) == u'any text'
258 def test_GermanLift():
259 v = wrpylib.wrvalidators.GermanLift()
260 assert v.to_python(u'') == (None, None)
261 assert v.to_python(u'Nein') == (False, None)
262 assert v.to_python(u'Sessellift (4 Euro)') == (True, u'Sessellift (4 Euro)')
263 assert v.from_python((None, None)) == u''
264 assert v.from_python((False, None)) == u'Nein'
265 assert v.from_python((True, u'Sessellift (4 Euro)')) == u'Sessellift (4 Euro)'
268 def test_SledRental():
269 v = wrpylib.wrvalidators.SledRental()
270 assert v.to_python(u'') == (None, None)
271 assert v.to_python(u'Nein') == (False, None)
272 assert v.to_python(u'Ja') == (True, u'Ja')
273 assert v.to_python(u'Talstation (nur mit Ticket); Schneealm') == (True, u'Talstation (nur mit Ticket); Schneealm')
274 assert v.from_python((None, None)) == u''
275 assert v.from_python((False, None)) == u'Nein'
276 assert v.from_python((True, u'Talstation (nur mit Ticket); Schneealm')) == u'Talstation (nur mit Ticket); Schneealm'
277 assert v.from_python((True, u'Ja')) == u'Ja'