a397e3dfa405b929db5a1d797f868aef7f29bf01
[philipp/winterrodeln/wrpylib.git] / tests / test_wrvalidators.py
1 #!/usr/bin/python3.4
2 # -*- coding: iso-8859-15 -*-
3 import collections
4 import wrpylib.wrvalidators
5 import formencode
6 import unittest
7
8
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())
25
26
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) == ''
31
32
33     # test_NeinValidator
34
35
36     # test_Unicode
37
38
39     # test_UnicodeNone
40
41
42     # test_Unsigned
43
44
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) == ''
51
52
53     # test_UnsignedNeinNone
54
55
56     # test_Loop
57
58
59     # test_DictValidator
60
61
62     # test_GermanBoolNone
63
64
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'
75
76
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'
87
88
89     # test_ValueComment
90
91
92     # test_SemicolonList
93
94
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')]
104
105
106     # test_GenericDateTime
107
108
109     # test_DateTimeNoSec
110
111
112     # test_DateNone
113
114
115     # test_Geo
116
117
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)
125
126         assert v.from_python((lat, lon)) == coord
127         assert v.from_python((None, None)) == ''
128
129
130     # test_MultiGeo
131
132
133     # test_AustrianPhoneNumber
134
135
136     # test_AustrianPhoneNumberNone
137
138
139     # test_AustrianPhoneNumberCommentLoop
140
141
142     # test_GermanDifficulty
143
144
145     # test_GermanAvalanches
146
147
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
157
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'
166
167
168     # test_GermanTristateFloatComment
169
170
171     # test_UnsignedCommentNone
172
173
174     # test_GermanCachet
175
176
177     # test_url
178
179
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'
188
189
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'
198
199
200     # test_PhoneNumber
201
202
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'
211
212
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'
221
222
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)'
234         try:
235             v.to_python(testvalue)
236             assert False
237         except formencode.Invalid:
238             pass
239         try:
240             v.from_python(testvalue)
241             assert False
242         except formencode.Invalid:
243             pass
244         v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True)
245         assert v.to_python(testvalue) == testvalue
246         assert v.from_python(testvalue) == testvalue
247
248
249     # test_WikiPage
250
251
252     # test_WikiPageList
253
254
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]]'
261
262
263     # test_TupleSecondValidator
264
265
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'
274
275
276
277
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)'
286
287
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'
298
299
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'),
308             ('Länge', '5045'),
309             ('Schwierigkeit', ''),
310             ('Lawinen', 'gelegentlich'),
311             ('Betreiber', ''),
312             ('Öffentliche Anreise', 'Ja'),
313             ('Aufstieg möglich', 'Ja'),
314             ('Aufstieg getrennt', 'Nein'),
315             ('Gehzeit', '105'),
316             ('Aufstiegshilfe', 'Nein'),
317             ('Beleuchtungsanlage', 'Nein'),
318             ('Beleuchtungstage', ''),
319             ('Rodelverleih', 'Ja'),
320             ('Gütesiegel', ''),
321             ('Webauskunft', ''),
322             ('Telefonauskunft', '+43-664-1808482 (Bergkristallhütte)'),
323             ('Bild', 'Rodelbahn Bergkristallhütte 2009-03-03.jpg'),
324             ('In Übersichtskarte', 'Ja'),
325             ('Forumid', '72')])
326         python = v.to_python(other, None)
327         other2 = v.from_python(python, None)
328         assert other == other2
329
330
331     def test_GasthausboxDictValidator(self):
332         v = wrpylib.wrvalidators.GasthausboxDictValidator()
333         other = collections.OrderedDict([
334             ('Position', '47.295549 N 9.986970 E'),
335             ('Höhe', '1250'),
336             ('Betreiber', ''),
337             ('Sitzplätze', ''),
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