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