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'
147 # test_GermanTristateFloatComment
150 # test_UnsignedCommentNone
159 def test_UrlNeinNone():
160 v = wrpylib.wrvalidators.UrlNeinNone()
161 assert v.to_python(u'') == None
162 assert v.to_python(u'Nein') == u'Nein'
163 assert v.to_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at'
164 assert v.from_python(None) == u''
165 assert v.from_python(u'Nein') == u'Nein'
166 assert v.from_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at'
169 def test_ValueCommentListNeinLoopNone():
170 v = wrpylib.wrvalidators.ValueCommentListNeinLoopNone()
171 assert v.to_python(u'') == None
172 assert v.to_python(u'Nein') == u'Nein'
173 assert v.to_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1'
174 assert v.from_python(None) == u''
175 assert v.from_python(u'Nein') == u'Nein'
176 assert v.from_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1'
182 def test_PhoneCommentListNeinLoopNone():
183 v = wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True)
184 assert v.to_python(u'') == None
185 assert v.to_python(u'Nein') == u'Nein'
186 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'
187 assert v.from_python(None) == u''
188 assert v.from_python(u'Nein') == u'Nein'
189 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'
192 def test_MaskedEmail():
193 v = wrpylib.wrvalidators.MaskedEmail()
194 assert v.to_python(u'') == (None, None)
195 assert v.to_python(u'abc.def@example.com') == (u'abc.def@example.com', False)
196 assert v.to_python(u'abc.def(at)example.com') == (u'abc.def@example.com', True)
197 assert v.from_python((None, None)) == u''
198 assert v.from_python((u'abc.def@example.com', False)) == u'abc.def@example.com'
199 assert v.from_python((u'abc.def@example.com', True)) == u'abc.def(at)example.com'
202 def test_EmailCommentListNeinLoopNone():
203 v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone()
204 assert v.to_python(u'') == None
205 assert v.to_python(u'Nein') == u'Nein'
206 assert v.to_python(u'first@example.com') == u'first@example.com'
207 assert v.to_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com'
208 assert v.from_python(None) == u''
209 assert v.from_python(u'Nein') == u'Nein'
210 assert v.from_python(u'first@example.com') == u'first@example.com'
211 assert v.from_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com'
212 testvalue = u'abc.def(at)example.com (comment)'
214 v.to_python(testvalue)
216 except formencode.Invalid:
219 v.from_python(testvalue)
221 except formencode.Invalid:
223 v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True)
224 assert v.to_python(testvalue) == testvalue
225 assert v.from_python(testvalue) == testvalue
234 def test_WikiPageListLoopNone():
235 v = wrpylib.wrvalidators.WikiPageListLoopNone()
236 assert v.to_python(u'') == None
237 assert v.to_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]'
238 assert v.from_python(None) == u''
239 assert v.from_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]'
242 # test_TupleSecondValidator
245 def test_BoolUnicodeTupleValidator():
246 v = wrpylib.wrvalidators.BoolUnicodeTupleValidator()
247 assert v.to_python(u'') == (None, None)
248 assert v.to_python(u'Nein') == (False, None)
249 assert v.to_python(u'any text') == (True, u'any text')
250 assert v.from_python((None, None)) == u''
251 assert v.from_python((False, None)) == u'Nein'
252 assert v.from_python((True, u'any text')) == u'any text'
257 def test_GermanLift():
258 v = wrpylib.wrvalidators.GermanLift()
259 assert v.to_python(u'') == (None, None)
260 assert v.to_python(u'Nein') == (False, None)
261 assert v.to_python(u'Sessellift (4 Euro)') == (True, u'Sessellift (4 Euro)')
262 assert v.from_python((None, None)) == u''
263 assert v.from_python((False, None)) == u'Nein'
264 assert v.from_python((True, u'Sessellift (4 Euro)')) == u'Sessellift (4 Euro)'
267 def test_SledRental():
268 v = wrpylib.wrvalidators.SledRental()
269 assert v.to_python(u'') == (None, None)
270 assert v.to_python(u'Nein') == (False, None)
271 assert v.to_python(u'Ja') == (True, u'Ja')
272 assert v.to_python(u'Talstation (nur mit Ticket); Schneealm') == (True, u'Talstation (nur mit Ticket); Schneealm')
273 assert v.from_python((None, None)) == u''
274 assert v.from_python((False, None)) == u'Nein'
275 assert v.from_python((True, u'Talstation (nur mit Ticket); Schneealm')) == u'Talstation (nur mit Ticket); Schneealm'
276 assert v.from_python((True, u'Ja')) == u'Ja'