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((True, 'abc'), no_german_from_str('abc', req_str_from_str))
17 self.assertEqual((False, None), no_german_from_str('Nein', req_str_from_str))
18 with self.assertRaises(ValueError):
19 no_german_from_str('', req_str_from_str)
21 def test_to_str(self):
22 self.assertEqual('abc', no_german_to_str((True, 'abc'), str_to_str))
23 self.assertEqual('Nein', no_german_to_str((False, None), str_to_str))
26 # "optional"/"no" converter
27 # -------------------------
29 class TestOptNoGerman(unittest.TestCase):
30 def test_from_str(self):
31 self.assertEqual((True, 'abc'), opt_no_german_from_str('abc', str_from_str))
32 self.assertEqual((False, None), opt_no_german_from_str('Nein', str_from_str))
33 self.assertEqual((None, None), opt_no_german_from_str('', str_from_str))
35 def test_to_str(self):
36 self.assertEqual('abc', opt_no_german_to_str((True, 'abc'), str_to_str))
37 self.assertEqual('Nein', opt_no_german_to_str((False, None), str_to_str))
38 self.assertEqual('', opt_no_german_to_str((None, None), str_to_str))
41 # TODO: choice converter
45 # TODO: dict converter
49 # enum/"list" converter
50 # ---------------------
52 class TestEnumConverter(unittest.TestCase):
53 def test_from_str(self):
54 self.assertEqual([], enum_from_str('', str_from_str))
55 self.assertEqual(['abc'], enum_from_str('abc', str_from_str))
56 self.assertEqual(['abc', 'def'], enum_from_str('abc; def', str_from_str))
57 self.assertEqual(['abc', 'def', 'ghi'], enum_from_str('abc; def;ghi', str_from_str))
59 def test_to_str(self):
60 self.assertEqual('abc; def; ghi', enum_to_str(['abc', 'def', 'ghi'], str_to_str))
61 self.assertEqual('abc', enum_to_str(['abc'], str_to_str))
62 self.assertEqual('', enum_to_str([''], str_to_str))
63 self.assertEqual('', enum_to_str([], str_to_str))
66 # value/comment converter
67 # -----------------------
69 class TestValueCommentConverter(unittest.TestCase):
70 def test_from_str(self):
71 self.assertEqual(('abc', 'defg'), value_comment_from_str('abc (defg)', str_from_str, str_from_str))
72 self.assertEqual(('abc', ''), value_comment_from_str('abc ()', str_from_str, str_from_str))
73 self.assertEqual(('', 'def'), value_comment_from_str('(def)', str_from_str, str_from_str))
74 self.assertEqual(('ab', '(cd)'), value_comment_from_str('ab((cd))', str_from_str, str_from_str))
75 self.assertEqual(('ab', '(c(d)[(]))'), value_comment_from_str('ab((c(d)[(])))', str_from_str, str_from_str))
76 self.assertEqual(('ab(', 'cd'), value_comment_from_str('ab((cd)', str_from_str, str_from_str))
77 self.assertEqual(('abcd', 'ef'), value_comment_from_str('abcd (ef) ', str_from_str, str_from_str))
78 self.assertEqual(('abc', ''), value_comment_from_str('abc', str_from_str, str_from_str, comment_optional=True))
79 with self.assertRaises(ValueError):
80 value_comment_from_str('abc (', str_from_str, str_from_str)
81 with self.assertRaises(ValueError):
82 value_comment_from_str('abc )', str_from_str, str_from_str)
83 with self.assertRaises(ValueError):
84 value_comment_from_str('abc (def)g', str_from_str, str_from_str)
85 with self.assertRaises(ValueError):
86 value_comment_from_str('abc (b))', str_from_str, str_from_str)
87 with self.assertRaises(ValueError):
88 value_comment_from_str('abc', str_from_str, str_from_str)
90 def test_to_str(self):
91 self.assertEqual('abc (defg)', value_comment_to_str(('abc', 'defg'), str_to_str, str_to_str))
92 self.assertEqual('abc ()', value_comment_to_str(('abc', ''), str_to_str, str_to_str))
93 self.assertEqual('(def)', value_comment_to_str(('', 'def'), str_to_str, str_to_str))
94 self.assertEqual('ab ((cd))', value_comment_to_str(('ab', '(cd)'), str_to_str, str_to_str))
95 self.assertEqual('ab ((c(d)[(])))', value_comment_to_str(('ab', '(c(d)[(]))'), str_to_str, str_to_str))
96 self.assertEqual('ab( (cd)', value_comment_to_str(('ab(', 'cd'), str_to_str, str_to_str))
97 self.assertEqual('abcd (ef)', value_comment_to_str(('abcd', 'ef'), str_to_str, str_to_str))
98 self.assertEqual('abc', value_comment_to_str(('abc', ''), str_to_str, str_to_str, comment_optional=True))
104 class TestStr(unittest.TestCase):
105 def test_from_str(self):
106 self.assertEqual('', str_from_str(''))
107 self.assertEqual('abc', str_from_str('abc'))
109 def test_to_str(self):
110 self.assertEqual('', str_to_str(''))
111 self.assertEqual('abc', str_to_str('abc'))
114 class TestReqStr(unittest.TestCase):
115 def test_from_str(self):
116 self.assertEqual('abc', req_str_from_str('abc'))
117 self.assertEqual(' ', req_str_from_str(' '))
118 with self.assertRaises(ValueError):
122 class TestOptStr(unittest.TestCase):
123 def test_from_str(self):
124 self.assertEqual('abc', opt_str_from_str('abc'))
125 self.assertEqual(' ', opt_str_from_str(' '))
126 self.assertEqual(None, opt_str_from_str(''))
128 def test_to_str(self):
129 self.assertEqual('abc', opt_str_to_str('abc'))
130 self.assertEqual(' ', opt_str_to_str(' '))
131 self.assertEqual('', opt_str_to_str(None))
134 # optional no or string converter
135 # -------------------------------
137 class TestOptNoOrStr(unittest.TestCase):
138 def test_from_str(self):
139 self.assertEqual((False, None), opt_no_or_str_from_str('Nein'))
140 self.assertEqual((True, 'Nur Wochenende'), opt_no_or_str_from_str('Nur Wochenende'))
141 self.assertEqual((True, 'Ja'), opt_no_or_str_from_str('Ja'))
142 self.assertEqual((None, None), opt_no_or_str_from_str(''))
144 def test_to_str(self):
145 self.assertEqual('Nein', opt_no_or_str_to_str((False, None)))
146 self.assertEqual('Nur Wochenende', opt_no_or_str_to_str((True, 'Nur Wochenende')))
147 self.assertEqual('Ja', opt_no_or_str_to_str((True, 'Ja')))
148 self.assertEqual('', opt_no_or_str_to_str((None, None)))
154 class TestInt(unittest.TestCase):
155 def test_from_str(self):
156 self.assertEqual(42, int_from_str('42'))
157 self.assertEqual(42, int_from_str('+42'))
158 self.assertEqual(-20, int_from_str('-20'))
159 self.assertEqual(0, int_from_str('0', min=0))
160 self.assertEqual(10, int_from_str('10', max=10))
161 with self.assertRaises(ValueError):
163 with self.assertRaises(ValueError):
165 with self.assertRaises(ValueError):
166 int_from_str('-1', min=0)
167 with self.assertRaises(ValueError):
168 int_from_str('11', max=10)
169 with self.assertRaises(ValueError):
171 with self.assertRaises(ValueError):
174 def test_to_str(self):
175 self.assertEqual('20', int_to_str(20))
176 self.assertEqual('-20', int_to_str(-20))
177 self.assertEqual('0', int_to_str(0))
180 class TestOptInt(unittest.TestCase):
181 def test_from_str(self):
182 self.assertEqual(42, opt_int_from_str('42'))
183 self.assertEqual(42, opt_int_from_str('+42'))
184 self.assertEqual(-20, opt_int_from_str('-20'))
185 self.assertEqual(None, opt_int_from_str(''))
186 with self.assertRaises(ValueError):
187 opt_int_from_str('abc')
188 with self.assertRaises(ValueError):
189 opt_int_from_str('10.0')
190 with self.assertRaises(ValueError):
191 opt_int_from_str('0d')
193 def test_to_str(self):
194 self.assertEqual('20', opt_int_to_str(20))
195 self.assertEqual('-20', opt_int_to_str(-20))
196 self.assertEqual('0', opt_int_to_str(0))
197 self.assertEqual('', opt_int_to_str(None))
200 class TestOptUInt(unittest.TestCase):
201 def test_from_str(self):
202 self.assertEqual(42, opt_uint_from_str('42'))
203 self.assertEqual(0, opt_uint_from_str('0'))
204 self.assertEqual(None, opt_uint_from_str(''))
205 with self.assertRaises(ValueError):
206 opt_uint_from_str('-1')
208 def test_to_str(self):
209 self.assertEqual('20', opt_uint_to_str(20))
210 self.assertEqual('0', opt_uint_to_str(0))
211 self.assertEqual('', opt_uint_to_str(None))
214 # TODO: bool converter
221 class TestTristateGerman(unittest.TestCase):
222 def test_from_str(self):
223 self.assertEqual(1.0, tristate_german_from_str('Ja'))
224 self.assertEqual(0.5, tristate_german_from_str('Teilweise'))
225 self.assertEqual(0, tristate_german_from_str('Nein'))
226 with self.assertRaises(ValueError):
227 tristate_german_from_str('')
228 with self.assertRaises(ValueError):
229 tristate_german_from_str('Vielleicht')
231 def test_to_str(self):
232 self.assertEqual('Ja', tristate_german_to_str(1.0))
233 self.assertEqual('Teilweise', tristate_german_to_str(0.5))
234 self.assertEqual('Nein', tristate_german_to_str(0.0))
237 class TestOptTristateGerman(unittest.TestCase):
238 def test_from_str(self):
239 self.assertEqual(1.0, opt_tristate_german_from_str('Ja'))
240 self.assertEqual(0.5, opt_tristate_german_from_str('Teilweise'))
241 self.assertEqual(0, opt_tristate_german_from_str('Nein'))
242 self.assertEqual(None, opt_tristate_german_from_str(''))
243 with self.assertRaises(ValueError):
244 opt_tristate_german_from_str('Vielleicht')
246 def test_to_str(self):
247 self.assertEqual('Ja', opt_tristate_german_to_str(1.0))
248 self.assertEqual('Teilweise', opt_tristate_german_to_str(0.5))
249 self.assertEqual('Nein', opt_tristate_german_to_str(0.0))
250 self.assertEqual('', opt_tristate_german_to_str(None))
253 # tristate with comment converter
254 # -------------------------------
256 class TestOptTristateGermanComment(unittest.TestCase):
257 def test_from_str(self):
258 self.assertEqual((1.0, None), opt_tristate_german_comment_from_str('Ja'))
259 self.assertEqual((0.5, None), opt_tristate_german_comment_from_str('Teilweise'))
260 self.assertEqual((0, None), opt_tristate_german_comment_from_str('Nein'))
261 self.assertEqual((0.5, 'nur ganz oben nicht'), opt_tristate_german_comment_from_str('Teilweise (nur ganz oben nicht)'))
262 self.assertEqual((None, None), opt_tristate_german_comment_from_str(''))
263 with self.assertRaises(ValueError):
264 opt_tristate_german_from_str('Vielleicht')
265 with self.assertRaises(ValueError):
266 opt_tristate_german_from_str('(Ja)')
268 def test_to_str(self):
269 self.assertEqual('Ja', opt_tristate_german_comment_to_str((1.0, None)))
270 self.assertEqual('Teilweise', opt_tristate_german_comment_to_str((0.5, None)))
271 self.assertEqual('Nein', opt_tristate_german_comment_to_str((0.0, None)))
272 self.assertEqual('', opt_tristate_german_comment_to_str((None, None)))
278 class TestUrl(unittest.TestCase):
279 def test_from_str(self):
280 self.assertEqual('http://www.winterrodeln.org/wiki/Arzler_Alm/', url_from_str('http://www.winterrodeln.org/wiki/Arzler_Alm/'))
281 self.assertEqual('http://www.winterrodeln.org/wiki/Nösslachhütte/', url_from_str('http://www.winterrodeln.org/wiki/Nösslachhütte/'))
282 self.assertEqual('https://www.winterrodeln.org/wiki/Nösslachhütte/', url_from_str('https://www.winterrodeln.org/wiki/Nösslachhütte/'))
283 with self.assertRaises(ValueError):
284 url_from_str('mailto:office@example.com')
285 with self.assertRaises(ValueError):
286 url_from_str('/wiki/Arzler_Alm/')
288 def test_to_str(self):
289 self.assertEqual('http://www.winterrodeln.org/wiki/Arzler_Alm/', url_to_str('http://www.winterrodeln.org/wiki/Arzler_Alm/'))
290 self.assertEqual('http://www.winterrodeln.org/wiki/Nösslachhütte/', url_to_str('http://www.winterrodeln.org/wiki/Nösslachhütte/'))
291 self.assertEqual('https://www.winterrodeln.org/wiki/Nösslachhütte/', url_to_str('https://www.winterrodeln.org/wiki/Nösslachhütte/'))
294 # webauskunft converter
295 # ---------------------
297 class TestWebauskunft(unittest.TestCase):
298 def test_from_str(self):
299 self.assertEqual((True, 'http://www.example.com/current'), webauskunft_from_str('http://www.example.com/current'))
300 self.assertEqual((None, None), webauskunft_from_str(''))
301 self.assertEqual((False, None), webauskunft_from_str('Nein'))
303 def test_to_str(self):
304 self.assertEqual('http://www.example.com/current', webauskunft_to_str((True, 'http://www.example.com/current')))
305 self.assertEqual('', webauskunft_to_str((None, None)))
306 self.assertEqual('Nein', webauskunft_to_str((False, None)))
312 class TestWikipage(unittest.TestCase):
313 def test_from_str(self):
314 self.assertEqual('[[Birgitzer Alm]]', wikipage_from_str('[[Birgitzer Alm]]'))
315 with self.assertRaises(ValueError):
316 wikipage_from_str('[[')
317 with self.assertRaises(ValueError):
318 wikipage_from_str('')
319 with self.assertRaises(ValueError):
320 wikipage_from_str('Birgitzer Alm')
322 def test_to_str(self):
323 self.assertEqual('[[Birgitzer Alm]]', wikipage_to_str('[[Birgitzer Alm]]'))
326 class TestOptWikipageEnum(unittest.TestCase):
327 def test_from_str(self):
328 self.assertEqual(['[[Birgitzer Alm]]', '[[Kemater Alm]]'], opt_wikipage_enum_from_str('[[Birgitzer Alm]]; [[Kemater Alm]]'))
329 self.assertEqual(['[[Birgitzer Alm]]'], opt_wikipage_enum_from_str('[[Birgitzer Alm]]'))
330 self.assertEqual([], opt_wikipage_enum_from_str('Nein'))
331 self.assertEqual(None, opt_wikipage_enum_from_str(''))
333 def test_to_str(self):
334 self.assertEqual('[[Birgitzer Alm]]; [[Kemater Alm]]', opt_wikipage_enum_to_str(['[[Birgitzer Alm]]', '[[Kemater Alm]]']))
335 self.assertEqual('[[Birgitzer Alm]]', opt_wikipage_enum_to_str(['[[Birgitzer Alm]]']))
336 self.assertEqual('Nein', opt_wikipage_enum_to_str([]))
337 self.assertEqual('', opt_wikipage_enum_to_str(None))
343 class TestEmail(unittest.TestCase):
345 self.good_addresses = ['office@example.com', 'winter+rodeln@localhost', 'joe.doe@exämple.com']
346 self.bad_addresses = ['öffice@example.com', 'winter rodeln@localhost', 'www.winterrodeln.org', 'mailto:info@example.com', 'info@example.com.']
348 def test_from_str(self):
349 for value in self.good_addresses:
350 self.assertEqual(value, email_from_str(value))
351 for value in self.bad_addresses:
352 with self.assertRaises(ValueError):
353 email_from_str(value)
355 def test_to_str(self):
356 for value in self.good_addresses:
357 self.assertEqual(value, email_to_str(value))
360 class TestMaskedEmail(unittest.TestCase):
361 def test_from_str(self):
362 self.assertEqual(('office@example.com', False), masked_email_from_str('office@example.com'))
363 self.assertEqual(('office@example.com', True), masked_email_from_str('office(at)example.com'))
364 with self.assertRaises(ValueError):
365 masked_email_from_str('office@example.com', masked_only=True)
366 with self.assertRaises(ValueError):
367 masked_email_from_str('off ice@example.com')
369 def test_to_str(self):
370 self.assertEqual('office@example.com', masked_email_to_str(('office@example.com', False)))
371 self.assertEqual('office(at)example.com', masked_email_to_str(('office@example.com', True)))
372 self.assertEqual('office()example.com', masked_email_to_str(('office@example.com', True), '()'))
375 class TestEmails(unittest.TestCase):
376 def test_from_str(self):
377 self.assertEqual(None, emails_from_str(''))
378 self.assertEqual([], emails_from_str('Nein'))
379 self.assertEqual([(('info@example.com', False), None)], emails_from_str('info@example.com'))
380 self.assertEqual([(('info@example.com', True), None)], emails_from_str('info(at)example.com'))
381 self.assertEqual([(('info@example.com', False), 'Office')], emails_from_str('info@example.com (Office)'))
382 self.assertEqual([(('info@example.com', False), None), (('home@example.com', False), 'Privat')], emails_from_str('info@example.com; home@example.com (Privat)'))
383 with self.assertRaises(ValueError):
384 emails_from_str('nein')
385 with self.assertRaises(ValueError):
386 emails_from_str('info@example.com; ho me@example.com (Privat)')
388 def test_to_str(self):
389 self.assertEqual('', emails_to_str(None))
390 self.assertEqual('Nein', emails_to_str([]))
391 self.assertEqual('info@example.com', emails_to_str([(('info@example.com', False), None)]))
392 self.assertEqual('info@example.com (Office)', emails_to_str([(('info@example.com', False), 'Office')]))
393 self.assertEqual('info@example.com; home@example.com (Privat)', emails_to_str([(('info@example.com', False), None), (('home@example.com', False), 'Privat')]))
399 class TestPhoneNumber(unittest.TestCase):
400 def test_from_str(self):
401 self.assertEqual('+43-699-123456789', phone_number_from_str('+43-699-123456789'))
402 self.assertEqual('+43-69945', phone_number_from_str('+43-69945'))
403 self.assertEqual('+43-512-507-6418', phone_number_from_str('+43-512-507-6418'))
404 with self.assertRaises(ValueError):
405 phone_number_from_str('+43-')
406 with self.assertRaises(ValueError):
407 phone_number_from_str('0512123456789')
409 def test_to_str(self):
410 self.assertEqual('+43-699-123456789', phone_number_to_str('+43-699-123456789'))
411 self.assertEqual('+43-69945', phone_number_to_str('+43-69945'))
412 self.assertEqual('+43-512-507-6418', phone_number_to_str('+43-512-507-6418'))
415 class TestOptPhoneCommentEnum(unittest.TestCase):
416 def test_from_str(self):
417 self.assertEqual(opt_phone_comment_enum_from_str(''), None)
418 self.assertEqual([], opt_phone_comment_enum_from_str('Nein'))
419 self.assertEqual([('+43-512-123456', 'untertags')], opt_phone_comment_enum_from_str('+43-512-123456 (untertags)'))
420 self.assertEqual([('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')], opt_phone_comment_enum_from_str('+43-512-1234 (untertags); +43-664-123456 (Alm)'))
421 self.assertEqual([('+43-512-1234', None), ('+43-664-123456', 'Sommer')], opt_phone_comment_enum_from_str('+43-512-1234; +43-664-123456 (Sommer)', True))
422 with self.assertRaises(ValueError):
423 opt_phone_comment_enum_from_str('+43-512-123456+ (untertags)')
424 with self.assertRaises(ValueError):
425 opt_phone_comment_enum_from_str('+43-512-123456')
427 def test_to_str(self):
428 self.assertEqual('', opt_phone_comment_enum_to_str(None))
429 self.assertEqual('Nein', opt_phone_comment_enum_to_str([]))
430 self.assertEqual('+43-512-123456 (untertags)', opt_phone_comment_enum_to_str([('+43-512-123456', 'untertags')]))
431 self.assertEqual('+43-512-1234 (untertags); +43-664-123456 (Alm)', opt_phone_comment_enum_to_str([('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')]))
432 self.assertEqual('+43-512-1234; +43-664-123456 (Sommer)', opt_phone_comment_enum_to_str([('+43-512-1234', None), ('+43-664-123456', 'Sommer')], True))
435 # longitude/latitude converter
436 # ----------------------------
438 class TestLonLat(unittest.TestCase):
439 def test_from_str(self):
440 self.assertEqual(LonLat(11.453553, 47.076207), lonlat_from_str('47.076207 N 11.453553 E'))
441 with self.assertRaises(ValueError):
442 lonlat_from_str('47.076207 N 11.453553')
444 def test_to_str(self):
445 self.assertEqual('47.076207 N 11.453553 E', lonlat_to_str(LonLat(11.453553, 47.076207)))
448 # difficulty converter
449 # --------------------
451 class TestDifficultyGerman(unittest.TestCase):
452 def test_from_str(self):
453 self.assertEqual(1, difficulty_german_from_str('leicht'))
454 self.assertEqual(2, difficulty_german_from_str('mittel'))
455 with self.assertRaises(ValueError):
456 difficulty_german_from_str('dontknow')
457 with self.assertRaises(ValueError):
458 difficulty_german_from_str('')
460 def test_to_str(self):
461 self.assertEqual('leicht', difficulty_german_to_str(1))
464 # TODO: avalanches converter
465 # --------------------
472 class TestLiftGermanValidator(unittest.TestCase):
473 def test_from_str(self):
474 self.assertEqual(lift_german_from_str(''), None)
475 self.assertEqual([], lift_german_from_str('Nein'))
476 self.assertEqual([('Sessellift', None)], lift_german_from_str('Sessellift'))
477 self.assertEqual([('Gondel', 'nur bis zur Hälfte')], lift_german_from_str('Gondel (nur bis zur Hälfte)'))
478 self.assertEqual([('Sessellift', None), ('Taxi', None)], lift_german_from_str('Sessellift; Taxi'))
479 self.assertEqual([('Sessellift', 'Wochenende'), ('Taxi', '6 Euro')], lift_german_from_str('Sessellift (Wochenende); Taxi (6 Euro)'))
481 def test_to_str(self):
482 self.assertEqual('', lift_german_to_str(None))
483 self.assertEqual('Nein', lift_german_to_str([]))
484 self.assertEqual('Sessellift', lift_german_to_str([('Sessellift', None)]))
485 self.assertEqual('Gondel (nur bis zur Hälfte)', lift_german_to_str([('Gondel', 'nur bis zur Hälfte')]))
486 self.assertEqual('Sessellift; Taxi', lift_german_to_str([('Sessellift', None), ('Taxi', None)]))
487 self.assertEqual('Sessellift (Wochenende); Taxi (6 Euro)', lift_german_to_str([('Sessellift', 'Wochenende'), ('Taxi', '6 Euro')]))
490 # TODO: public transport converter
491 # --------------------------
497 class TestSingleCachet(unittest.TestCase):
498 def test_from_str(self):
499 self.assertEqual(('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel'), single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'))
500 self.assertEqual(('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer'), single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer'))
501 with self.assertRaises(ValueError):
502 single_cachet_german_from_str('')
503 with self.assertRaises(ValueError):
504 single_cachet_german_from_str('Salzburger Naturrodelbahn-Gütesiegel 2013 schwer')
505 with self.assertRaises(ValueError):
506 single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 4013 schwer')
507 with self.assertRaises(ValueError):
508 single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 13 schwer')
509 with self.assertRaises(ValueError):
510 single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwerer')
512 def test_to_str(self):
513 self.assertEqual('Tiroler Naturrodelbahn-Gütesiegel 2009 mittel', single_cachet_german_to_str(('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')))
514 self.assertEqual('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer', single_cachet_german_to_str(('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer')))
517 class TestCachetGerman(unittest.TestCase):
518 def test_from_str(self):
519 self.assertEqual(cachet_german_from_str(''), None)
520 self.assertEqual([], cachet_german_from_str('Nein'))
521 self.assertEqual([('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')], cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'))
522 self.assertEqual([('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer'), ('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')],
523 cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer; Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'))
524 with self.assertRaises(ValueError):
525 cachet_german_from_str('Ja')
526 with self.assertRaises(ValueError):
527 cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
529 def test_to_str(self):
530 self.assertEqual('', cachet_german_to_str(None))
531 self.assertEqual('Nein', cachet_german_to_str([]))
532 self.assertEqual('Tiroler Naturrodelbahn-Gütesiegel 2009 mittel', cachet_german_to_str([('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')]))
533 self.assertEqual('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer; Tiroler Naturrodelbahn-Gütesiegel 2009 mittel', cachet_german_to_str([('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer'), ('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')]))
536 # night light days converter
537 # --------------------------
539 class TestNightLightDays(unittest.TestCase):
540 def test_from_str(self):
541 self.assertEqual((None, None), nightlightdays_from_str(''))
542 self.assertEqual((2, 'Mo, Di'), nightlightdays_from_str('2 (Mo, Di)'))
543 self.assertEqual((7, None), nightlightdays_from_str('7'))
544 self.assertEqual((0, None), nightlightdays_from_str('0'))
545 self.assertEqual((None, 'keine Ahnung'), nightlightdays_from_str('(keine Ahnung)'))
546 with self.assertRaises(ValueError):
547 nightlightdays_from_str('8')
548 with self.assertRaises(ValueError):
549 nightlightdays_from_str('5 (Montag')
550 with self.assertRaises(ValueError):
551 nightlightdays_from_str('5.2')
553 def test_to_str(self):
554 self.assertEqual('', nightlightdays_to_str((None, None)))
555 self.assertEqual('2 (Mo, Di)', nightlightdays_to_str((2, 'Mo, Di')))
556 self.assertEqual('7', nightlightdays_to_str((7, None)))
557 self.assertEqual('0', nightlightdays_to_str((0, None)))
560 # string with optional comment enum/list converter
561 # ------------------------------------------------
563 class TestOptStrOptCommentEnum(unittest.TestCase):
564 def test_from_str(self):
565 self.assertEqual(None, opt_str_opt_comment_enum_from_str(''))
566 self.assertEqual([], opt_str_opt_comment_enum_from_str('Nein'))
567 self.assertEqual([('Talstation', None)], opt_str_opt_comment_enum_from_str('Talstation'))
568 self.assertEqual([('Talstation', 'unten')], opt_str_opt_comment_enum_from_str('Talstation (unten)'))
569 self.assertEqual([('Talstation', 'unten'), ('Mittelstation', None)], opt_str_opt_comment_enum_from_str('Talstation (unten); Mittelstation'))
570 with self.assertRaises(ValueError):
571 opt_str_opt_comment_enum_from_str('(unten)')
572 with self.assertRaises(ValueError):
573 opt_str_opt_comment_enum_from_str('Talstation (unten); ; Mittelstation')
575 def test_to_str(self):
576 self.assertEqual('', opt_str_opt_comment_enum_to_str(None))
577 self.assertEqual('Nein', opt_str_opt_comment_enum_to_str([]))
578 self.assertEqual('Talstation', opt_str_opt_comment_enum_to_str([('Talstation', None)]))
579 self.assertEqual('Talstation (unten)', opt_str_opt_comment_enum_to_str([('Talstation', 'unten')]))
580 self.assertEqual('Talstation (unten); Mittelstation', opt_str_opt_comment_enum_to_str([('Talstation', 'unten'), ('Mittelstation', None)]))
586 class TestWikibox(unittest.TestCase):
587 def test_from_str(self):
588 value = '{{MyTemplate|apple=2|banana=5}}'
589 converter_dict = OrderedDict([('apple', opt_uint_converter), ('banana', opt_uint_converter)])
590 result = wikibox_from_str(value, 'MyTemplate', converter_dict)
591 self.assertEqual(2, result['apple'])
592 self.assertEqual(5, result['banana'])
594 value = '{{MyTemplate\n | apple = 2 \n| banana = 5 }}'
595 result = wikibox_from_str(value, 'MyTemplate', converter_dict)
596 self.assertEqual(2, result['apple'])
597 self.assertEqual(5, result['banana'])
599 with self.assertRaises(ValueError):
600 wikibox_from_str(value, 'myTemplate', converter_dict)
601 with self.assertRaises(ValueError):
602 value = '{{MyTemplate|apple=2|banana=five}}'
603 wikibox_from_str(value, 'MyTemplate', converter_dict)
604 with self.assertRaises(ValueError):
605 value = '{{MyTemplate|apple=2}}'
606 wikibox_from_str(value, 'MyTemplate', converter_dict)
607 with self.assertRaises(ValueError):
608 value = '{{MyTemplate|apple=2|banana=5|cherry=6}}'
609 wikibox_from_str(value, 'MyTemplate', converter_dict)
611 def test_to_str(self):
612 value = OrderedDict([('apple', 2), ('banana', 5)])
613 converter_dict = OrderedDict([('apple', opt_uint_converter), ('banana', opt_uint_converter)])
614 result = wikibox_to_str(value, 'MyTemplate', converter_dict)
615 self.assertEqual('{{MyTemplate|apple=2|banana=5}}', result)
618 # Rodelbahnbox converter
619 # ----------------------
621 class TestRodelbahnbox(unittest.TestCase):
626 | Position = 46.807218 N 12.806522 E
627 | Position oben = 46.799014 N 12.818658 E
632 | Schwierigkeit = mittel
634 | Betreiber = Bringungsgemeinschaft Kreithof-Dolomitenhütte
635 | Öffentliche Anreise = Schlecht
636 | Aufstieg möglich = Ja
637 | Aufstieg getrennt = Teilweise
639 | Aufstiegshilfe = Taxi; Sonstige (PKW bis Kreithof)
640 | Beleuchtungsanlage = Ja
641 | Beleuchtungstage = 7
642 | Rodelverleih = Nein
643 | Gütesiegel = Tiroler Naturrodelbahn-Gütesiegel 2009 mittel
644 | Webauskunft = http://www.lienzerdolomiten.info/at/tobogorpt.html
645 | Telefonauskunft = +43-664-2253782 (Dolomitenhütte)
646 | Bild = Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg
647 | In Übersichtskarte = Ja
651 def test_from_str(self):
652 value = rodelbahnbox_from_str(self.value)
653 self.assertEqual(LonLat(12.806522, 46.807218), value['Position'])
654 self.assertEqual(LonLat(12.818658, 46.799014), value['Position oben'])
655 self.assertEqual(1046, value['Höhe oben'])
656 self.assertEqual(LonLat(None, None), value['Position unten'])
657 self.assertEqual(None, value['Höhe unten'])
658 self.assertEqual(3500, value['Länge'])
659 self.assertEqual(2, value['Schwierigkeit'])
660 self.assertEqual(1, value['Lawinen'])
661 self.assertEqual('Bringungsgemeinschaft Kreithof-Dolomitenhütte', value['Betreiber'])
662 self.assertEqual(4, value['Öffentliche Anreise'])
663 self.assertEqual(True, value['Aufstieg möglich'])
664 self.assertEqual((0.5, None), value['Aufstieg getrennt'])
665 self.assertEqual(75, value['Gehzeit'])
666 self.assertEqual([('Taxi', None), ('Sonstige', 'PKW bis Kreithof')], value['Aufstiegshilfe'])
667 self.assertEqual((1.0, None), value['Beleuchtungsanlage'])
668 self.assertEqual((7, None), value['Beleuchtungstage'])
669 self.assertEqual([], value['Rodelverleih'])
670 self.assertEqual([('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')], value['Gütesiegel'])
671 self.assertEqual((True, 'http://www.lienzerdolomiten.info/at/tobogorpt.html'), value['Webauskunft'])
672 self.assertEqual([('+43-664-2253782', 'Dolomitenhütte')], value['Telefonauskunft'])
673 self.assertEqual('Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg', value['Bild'])
674 self.assertEqual(True, value['In Übersichtskarte'])
675 self.assertEqual(139, value['Forumid'])
677 def test_to_str(self):
678 value = OrderedDict([
679 ('Position', LonLat(12.806522, 46.807218)),
680 ('Position oben', LonLat(12.818658, 46.799014)),
682 ('Position unten', LonLat(None, None)),
683 ('Höhe unten', None),
685 ('Schwierigkeit', 2),
687 ('Betreiber', 'Bringungsgemeinschaft Kreithof-Dolomitenhütte'),
688 ('Öffentliche Anreise', 4),
689 ('Aufstieg möglich', True),
690 ('Aufstieg getrennt', (0.5, None)),
692 ('Aufstiegshilfe', [('Taxi', None), ('Sonstige', 'PKW bis Kreithof')]),
693 ('Beleuchtungsanlage', (1.0, None)),
694 ('Beleuchtungstage', (7, None)),
695 ('Rodelverleih', []),
696 ('Gütesiegel', [('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')]),
697 ('Webauskunft', (True, 'http://www.lienzerdolomiten.info/at/tobogorpt.html')),
698 ('Telefonauskunft', [('+43-664-2253782', 'Dolomitenhütte')]),
699 ('Bild', 'Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg'),
700 ('In Übersichtskarte', True),
702 self.assertEqual(self.value, rodelbahnbox_to_str(value))
705 # TODO: Gasthausbox converter
706 # ---------------------
708 class TestGasthausbox(unittest.TestCase):
713 | Position = 47.123456 N 11.123456 E
715 | Betreiber = Max Mustermann
717 | Übernachtung = 20 Matrazenlager, 3 Doppelzimmer
719 | Rodelverleih = 2 Euro (Ausweis erforderlich)
720 | Handyempfang = A1; T-Mobile A
721 | Homepage = http://www.birgitzeralm.at/
723 | Telefon = +43-664-5487520; +43-512-123456 (wenn geschlossen)
724 | Bild = Gasthaus_Birgitzer_Alm_03.jpg
725 | Rodelbahnen = [[Birgitzer Alm (vom Adelshof)]]; [[Birgitzer Alm (von Birgitz)]]
728 def test_from_str(self):
729 value = gasthausbox_from_str(self.value)
730 self.assertEqual(LonLat(11.123456, 47.123456), value['Position'])
731 self.assertEqual(1808, value['Höhe'])
732 self.assertEqual('Max Mustermann', value['Betreiber'])
733 self.assertEqual(50, value['Sitzplätze'])
734 self.assertEqual((True, '20 Matrazenlager, 3 Doppelzimmer'), value['Übernachtung'])
735 self.assertEqual((True, '2 Euro (Ausweis erforderlich)'), value['Rodelverleih'])
736 self.assertEqual(1.0, value['Rauchfrei'])
737 self.assertEqual([('A1', None), ('T-Mobile A', None)], value['Handyempfang'])
738 self.assertEqual((True, 'http://www.birgitzeralm.at/'), value['Homepage'])
739 self.assertEqual([], value['E-Mail'])
740 self.assertEqual([('+43-664-5487520', None), ('+43-512-123456', 'wenn geschlossen')], value['Telefon'])
741 self.assertEqual('Gasthaus_Birgitzer_Alm_03.jpg', value['Bild'])
742 self.assertEqual(['[[Birgitzer Alm (vom Adelshof)]]', '[[Birgitzer Alm (von Birgitz)]]'], value['Rodelbahnen'])
744 def test_to_str(self):
745 value = OrderedDict([
746 ('Position', LonLat(11.123456, 47.123456)),
748 ('Betreiber', 'Max Mustermann'),
750 ('Übernachtung', (True, '20 Matrazenlager, 3 Doppelzimmer')),
751 ('Rodelverleih', (True, '2 Euro (Ausweis erforderlich)')),
753 ('Handyempfang', [('A1', None), ('T-Mobile A', None)]),
754 ('Homepage', (True, 'http://www.birgitzeralm.at/')),
756 ('Telefon', [('+43-664-5487520', None), ('+43-512-123456', 'wenn geschlossen')]),
757 ('Bild', 'Gasthaus_Birgitzer_Alm_03.jpg'),
758 ('Rodelbahnen', ['[[Birgitzer Alm (vom Adelshof)]]', '[[Birgitzer Alm (von Birgitz)]]'])])
759 self.assertEqual(self.value, gasthausbox_to_str(value))