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 # test_GermanPublicTransport
131 # test_GermanTristateFloatComment
134 # test_UnsignedCommentNone
143 def test_UrlNeinNone():
144 v = wrpylib.wrvalidators.UrlNeinNone()
145 assert v.to_python(u'') == None
146 assert v.to_python(u'Nein') == u'Nein'
147 assert v.to_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at'
148 assert v.from_python(None) == u''
149 assert v.from_python(u'Nein') == u'Nein'
150 assert v.from_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at'
153 def test_ValueCommentListNeinLoopNone():
154 v = wrpylib.wrvalidators.ValueCommentListNeinLoopNone()
155 assert v.to_python(u'') == None
156 assert v.to_python(u'Nein') == u'Nein'
157 assert v.to_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1'
158 assert v.from_python(None) == u''
159 assert v.from_python(u'Nein') == u'Nein'
160 assert v.from_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1'
166 def test_PhoneCommentListNeinLoopNone():
167 v = wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True)
168 assert v.to_python(u'') == None
169 assert v.to_python(u'Nein') == u'Nein'
170 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'
171 assert v.from_python(None) == u''
172 assert v.from_python(u'Nein') == u'Nein'
173 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'
176 def test_MaskedEmail():
177 v = wrpylib.wrvalidators.MaskedEmail()
178 assert v.to_python(u'') == (None, None)
179 assert v.to_python(u'abc.def@example.com') == (u'abc.def@example.com', False)
180 assert v.to_python(u'abc.def(at)example.com') == (u'abc.def@example.com', True)
181 assert v.from_python((None, None)) == u''
182 assert v.from_python((u'abc.def@example.com', False)) == u'abc.def@example.com'
183 assert v.from_python((u'abc.def@example.com', True)) == u'abc.def(at)example.com'
186 def test_EmailCommentListNeinLoopNone():
187 v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone()
188 assert v.to_python(u'') == None
189 assert v.to_python(u'Nein') == u'Nein'
190 assert v.to_python(u'first@example.com') == u'first@example.com'
191 assert v.to_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com'
192 assert v.from_python(None) == u''
193 assert v.from_python(u'Nein') == u'Nein'
194 assert v.from_python(u'first@example.com') == u'first@example.com'
195 assert v.from_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com'
196 testvalue = u'abc.def(at)example.com (comment)'
198 v.to_python(testvalue)
200 except formencode.Invalid:
203 v.from_python(testvalue)
205 except formencode.Invalid:
207 v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True)
208 assert v.to_python(testvalue) == testvalue
209 assert v.from_python(testvalue) == testvalue
218 def test_WikiPageListLoopNone():
219 v = wrpylib.wrvalidators.WikiPageListLoopNone()
220 assert v.to_python(u'') == None
221 assert v.to_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]'
222 assert v.from_python(None) == u''
223 assert v.from_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]'
226 # test_TupleSecondValidator
229 def test_BoolUnicodeTupleValidator():
230 v = wrpylib.wrvalidators.BoolUnicodeTupleValidator()
231 assert v.to_python(u'') == (None, None)
232 assert v.to_python(u'Nein') == (False, None)
233 assert v.to_python(u'any text') == (True, u'any text')
234 assert v.from_python((None, None)) == u''
235 assert v.from_python((False, None)) == u'Nein'
236 assert v.from_python((True, u'any text')) == u'any text'
241 def test_GermanLift():
242 v = wrpylib.wrvalidators.GermanLift()
243 assert v.to_python(u'') == (None, None)
244 assert v.to_python(u'Nein') == (False, None)
245 assert v.to_python(u'Sessellift (4 Euro)') == (True, u'Sessellift (4 Euro)')
246 assert v.from_python((None, None)) == u''
247 assert v.from_python((False, None)) == u'Nein'
248 assert v.from_python((True, u'Sessellift (4 Euro)')) == u'Sessellift (4 Euro)'
251 def test_SledRental():
252 v = wrpylib.wrvalidators.SledRental()
253 assert v.to_python(u'') == (None, None)
254 assert v.to_python(u'Nein') == (False, None)
255 assert v.to_python(u'Ja') == (True, u'Ja')
256 assert v.to_python(u'Talstation (nur mit Ticket); Schneealm') == (True, u'Talstation (nur mit Ticket); Schneealm')
257 assert v.from_python((None, None)) == u''
258 assert v.from_python((False, None)) == u'Nein'
259 assert v.from_python((True, u'Talstation (nur mit Ticket); Schneealm')) == u'Talstation (nur mit Ticket); Schneealm'
260 assert v.from_python((True, u'Ja')) == u'Ja'