8cc33e636f46d92fd2c352cd460fbb5da34b1721
[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 from wrpylib.wrvalidators import *
8
9
10 class TestConverter(unittest.TestCase):
11     def test_from_str(self):
12         self.assertEqual(Converter.from_str('abc'), 'abc')
13         self.assertEqual(Converter.from_str(''), '')
14
15     def test_to_str(self):
16         self.assertEqual(Converter.to_str('abc'), 'abc')
17         self.assertEqual(Converter.to_str(''), '')
18         self.assertEqual(Converter.to_str(42), '42')
19
20
21 class TestInt(unittest.TestCase):
22     def test_from_str(self):
23         self.assertEqual(int_from_str('42'), 42)
24         self.assertEqual(int_from_str('+42'), 42)
25         self.assertEqual(int_from_str('-20'), -20)
26         self.assertEqual(int_from_str('0', min=0), 0)
27         self.assertEqual(int_from_str('10', max=10), 10)
28         with self.assertRaises(ValueError):
29             int_from_str('abc')
30         with self.assertRaises(ValueError):
31             int_from_str('')
32         with self.assertRaises(ValueError):
33             int_from_str('-1', min=0)
34         with self.assertRaises(ValueError):
35             int_from_str('11', max=10)
36         with self.assertRaises(ValueError):
37             int_from_str('10.0')
38         with self.assertRaises(ValueError):
39             int_from_str('0d')
40
41     def test_to_str(self):
42         self.assertEqual(int_to_str(20), '20')
43         self.assertEqual(int_to_str(-20), '-20')
44         self.assertEqual(int_to_str(0), '0')
45
46
47 class TestOptInt(unittest.TestCase):
48     def test_from_str(self):
49         self.assertEqual(opt_int_from_str('42'), 42)
50         self.assertEqual(opt_int_from_str('+42'), 42)
51         self.assertEqual(opt_int_from_str('-20'), -20)
52         self.assertEqual(opt_int_from_str(''), None)
53         with self.assertRaises(ValueError):
54             opt_int_from_str('abc')
55         with self.assertRaises(ValueError):
56             opt_int_from_str('10.0')
57         with self.assertRaises(ValueError):
58             opt_int_from_str('0d')
59
60     def test_to_str(self):
61         self.assertEqual(opt_int_to_str(20), '20')
62         self.assertEqual(opt_int_to_str(-20), '-20')
63         self.assertEqual(opt_int_to_str(0), '0')
64         self.assertEqual(opt_int_to_str(None), '')
65
66
67 class TestEnumConverter(unittest.TestCase):
68     def test_from_str(self):
69         self.assertEqual(enum_from_str(''), [])
70         self.assertEqual(enum_from_str('abc'), ['abc'])
71         self.assertEqual(enum_from_str('abc; def'), ['abc', 'def'])
72         self.assertEqual(enum_from_str('abc; def;ghi'), ['abc', 'def', 'ghi'])
73
74     def test_to_str(self):
75         self.assertEqual(enum_to_str(['abc', 'def', 'ghi']), 'abc; def; ghi')
76         self.assertEqual(enum_to_str(['abc']), 'abc')
77         self.assertEqual(enum_to_str(['']), '')
78         self.assertEqual(enum_to_str([]), '')
79
80
81 class TestDateTime(unittest.TestCase):
82     def test_from_str(self):
83         self.assertEqual(DateTime.from_str('2015-12-31 23:07:42'), datetime.datetime(2015, 12, 31, 23, 7, 42))
84
85     def test_to_str(self):
86         self.assertEqual(DateTime.to_str(datetime.datetime(2015, 12, 31, 23, 7, 42)), '2015-12-31 23:07:42')
87
88
89 class TestDifficultyGerman(unittest.TestCase):
90     def test_from_str(self):
91         self.assertEqual(difficulty_german_from_str('leicht'), 1)
92         self.assertEqual(difficulty_german_from_str('mittel'), 2)
93         with self.assertRaises(ValueError):
94             difficulty_german_from_str('dontknow')
95         with self.assertRaises(ValueError):
96             difficulty_german_from_str('')
97
98     def test_to_str(self):
99         self.assertEqual(difficulty_german_to_str(1), 'leicht')
100
101
102 class TestTristateGerman(unittest.TestCase):
103     def test_from_str(self):
104         self.assertEqual(tristate_german_from_str('Ja'), 1.0)
105         self.assertEqual(tristate_german_from_str('Teilweise'), 0.5)
106         self.assertEqual(tristate_german_from_str('Nein'), 0)
107         with self.assertRaises(ValueError):
108             tristate_german_from_str('')
109         with self.assertRaises(ValueError):
110             tristate_german_from_str('Vielleicht')
111
112     def test_to_str(self):
113         self.assertEqual(tristate_german_to_str(1.0), 'Ja')
114         self.assertEqual(tristate_german_to_str(0.5), 'Teilweise')
115         self.assertEqual(tristate_german_to_str(0.0), 'Nein')
116
117
118 class TestOptTristateGerman(unittest.TestCase):
119     def test_from_str(self):
120         self.assertEqual(opt_tristate_german_from_str('Ja'), 1.0)
121         self.assertEqual(opt_tristate_german_from_str('Teilweise'), 0.5)
122         self.assertEqual(opt_tristate_german_from_str('Nein'), 0)
123         self.assertEqual(opt_tristate_german_from_str(''), None)
124         with self.assertRaises(ValueError):
125             opt_tristate_german_from_str('Vielleicht')
126
127     def test_to_str(self):
128         self.assertEqual(opt_tristate_german_to_str(1.0), 'Ja')
129         self.assertEqual(opt_tristate_german_to_str(0.5), 'Teilweise')
130         self.assertEqual(opt_tristate_german_to_str(0.0), 'Nein')
131         self.assertEqual(opt_tristate_german_to_str(None), '')
132
133
134 class TestOptTristateGermanComment(unittest.TestCase):
135     def test_from_str(self):
136         self.assertEqual(opt_tristate_german_comment_from_str('Ja'), (1.0, None))
137         self.assertEqual(opt_tristate_german_comment_from_str('Teilweise'), (0.5, None))
138         self.assertEqual(opt_tristate_german_comment_from_str('Nein'), (0, None))
139         self.assertEqual(opt_tristate_german_comment_from_str('Teilweise (nur ganz oben nicht)'), (0.5, 'nur ganz oben nicht'))
140         self.assertEqual(opt_tristate_german_comment_from_str(''), (None, None))
141         with self.assertRaises(ValueError):
142             opt_tristate_german_from_str('Vielleicht')
143         with self.assertRaises(ValueError):
144             opt_tristate_german_from_str('(Ja)')
145
146     def test_to_str(self):
147         self.assertEqual(opt_tristate_german_comment_to_str((1.0, None)), 'Ja')
148         self.assertEqual(opt_tristate_german_comment_to_str((0.5, None)), 'Teilweise')
149         self.assertEqual(opt_tristate_german_comment_to_str((0.0, None)), 'Nein')
150         self.assertEqual(opt_tristate_german_comment_to_str((None, None)), '')
151
152
153 class TestLonLat(unittest.TestCase):
154     def test_from_str(self):
155         self.assertEqual(lonlat_from_str('47.076207 N 11.453553 E'), LonLat(11.453553, 47.076207))
156         with self.assertRaises(ValueError):
157             lonlat_from_str('47.076207 N 11.453553')
158
159     def test_to_str(self):
160         self.assertEqual(lonlat_to_str(LonLat(11.453553, 47.076207)), '47.076207 N 11.453553 E')
161
162
163 class TestValueCommentConverter(unittest.TestCase):
164     def test_from_str(self):
165         self.assertEqual(value_comment_from_str('abc (defg)'), ('abc', 'defg'))
166         self.assertEqual(value_comment_from_str('abc ()'), ('abc', ''))
167         self.assertEqual(value_comment_from_str('(def)'), ('', 'def'))
168         self.assertEqual(value_comment_from_str('ab((cd))'), ('ab', '(cd)'))
169         self.assertEqual(value_comment_from_str('ab((c(d)[(])))'), ('ab', '(c(d)[(]))'))
170         self.assertEqual(value_comment_from_str('ab((cd)'), ('ab(', 'cd'))
171         self.assertEqual(value_comment_from_str('abcd  (ef) '), ('abcd', 'ef'))
172         self.assertEqual(value_comment_from_str('abc', comment_optional=True), ('abc', ''))
173         with self.assertRaises(ValueError):
174             value_comment_from_str('abc (')
175         with self.assertRaises(ValueError):
176             value_comment_from_str('abc )')
177         with self.assertRaises(ValueError):
178             value_comment_from_str('abc (def)g')
179         with self.assertRaises(ValueError):
180             value_comment_from_str('abc (b))')
181         with self.assertRaises(ValueError):
182             value_comment_from_str('abc')
183
184     def test_to_str(self):
185         self.assertEqual(value_comment_to_str(('abc', 'defg')), 'abc (defg)')
186         self.assertEqual(value_comment_to_str(('abc', '')), 'abc ()')
187         self.assertEqual(value_comment_to_str(('', 'def')), '(def)')
188         self.assertEqual(value_comment_to_str(('ab', '(cd)')), 'ab ((cd))')
189         self.assertEqual(value_comment_to_str(('ab', '(c(d)[(]))')), 'ab ((c(d)[(])))')
190         self.assertEqual(value_comment_to_str(('ab(', 'cd')), 'ab( (cd)')
191         self.assertEqual(value_comment_to_str(('abcd', 'ef')), 'abcd (ef)')
192         self.assertEqual(value_comment_to_str(('abc', ''), comment_optional=True), 'abc')
193
194
195 class TestNoGermanConverter(unittest.TestCase):
196     def test_from_str(self):
197         self.assertEqual(no_german_from_str('abc'), (True, 'abc'))
198         self.assertEqual(no_german_from_str('Nein'), (False, None))
199         with self.assertRaises(ValueError):
200             no_german_from_str('')
201
202     def test_to_str(self):
203         self.assertEqual(no_german_to_str((True, 'abc')), 'abc')
204         self.assertEqual(no_german_to_str((False, None)), 'Nein')
205
206
207 class TestOptNoGerman(unittest.TestCase):
208     def test_from_str(self):
209         self.assertEqual(opt_no_german_from_str('abc'), (True, 'abc'))
210         self.assertEqual(opt_no_german_from_str('Nein'), (False, None))
211         self.assertEqual(opt_no_german_from_str(''), (None, None))
212
213     def test_to_str(self):
214         self.assertEqual(opt_no_german_to_str((True, 'abc')), 'abc')
215         self.assertEqual(opt_no_german_to_str((False, None)), 'Nein')
216         self.assertEqual(opt_no_german_to_str((None, None)), '')
217
218
219 class TestLiftGermanValidator(unittest.TestCase):
220     def test_from_str(self):
221         self.assertEqual(lift_german_from_str(''), None)
222         self.assertEqual(lift_german_from_str('Nein'), [])
223         self.assertEqual(lift_german_from_str('Sessellift'), [('Sessellift', None)])
224         self.assertEqual(lift_german_from_str('Gondel (nur bis zur Hälfte)'), [('Gondel', 'nur bis zur Hälfte')])
225         self.assertEqual(lift_german_from_str('Sessellift; Taxi'), [('Sessellift', None), ('Taxi', None)])
226         self.assertEqual(lift_german_from_str('Sessellift (Wochenende); Taxi (6 Euro)'), [('Sessellift', 'Wochenende'), ('Taxi', '6 Euro')])
227
228     def test_to_str(self):
229         self.assertEqual(lift_german_to_str(None), '')
230         self.assertEqual(lift_german_to_str([]), 'Nein')
231         self.assertEqual(lift_german_to_str([('Sessellift', None)]), 'Sessellift')
232         self.assertEqual(lift_german_to_str([('Gondel', 'nur bis zur Hälfte')]), 'Gondel (nur bis zur Hälfte)')
233         self.assertEqual(lift_german_to_str([('Sessellift', None), ('Taxi', None)]), 'Sessellift; Taxi')
234         self.assertEqual(lift_german_to_str([('Sessellift', 'Wochenende'), ('Taxi', '6 Euro')]), 'Sessellift (Wochenende); Taxi (6 Euro)')
235
236
237 class TestNightLightDays(unittest.TestCase):
238     def test_from_str(self):
239         self.assertEqual(nightlightdays_from_str(''), (None, None))
240         self.assertEqual(nightlightdays_from_str('2 (Mo, Di)'), (2, 'Mo, Di'))
241         self.assertEqual(nightlightdays_from_str('7'), (7, None))
242         self.assertEqual(nightlightdays_from_str('0'), (0, None))
243         self.assertEqual(nightlightdays_from_str('(keine Ahnung)'), (None, 'keine Ahnung'))
244         with self.assertRaises(ValueError):
245             nightlightdays_from_str('8')
246         with self.assertRaises(ValueError):
247             nightlightdays_from_str('5 (Montag')
248         with self.assertRaises(ValueError):
249             nightlightdays_from_str('5.2')
250
251     def test_to_str(self):
252         self.assertEqual(nightlightdays_to_str((None, None)), '')
253         self.assertEqual(nightlightdays_to_str((2, 'Mo, Di')), '2 (Mo, Di)')
254         self.assertEqual(nightlightdays_to_str((7, None)), '7')
255         self.assertEqual(nightlightdays_to_str((0, None)), '0')
256
257
258 class TestSledRental(unittest.TestCase):
259     def test_from_str(self):
260         self.assertEqual(sledrental_from_str(''), None)
261         self.assertEqual(sledrental_from_str('Nein'), [])
262         self.assertEqual(sledrental_from_str('Talstation'), [('Talstation', None)])
263         self.assertEqual(sledrental_from_str('Talstation (unten)'), [('Talstation', 'unten')])
264         self.assertEqual(sledrental_from_str('Talstation (unten); Mittelstation'), [('Talstation', 'unten'), ('Mittelstation', None)])
265         with self.assertRaises(ValueError):
266             sledrental_from_str('(unten)')
267         with self.assertRaises(ValueError):
268             sledrental_from_str('Talstation (unten); ; Mittelstation')
269
270     def test_to_str(self):
271         self.assertEqual(sledrental_to_str(None), '')
272         self.assertEqual(sledrental_to_str([]), 'Nein')
273         self.assertEqual(sledrental_to_str([('Talstation', None)]), 'Talstation')
274         self.assertEqual(sledrental_to_str([('Talstation', 'unten')]), 'Talstation (unten)')
275         self.assertEqual(sledrental_to_str([('Talstation', 'unten'), ('Mittelstation', None)]), 'Talstation (unten); Mittelstation')
276
277
278 class TestSingleCachet(unittest.TestCase):
279     def test_from_str(self):
280         self.assertEqual(single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'), ('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel'))
281         self.assertEqual(single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer'), ('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer'))
282         with self.assertRaises(ValueError):
283             single_cachet_german_from_str('')
284         with self.assertRaises(ValueError):
285             single_cachet_german_from_str('Salzburger Naturrodelbahn-Gütesiegel 2013 schwer')
286         with self.assertRaises(ValueError):
287             single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 4013 schwer')
288         with self.assertRaises(ValueError):
289             single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 13 schwer')
290         with self.assertRaises(ValueError):
291             single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwerer')
292
293     def test_to_str(self):
294         self.assertEqual(single_cachet_german_to_str(('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')), 'Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
295         self.assertEqual(single_cachet_german_to_str(('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer')), 'Tiroler Naturrodelbahn-Gütesiegel 2013 schwer')
296
297
298 class TestCachetGerman(unittest.TestCase):
299     def test_from_str(self):
300         self.assertEqual(cachet_german_from_str(''), None)
301         self.assertEqual(cachet_german_from_str('Nein'), [])
302         self.assertEqual(cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'), [('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')])
303         self.assertEqual(cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer; Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'),
304             [('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer'), ('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')])
305         with self.assertRaises(ValueError):
306             cachet_german_from_str('Ja')
307         with self.assertRaises(ValueError):
308             cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
309
310     def test_to_str(self):
311         self.assertEqual(cachet_german_to_str(None), '')
312         self.assertEqual(cachet_german_to_str([]), 'Nein')
313         self.assertEqual(cachet_german_to_str([('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')]), 'Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
314         self.assertEqual(cachet_german_to_str([('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer'), ('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')]),
315             'Tiroler Naturrodelbahn-Gütesiegel 2013 schwer; Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
316
317
318 class TestUrl(unittest.TestCase):
319     def test_from_str(self):
320         self.assertEqual(url_from_str('http://www.winterrodeln.org/wiki/Arzler_Alm/'), 'http://www.winterrodeln.org/wiki/Arzler_Alm/')
321         self.assertEqual(url_from_str('http://www.winterrodeln.org/wiki/Nösslachhütte/'), 'http://www.winterrodeln.org/wiki/Nösslachhütte/')
322         self.assertEqual(url_from_str('https://www.winterrodeln.org/wiki/Nösslachhütte/'), 'https://www.winterrodeln.org/wiki/Nösslachhütte/')
323         with self.assertRaises(ValueError):
324             url_from_str('mailto:office@example.com')
325         with self.assertRaises(ValueError):
326             url_from_str('/wiki/Arzler_Alm/')
327
328     def test_to_str(self):
329         self.assertEqual(url_to_str('http://www.winterrodeln.org/wiki/Arzler_Alm/'), 'http://www.winterrodeln.org/wiki/Arzler_Alm/')
330         self.assertEqual(url_to_str('http://www.winterrodeln.org/wiki/Nösslachhütte/'), 'http://www.winterrodeln.org/wiki/Nösslachhütte/')
331         self.assertEqual(url_to_str('https://www.winterrodeln.org/wiki/Nösslachhütte/'), 'https://www.winterrodeln.org/wiki/Nösslachhütte/')
332
333
334 class TestWebauskunft(unittest.TestCase):
335     def test_from_str(self):
336         self.assertEqual(webauskunft_from_str('http://www.example.com/current'), (True, 'http://www.example.com/current'))
337         self.assertEqual(webauskunft_from_str(''), (None, None))
338         self.assertEqual(webauskunft_from_str('Nein'), (False, None))
339
340     def test_to_str(self):
341         self.assertEqual(webauskunft_to_str((True, 'http://www.example.com/current')), 'http://www.example.com/current')
342         self.assertEqual(webauskunft_to_str((None, None)), '')
343         self.assertEqual(webauskunft_to_str((False, None)), 'Nein')
344
345
346 class TestPhoneNumber(unittest.TestCase):
347     def test_from_str(self):
348         self.assertEqual(phone_number_from_str('+43-699-123456789'), '+43-699-123456789')
349         self.assertEqual(phone_number_from_str('+43-69945'), '+43-69945')
350         self.assertEqual(phone_number_from_str('+43-512-507-6418'), '+43-512-507-6418')
351         with self.assertRaises(ValueError):
352             phone_number_from_str('+43-')
353         with self.assertRaises(ValueError):
354             phone_number_from_str('0512123456789')
355
356     def test_to_str(self):
357         self.assertEqual(phone_number_to_str('+43-699-123456789'), '+43-699-123456789')
358         self.assertEqual(phone_number_to_str('+43-69945'), '+43-69945')
359         self.assertEqual(phone_number_to_str('+43-512-507-6418'), '+43-512-507-6418')
360
361
362 class TestTelefonauskunft(unittest.TestCase):
363     def test_from_str(self):
364         self.assertEqual(telefonauskunft_from_str(''), None)
365         self.assertEqual(telefonauskunft_from_str('Nein'), [])
366         self.assertEqual(telefonauskunft_from_str('+43-512-123456 (untertags)'), [('+43-512-123456', 'untertags')])
367         self.assertEqual(telefonauskunft_from_str('+43-512-1234 (untertags); +43-664-123456 (Alm)'), [('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')])
368         with self.assertRaises(ValueError):
369             telefonauskunft_from_str('+43-512-123456+ (untertags)')
370         with self.assertRaises(ValueError):
371             telefonauskunft_from_str('+43-512-123456')
372
373     def test_to_str(self):
374         self.assertEqual(telefonauskunft_to_str(None), '')
375         self.assertEqual(telefonauskunft_to_str([]), 'Nein')
376         self.assertEqual(telefonauskunft_to_str([('+43-512-123456', 'untertags')]), '+43-512-123456 (untertags)')
377         self.assertEqual(telefonauskunft_to_str([('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')]), '+43-512-1234 (untertags); +43-664-123456 (Alm)')
378
379
380 class TestBox(unittest.TestCase):
381     def test_from_str(self):
382         value = '{{MyTemplate|apple=2|banana=5}}'
383         converter_dict = OrderedDict([('apple', opt_int_converter), ('banana', opt_int_converter)])
384         result = box_from_str(value, 'MyTemplate', converter_dict)
385         self.assertEqual(result['apple'], 2)
386         self.assertEqual(result['banana'], 5)
387
388         value = '{{MyTemplate\n | apple = 2 \n| banana = 5 }}'
389         result = box_from_str(value, 'MyTemplate', converter_dict)
390         self.assertEqual(result['apple'], 2)
391         self.assertEqual(result['banana'], 5)
392
393         with self.assertRaises(ValueError):
394             box_from_str(value, 'myTemplate', converter_dict)
395         with self.assertRaises(ValueError):
396             value = '{{MyTemplate|apple=2|banana=five}}'
397             box_from_str(value, 'MyTemplate', converter_dict)
398         with self.assertRaises(ValueError):
399             value = '{{MyTemplate|apple=2}}'
400             box_from_str(value, 'MyTemplate', converter_dict)
401         with self.assertRaises(ValueError):
402             value = '{{MyTemplate|apple=2|banana=5|cherry=6}}'
403             box_from_str(value, 'MyTemplate', converter_dict)
404
405     def test_to_str(self):
406         value = OrderedDict([('apple', 2), ('banana', 5)])
407         converter_dict = OrderedDict([('apple', opt_int_converter), ('banana', opt_int_converter)])
408         result = box_to_str(value, 'MyTemplate', converter_dict)
409         self.assertEqual(result, '{{MyTemplate|apple=2|banana=5}}')
410
411
412 class TestRodelbahnbox(unittest.TestCase):
413     def setUp(self):
414         self.maxDiff = None
415         self.value = \
416 '''{{Rodelbahnbox
417 | Position             = 46.807218 N 12.806522 E
418 | Position oben        = 46.799014 N 12.818658 E
419 | Höhe oben            = 1046
420 | Position unten       =
421 | Höhe unten           =
422 | Länge                = 3500
423 | Schwierigkeit        = mittel
424 | Lawinen              = kaum
425 | Betreiber            = Bringungsgemeinschaft Kreithof-Dolomitenhütte
426 | Öffentliche Anreise  = Schlecht
427 | Aufstieg möglich     = Ja
428 | Aufstieg getrennt    = Teilweise
429 | Gehzeit              = 75
430 | Aufstiegshilfe       = Taxi; Sonstige (PKW bis Kreithof)
431 | Beleuchtungsanlage   = Ja
432 | Beleuchtungstage     = 7
433 | Rodelverleih         = Nein
434 | Gütesiegel           = Tiroler Naturrodelbahn-Gütesiegel 2009 mittel
435 | Webauskunft          = http://www.lienzerdolomiten.info/at/tobogorpt.html
436 | Telefonauskunft      = +43-664-2253782 (Dolomitenhütte)
437 | Bild                 = Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg
438 | In Übersichtskarte   = Ja
439 | Forumid              = 139
440 }}'''
441
442     def test_from_str(self):
443         value = rodelbahnbox_from_str(self.value)
444         self.assertEqual(value['Position'], LonLat(12.806522, 46.807218))
445         self.assertEqual(value['Position oben'], LonLat(12.818658, 46.799014))
446         self.assertEqual(value['Höhe oben'], 1046)
447         self.assertEqual(value['Position unten'], LonLat(None, None))
448         self.assertEqual(value['Höhe unten'], None)
449         self.assertEqual(value['Länge'], 3500)
450         self.assertEqual(value['Schwierigkeit'], 2)
451         self.assertEqual(value['Lawinen'], 1)
452         self.assertEqual(value['Betreiber'], 'Bringungsgemeinschaft Kreithof-Dolomitenhütte')
453         self.assertEqual(value['Öffentliche Anreise'], 4)
454         self.assertEqual(value['Aufstieg möglich'], True)
455         self.assertEqual(value['Aufstieg getrennt'], (0.5, None))
456         self.assertEqual(value['Gehzeit'], 75)
457         self.assertEqual(value['Aufstiegshilfe'], [('Taxi', None), ('Sonstige', 'PKW bis Kreithof')])
458         self.assertEqual(value['Beleuchtungsanlage'], (1.0, None))
459         self.assertEqual(value['Beleuchtungstage'], (7, None))
460         self.assertEqual(value['Rodelverleih'], [])
461         self.assertEqual(value['Gütesiegel'], [('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')])
462         self.assertEqual(value['Webauskunft'], (True, 'http://www.lienzerdolomiten.info/at/tobogorpt.html'))
463         self.assertEqual(value['Telefonauskunft'], [('+43-664-2253782', 'Dolomitenhütte')])
464         self.assertEqual(value['Bild'], 'Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg')
465         self.assertEqual(value['In Übersichtskarte'], True)
466         self.assertEqual(value['Forumid'], 139)
467
468     def test_to_str(self):
469         value = OrderedDict([
470             ('Position', LonLat(12.806522, 46.807218)),
471             ('Position oben', LonLat(12.818658, 46.799014)),
472             ('Höhe oben', 1046),
473             ('Position unten', LonLat(None, None)),
474             ('Höhe unten', None),
475             ('Länge', 3500),
476             ('Schwierigkeit', 2),
477             ('Lawinen', 1),
478             ('Betreiber', 'Bringungsgemeinschaft Kreithof-Dolomitenhütte'),
479             ('Öffentliche Anreise', 4),
480             ('Aufstieg möglich', True),
481             ('Aufstieg getrennt', (0.5, None)),
482             ('Gehzeit', 75),
483             ('Aufstiegshilfe', [('Taxi', None), ('Sonstige', 'PKW bis Kreithof')]),
484             ('Beleuchtungsanlage', (1.0, None)),
485             ('Beleuchtungstage', (7, None)),
486             ('Rodelverleih', []),
487             ('Gütesiegel', [('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')]),
488             ('Webauskunft', (True, 'http://www.lienzerdolomiten.info/at/tobogorpt.html')),
489             ('Telefonauskunft', [('+43-664-2253782', 'Dolomitenhütte')]),
490             ('Bild', 'Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg'),
491             ('In Übersichtskarte', True),
492             ('Forumid', 139)])
493         self.assertEqual(rodelbahnbox_to_str(value), self.value)
494
495
496 class TestWrValidators(unittest.TestCase):
497     def test_ValueCommentListNeinLoopNone(self):
498         v = wrpylib.wrvalidators.ValueCommentListNeinLoopNone()
499         assert v.to_python('') == None
500         assert v.to_python('Nein') == 'Nein'
501         assert v.to_python('T-Mobile (gut); A1') == 'T-Mobile (gut); A1'
502         assert v.from_python(None) == ''
503         assert v.from_python('Nein') == 'Nein'
504         assert v.from_python('T-Mobile (gut); A1') == 'T-Mobile (gut); A1'
505
506
507     def test_PhoneCommentListNeinLoopNone(self):
508         v = wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True)
509         assert v.to_python('') == None
510         assert v.to_python('Nein') == 'Nein'
511         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'
512         assert v.from_python(None) == ''
513         assert v.from_python('Nein') == 'Nein'
514         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'
515
516
517     def test_MaskedEmail(self):
518         v = wrpylib.wrvalidators.MaskedEmail()
519         assert v.to_python('') == (None, None)
520         assert v.to_python('abc.def@example.com') == ('abc.def@example.com', False)
521         assert v.to_python('abc.def(at)example.com') == ('abc.def@example.com', True)
522         assert v.from_python((None, None)) == ''
523         assert v.from_python(('abc.def@example.com', False)) == 'abc.def@example.com'
524         assert v.from_python(('abc.def@example.com', True)) == 'abc.def(at)example.com'
525
526
527     def test_EmailCommentListNeinLoopNone(self):
528         v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone()
529         assert v.to_python('') == None
530         assert v.to_python('Nein') == 'Nein'
531         assert v.to_python('first@example.com') == 'first@example.com'
532         assert v.to_python('first@example.com (Nur Winter); second@example.com') == 'first@example.com (Nur Winter); second@example.com'
533         assert v.from_python(None) == ''
534         assert v.from_python('Nein') == 'Nein'
535         assert v.from_python('first@example.com') == 'first@example.com'
536         assert v.from_python('first@example.com (Nur Winter); second@example.com') == 'first@example.com (Nur Winter); second@example.com'
537         testvalue = 'abc.def(at)example.com (comment)'
538         try:
539             v.to_python(testvalue)
540             assert False
541         except formencode.Invalid:
542             pass
543         try:
544             v.from_python(testvalue)
545             assert False
546         except formencode.Invalid:
547             pass
548         v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True)
549         assert v.to_python(testvalue) == testvalue
550         assert v.from_python(testvalue) == testvalue
551
552
553     def test_WikiPageListLoopNone(self):
554         v = wrpylib.wrvalidators.WikiPageListLoopNone()
555         assert v.to_python('') == None
556         assert v.to_python('[[Birgitzer Alm]]; [[Kemater Alm]]') == '[[Birgitzer Alm]]; [[Kemater Alm]]'
557         assert v.from_python(None) == ''
558         assert v.from_python('[[Birgitzer Alm]]; [[Kemater Alm]]') == '[[Birgitzer Alm]]; [[Kemater Alm]]'
559
560
561     def test_GasthausboxDictValidator(self):
562         v = wrpylib.wrvalidators.GasthausboxDictValidator()
563         other = collections.OrderedDict([
564             ('Position', '47.295549 N 9.986970 E'),
565             ('Höhe', '1250'),
566             ('Betreiber', ''),
567             ('Sitzplätze', ''),
568             ('Übernachtung', ''),
569             ('Rauchfrei', 'Nein'),
570             ('Rodelverleih', ''),
571             ('Handyempfang', 'A1; T-Mobile/Telering'),
572             ('Homepage', 'http://www.bergkristallhuette.com/'),
573             ('E-Mail', 'bergkristallhuette@gmx.at'),
574             ('Telefon', '+43-664-1808482'),
575             ('Bild', 'Bergkritsallhütte 2009-02-07.JPG'),
576             ('Rodelbahnen', '[[Bergkristallhütte]]')])
577         python = v.to_python(other, None)
578         other2 = v.from_python(python, None)
579         assert other == other2