1 import wradmin.model.validators
4 def test_bool_validator():
5 v = wradmin.model.validators.GermanBool()
6 assert v.to_python(u'Ja') == True
7 assert v.to_python(u'Nein') == False
8 assert v.to_python(None) == None
9 assert v.to_python(u'') == None
12 assert True, u"The value 'Wrong' must not be accepted by the validator."
13 except formencode.Invalid: pass
15 assert v.from_python(True) == u'Ja'
16 assert v.from_python(False) == u'Nein'
17 assert v.from_python(None) == u''
20 def test_GermanTristate_validator():
21 v = wradmin.model.validators.GermanTristate()
22 assert v.to_python(u'Ja') == (True, False)
23 assert v.to_python(u'Nein') == (False, True)
24 assert v.to_python(u'Teilweise') == (True, True)
25 assert v.to_python(u'') == (None, None)
26 assert v.to_python(None) == (None, None)
29 assert False, u"The value 'Wrong' must not be accepted by the validator."
30 except formencode.Invalid: pass
32 assert v.from_python((True, False)) == u'Ja'
33 assert v.from_python((False, True)) == u'Nein'
34 assert v.from_python((True, True)) == u'Teilweise'
35 assert v.from_python((None, None)) == u''
39 coord = u'47.076207 N 11.453553 E'
40 v = wradmin.model.validators.Geo()
41 (lat, lon) = v.to_python(coord)
42 assert lat == 47.076207
43 assert lon == 11.453553
44 assert v.to_python(u'') == (None, None)
46 assert v.from_python((lat, lon)) == coord
47 assert v.from_python((None, None)) == u''
50 def test_AustrianPhoneNumber():
51 v = wradmin.model.validators.AustrianPhoneNumber()
52 assert v.to_python(u'') is None
53 assert v.to_python(u'0512/12345678') == u'+43/512/12345678'
54 assert v.to_python(u'+43/512/12345678') == u'+43/512/12345678'
55 assert v.to_python(u'0512/1234567-89') == u'+43/512/1234567-89'
56 assert v.to_python(u'+43/512/1234567-89') == u'+43/512/1234567-89'
57 for n in [u'0512 / 12345678', u'0512-12345678', u'0049(0)8386/8113']:
59 v.to_python(n) # has to throw an exception
60 assert False, u"The telephone number '%s' should throw an exception." % n
61 except formencode.Invalid: pass
65 v = wradmin.model.validators.PhoneInfo()
66 assert v.to_python(u'') is None
67 assert v.to_python(u'0512/12345678 (Schnee Alm)') == u'+43/512/12345678 (Schnee Alm)'
68 assert v.to_python(u'+43/512/12345678 (Schnee Alm)') == u'+43/512/12345678 (Schnee Alm)'
69 assert v.to_python(u'0512/12345678 (Schnee (Winter) Alm)') == u'+43/512/12345678 (Schnee (Winter) Alm)'
70 for n in [u'0512/12345678', u'+43/512/12345678', u'+43 (Schnee Alm)', u'0512/12345678 ()', u'(Schnee Alm)', u'(Schnee Alm) +43/512/12345678', u'+43/512/12345678 Schnee Alm', u'(Schnee Alm) +43/512/12345678']:
72 v.to_python(n) # has to throw an exception
73 assert False, u"The telephone info '%s' should throw an exception." % n
74 except formencode.Invalid: pass
76 def test_ValueCommentList():
77 v = wradmin.model.validators.ValueCommentList()
78 assert v.to_python('abc') == [('abc', None)]
79 assert v.to_python(u'abc def') == [(u'abc def', None)]
80 assert v.to_python('value (comment)') == [('value', 'comment')]
81 assert v.to_python(u'value (comment)') == [(u'value', u'comment')]
82 assert v.to_python('value1 (comment); value2') == [('value1', 'comment'), ('value2', None)]
83 assert v.to_python('value1 (comment1); value2; value3 (comment3)') == [('value1', 'comment1'), ('value2', None), ('value3', 'comment3')]
84 assert v.to_python('value1 (comment1); value2 (test (not easy))') == [('value1', 'comment1'), ('value2', 'test (not easy)')]