2 # -*- coding: iso-8859-15 -*-
4 import wrpylib.wrvalidators
6 from wrpylib.wrvalidators import *
8 # TODO: optional converter
14 class TestNoGermanConverter(unittest.TestCase):
15 def test_from_str(self):
16 self.assertEqual(no_german_from_str('abc'), (True, 'abc'))
17 self.assertEqual(no_german_from_str('Nein'), (False, None))
18 with self.assertRaises(ValueError):
19 no_german_from_str('')
21 def test_to_str(self):
22 self.assertEqual(no_german_to_str((True, 'abc')), 'abc')
23 self.assertEqual(no_german_to_str((False, None)), 'Nein')
26 # "optional"/"no" converter
27 # -------------------------
29 class TestOptNoGerman(unittest.TestCase):
30 def test_from_str(self):
31 self.assertEqual(opt_no_german_from_str('abc'), (True, 'abc'))
32 self.assertEqual(opt_no_german_from_str('Nein'), (False, None))
33 self.assertEqual(opt_no_german_from_str(''), (None, None))
35 def test_to_str(self):
36 self.assertEqual(opt_no_german_to_str((True, 'abc')), 'abc')
37 self.assertEqual(opt_no_german_to_str((False, None)), 'Nein')
38 self.assertEqual(opt_no_german_to_str((None, None)), '')
41 # TODO: choice converter
45 # TODO: dict converter
49 # TODO: enum/"list" converter
50 # ---------------------
53 # TODO: value/comment converter
54 # -----------------------
60 class TestStr(unittest.TestCase):
61 def test_from_str(self):
62 self.assertEqual('', str_from_str(''))
63 self.assertEqual('abc', str_from_str('abc'))
65 def test_to_str(self):
66 self.assertEqual('', str_to_str(''))
67 self.assertEqual('abc', str_to_str('abc'))
70 class TestReqStr(unittest.TestCase):
71 def test_from_str(self):
72 self.assertEqual('abc', req_str_from_str('abc'))
73 self.assertEqual(' ', req_str_from_str(' '))
74 with self.assertRaises(ValueError):
78 class TestOptStr(unittest.TestCase):
79 def test_from_str(self):
80 self.assertEqual('abc', opt_str_from_str('abc'))
81 self.assertEqual(' ', opt_str_from_str(' '))
82 self.assertEqual(None, opt_str_from_str(''))
84 def test_to_str(self):
85 self.assertEqual('abc', opt_str_to_str('abc'))
86 self.assertEqual(' ', opt_str_to_str(' '))
87 self.assertEqual('', opt_str_to_str(None))
90 # TODO: optional no or string converter
91 # -------------------------------
97 class TestInt(unittest.TestCase):
98 def test_from_str(self):
99 self.assertEqual(42, int_from_str('42'))
100 self.assertEqual(42, int_from_str('+42'))
101 self.assertEqual(-20, int_from_str('-20'))
102 self.assertEqual(0, int_from_str('0', min=0))
103 self.assertEqual(10, int_from_str('10', max=10))
104 with self.assertRaises(ValueError):
106 with self.assertRaises(ValueError):
108 with self.assertRaises(ValueError):
109 int_from_str('-1', min=0)
110 with self.assertRaises(ValueError):
111 int_from_str('11', max=10)
112 with self.assertRaises(ValueError):
114 with self.assertRaises(ValueError):
117 def test_to_str(self):
118 self.assertEqual('20', int_to_str(20))
119 self.assertEqual('-20', int_to_str(-20))
120 self.assertEqual('0', int_to_str(0))
123 class TestOptInt(unittest.TestCase):
124 def test_from_str(self):
125 self.assertEqual(42, opt_int_from_str('42'))
126 self.assertEqual(42, opt_int_from_str('+42'))
127 self.assertEqual(-20, opt_int_from_str('-20'))
128 self.assertEqual(None, opt_int_from_str(''))
129 with self.assertRaises(ValueError):
130 opt_int_from_str('abc')
131 with self.assertRaises(ValueError):
132 opt_int_from_str('10.0')
133 with self.assertRaises(ValueError):
134 opt_int_from_str('0d')
136 def test_to_str(self):
137 self.assertEqual('20', opt_int_to_str(20))
138 self.assertEqual('-20', opt_int_to_str(-20))
139 self.assertEqual('0', opt_int_to_str(0))
140 self.assertEqual('', opt_int_to_str(None))
143 class TestOptUInt(unittest.TestCase):
144 def test_from_str(self):
145 self.assertEqual(42, opt_uint_from_str('42'))
146 self.assertEqual(0, opt_uint_from_str('0'))
147 self.assertEqual(None, opt_uint_from_str(''))
148 with self.assertRaises(ValueError):
149 opt_uint_from_str('-1')
151 def test_to_str(self):
152 self.assertEqual('20', opt_uint_to_str(20))
153 self.assertEqual('0', opt_uint_to_str(0))
154 self.assertEqual('', opt_uint_to_str(None))
161 class TestEnumConverter(unittest.TestCase):
162 def test_from_str(self):
163 self.assertEqual(enum_from_str(''), [])
164 self.assertEqual(enum_from_str('abc'), ['abc'])
165 self.assertEqual(enum_from_str('abc; def'), ['abc', 'def'])
166 self.assertEqual(enum_from_str('abc; def;ghi'), ['abc', 'def', 'ghi'])
168 def test_to_str(self):
169 self.assertEqual(enum_to_str(['abc', 'def', 'ghi']), 'abc; def; ghi')
170 self.assertEqual(enum_to_str(['abc']), 'abc')
171 self.assertEqual(enum_to_str(['']), '')
172 self.assertEqual(enum_to_str([]), '')
175 class TestDifficultyGerman(unittest.TestCase):
176 def test_from_str(self):
177 self.assertEqual(difficulty_german_from_str('leicht'), 1)
178 self.assertEqual(difficulty_german_from_str('mittel'), 2)
179 with self.assertRaises(ValueError):
180 difficulty_german_from_str('dontknow')
181 with self.assertRaises(ValueError):
182 difficulty_german_from_str('')
184 def test_to_str(self):
185 self.assertEqual(difficulty_german_to_str(1), 'leicht')
188 class TestTristateGerman(unittest.TestCase):
189 def test_from_str(self):
190 self.assertEqual(tristate_german_from_str('Ja'), 1.0)
191 self.assertEqual(tristate_german_from_str('Teilweise'), 0.5)
192 self.assertEqual(tristate_german_from_str('Nein'), 0)
193 with self.assertRaises(ValueError):
194 tristate_german_from_str('')
195 with self.assertRaises(ValueError):
196 tristate_german_from_str('Vielleicht')
198 def test_to_str(self):
199 self.assertEqual(tristate_german_to_str(1.0), 'Ja')
200 self.assertEqual(tristate_german_to_str(0.5), 'Teilweise')
201 self.assertEqual(tristate_german_to_str(0.0), 'Nein')
204 class TestOptTristateGerman(unittest.TestCase):
205 def test_from_str(self):
206 self.assertEqual(opt_tristate_german_from_str('Ja'), 1.0)
207 self.assertEqual(opt_tristate_german_from_str('Teilweise'), 0.5)
208 self.assertEqual(opt_tristate_german_from_str('Nein'), 0)
209 self.assertEqual(opt_tristate_german_from_str(''), None)
210 with self.assertRaises(ValueError):
211 opt_tristate_german_from_str('Vielleicht')
213 def test_to_str(self):
214 self.assertEqual(opt_tristate_german_to_str(1.0), 'Ja')
215 self.assertEqual(opt_tristate_german_to_str(0.5), 'Teilweise')
216 self.assertEqual(opt_tristate_german_to_str(0.0), 'Nein')
217 self.assertEqual(opt_tristate_german_to_str(None), '')
220 class TestOptTristateGermanComment(unittest.TestCase):
221 def test_from_str(self):
222 self.assertEqual(opt_tristate_german_comment_from_str('Ja'), (1.0, None))
223 self.assertEqual(opt_tristate_german_comment_from_str('Teilweise'), (0.5, None))
224 self.assertEqual(opt_tristate_german_comment_from_str('Nein'), (0, None))
225 self.assertEqual(opt_tristate_german_comment_from_str('Teilweise (nur ganz oben nicht)'), (0.5, 'nur ganz oben nicht'))
226 self.assertEqual(opt_tristate_german_comment_from_str(''), (None, None))
227 with self.assertRaises(ValueError):
228 opt_tristate_german_from_str('Vielleicht')
229 with self.assertRaises(ValueError):
230 opt_tristate_german_from_str('(Ja)')
232 def test_to_str(self):
233 self.assertEqual(opt_tristate_german_comment_to_str((1.0, None)), 'Ja')
234 self.assertEqual(opt_tristate_german_comment_to_str((0.5, None)), 'Teilweise')
235 self.assertEqual(opt_tristate_german_comment_to_str((0.0, None)), 'Nein')
236 self.assertEqual(opt_tristate_german_comment_to_str((None, None)), '')
239 class TestLonLat(unittest.TestCase):
240 def test_from_str(self):
241 self.assertEqual(lonlat_from_str('47.076207 N 11.453553 E'), LonLat(11.453553, 47.076207))
242 with self.assertRaises(ValueError):
243 lonlat_from_str('47.076207 N 11.453553')
245 def test_to_str(self):
246 self.assertEqual(lonlat_to_str(LonLat(11.453553, 47.076207)), '47.076207 N 11.453553 E')
249 class TestValueCommentConverter(unittest.TestCase):
250 def test_from_str(self):
251 self.assertEqual(value_comment_from_str('abc (defg)'), ('abc', 'defg'))
252 self.assertEqual(value_comment_from_str('abc ()'), ('abc', ''))
253 self.assertEqual(value_comment_from_str('(def)'), ('', 'def'))
254 self.assertEqual(value_comment_from_str('ab((cd))'), ('ab', '(cd)'))
255 self.assertEqual(value_comment_from_str('ab((c(d)[(])))'), ('ab', '(c(d)[(]))'))
256 self.assertEqual(value_comment_from_str('ab((cd)'), ('ab(', 'cd'))
257 self.assertEqual(value_comment_from_str('abcd (ef) '), ('abcd', 'ef'))
258 self.assertEqual(value_comment_from_str('abc', comment_optional=True), ('abc', ''))
259 with self.assertRaises(ValueError):
260 value_comment_from_str('abc (')
261 with self.assertRaises(ValueError):
262 value_comment_from_str('abc )')
263 with self.assertRaises(ValueError):
264 value_comment_from_str('abc (def)g')
265 with self.assertRaises(ValueError):
266 value_comment_from_str('abc (b))')
267 with self.assertRaises(ValueError):
268 value_comment_from_str('abc')
270 def test_to_str(self):
271 self.assertEqual(value_comment_to_str(('abc', 'defg')), 'abc (defg)')
272 self.assertEqual(value_comment_to_str(('abc', '')), 'abc ()')
273 self.assertEqual(value_comment_to_str(('', 'def')), '(def)')
274 self.assertEqual(value_comment_to_str(('ab', '(cd)')), 'ab ((cd))')
275 self.assertEqual(value_comment_to_str(('ab', '(c(d)[(]))')), 'ab ((c(d)[(])))')
276 self.assertEqual(value_comment_to_str(('ab(', 'cd')), 'ab( (cd)')
277 self.assertEqual(value_comment_to_str(('abcd', 'ef')), 'abcd (ef)')
278 self.assertEqual(value_comment_to_str(('abc', ''), comment_optional=True), 'abc')
281 class TestLiftGermanValidator(unittest.TestCase):
282 def test_from_str(self):
283 self.assertEqual(lift_german_from_str(''), None)
284 self.assertEqual(lift_german_from_str('Nein'), [])
285 self.assertEqual(lift_german_from_str('Sessellift'), [('Sessellift', None)])
286 self.assertEqual(lift_german_from_str('Gondel (nur bis zur Hälfte)'), [('Gondel', 'nur bis zur Hälfte')])
287 self.assertEqual(lift_german_from_str('Sessellift; Taxi'), [('Sessellift', None), ('Taxi', None)])
288 self.assertEqual(lift_german_from_str('Sessellift (Wochenende); Taxi (6 Euro)'), [('Sessellift', 'Wochenende'), ('Taxi', '6 Euro')])
290 def test_to_str(self):
291 self.assertEqual(lift_german_to_str(None), '')
292 self.assertEqual(lift_german_to_str([]), 'Nein')
293 self.assertEqual(lift_german_to_str([('Sessellift', None)]), 'Sessellift')
294 self.assertEqual(lift_german_to_str([('Gondel', 'nur bis zur Hälfte')]), 'Gondel (nur bis zur Hälfte)')
295 self.assertEqual(lift_german_to_str([('Sessellift', None), ('Taxi', None)]), 'Sessellift; Taxi')
296 self.assertEqual(lift_german_to_str([('Sessellift', 'Wochenende'), ('Taxi', '6 Euro')]), 'Sessellift (Wochenende); Taxi (6 Euro)')
299 class TestNightLightDays(unittest.TestCase):
300 def test_from_str(self):
301 self.assertEqual(nightlightdays_from_str(''), (None, None))
302 self.assertEqual(nightlightdays_from_str('2 (Mo, Di)'), (2, 'Mo, Di'))
303 self.assertEqual(nightlightdays_from_str('7'), (7, None))
304 self.assertEqual(nightlightdays_from_str('0'), (0, None))
305 self.assertEqual(nightlightdays_from_str('(keine Ahnung)'), (None, 'keine Ahnung'))
306 with self.assertRaises(ValueError):
307 nightlightdays_from_str('8')
308 with self.assertRaises(ValueError):
309 nightlightdays_from_str('5 (Montag')
310 with self.assertRaises(ValueError):
311 nightlightdays_from_str('5.2')
313 def test_to_str(self):
314 self.assertEqual(nightlightdays_to_str((None, None)), '')
315 self.assertEqual(nightlightdays_to_str((2, 'Mo, Di')), '2 (Mo, Di)')
316 self.assertEqual(nightlightdays_to_str((7, None)), '7')
317 self.assertEqual(nightlightdays_to_str((0, None)), '0')
320 class TestOptStrOptCommentEnum(unittest.TestCase):
321 def test_from_str(self):
322 self.assertEqual(opt_str_opt_comment_enum_from_str(''), None)
323 self.assertEqual(opt_str_opt_comment_enum_from_str('Nein'), [])
324 self.assertEqual(opt_str_opt_comment_enum_from_str('Talstation'), [('Talstation', None)])
325 self.assertEqual(opt_str_opt_comment_enum_from_str('Talstation (unten)'), [('Talstation', 'unten')])
326 self.assertEqual(opt_str_opt_comment_enum_from_str('Talstation (unten); Mittelstation'), [('Talstation', 'unten'), ('Mittelstation', None)])
327 with self.assertRaises(ValueError):
328 opt_str_opt_comment_enum_from_str('(unten)')
329 with self.assertRaises(ValueError):
330 opt_str_opt_comment_enum_from_str('Talstation (unten); ; Mittelstation')
332 def test_to_str(self):
333 self.assertEqual(opt_str_opt_comment_enum_to_str(None), '')
334 self.assertEqual(opt_str_opt_comment_enum_to_str([]), 'Nein')
335 self.assertEqual(opt_str_opt_comment_enum_to_str([('Talstation', None)]), 'Talstation')
336 self.assertEqual(opt_str_opt_comment_enum_to_str([('Talstation', 'unten')]), 'Talstation (unten)')
337 self.assertEqual(opt_str_opt_comment_enum_to_str([('Talstation', 'unten'), ('Mittelstation', None)]), 'Talstation (unten); Mittelstation')
340 class TestNoOrStr(unittest.TestCase):
341 def test_from_str(self):
342 self.assertEqual((False, None), opt_no_or_str_from_str('Nein'))
343 self.assertEqual((True, 'Nur Wochenende'), opt_no_or_str_from_str('Nur Wochenende'))
344 self.assertEqual((True, 'Ja'), opt_no_or_str_from_str('Ja'))
345 self.assertEqual((None, None), opt_no_or_str_from_str(''))
347 def test_to_str(self):
348 self.assertEqual('Nein', opt_no_or_str_to_str((False, None)))
349 self.assertEqual('Nur Wochenende', opt_no_or_str_to_str((True, 'Nur Wochenende')))
350 self.assertEqual('Ja', opt_no_or_str_to_str((True, 'Ja')))
351 self.assertEqual('', opt_no_or_str_to_str((None, None)))
355 class TestSingleCachet(unittest.TestCase):
356 def test_from_str(self):
357 self.assertEqual(single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'), ('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel'))
358 self.assertEqual(single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer'), ('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer'))
359 with self.assertRaises(ValueError):
360 single_cachet_german_from_str('')
361 with self.assertRaises(ValueError):
362 single_cachet_german_from_str('Salzburger Naturrodelbahn-Gütesiegel 2013 schwer')
363 with self.assertRaises(ValueError):
364 single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 4013 schwer')
365 with self.assertRaises(ValueError):
366 single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 13 schwer')
367 with self.assertRaises(ValueError):
368 single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwerer')
370 def test_to_str(self):
371 self.assertEqual(single_cachet_german_to_str(('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')), 'Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
372 self.assertEqual(single_cachet_german_to_str(('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer')), 'Tiroler Naturrodelbahn-Gütesiegel 2013 schwer')
375 class TestCachetGerman(unittest.TestCase):
376 def test_from_str(self):
377 self.assertEqual(cachet_german_from_str(''), None)
378 self.assertEqual(cachet_german_from_str('Nein'), [])
379 self.assertEqual(cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'), [('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')])
380 self.assertEqual(cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer; Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'),
381 [('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer'), ('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')])
382 with self.assertRaises(ValueError):
383 cachet_german_from_str('Ja')
384 with self.assertRaises(ValueError):
385 cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
387 def test_to_str(self):
388 self.assertEqual(cachet_german_to_str(None), '')
389 self.assertEqual(cachet_german_to_str([]), 'Nein')
390 self.assertEqual(cachet_german_to_str([('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')]), 'Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
391 self.assertEqual(cachet_german_to_str([('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer'), ('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')]),
392 'Tiroler Naturrodelbahn-Gütesiegel 2013 schwer; Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
395 class TestUrl(unittest.TestCase):
396 def test_from_str(self):
397 self.assertEqual(url_from_str('http://www.winterrodeln.org/wiki/Arzler_Alm/'), 'http://www.winterrodeln.org/wiki/Arzler_Alm/')
398 self.assertEqual(url_from_str('http://www.winterrodeln.org/wiki/Nösslachhütte/'), 'http://www.winterrodeln.org/wiki/Nösslachhütte/')
399 self.assertEqual(url_from_str('https://www.winterrodeln.org/wiki/Nösslachhütte/'), 'https://www.winterrodeln.org/wiki/Nösslachhütte/')
400 with self.assertRaises(ValueError):
401 url_from_str('mailto:office@example.com')
402 with self.assertRaises(ValueError):
403 url_from_str('/wiki/Arzler_Alm/')
405 def test_to_str(self):
406 self.assertEqual(url_to_str('http://www.winterrodeln.org/wiki/Arzler_Alm/'), 'http://www.winterrodeln.org/wiki/Arzler_Alm/')
407 self.assertEqual(url_to_str('http://www.winterrodeln.org/wiki/Nösslachhütte/'), 'http://www.winterrodeln.org/wiki/Nösslachhütte/')
408 self.assertEqual(url_to_str('https://www.winterrodeln.org/wiki/Nösslachhütte/'), 'https://www.winterrodeln.org/wiki/Nösslachhütte/')
411 class TestWebauskunft(unittest.TestCase):
412 def test_from_str(self):
413 self.assertEqual(webauskunft_from_str('http://www.example.com/current'), (True, 'http://www.example.com/current'))
414 self.assertEqual(webauskunft_from_str(''), (None, None))
415 self.assertEqual(webauskunft_from_str('Nein'), (False, None))
417 def test_to_str(self):
418 self.assertEqual(webauskunft_to_str((True, 'http://www.example.com/current')), 'http://www.example.com/current')
419 self.assertEqual(webauskunft_to_str((None, None)), '')
420 self.assertEqual(webauskunft_to_str((False, None)), 'Nein')
423 class TestPhoneNumber(unittest.TestCase):
424 def test_from_str(self):
425 self.assertEqual(phone_number_from_str('+43-699-123456789'), '+43-699-123456789')
426 self.assertEqual(phone_number_from_str('+43-69945'), '+43-69945')
427 self.assertEqual(phone_number_from_str('+43-512-507-6418'), '+43-512-507-6418')
428 with self.assertRaises(ValueError):
429 phone_number_from_str('+43-')
430 with self.assertRaises(ValueError):
431 phone_number_from_str('0512123456789')
433 def test_to_str(self):
434 self.assertEqual(phone_number_to_str('+43-699-123456789'), '+43-699-123456789')
435 self.assertEqual(phone_number_to_str('+43-69945'), '+43-69945')
436 self.assertEqual(phone_number_to_str('+43-512-507-6418'), '+43-512-507-6418')
439 class TestTelefonauskunft(unittest.TestCase):
440 def test_from_str(self):
441 self.assertEqual(opt_phone_comment_enum_from_str(''), None)
442 self.assertEqual(opt_phone_comment_enum_from_str('Nein'), [])
443 self.assertEqual(opt_phone_comment_enum_from_str('+43-512-123456 (untertags)'), [('+43-512-123456', 'untertags')])
444 self.assertEqual(opt_phone_comment_enum_from_str('+43-512-1234 (untertags); +43-664-123456 (Alm)'), [('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')])
445 with self.assertRaises(ValueError):
446 opt_phone_comment_enum_from_str('+43-512-123456+ (untertags)')
447 with self.assertRaises(ValueError):
448 opt_phone_comment_enum_from_str('+43-512-123456')
450 def test_to_str(self):
451 self.assertEqual(opt_phone_comment_enum_to_str(None), '')
452 self.assertEqual(opt_phone_comment_enum_to_str([]), 'Nein')
453 self.assertEqual(opt_phone_comment_enum_to_str([('+43-512-123456', 'untertags')]), '+43-512-123456 (untertags)')
454 self.assertEqual(opt_phone_comment_enum_to_str([('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')]), '+43-512-1234 (untertags); +43-664-123456 (Alm)')
457 class TestEmail(unittest.TestCase):
459 self.good_addresses = ['office@example.com', 'winter+rodeln@localhost', 'joe.doe@exämple.com']
460 self.bad_addresses = ['öffice@example.com', 'winter rodeln@localhost', 'www.winterrodeln.org', 'mailto:info@example.com', 'info@example.com.']
462 def test_from_str(self):
463 for value in self.good_addresses:
464 self.assertEqual(value, email_from_str(value))
465 for value in self.bad_addresses:
466 with self.assertRaises(ValueError):
467 email_from_str(value)
469 def test_to_str(self):
470 for value in self.good_addresses:
471 self.assertEqual(value, email_to_str(value))
474 class TestMaskedEmail(unittest.TestCase):
475 def test_from_str(self):
476 self.assertEqual(('office@example.com', False), masked_email_from_str('office@example.com'))
477 self.assertEqual(('office@example.com', True), masked_email_from_str('office(at)example.com'))
478 with self.assertRaises(ValueError):
479 masked_email_from_str('office@example.com', masked_only=True)
480 with self.assertRaises(ValueError):
481 masked_email_from_str('off ice@example.com')
483 def test_to_str(self):
484 self.assertEqual('office@example.com', masked_email_to_str(('office@example.com', False)))
485 self.assertEqual('office(at)example.com', masked_email_to_str(('office@example.com', True)))
486 self.assertEqual('office()example.com', masked_email_to_str(('office@example.com', True), '()'))
489 class TestEmails(unittest.TestCase):
490 def test_from_str(self):
491 self.assertEqual(None, emails_from_str(''))
492 self.assertEqual([], emails_from_str('Nein'))
493 self.assertEqual([(('info@example.com', False), None)], emails_from_str('info@example.com'))
494 # self.assertEqual([(('info@example.com', True), None)], emails_from_str('info(at)example.com'))
495 self.assertEqual([(('info@example.com', False), 'Office')], emails_from_str('info@example.com (Office)'))
496 self.assertEqual([(('info@example.com', False), None), (('home@example.com', False), 'Privat')], emails_from_str('info@example.com; home@example.com (Privat)'))
497 with self.assertRaises(ValueError):
498 emails_from_str('nein')
499 with self.assertRaises(ValueError):
500 emails_from_str('info@example.com; ho me@example.com (Privat)')
502 def test_to_str(self):
503 self.assertEqual('', emails_to_str(None))
504 self.assertEqual('Nein', emails_to_str([]))
505 self.assertEqual('info@example.com', emails_to_str([(('info@example.com', False), None)]))
506 self.assertEqual('info@example.com (Office)', emails_to_str([(('info@example.com', False), 'Office')]))
507 self.assertEqual('info@example.com; home@example.com (Privat)', emails_to_str([(('info@example.com', False), None), (('home@example.com', False), 'Privat')]))
510 class TestWikipage(unittest.TestCase):
511 def test_from_str(self):
512 self.assertEqual('[[Birgitzer Alm]]', wikipage_from_str('[[Birgitzer Alm]]'))
513 with self.assertRaises(ValueError):
514 wikipage_from_str('[[')
515 with self.assertRaises(ValueError):
516 wikipage_from_str('')
517 with self.assertRaises(ValueError):
518 wikipage_from_str('Birgitzer Alm')
520 def test_to_str(self):
521 self.assertEqual('[[Birgitzer Alm]]', wikipage_to_str('[[Birgitzer Alm]]'))
524 class TestOptWikipageEnum(unittest.TestCase):
525 def test_from_str(self):
526 self.assertEqual(['[[Birgitzer Alm]]', '[[Kemater Alm]]'], opt_wikipage_enum_from_str('[[Birgitzer Alm]]; [[Kemater Alm]]'))
527 self.assertEqual(['[[Birgitzer Alm]]'], opt_wikipage_enum_from_str('[[Birgitzer Alm]]'))
528 self.assertEqual([], opt_wikipage_enum_from_str('Nein'))
529 self.assertEqual(None, opt_wikipage_enum_from_str(''))
531 def test_to_str(self):
532 self.assertEqual('[[Birgitzer Alm]]; [[Kemater Alm]]', opt_wikipage_enum_to_str(['[[Birgitzer Alm]]', '[[Kemater Alm]]']))
533 self.assertEqual('[[Birgitzer Alm]]', opt_wikipage_enum_to_str(['[[Birgitzer Alm]]']))
534 self.assertEqual('Nein', opt_wikipage_enum_to_str([]))
535 self.assertEqual('', opt_wikipage_enum_to_str(None))
538 class TestBox(unittest.TestCase):
539 def test_from_str(self):
540 value = '{{MyTemplate|apple=2|banana=5}}'
541 converter_dict = OrderedDict([('apple', opt_uint_converter), ('banana', opt_uint_converter)])
542 result = wikibox_from_str(value, 'MyTemplate', converter_dict)
543 self.assertEqual(result['apple'], 2)
544 self.assertEqual(result['banana'], 5)
546 value = '{{MyTemplate\n | apple = 2 \n| banana = 5 }}'
547 result = wikibox_from_str(value, 'MyTemplate', converter_dict)
548 self.assertEqual(result['apple'], 2)
549 self.assertEqual(result['banana'], 5)
551 with self.assertRaises(ValueError):
552 wikibox_from_str(value, 'myTemplate', converter_dict)
553 with self.assertRaises(ValueError):
554 value = '{{MyTemplate|apple=2|banana=five}}'
555 wikibox_from_str(value, 'MyTemplate', converter_dict)
556 with self.assertRaises(ValueError):
557 value = '{{MyTemplate|apple=2}}'
558 wikibox_from_str(value, 'MyTemplate', converter_dict)
559 with self.assertRaises(ValueError):
560 value = '{{MyTemplate|apple=2|banana=5|cherry=6}}'
561 wikibox_from_str(value, 'MyTemplate', converter_dict)
563 def test_to_str(self):
564 value = OrderedDict([('apple', 2), ('banana', 5)])
565 converter_dict = OrderedDict([('apple', opt_uint_converter), ('banana', opt_uint_converter)])
566 result = wikibox_to_str(value, 'MyTemplate', converter_dict)
567 self.assertEqual(result, '{{MyTemplate|apple=2|banana=5}}')
570 class TestRodelbahnbox(unittest.TestCase):
575 | Position = 46.807218 N 12.806522 E
576 | Position oben = 46.799014 N 12.818658 E
581 | Schwierigkeit = mittel
583 | Betreiber = Bringungsgemeinschaft Kreithof-Dolomitenhütte
584 | Öffentliche Anreise = Schlecht
585 | Aufstieg möglich = Ja
586 | Aufstieg getrennt = Teilweise
588 | Aufstiegshilfe = Taxi; Sonstige (PKW bis Kreithof)
589 | Beleuchtungsanlage = Ja
590 | Beleuchtungstage = 7
591 | Rodelverleih = Nein
592 | Gütesiegel = Tiroler Naturrodelbahn-Gütesiegel 2009 mittel
593 | Webauskunft = http://www.lienzerdolomiten.info/at/tobogorpt.html
594 | Telefonauskunft = +43-664-2253782 (Dolomitenhütte)
595 | Bild = Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg
596 | In Übersichtskarte = Ja
600 def test_from_str(self):
601 value = rodelbahnbox_from_str(self.value)
602 self.assertEqual(value['Position'], LonLat(12.806522, 46.807218))
603 self.assertEqual(value['Position oben'], LonLat(12.818658, 46.799014))
604 self.assertEqual(value['Höhe oben'], 1046)
605 self.assertEqual(value['Position unten'], LonLat(None, None))
606 self.assertEqual(value['Höhe unten'], None)
607 self.assertEqual(value['Länge'], 3500)
608 self.assertEqual(value['Schwierigkeit'], 2)
609 self.assertEqual(value['Lawinen'], 1)
610 self.assertEqual(value['Betreiber'], 'Bringungsgemeinschaft Kreithof-Dolomitenhütte')
611 self.assertEqual(value['Öffentliche Anreise'], 4)
612 self.assertEqual(value['Aufstieg möglich'], True)
613 self.assertEqual(value['Aufstieg getrennt'], (0.5, None))
614 self.assertEqual(value['Gehzeit'], 75)
615 self.assertEqual(value['Aufstiegshilfe'], [('Taxi', None), ('Sonstige', 'PKW bis Kreithof')])
616 self.assertEqual(value['Beleuchtungsanlage'], (1.0, None))
617 self.assertEqual(value['Beleuchtungstage'], (7, None))
618 self.assertEqual(value['Rodelverleih'], [])
619 self.assertEqual(value['Gütesiegel'], [('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')])
620 self.assertEqual(value['Webauskunft'], (True, 'http://www.lienzerdolomiten.info/at/tobogorpt.html'))
621 self.assertEqual(value['Telefonauskunft'], [('+43-664-2253782', 'Dolomitenhütte')])
622 self.assertEqual(value['Bild'], 'Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg')
623 self.assertEqual(value['In Übersichtskarte'], True)
624 self.assertEqual(value['Forumid'], 139)
626 def test_to_str(self):
627 value = OrderedDict([
628 ('Position', LonLat(12.806522, 46.807218)),
629 ('Position oben', LonLat(12.818658, 46.799014)),
631 ('Position unten', LonLat(None, None)),
632 ('Höhe unten', None),
634 ('Schwierigkeit', 2),
636 ('Betreiber', 'Bringungsgemeinschaft Kreithof-Dolomitenhütte'),
637 ('Öffentliche Anreise', 4),
638 ('Aufstieg möglich', True),
639 ('Aufstieg getrennt', (0.5, None)),
641 ('Aufstiegshilfe', [('Taxi', None), ('Sonstige', 'PKW bis Kreithof')]),
642 ('Beleuchtungsanlage', (1.0, None)),
643 ('Beleuchtungstage', (7, None)),
644 ('Rodelverleih', []),
645 ('Gütesiegel', [('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')]),
646 ('Webauskunft', (True, 'http://www.lienzerdolomiten.info/at/tobogorpt.html')),
647 ('Telefonauskunft', [('+43-664-2253782', 'Dolomitenhütte')]),
648 ('Bild', 'Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg'),
649 ('In Übersichtskarte', True),
651 self.assertEqual(rodelbahnbox_to_str(value), self.value)
654 class TestWrValidators(unittest.TestCase):
655 def test_ValueCommentListNeinLoopNone(self):
656 v = wrpylib.wrvalidators.ValueCommentListNeinLoopNone()
657 assert v.to_python('') == None
658 assert v.to_python('Nein') == 'Nein'
659 assert v.to_python('T-Mobile (gut); A1') == 'T-Mobile (gut); A1'
660 assert v.from_python(None) == ''
661 assert v.from_python('Nein') == 'Nein'
662 assert v.from_python('T-Mobile (gut); A1') == 'T-Mobile (gut); A1'
665 class TestGasthausbox(unittest.TestCase):
666 def test_GasthausboxDictValidator(self):
667 v = wrpylib.wrvalidators.GasthausboxDictValidator()
668 other = collections.OrderedDict([
669 ('Position', '47.295549 N 9.986970 E'),
673 ('Übernachtung', ''),
674 ('Rauchfrei', 'Nein'),
675 ('Rodelverleih', ''),
676 ('Handyempfang', 'A1; T-Mobile/Telering'),
677 ('Homepage', 'http://www.bergkristallhuette.com/'),
678 ('E-Mail', 'bergkristallhuette@gmx.at'),
679 ('Telefon', '+43-664-1808482'),
680 ('Bild', 'Bergkritsallhütte 2009-02-07.JPG'),
681 ('Rodelbahnen', '[[Bergkristallhütte]]')])
682 python = v.to_python(other, None)
683 other2 = v.from_python(python, None)
684 assert other == other2