]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/wradmin/tests/test_models.py
01ca6cf1fd25ff41e5c7be6674abba302fe10ca0
[philipp/winterrodeln/wradmin.git] / wradmin / wradmin / tests / test_models.py
1 import wradmin.model.validators
2 import formencode
3
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
10     try:
11         v.to_python(u'Wrong')
12         assert True, u"The value 'Wrong' must not be accepted by the validator."
13     except formencode.Invalid: pass
14     
15     assert v.from_python(True) == u'Ja'
16     assert v.from_python(False) == u'Nein'
17     assert v.from_python(None) == u''
18
19
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)
27     try:
28         v.to_python(u'Wrong')
29         assert False, u"The value 'Wrong' must not be accepted by the validator."
30     except formencode.Invalid: pass
31
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''
36
37
38 def test_geo():
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)
45
46     assert v.from_python((lat, lon)) == coord
47     assert v.from_python((None, None)) == u''
48
49
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']:
58         try:
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
62
63
64 def test_PhoneInfo():
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']:
71         try:
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        
75