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 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))
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')
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('')
87 def test_to_str(self):
88 self.assertEqual(difficulty_german_to_str(1), 'leicht')
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')
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')
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')
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), '')
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)')
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)), '')
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')
148 def test_to_str(self):
149 self.assertEqual(lonlat_to_str(LonLat(11.453553, 47.076207)), '47.076207 N 11.453553 E')
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')
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')
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('')
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')
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))
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)), '')
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')])
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)')
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')
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')
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')
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')
267 class TestSingleCachet(unittest.TestCase):
268 def test_from_str(self):
269 self.assertEqual(single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'), ('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel'))
270 self.assertEqual(single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer'), ('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer'))
271 with self.assertRaises(ValueError):
272 single_cachet_german_from_str('')
273 with self.assertRaises(ValueError):
274 single_cachet_german_from_str('Salzburger Naturrodelbahn-Gütesiegel 2013 schwer')
275 with self.assertRaises(ValueError):
276 single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 4013 schwer')
277 with self.assertRaises(ValueError):
278 single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 13 schwer')
279 with self.assertRaises(ValueError):
280 single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwerer')
282 def test_to_str(self):
283 self.assertEqual(single_cachet_german_to_str(('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')), 'Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
284 self.assertEqual(single_cachet_german_to_str(('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer')), 'Tiroler Naturrodelbahn-Gütesiegel 2013 schwer')
287 class TestCachetGerman(unittest.TestCase):
288 def test_from_str(self):
289 self.assertEqual(cachet_german_from_str(''), None)
290 self.assertEqual(cachet_german_from_str('Nein'), [])
291 self.assertEqual(cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'), [('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')])
292 self.assertEqual(cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer; Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'),
293 [('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer'), ('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')])
294 with self.assertRaises(ValueError):
295 cachet_german_from_str('Ja')
296 with self.assertRaises(ValueError):
297 cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2013 schwer Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
299 def test_to_str(self):
300 self.assertEqual(cachet_german_to_str(None), '')
301 self.assertEqual(cachet_german_to_str([]), 'Nein')
302 self.assertEqual(cachet_german_to_str([('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')]), 'Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
303 self.assertEqual(cachet_german_to_str([('Tiroler Naturrodelbahn-Gütesiegel', '2013', 'schwer'), ('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')]),
304 'Tiroler Naturrodelbahn-Gütesiegel 2013 schwer; Tiroler Naturrodelbahn-Gütesiegel 2009 mittel')
307 class TestUrl(unittest.TestCase):
308 def test_from_str(self):
309 self.assertEqual(url_from_str('http://www.winterrodeln.org/wiki/Arzler_Alm/'), 'http://www.winterrodeln.org/wiki/Arzler_Alm/')
310 self.assertEqual(url_from_str('http://www.winterrodeln.org/wiki/Nösslachhütte/'), 'http://www.winterrodeln.org/wiki/Nösslachhütte/')
311 self.assertEqual(url_from_str('https://www.winterrodeln.org/wiki/Nösslachhütte/'), 'https://www.winterrodeln.org/wiki/Nösslachhütte/')
312 with self.assertRaises(ValueError):
313 url_from_str('mailto:office@example.com')
314 with self.assertRaises(ValueError):
315 url_from_str('/wiki/Arzler_Alm/')
317 def test_to_str(self):
318 self.assertEqual(url_to_str('http://www.winterrodeln.org/wiki/Arzler_Alm/'), 'http://www.winterrodeln.org/wiki/Arzler_Alm/')
319 self.assertEqual(url_to_str('http://www.winterrodeln.org/wiki/Nösslachhütte/'), 'http://www.winterrodeln.org/wiki/Nösslachhütte/')
320 self.assertEqual(url_to_str('https://www.winterrodeln.org/wiki/Nösslachhütte/'), 'https://www.winterrodeln.org/wiki/Nösslachhütte/')
323 class TestWebauskunft(unittest.TestCase):
324 def test_from_str(self):
325 self.assertEqual(webauskunft_from_str('http://www.example.com/current'), (True, 'http://www.example.com/current'))
326 self.assertEqual(webauskunft_from_str(''), (None, None))
327 self.assertEqual(webauskunft_from_str('Nein'), (False, None))
329 def test_to_str(self):
330 self.assertEqual(webauskunft_to_str((True, 'http://www.example.com/current')), 'http://www.example.com/current')
331 self.assertEqual(webauskunft_to_str((None, None)), '')
332 self.assertEqual(webauskunft_to_str((False, None)), 'Nein')
335 class TestPhoneNumber(unittest.TestCase):
336 def test_from_str(self):
337 self.assertEqual(phone_number_from_str('+43-699-123456789'), '+43-699-123456789')
338 self.assertEqual(phone_number_from_str('+43-69945'), '+43-69945')
339 self.assertEqual(phone_number_from_str('+43-512-507-6418'), '+43-512-507-6418')
340 with self.assertRaises(ValueError):
341 phone_number_from_str('+43-')
342 with self.assertRaises(ValueError):
343 phone_number_from_str('0512123456789')
345 def test_to_str(self):
346 self.assertEqual(phone_number_to_str('+43-699-123456789'), '+43-699-123456789')
347 self.assertEqual(phone_number_to_str('+43-69945'), '+43-69945')
348 self.assertEqual(phone_number_to_str('+43-512-507-6418'), '+43-512-507-6418')
351 class TestTelefonauskunft(unittest.TestCase):
352 def test_from_str(self):
353 self.assertEqual(telefonauskunft_from_str(''), None)
354 self.assertEqual(telefonauskunft_from_str('Nein'), [])
355 self.assertEqual(telefonauskunft_from_str('+43-512-123456 (untertags)'), [('+43-512-123456', 'untertags')])
356 self.assertEqual(telefonauskunft_from_str('+43-512-1234 (untertags); +43-664-123456 (Alm)'), [('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')])
357 with self.assertRaises(ValueError):
358 telefonauskunft_from_str('+43-512-123456+ (untertags)')
359 with self.assertRaises(ValueError):
360 telefonauskunft_from_str('+43-512-123456')
362 def test_to_str(self):
363 self.assertEqual(telefonauskunft_to_str(None), '')
364 self.assertEqual(telefonauskunft_to_str([]), 'Nein')
365 self.assertEqual(telefonauskunft_to_str([('+43-512-123456', 'untertags')]), '+43-512-123456 (untertags)')
366 self.assertEqual(telefonauskunft_to_str([('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')]), '+43-512-1234 (untertags); +43-664-123456 (Alm)')
369 class TestEmail(unittest.TestCase):
371 self.good_addresses = ['office@example.com', 'winter+rodeln@localhost', 'joe.doe@exämple.com']
372 self.bad_addresses = ['öffice@example.com', 'winter rodeln@localhost', 'www.winterrodeln.org', 'mailto:info@example.com', 'info@example.com.']
374 def test_from_str(self):
375 for value in self.good_addresses:
376 self.assertEqual(value, email_from_str(value))
377 for value in self.bad_addresses:
378 with self.assertRaises(ValueError):
379 email_from_str(value)
381 def test_to_str(self):
382 for value in self.good_addresses:
383 self.assertEqual(value, email_to_str(value))
386 class TestMaskedEmail(unittest.TestCase):
387 def test_from_str(self):
388 self.assertEqual(('office@example.com', False), masked_email_from_str('office@example.com'))
389 self.assertEqual(('office@example.com', True), masked_email_from_str('office(at)example.com'))
390 with self.assertRaises(ValueError):
391 masked_email_from_str('office@example.com', masked_only=True)
392 with self.assertRaises(ValueError):
393 masked_email_from_str('off ice@example.com')
395 def test_to_str(self):
396 self.assertEqual('office@example.com', masked_email_to_str(('office@example.com', False)))
397 self.assertEqual('office(at)example.com', masked_email_to_str(('office@example.com', True)))
398 self.assertEqual('office()example.com', masked_email_to_str(('office@example.com', True), '()'))
401 class TestEmails(unittest.TestCase):
402 def test_from_str(self):
403 self.assertEqual(None, emails_from_str(''))
404 self.assertEqual([], emails_from_str('Nein'))
405 self.assertEqual([(('info@example.com', False), None)], emails_from_str('info@example.com'))
406 self.assertEqual([(('info@example.com', True), None)], emails_from_str('info(at)example.com'))
407 self.assertEqual([(('info@example.com', False), 'Office')], emails_from_str('info@example.com (Office)'))
408 self.assertEqual([(('info@example.com', False), None), ('home@example.com', 'Privat')], emails_from_str('info@example.com; home@example.com (Privat)'))
409 with self.assertRaises(ValueError):
410 emails_from_str('nein')
411 with self.assertRaises(ValueError):
412 emails_from_str('info@example.com; ho me@example.com (Privat)')
414 def test_to_str(self):
415 self.assertEqual('', emails_to_str(None))
416 self.assertEqual('Nein', emails_to_str([]))
417 self.assertEqual('info@example.com', emails_to_str([(('info@example.com', False), None)]))
418 self.assertEqual('info@example.com (Office)', emails_to_str([(('info@example.com', False), 'Office')]))
419 self.assertEqual('info@example.com; home@example.com (Privat)', emails_to_str([(('info@example.com', False), None), (('home@example.com', False), 'Privat')]))
422 class TestBox(unittest.TestCase):
423 def test_from_str(self):
424 value = '{{MyTemplate|apple=2|banana=5}}'
425 converter_dict = OrderedDict([('apple', opt_int_converter), ('banana', opt_int_converter)])
426 result = box_from_str(value, 'MyTemplate', converter_dict)
427 self.assertEqual(result['apple'], 2)
428 self.assertEqual(result['banana'], 5)
430 value = '{{MyTemplate\n | apple = 2 \n| banana = 5 }}'
431 result = box_from_str(value, 'MyTemplate', converter_dict)
432 self.assertEqual(result['apple'], 2)
433 self.assertEqual(result['banana'], 5)
435 with self.assertRaises(ValueError):
436 box_from_str(value, 'myTemplate', converter_dict)
437 with self.assertRaises(ValueError):
438 value = '{{MyTemplate|apple=2|banana=five}}'
439 box_from_str(value, 'MyTemplate', converter_dict)
440 with self.assertRaises(ValueError):
441 value = '{{MyTemplate|apple=2}}'
442 box_from_str(value, 'MyTemplate', converter_dict)
443 with self.assertRaises(ValueError):
444 value = '{{MyTemplate|apple=2|banana=5|cherry=6}}'
445 box_from_str(value, 'MyTemplate', converter_dict)
447 def test_to_str(self):
448 value = OrderedDict([('apple', 2), ('banana', 5)])
449 converter_dict = OrderedDict([('apple', opt_int_converter), ('banana', opt_int_converter)])
450 result = box_to_str(value, 'MyTemplate', converter_dict)
451 self.assertEqual(result, '{{MyTemplate|apple=2|banana=5}}')
454 class TestRodelbahnbox(unittest.TestCase):
459 | Position = 46.807218 N 12.806522 E
460 | Position oben = 46.799014 N 12.818658 E
465 | Schwierigkeit = mittel
467 | Betreiber = Bringungsgemeinschaft Kreithof-Dolomitenhütte
468 | Öffentliche Anreise = Schlecht
469 | Aufstieg möglich = Ja
470 | Aufstieg getrennt = Teilweise
472 | Aufstiegshilfe = Taxi; Sonstige (PKW bis Kreithof)
473 | Beleuchtungsanlage = Ja
474 | Beleuchtungstage = 7
475 | Rodelverleih = Nein
476 | Gütesiegel = Tiroler Naturrodelbahn-Gütesiegel 2009 mittel
477 | Webauskunft = http://www.lienzerdolomiten.info/at/tobogorpt.html
478 | Telefonauskunft = +43-664-2253782 (Dolomitenhütte)
479 | Bild = Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg
480 | In Übersichtskarte = Ja
484 def test_from_str(self):
485 value = rodelbahnbox_from_str(self.value)
486 self.assertEqual(value['Position'], LonLat(12.806522, 46.807218))
487 self.assertEqual(value['Position oben'], LonLat(12.818658, 46.799014))
488 self.assertEqual(value['Höhe oben'], 1046)
489 self.assertEqual(value['Position unten'], LonLat(None, None))
490 self.assertEqual(value['Höhe unten'], None)
491 self.assertEqual(value['Länge'], 3500)
492 self.assertEqual(value['Schwierigkeit'], 2)
493 self.assertEqual(value['Lawinen'], 1)
494 self.assertEqual(value['Betreiber'], 'Bringungsgemeinschaft Kreithof-Dolomitenhütte')
495 self.assertEqual(value['Öffentliche Anreise'], 4)
496 self.assertEqual(value['Aufstieg möglich'], True)
497 self.assertEqual(value['Aufstieg getrennt'], (0.5, None))
498 self.assertEqual(value['Gehzeit'], 75)
499 self.assertEqual(value['Aufstiegshilfe'], [('Taxi', None), ('Sonstige', 'PKW bis Kreithof')])
500 self.assertEqual(value['Beleuchtungsanlage'], (1.0, None))
501 self.assertEqual(value['Beleuchtungstage'], (7, None))
502 self.assertEqual(value['Rodelverleih'], [])
503 self.assertEqual(value['Gütesiegel'], [('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')])
504 self.assertEqual(value['Webauskunft'], (True, 'http://www.lienzerdolomiten.info/at/tobogorpt.html'))
505 self.assertEqual(value['Telefonauskunft'], [('+43-664-2253782', 'Dolomitenhütte')])
506 self.assertEqual(value['Bild'], 'Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg')
507 self.assertEqual(value['In Übersichtskarte'], True)
508 self.assertEqual(value['Forumid'], 139)
510 def test_to_str(self):
511 value = OrderedDict([
512 ('Position', LonLat(12.806522, 46.807218)),
513 ('Position oben', LonLat(12.818658, 46.799014)),
515 ('Position unten', LonLat(None, None)),
516 ('Höhe unten', None),
518 ('Schwierigkeit', 2),
520 ('Betreiber', 'Bringungsgemeinschaft Kreithof-Dolomitenhütte'),
521 ('Öffentliche Anreise', 4),
522 ('Aufstieg möglich', True),
523 ('Aufstieg getrennt', (0.5, None)),
525 ('Aufstiegshilfe', [('Taxi', None), ('Sonstige', 'PKW bis Kreithof')]),
526 ('Beleuchtungsanlage', (1.0, None)),
527 ('Beleuchtungstage', (7, None)),
528 ('Rodelverleih', []),
529 ('Gütesiegel', [('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')]),
530 ('Webauskunft', (True, 'http://www.lienzerdolomiten.info/at/tobogorpt.html')),
531 ('Telefonauskunft', [('+43-664-2253782', 'Dolomitenhütte')]),
532 ('Bild', 'Dolomitenrodelbahn Tristach 2011-12-22 oberer Bereich.jpg'),
533 ('In Übersichtskarte', True),
535 self.assertEqual(rodelbahnbox_to_str(value), self.value)
538 class TestWrValidators(unittest.TestCase):
539 def test_ValueCommentListNeinLoopNone(self):
540 v = wrpylib.wrvalidators.ValueCommentListNeinLoopNone()
541 assert v.to_python('') == None
542 assert v.to_python('Nein') == 'Nein'
543 assert v.to_python('T-Mobile (gut); A1') == 'T-Mobile (gut); A1'
544 assert v.from_python(None) == ''
545 assert v.from_python('Nein') == 'Nein'
546 assert v.from_python('T-Mobile (gut); A1') == 'T-Mobile (gut); A1'
549 def test_PhoneCommentListNeinLoopNone(self):
550 v = wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True)
551 assert v.to_python('') == None
552 assert v.to_python('Nein') == 'Nein'
553 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'
554 assert v.from_python(None) == ''
555 assert v.from_python('Nein') == 'Nein'
556 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'
559 def test_MaskedEmail(self):
560 v = wrpylib.wrvalidators.MaskedEmail()
561 assert v.to_python('') == (None, None)
562 assert v.to_python('abc.def@example.com') == ('abc.def@example.com', False)
563 assert v.to_python('abc.def(at)example.com') == ('abc.def@example.com', True)
564 assert v.from_python((None, None)) == ''
565 assert v.from_python(('abc.def@example.com', False)) == 'abc.def@example.com'
566 assert v.from_python(('abc.def@example.com', True)) == 'abc.def(at)example.com'
569 def test_EmailCommentListNeinLoopNone(self):
570 v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone()
571 assert v.to_python('') == None
572 assert v.to_python('Nein') == 'Nein'
573 assert v.to_python('first@example.com') == 'first@example.com'
574 assert v.to_python('first@example.com (Nur Winter); second@example.com') == 'first@example.com (Nur Winter); second@example.com'
575 assert v.from_python(None) == ''
576 assert v.from_python('Nein') == 'Nein'
577 assert v.from_python('first@example.com') == 'first@example.com'
578 assert v.from_python('first@example.com (Nur Winter); second@example.com') == 'first@example.com (Nur Winter); second@example.com'
579 testvalue = 'abc.def(at)example.com (comment)'
581 v.to_python(testvalue)
583 except formencode.Invalid:
586 v.from_python(testvalue)
588 except formencode.Invalid:
590 v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True)
591 assert v.to_python(testvalue) == testvalue
592 assert v.from_python(testvalue) == testvalue
595 def test_WikiPageListLoopNone(self):
596 v = wrpylib.wrvalidators.WikiPageListLoopNone()
597 assert v.to_python('') == None
598 assert v.to_python('[[Birgitzer Alm]]; [[Kemater Alm]]') == '[[Birgitzer Alm]]; [[Kemater Alm]]'
599 assert v.from_python(None) == ''
600 assert v.from_python('[[Birgitzer Alm]]; [[Kemater Alm]]') == '[[Birgitzer Alm]]; [[Kemater Alm]]'
603 def test_GasthausboxDictValidator(self):
604 v = wrpylib.wrvalidators.GasthausboxDictValidator()
605 other = collections.OrderedDict([
606 ('Position', '47.295549 N 9.986970 E'),
610 ('Übernachtung', ''),
611 ('Rauchfrei', 'Nein'),
612 ('Rodelverleih', ''),
613 ('Handyempfang', 'A1; T-Mobile/Telering'),
614 ('Homepage', 'http://www.bergkristallhuette.com/'),
615 ('E-Mail', 'bergkristallhuette@gmx.at'),
616 ('Telefon', '+43-664-1808482'),
617 ('Bild', 'Bergkritsallhütte 2009-02-07.JPG'),
618 ('Rodelbahnen', '[[Bergkristallhütte]]')])
619 python = v.to_python(other, None)
620 other2 = v.from_python(python, None)
621 assert other == other2