2 # -*- coding: iso-8859-15 -*-
4 import wrpylib.wrvalidators
9 class TestWrValidators(unittest.TestCase):
10 def test_OrderedSchema(self):
11 v = wrpylib.wrvalidators.OrderedSchema()
12 v.pre_validators = [formencode.Validator()]
13 v.chained_validators = [formencode.Validator()]
14 v.add_field('c', formencode.Validator())
15 v.add_field('b', formencode.Validator())
16 v.add_field('a', formencode.Validator())
17 v.add_field('d', formencode.Validator())
18 other = collections.OrderedDict([('d', 'd'), ('b', 'b'), ('a', 'a'), ('c', 'c')])
19 python = v.to_python(other)
20 assert list(python.keys()) == list(other.keys())
21 assert list(python.values()) == list(other.values())
22 other2 = v.from_python(python)
23 assert list(other.keys()) == list(other2.keys())
24 assert list(other.values()) == list(other2.values())
27 def test_NoneValidator(self):
28 v = wrpylib.wrvalidators.NoneValidator(wrpylib.wrvalidators.Unicode())
29 assert v.to_python('') == None
30 assert v.from_python(None) == ''
45 def test_UnsignedNone(self):
46 v = wrpylib.wrvalidators.UnsignedNone()
47 assert v.to_python('42') == 42
48 assert v.to_python('') == None
49 assert v.from_python(42) == '42'
50 assert v.from_python(None) == ''
53 # test_UnsignedNeinNone
65 def test_GermanTristateTuple(self):
66 v = wrpylib.wrvalidators.GermanTristateTuple()
67 assert v.to_python('') == (None, None)
68 assert v.to_python('Ja') == (True, False)
69 assert v.to_python('Nein') == (False, True)
70 assert v.to_python('Teilweise') == (True, True)
71 assert v.from_python((None, None)) == ''
72 assert v.from_python((False, True)) == 'Nein'
73 assert v.from_python((True, False)) == 'Ja'
74 assert v.from_python((True, True)) == 'Teilweise'
77 def test_GermanTristateFloat(self):
78 v = wrpylib.wrvalidators.GermanTristateFloat()
79 assert v.to_python('') == None
80 assert v.to_python('Ja') == 1.0
81 assert v.to_python('Nein') == 0.0
82 assert v.to_python('Teilweise') == 0.5
83 assert v.from_python(None) == ''
84 assert v.from_python(0.0) == 'Nein'
85 assert v.from_python(1.0) == 'Ja'
86 assert v.from_python(0.5) == 'Teilweise'
95 def test_ValueCommentList(self):
96 v = wrpylib.wrvalidators.ValueCommentList()
97 assert v.to_python('abc') == [('abc', None)]
98 assert v.to_python('abc def') == [('abc def', None)]
99 assert v.to_python('value (comment)') == [('value', 'comment')]
100 assert v.to_python('value (comment)') == [('value', 'comment')]
101 assert v.to_python('value1 (comment); value2') == [('value1', 'comment'), ('value2', None)]
102 assert v.to_python('value1 (comment1); value2; value3 (comment3)') == [('value1', 'comment1'), ('value2', None), ('value3', 'comment3')]
103 assert v.to_python('value1 (comment1); [[link (linkcomment)]] (not easy)') == [('value1', 'comment1'), ('[[link (linkcomment)]]', 'not easy')]
106 # test_GenericDateTime
118 def test_GeoNone(self):
119 coord = '47.076207 N 11.453553 E'
120 v = wrpylib.wrvalidators.GeoNone()
121 (lat, lon) = v.to_python(coord)
122 assert lat == 47.076207
123 assert lon == 11.453553
124 assert v.to_python('') == (None, None)
126 assert v.from_python((lat, lon)) == coord
127 assert v.from_python((None, None)) == ''
133 # test_AustrianPhoneNumber
136 # test_AustrianPhoneNumberNone
139 # test_AustrianPhoneNumberCommentLoop
142 # test_GermanDifficulty
145 # test_GermanAvalanches
148 def test_GermanPublicTransport(self):
149 v = wrpylib.wrvalidators.GermanPublicTransport()
150 assert v.to_python('') is None
151 assert v.to_python('Sehr gut') == 1
152 assert v.to_python('Gut') == 2
153 assert v.to_python('Mittelmäßig') == 3
154 assert v.to_python('Schlecht') == 4
155 assert v.to_python('Nein') == 5
156 assert v.to_python('Ja') == 6
158 assert v.from_python(None) == ''
159 assert v.from_python(1) == 'Sehr gut'
160 assert v.from_python(2) == 'Gut'
161 assert v.from_python(3) == 'Mittelmäßig'
162 assert v.from_python(4) == 'Schlecht'
163 assert v.from_python(5) == 'Nein'
164 assert v.from_python(6) == 'Ja'
165 assert v.from_python(1) == 'Sehr gut'
168 # test_GermanTristateFloatComment
171 # test_UnsignedCommentNone
180 def test_UrlNeinNone(self):
181 v = wrpylib.wrvalidators.UrlNeinNone()
182 assert v.to_python('') == None
183 assert v.to_python('Nein') == 'Nein'
184 assert v.to_python('http://www.höttingeralm.at') == 'http://www.höttingeralm.at'
185 assert v.from_python(None) == ''
186 assert v.from_python('Nein') == 'Nein'
187 assert v.from_python('http://www.höttingeralm.at') == 'http://www.höttingeralm.at'
190 def test_ValueCommentListNeinLoopNone(self):
191 v = wrpylib.wrvalidators.ValueCommentListNeinLoopNone()
192 assert v.to_python('') == None
193 assert v.to_python('Nein') == 'Nein'
194 assert v.to_python('T-Mobile (gut); A1') == 'T-Mobile (gut); A1'
195 assert v.from_python(None) == ''
196 assert v.from_python('Nein') == 'Nein'
197 assert v.from_python('T-Mobile (gut); A1') == 'T-Mobile (gut); A1'
203 def test_PhoneCommentListNeinLoopNone(self):
204 v = wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True)
205 assert v.to_python('') == None
206 assert v.to_python('Nein') == 'Nein'
207 assert v.to_python('+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == '+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456'
208 assert v.from_python(None) == ''
209 assert v.from_python('Nein') == 'Nein'
210 assert v.from_python('+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == '+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456'
213 def test_MaskedEmail(self):
214 v = wrpylib.wrvalidators.MaskedEmail()
215 assert v.to_python('') == (None, None)
216 assert v.to_python('abc.def@example.com') == ('abc.def@example.com', False)
217 assert v.to_python('abc.def(at)example.com') == ('abc.def@example.com', True)
218 assert v.from_python((None, None)) == ''
219 assert v.from_python(('abc.def@example.com', False)) == 'abc.def@example.com'
220 assert v.from_python(('abc.def@example.com', True)) == 'abc.def(at)example.com'
223 def test_EmailCommentListNeinLoopNone(self):
224 v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone()
225 assert v.to_python('') == None
226 assert v.to_python('Nein') == 'Nein'
227 assert v.to_python('first@example.com') == 'first@example.com'
228 assert v.to_python('first@example.com (Nur Winter); second@example.com') == 'first@example.com (Nur Winter); second@example.com'
229 assert v.from_python(None) == ''
230 assert v.from_python('Nein') == 'Nein'
231 assert v.from_python('first@example.com') == 'first@example.com'
232 assert v.from_python('first@example.com (Nur Winter); second@example.com') == 'first@example.com (Nur Winter); second@example.com'
233 testvalue = 'abc.def(at)example.com (comment)'
235 v.to_python(testvalue)
237 except formencode.Invalid:
240 v.from_python(testvalue)
242 except formencode.Invalid:
244 v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True)
245 assert v.to_python(testvalue) == testvalue
246 assert v.from_python(testvalue) == testvalue
255 def test_WikiPageListLoopNone(self):
256 v = wrpylib.wrvalidators.WikiPageListLoopNone()
257 assert v.to_python('') == None
258 assert v.to_python('[[Birgitzer Alm]]; [[Kemater Alm]]') == '[[Birgitzer Alm]]; [[Kemater Alm]]'
259 assert v.from_python(None) == ''
260 assert v.from_python('[[Birgitzer Alm]]; [[Kemater Alm]]') == '[[Birgitzer Alm]]; [[Kemater Alm]]'
263 # test_TupleSecondValidator
266 def test_BoolUnicodeTupleValidator(self):
267 v = wrpylib.wrvalidators.BoolUnicodeTupleValidator()
268 assert v.to_python('') == (None, None)
269 assert v.to_python('Nein') == (False, None)
270 assert v.to_python('any text') == (True, 'any text')
271 assert v.from_python((None, None)) == ''
272 assert v.from_python((False, None)) == 'Nein'
273 assert v.from_python((True, 'any text')) == 'any text'
278 def test_GermanLift(self):
279 v = wrpylib.wrvalidators.GermanLift()
280 assert v.to_python('') == (None, None)
281 assert v.to_python('Nein') == (False, None)
282 assert v.to_python('Sessellift (4 Euro)') == (True, 'Sessellift (4 Euro)')
283 assert v.from_python((None, None)) == ''
284 assert v.from_python((False, None)) == 'Nein'
285 assert v.from_python((True, 'Sessellift (4 Euro)')) == 'Sessellift (4 Euro)'
288 def test_SledRental(self):
289 v = wrpylib.wrvalidators.SledRental()
290 assert v.to_python('') == (None, None)
291 assert v.to_python('Nein') == (False, None)
292 assert v.to_python('Ja') == (True, 'Ja')
293 assert v.to_python('Talstation (nur mit Ticket); Schneealm') == (True, 'Talstation (nur mit Ticket); Schneealm')
294 assert v.from_python((None, None)) == ''
295 assert v.from_python((False, None)) == 'Nein'
296 assert v.from_python((True, 'Talstation (nur mit Ticket); Schneealm')) == 'Talstation (nur mit Ticket); Schneealm'
297 assert v.from_python((True, 'Ja')) == 'Ja'
300 def test_RodelbahnboxDictValidator(self):
301 v = wrpylib.wrvalidators.RodelbahnboxDictValidator()
302 other = collections.OrderedDict([
303 ('Position', '47.309820 N 9.986508 E'),
304 ('Position oben', ''),
305 ('Höhe oben', '1244'),
306 ('Position unten', ''),
307 ('Höhe unten', '806'),
309 ('Schwierigkeit', ''),
310 ('Lawinen', 'gelegentlich'),
312 ('Öffentliche Anreise', 'Ja'),
313 ('Aufstieg möglich', 'Ja'),
314 ('Aufstieg getrennt', 'Nein'),
316 ('Aufstiegshilfe', 'Nein'),
317 ('Beleuchtungsanlage', 'Nein'),
318 ('Beleuchtungstage', ''),
319 ('Rodelverleih', 'Ja'),
322 ('Telefonauskunft', '+43-664-1808482 (Bergkristallhütte)'),
323 ('Bild', 'Rodelbahn Bergkristallhütte 2009-03-03.jpg'),
324 ('In Übersichtskarte', 'Ja'),
326 python = v.to_python(other, None)
327 other2 = v.from_python(python, None)
328 assert other == other2
331 def test_GasthausboxDictValidator(self):
332 v = wrpylib.wrvalidators.GasthausboxDictValidator()
333 other = collections.OrderedDict([
334 ('Position', '47.295549 N 9.986970 E'),
338 ('Übernachtung', ''),
339 ('Rauchfrei', 'Nein'),
340 ('Rodelverleih', ''),
341 ('Handyempfang', 'A1; T-Mobile/Telering'),
342 ('Homepage', 'http://www.bergkristallhuette.com/'),
343 ('E-Mail', 'bergkristallhuette@gmx.at'),
344 ('Telefon', '+43-664-1808482'),
345 ('Bild', 'Bergkritsallhütte 2009-02-07.JPG'),
346 ('Rodelbahnen', '[[Bergkristallhütte]]')])
347 python = v.to_python(other, None)
348 other2 = v.from_python(python, None)
349 assert other == other2