2 # -*- coding: iso-8859-15 -*-
4 import wrpylib.wrvalidators
7 from wrpylib.wrvalidators import *
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):
19 with self.assertRaises(ValueError):
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):
27 with self.assertRaises(ValueError):
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')
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')
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), '')
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'])
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([]), '')
70 class TestDifficultyGerman(unittest.TestCase):
71 def test_from_str(self):
72 self.assertEqual(difficulty_german_from_str('leicht'), 1)
73 self.assertEqual(difficulty_german_from_str('mittel'), 2)
74 with self.assertRaises(ValueError):
75 difficulty_german_from_str('dontknow')
76 with self.assertRaises(ValueError):
77 difficulty_german_from_str('')
79 def test_to_str(self):
80 self.assertEqual(difficulty_german_to_str(1), 'leicht')
83 class TestTristateGerman(unittest.TestCase):
84 def test_from_str(self):
85 self.assertEqual(tristate_german_from_str('Ja'), 1.0)
86 self.assertEqual(tristate_german_from_str('Teilweise'), 0.5)
87 self.assertEqual(tristate_german_from_str('Nein'), 0)
88 with self.assertRaises(ValueError):
89 tristate_german_from_str('')
90 with self.assertRaises(ValueError):
91 tristate_german_from_str('Vielleicht')
93 def test_to_str(self):
94 self.assertEqual(tristate_german_to_str(1.0), 'Ja')
95 self.assertEqual(tristate_german_to_str(0.5), 'Teilweise')
96 self.assertEqual(tristate_german_to_str(0.0), 'Nein')
99 class TestOptTristateGerman(unittest.TestCase):
100 def test_from_str(self):
101 self.assertEqual(opt_tristate_german_from_str('Ja'), 1.0)
102 self.assertEqual(opt_tristate_german_from_str('Teilweise'), 0.5)
103 self.assertEqual(opt_tristate_german_from_str('Nein'), 0)
104 self.assertEqual(opt_tristate_german_from_str(''), None)
105 with self.assertRaises(ValueError):
106 opt_tristate_german_from_str('Vielleicht')
108 def test_to_str(self):
109 self.assertEqual(opt_tristate_german_to_str(1.0), 'Ja')
110 self.assertEqual(opt_tristate_german_to_str(0.5), 'Teilweise')
111 self.assertEqual(opt_tristate_german_to_str(0.0), 'Nein')
112 self.assertEqual(opt_tristate_german_to_str(None), '')
115 class TestOptTristateGermanComment(unittest.TestCase):
116 def test_from_str(self):
117 self.assertEqual(opt_tristate_german_comment_from_str('Ja'), (1.0, None))
118 self.assertEqual(opt_tristate_german_comment_from_str('Teilweise'), (0.5, None))
119 self.assertEqual(opt_tristate_german_comment_from_str('Nein'), (0, None))
120 self.assertEqual(opt_tristate_german_comment_from_str('Teilweise (nur ganz oben nicht)'), (0.5, 'nur ganz oben nicht'))
121 self.assertEqual(opt_tristate_german_comment_from_str(''), (None, None))
122 with self.assertRaises(ValueError):
123 opt_tristate_german_from_str('Vielleicht')
124 with self.assertRaises(ValueError):
125 opt_tristate_german_from_str('(Ja)')
127 def test_to_str(self):
128 self.assertEqual(opt_tristate_german_comment_to_str((1.0, None)), 'Ja')
129 self.assertEqual(opt_tristate_german_comment_to_str((0.5, None)), 'Teilweise')
130 self.assertEqual(opt_tristate_german_comment_to_str((0.0, None)), 'Nein')
131 self.assertEqual(opt_tristate_german_comment_to_str((None, None)), '')
134 class TestLonLat(unittest.TestCase):
135 def test_from_str(self):
136 self.assertEqual(lonlat_from_str('47.076207 N 11.453553 E'), LonLat(11.453553, 47.076207))
137 with self.assertRaises(ValueError):
138 lonlat_from_str('47.076207 N 11.453553')
140 def test_to_str(self):
141 self.assertEqual(lonlat_to_str(LonLat(11.453553, 47.076207)), '47.076207 N 11.453553 E')
144 class TestValueCommentConverter(unittest.TestCase):
145 def test_from_str(self):
146 self.assertEqual(value_comment_from_str('abc (defg)'), ('abc', 'defg'))
147 self.assertEqual(value_comment_from_str('abc ()'), ('abc', ''))
148 self.assertEqual(value_comment_from_str('(def)'), ('', 'def'))
149 self.assertEqual(value_comment_from_str('ab((cd))'), ('ab', '(cd)'))
150 self.assertEqual(value_comment_from_str('ab((c(d)[(])))'), ('ab', '(c(d)[(]))'))
151 self.assertEqual(value_comment_from_str('ab((cd)'), ('ab(', 'cd'))
152 self.assertEqual(value_comment_from_str('abcd (ef) '), ('abcd', 'ef'))
153 self.assertEqual(value_comment_from_str('abc', comment_optional=True), ('abc', ''))
154 with self.assertRaises(ValueError):
155 value_comment_from_str('abc (')
156 with self.assertRaises(ValueError):
157 value_comment_from_str('abc )')
158 with self.assertRaises(ValueError):
159 value_comment_from_str('abc (def)g')
160 with self.assertRaises(ValueError):
161 value_comment_from_str('abc (b))')
162 with self.assertRaises(ValueError):
163 value_comment_from_str('abc')
165 def test_to_str(self):
166 self.assertEqual(value_comment_to_str(('abc', 'defg')), 'abc (defg)')
167 self.assertEqual(value_comment_to_str(('abc', '')), 'abc ()')
168 self.assertEqual(value_comment_to_str(('', 'def')), '(def)')
169 self.assertEqual(value_comment_to_str(('ab', '(cd)')), 'ab ((cd))')
170 self.assertEqual(value_comment_to_str(('ab', '(c(d)[(]))')), 'ab ((c(d)[(])))')
171 self.assertEqual(value_comment_to_str(('ab(', 'cd')), 'ab( (cd)')
172 self.assertEqual(value_comment_to_str(('abcd', 'ef')), 'abcd (ef)')
173 self.assertEqual(value_comment_to_str(('abc', ''), comment_optional=True), 'abc')
176 class TestNoGermanConverter(unittest.TestCase):
177 def test_from_str(self):
178 self.assertEqual(no_german_from_str('abc'), (True, 'abc'))
179 self.assertEqual(no_german_from_str('Nein'), (False, None))
180 with self.assertRaises(ValueError):
181 no_german_from_str('')
183 def test_to_str(self):
184 self.assertEqual(no_german_to_str((True, 'abc')), 'abc')
185 self.assertEqual(no_german_to_str((False, None)), 'Nein')
188 class TestOptNoGerman(unittest.TestCase):
189 def test_from_str(self):
190 self.assertEqual(opt_no_german_from_str('abc'), (True, 'abc'))
191 self.assertEqual(opt_no_german_from_str('Nein'), (False, None))
192 self.assertEqual(opt_no_german_from_str(''), (None, None))
194 def test_to_str(self):
195 self.assertEqual(opt_no_german_to_str((True, 'abc')), 'abc')
196 self.assertEqual(opt_no_german_to_str((False, None)), 'Nein')
197 self.assertEqual(opt_no_german_to_str((None, None)), '')
200 class TestLiftGermanValidator(unittest.TestCase):
201 def test_from_str(self):
202 self.assertEqual(lift_german_from_str(''), None)
203 self.assertEqual(lift_german_from_str('Nein'), [])
204 self.assertEqual(lift_german_from_str('Sessellift'), [('Sessellift', None)])
205 self.assertEqual(lift_german_from_str('Gondel (nur bis zur Hälfte)'), [('Gondel', 'nur bis zur Hälfte')])
206 self.assertEqual(lift_german_from_str('Sessellift; Taxi'), [('Sessellift', None), ('Taxi', None)])
207 self.assertEqual(lift_german_from_str('Sessellift (Wochenende); Taxi (6 Euro)'), [('Sessellift', 'Wochenende'), ('Taxi', '6 Euro')])
209 def test_to_str(self):
210 self.assertEqual(lift_german_to_str(None), '')
211 self.assertEqual(lift_german_to_str([]), 'Nein')
212 self.assertEqual(lift_german_to_str([('Sessellift', None)]), 'Sessellift')
213 self.assertEqual(lift_german_to_str([('Gondel', 'nur bis zur Hälfte')]), 'Gondel (nur bis zur Hälfte)')
214 self.assertEqual(lift_german_to_str([('Sessellift', None), ('Taxi', None)]), 'Sessellift; Taxi')
215 self.assertEqual(lift_german_to_str([('Sessellift', 'Wochenende'), ('Taxi', '6 Euro')]), 'Sessellift (Wochenende); Taxi (6 Euro)')
218 class TestNightLightDays(unittest.TestCase):
219 def test_from_str(self):
220 self.assertEqual(nightlightdays_from_str(''), (None, None))
221 self.assertEqual(nightlightdays_from_str('2 (Mo, Di)'), (2, 'Mo, Di'))
222 self.assertEqual(nightlightdays_from_str('7'), (7, None))
223 self.assertEqual(nightlightdays_from_str('0'), (0, None))
224 self.assertEqual(nightlightdays_from_str('(keine Ahnung)'), (None, 'keine Ahnung'))
225 with self.assertRaises(ValueError):
226 nightlightdays_from_str('8')
227 with self.assertRaises(ValueError):
228 nightlightdays_from_str('5 (Montag')
229 with self.assertRaises(ValueError):
230 nightlightdays_from_str('5.2')
232 def test_to_str(self):
233 self.assertEqual(nightlightdays_to_str((None, None)), '')
234 self.assertEqual(nightlightdays_to_str((2, 'Mo, Di')), '2 (Mo, Di)')
235 self.assertEqual(nightlightdays_to_str((7, None)), '7')
236 self.assertEqual(nightlightdays_to_str((0, None)), '0')
239 class TestOptStrOptCommentEnum(unittest.TestCase):
240 def test_from_str(self):
241 self.assertEqual(opt_str_opt_comment_enum_from_str(''), None)
242 self.assertEqual(opt_str_opt_comment_enum_from_str('Nein'), [])
243 self.assertEqual(opt_str_opt_comment_enum_from_str('Talstation'), [('Talstation', None)])
244 self.assertEqual(opt_str_opt_comment_enum_from_str('Talstation (unten)'), [('Talstation', 'unten')])
245 self.assertEqual(opt_str_opt_comment_enum_from_str('Talstation (unten); Mittelstation'), [('Talstation', 'unten'), ('Mittelstation', None)])
246 with self.assertRaises(ValueError):
247 opt_str_opt_comment_enum_from_str('(unten)')
248 with self.assertRaises(ValueError):
249 opt_str_opt_comment_enum_from_str('Talstation (unten); ; Mittelstation')
251 def test_to_str(self):
252 self.assertEqual(sledrental_to_str(None), '')
253 self.assertEqual(sledrental_to_str([]), 'Nein')
254 self.assertEqual(sledrental_to_str([('Talstation', None)]), 'Talstation')
255 self.assertEqual(sledrental_to_str([('Talstation', 'unten')]), 'Talstation (unten)')
256 self.assertEqual(sledrental_to_str([('Talstation', 'unten'), ('Mittelstation', None)]), 'Talstation (unten); Mittelstation')
259 class TestNoOrStr(unittest.TestCase):
260 def test_from_str(self):
261 self.assertEqual((False, None), opt_no_or_str_from_str('Nein'))
262 self.assertEqual((True, 'Nur Wochenende'), opt_no_or_str_from_str('Nur Wochenende'))
263 self.assertEqual((True, 'Ja'), opt_no_or_str_from_str('Ja'))
264 self.assertEqual((None, None), opt_no_or_str_from_str(''))
266 def test_to_str(self):
267 self.assertEqual('Nein', opt_no_or_str_to_str((False, None)))
268 self.assertEqual('Nur Wochenende', opt_no_or_str_to_str((True, 'Nur Wochenende')))
269 self.assertEqual('Ja', opt_no_or_str_to_str((True, 'Ja')))
270 self.assertEqual('', opt_no_or_str_to_str((None, None)))
274 class TestSingleCachet(unittest.TestCase):
275 def test_from_str(self):
276 self.assertEqual(single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'), ('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel'))
277 self.assertEqual(single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer'), ('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer'))
278 with self.assertRaises(ValueError):
279 single_cachet_german_from_str('')
280 with self.assertRaises(ValueError):
281 single_cachet_german_from_str('Salzburger Naturrodelbahn-Gütesiegel 2013 schwer')
282 with self.assertRaises(ValueError):
283 single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 4013 schwer')
284 with self.assertRaises(ValueError):
285 single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 13 schwer')
286 with self.assertRaises(ValueError):
287 single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwerer')
289 def test_to_str(self):
290 self.assertEqual(single_cachet_german_to_str(('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')), 'Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
291 self.assertEqual(single_cachet_german_to_str(('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer')), 'Tiroler Naturrodelbahn-Gütesiegel 2013 schwer')
294 class TestCachetGerman(unittest.TestCase):
295 def test_from_str(self):
296 self.assertEqual(cachet_german_from_str(''), None)
297 self.assertEqual(cachet_german_from_str('Nein'), [])
298 self.assertEqual(cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'), [('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')])
299 self.assertEqual(cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer; Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'),
300 [('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer'), ('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')])
301 with self.assertRaises(ValueError):
302 cachet_german_from_str('Ja')
303 with self.assertRaises(ValueError):
304 cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
306 def test_to_str(self):
307 self.assertEqual(cachet_german_to_str(None), '')
308 self.assertEqual(cachet_german_to_str([]), 'Nein')
309 self.assertEqual(cachet_german_to_str([('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')]), 'Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
310 self.assertEqual(cachet_german_to_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')
314 class TestUrl(unittest.TestCase):
315 def test_from_str(self):
316 self.assertEqual(url_from_str('http://www.winterrodeln.org/wiki/Arzler_Alm/'), 'http://www.winterrodeln.org/wiki/Arzler_Alm/')
317 self.assertEqual(url_from_str('http://www.winterrodeln.org/wiki/Nösslachhütte/'), 'http://www.winterrodeln.org/wiki/Nösslachhütte/')
318 self.assertEqual(url_from_str('https://www.winterrodeln.org/wiki/Nösslachhütte/'), 'https://www.winterrodeln.org/wiki/Nösslachhütte/')
319 with self.assertRaises(ValueError):
320 url_from_str('mailto:office@example.com')
321 with self.assertRaises(ValueError):
322 url_from_str('/wiki/Arzler_Alm/')
324 def test_to_str(self):
325 self.assertEqual(url_to_str('http://www.winterrodeln.org/wiki/Arzler_Alm/'), 'http://www.winterrodeln.org/wiki/Arzler_Alm/')
326 self.assertEqual(url_to_str('http://www.winterrodeln.org/wiki/Nösslachhütte/'), 'http://www.winterrodeln.org/wiki/Nösslachhütte/')
327 self.assertEqual(url_to_str('https://www.winterrodeln.org/wiki/Nösslachhütte/'), 'https://www.winterrodeln.org/wiki/Nösslachhütte/')
330 class TestWebauskunft(unittest.TestCase):
331 def test_from_str(self):
332 self.assertEqual(webauskunft_from_str('http://www.example.com/current'), (True, 'http://www.example.com/current'))
333 self.assertEqual(webauskunft_from_str(''), (None, None))
334 self.assertEqual(webauskunft_from_str('Nein'), (False, None))
336 def test_to_str(self):
337 self.assertEqual(webauskunft_to_str((True, 'http://www.example.com/current')), 'http://www.example.com/current')
338 self.assertEqual(webauskunft_to_str((None, None)), '')
339 self.assertEqual(webauskunft_to_str((False, None)), 'Nein')
342 class TestPhoneNumber(unittest.TestCase):
343 def test_from_str(self):
344 self.assertEqual(phone_number_from_str('+43-699-123456789'), '+43-699-123456789')
345 self.assertEqual(phone_number_from_str('+43-69945'), '+43-69945')
346 self.assertEqual(phone_number_from_str('+43-512-507-6418'), '+43-512-507-6418')
347 with self.assertRaises(ValueError):
348 phone_number_from_str('+43-')
349 with self.assertRaises(ValueError):
350 phone_number_from_str('0512123456789')
352 def test_to_str(self):
353 self.assertEqual(phone_number_to_str('+43-699-123456789'), '+43-699-123456789')
354 self.assertEqual(phone_number_to_str('+43-69945'), '+43-69945')
355 self.assertEqual(phone_number_to_str('+43-512-507-6418'), '+43-512-507-6418')
358 class TestTelefonauskunft(unittest.TestCase):
359 def test_from_str(self):
360 self.assertEqual(telefonauskunft_from_str(''), None)
361 self.assertEqual(telefonauskunft_from_str('Nein'), [])
362 self.assertEqual(telefonauskunft_from_str('+43-512-123456 (untertags)'), [('+43-512-123456', 'untertags')])
363 self.assertEqual(telefonauskunft_from_str('+43-512-1234 (untertags); +43-664-123456 (Alm)'), [('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')])
364 with self.assertRaises(ValueError):
365 telefonauskunft_from_str('+43-512-123456+ (untertags)')
366 with self.assertRaises(ValueError):
367 telefonauskunft_from_str('+43-512-123456')
369 def test_to_str(self):
370 self.assertEqual(telefonauskunft_to_str(None), '')
371 self.assertEqual(telefonauskunft_to_str([]), 'Nein')
372 self.assertEqual(telefonauskunft_to_str([('+43-512-123456', 'untertags')]), '+43-512-123456 (untertags)')
373 self.assertEqual(telefonauskunft_to_str([('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')]), '+43-512-1234 (untertags); +43-664-123456 (Alm)')
376 class TestEmail(unittest.TestCase):
378 self.good_addresses = ['office@example.com', 'winter+rodeln@localhost', 'joe.doe@exämple.com']
379 self.bad_addresses = ['öffice@example.com', 'winter rodeln@localhost', 'www.winterrodeln.org', 'mailto:info@example.com', 'info@example.com.']
381 def test_from_str(self):
382 for value in self.good_addresses:
383 self.assertEqual(value, email_from_str(value))
384 for value in self.bad_addresses:
385 with self.assertRaises(ValueError):
386 email_from_str(value)
388 def test_to_str(self):
389 for value in self.good_addresses:
390 self.assertEqual(value, email_to_str(value))
393 class TestMaskedEmail(unittest.TestCase):
394 def test_from_str(self):
395 self.assertEqual(('office@example.com', False), masked_email_from_str('office@example.com'))
396 self.assertEqual(('office@example.com', True), masked_email_from_str('office(at)example.com'))
397 with self.assertRaises(ValueError):
398 masked_email_from_str('office@example.com', masked_only=True)
399 with self.assertRaises(ValueError):
400 masked_email_from_str('off ice@example.com')
402 def test_to_str(self):
403 self.assertEqual('office@example.com', masked_email_to_str(('office@example.com', False)))
404 self.assertEqual('office(at)example.com', masked_email_to_str(('office@example.com', True)))
405 self.assertEqual('office()example.com', masked_email_to_str(('office@example.com', True), '()'))
408 class TestEmails(unittest.TestCase):
409 def test_from_str(self):
410 self.assertEqual(None, emails_from_str(''))
411 self.assertEqual([], emails_from_str('Nein'))
412 self.assertEqual([(('info@example.com', False), None)], emails_from_str('info@example.com'))
413 # self.assertEqual([(('info@example.com', True), None)], emails_from_str('info(at)example.com'))
414 self.assertEqual([(('info@example.com', False), 'Office')], emails_from_str('info@example.com (Office)'))
415 self.assertEqual([(('info@example.com', False), None), (('home@example.com', False), 'Privat')], emails_from_str('info@example.com; home@example.com (Privat)'))
416 with self.assertRaises(ValueError):
417 emails_from_str('nein')
418 with self.assertRaises(ValueError):
419 emails_from_str('info@example.com; ho me@example.com (Privat)')
421 def test_to_str(self):
422 self.assertEqual('', emails_to_str(None))
423 self.assertEqual('Nein', emails_to_str([]))
424 self.assertEqual('info@example.com', emails_to_str([(('info@example.com', False), None)]))
425 self.assertEqual('info@example.com (Office)', emails_to_str([(('info@example.com', False), 'Office')]))
426 self.assertEqual('info@example.com; home@example.com (Privat)', emails_to_str([(('info@example.com', False), None), (('home@example.com', False), 'Privat')]))
429 class TestWikipage(unittest.TestCase):
430 def test_from_str(self):
431 self.assertEqual('[[Birgitzer Alm]]', wikipage_from_str('[[Birgitzer Alm]]'))
432 with self.assertRaises(ValueError):
433 wikipage_from_str('[[')
434 with self.assertRaises(ValueError):
435 wikipage_from_str('')
436 with self.assertRaises(ValueError):
437 wikipage_from_str('Birgitzer Alm')
439 def test_to_str(self):
440 self.assertEqual('[[Birgitzer Alm]]', wikipage_to_str('[[Birgitzer Alm]]'))
443 class TestOptWikipageEnum(unittest.TestCase):
444 def test_from_str(self):
445 self.assertEqual(['[[Birgitzer Alm]]', '[[Kemater Alm]]'], opt_wikipage_enum_from_str('[[Birgitzer Alm]]; [[Kemater Alm]]'))
446 self.assertEqual(['[[Birgitzer Alm]]'], opt_wikipage_enum_from_str('[[Birgitzer Alm]]'))
447 self.assertEqual([], opt_wikipage_enum_from_str('Nein'))
448 self.assertEqual(None, opt_wikipage_enum_from_str(''))
450 def test_to_str(self):
451 self.assertEqual('[[Birgitzer Alm]]; [[Kemater Alm]]', opt_wikipage_enum_to_str(['[[Birgitzer Alm]]', '[[Kemater Alm]]']))
452 self.assertEqual('[[Birgitzer Alm]]', opt_wikipage_enum_to_str(['[[Birgitzer Alm]]']))
453 self.assertEqual('Nein', opt_wikipage_enum_to_str([]))
454 self.assertEqual('', opt_wikipage_enum_to_str(None))
457 class TestBox(unittest.TestCase):
458 def test_from_str(self):
459 value = '{{MyTemplate|apple=2|banana=5}}'
460 converter_dict = OrderedDict([('apple', opt_uint_converter), ('banana', opt_uint_converter)])
461 result = box_from_str(value, 'MyTemplate', converter_dict)
462 self.assertEqual(result['apple'], 2)
463 self.assertEqual(result['banana'], 5)
465 value = '{{MyTemplate\n | apple = 2 \n| banana = 5 }}'
466 result = box_from_str(value, 'MyTemplate', converter_dict)
467 self.assertEqual(result['apple'], 2)
468 self.assertEqual(result['banana'], 5)
470 with self.assertRaises(ValueError):
471 box_from_str(value, 'myTemplate', converter_dict)
472 with self.assertRaises(ValueError):
473 value = '{{MyTemplate|apple=2|banana=five}}'
474 box_from_str(value, 'MyTemplate', converter_dict)
475 with self.assertRaises(ValueError):
476 value = '{{MyTemplate|apple=2}}'
477 box_from_str(value, 'MyTemplate', converter_dict)
478 with self.assertRaises(ValueError):
479 value = '{{MyTemplate|apple=2|banana=5|cherry=6}}'
480 box_from_str(value, 'MyTemplate', converter_dict)
482 def test_to_str(self):
483 value = OrderedDict([('apple', 2), ('banana', 5)])
484 converter_dict = OrderedDict([('apple', opt_uint_converter), ('banana', opt_uint_converter)])
485 result = box_to_str(value, 'MyTemplate', converter_dict)
486 self.assertEqual(result, '{{MyTemplate|apple=2|banana=5}}')
489 class TestRodelbahnbox(unittest.TestCase):
494 | Position = 46.807218 N 12.806522 E
495 | Position oben = 46.799014 N 12.818658 E
500 | Schwierigkeit = mittel
502 | Betreiber = Bringungsgemeinschaft Kreithof-Dolomitenhütte
503 | Öffentliche Anreise = Schlecht
504 | Aufstieg möglich = Ja
505 | Aufstieg getrennt = Teilweise
507 | Aufstiegshilfe = Taxi; Sonstige (PKW bis Kreithof)
508 | Beleuchtungsanlage = Ja
509 | Beleuchtungstage = 7
510 | Rodelverleih = Nein
511 | Gütesiegel = Tiroler Naturrodelbahn-Gütesiegel 2009 mittel
512 | Webauskunft = http://www.lienzerdolomiten.info/at/tobogorpt.html
513 | Telefonauskunft = +43-664-2253782 (Dolomitenhütte)
514 | Bild = Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg
515 | In Übersichtskarte = Ja
519 def test_from_str(self):
520 value = rodelbahnbox_from_str(self.value)
521 self.assertEqual(value['Position'], LonLat(12.806522, 46.807218))
522 self.assertEqual(value['Position oben'], LonLat(12.818658, 46.799014))
523 self.assertEqual(value['Höhe oben'], 1046)
524 self.assertEqual(value['Position unten'], LonLat(None, None))
525 self.assertEqual(value['Höhe unten'], None)
526 self.assertEqual(value['Länge'], 3500)
527 self.assertEqual(value['Schwierigkeit'], 2)
528 self.assertEqual(value['Lawinen'], 1)
529 self.assertEqual(value['Betreiber'], 'Bringungsgemeinschaft Kreithof-Dolomitenhütte')
530 self.assertEqual(value['Öffentliche Anreise'], 4)
531 self.assertEqual(value['Aufstieg möglich'], True)
532 self.assertEqual(value['Aufstieg getrennt'], (0.5, None))
533 self.assertEqual(value['Gehzeit'], 75)
534 self.assertEqual(value['Aufstiegshilfe'], [('Taxi', None), ('Sonstige', 'PKW bis Kreithof')])
535 self.assertEqual(value['Beleuchtungsanlage'], (1.0, None))
536 self.assertEqual(value['Beleuchtungstage'], (7, None))
537 self.assertEqual(value['Rodelverleih'], [])
538 self.assertEqual(value['Gütesiegel'], [('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')])
539 self.assertEqual(value['Webauskunft'], (True, 'http://www.lienzerdolomiten.info/at/tobogorpt.html'))
540 self.assertEqual(value['Telefonauskunft'], [('+43-664-2253782', 'Dolomitenhütte')])
541 self.assertEqual(value['Bild'], 'Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg')
542 self.assertEqual(value['In Übersichtskarte'], True)
543 self.assertEqual(value['Forumid'], 139)
545 def test_to_str(self):
546 value = OrderedDict([
547 ('Position', LonLat(12.806522, 46.807218)),
548 ('Position oben', LonLat(12.818658, 46.799014)),
550 ('Position unten', LonLat(None, None)),
551 ('Höhe unten', None),
553 ('Schwierigkeit', 2),
555 ('Betreiber', 'Bringungsgemeinschaft Kreithof-Dolomitenhütte'),
556 ('Öffentliche Anreise', 4),
557 ('Aufstieg möglich', True),
558 ('Aufstieg getrennt', (0.5, None)),
560 ('Aufstiegshilfe', [('Taxi', None), ('Sonstige', 'PKW bis Kreithof')]),
561 ('Beleuchtungsanlage', (1.0, None)),
562 ('Beleuchtungstage', (7, None)),
563 ('Rodelverleih', []),
564 ('Gütesiegel', [('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')]),
565 ('Webauskunft', (True, 'http://www.lienzerdolomiten.info/at/tobogorpt.html')),
566 ('Telefonauskunft', [('+43-664-2253782', 'Dolomitenhütte')]),
567 ('Bild', 'Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg'),
568 ('In Übersichtskarte', True),
570 self.assertEqual(rodelbahnbox_to_str(value), self.value)
573 class TestWrValidators(unittest.TestCase):
574 def test_ValueCommentListNeinLoopNone(self):
575 v = wrpylib.wrvalidators.ValueCommentListNeinLoopNone()
576 assert v.to_python('') == None
577 assert v.to_python('Nein') == 'Nein'
578 assert v.to_python('T-Mobile (gut); A1') == 'T-Mobile (gut); A1'
579 assert v.from_python(None) == ''
580 assert v.from_python('Nein') == 'Nein'
581 assert v.from_python('T-Mobile (gut); A1') == 'T-Mobile (gut); A1'
584 def test_PhoneCommentListNeinLoopNone(self):
585 v = wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True)
586 assert v.to_python('') == None
587 assert v.to_python('Nein') == 'Nein'
588 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'
589 assert v.from_python(None) == ''
590 assert v.from_python('Nein') == 'Nein'
591 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'
594 def test_GasthausboxDictValidator(self):
595 v = wrpylib.wrvalidators.GasthausboxDictValidator()
596 other = collections.OrderedDict([
597 ('Position', '47.295549 N 9.986970 E'),
601 ('Übernachtung', ''),
602 ('Rauchfrei', 'Nein'),
603 ('Rodelverleih', ''),
604 ('Handyempfang', 'A1; T-Mobile/Telering'),
605 ('Homepage', 'http://www.bergkristallhuette.com/'),
606 ('E-Mail', 'bergkristallhuette@gmx.at'),
607 ('Telefon', '+43-664-1808482'),
608 ('Bild', 'Bergkritsallhütte 2009-02-07.JPG'),
609 ('Rodelbahnen', '[[Bergkristallhütte]]')])
610 python = v.to_python(other, None)
611 other2 = v.from_python(python, None)
612 assert other == other2