e240aef122d6d42828d84a610cea2bfa22609cc5
[philipp/winterrodeln/wrpylib.git] / tests / test_wrvalidators.py
1 #!/usr/bin/python2.6
2 # -*- coding: iso-8859-15 -*-
3 import wrpylib.wrvalidators
4 import formencode
5
6
7 def test_NoneValidator():
8     v =  wrpylib.wrvalidators.NoneValidator(wrpylib.wrvalidators.Unicode())
9     assert v.to_python(u'') == None
10     assert v.from_python(None) == u''
11
12
13 def test_UnsignedNone():
14     v = wrpylib.wrvalidators.UnsignedNone()
15     assert v.to_python(u'42') == 42
16     assert v.to_python(u'') == None
17     assert v.from_python(42) == u'42'
18     assert v.from_python(None) == u''
19
20
21 def test_GeoNone():
22     coord = u'47.076207 N 11.453553 E'
23     v = wrpylib.wrvalidators.GeoNone()
24     (lat, lon) = v.to_python(coord)
25     assert lat == 47.076207
26     assert lon == 11.453553
27     assert v.to_python(u'') == (None, None)
28
29     assert v.from_python((lat, lon)) == coord
30     assert v.from_python((None, None)) == u''
31
32
33 def test_GermanTristateTuple():
34     v = wrpylib.wrvalidators.GermanTristateTuple()
35     assert v.to_python(u'') == (None, None)
36     assert v.to_python(u'Ja') == (True, False)
37     assert v.to_python(u'Nein') == (False, True)
38     assert v.to_python(u'Teilweise') == (True, True)
39     assert v.from_python((None, None)) == u''
40     assert v.from_python((False, True)) == u'Nein'
41     assert v.from_python((True, False)) == u'Ja'
42     assert v.from_python((True, True)) == u'Teilweise'
43
44
45 def test_GermanTristateFloat():
46     v = wrpylib.wrvalidators.GermanTristateFloat()
47     assert v.to_python(u'') == None
48     assert v.to_python(u'Ja') == 1.0
49     assert v.to_python(u'Nein') == 0.0
50     assert v.to_python(u'Teilweise') == 0.5
51     assert v.from_python(None) == u''
52     assert v.from_python(0.0) == u'Nein'
53     assert v.from_python(1.0) == u'Ja'
54     assert v.from_python(0.5) == u'Teilweise'
55
56
57 def test_ValueCommentList():
58     v = wrpylib.wrvalidators.ValueCommentList()
59     assert v.to_python(u'abc') == [(u'abc', None)]
60     assert v.to_python(u'abc def') == [(u'abc def', None)]
61     assert v.to_python(u'value (comment)') == [(u'value', u'comment')]
62     assert v.to_python(u'value (comment)') == [(u'value', u'comment')]
63     assert v.to_python(u'value1 (comment); value2') == [(u'value1', u'comment'), (u'value2', None)]
64     assert v.to_python(u'value1 (comment1); value2; value3 (comment3)') == [(u'value1', u'comment1'), (u'value2', None), ('value3', 'comment3')]
65     assert v.to_python(u'value1 (comment1); [[link (linkcomment)]] (not easy)') == [(u'value1', u'comment1'), (u'[[link (linkcomment)]]', u'not easy')]
66
67
68 def test_UrlNeinNone():
69     v = wrpylib.wrvalidators.UrlNeinNone()
70     assert v.to_python(u'') == None
71     assert v.to_python(u'Nein') == u'Nein'
72     assert v.to_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at'
73     assert v.from_python(None) == u''
74     assert v.from_python(u'Nein') == u'Nein'
75     assert v.from_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at'
76
77
78 def test_ValueCommentListNeinLoopNone():
79     v = wrpylib.wrvalidators.ValueCommentListNeinLoopNone()
80     assert v.to_python(u'') == None
81     assert v.to_python(u'Nein') == u'Nein'
82     assert v.to_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1'
83     assert v.from_python(None) == u''
84     assert v.from_python(u'Nein') == u'Nein'
85     assert v.from_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1'
86     
87
88 def test_PhoneCommentListNeinLoopNone():
89     v = wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True)
90     assert v.to_python(u'') == None
91     assert v.to_python(u'Nein') == u'Nein'
92     assert v.to_python(u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456'
93     assert v.from_python(None) == u''
94     assert v.from_python(u'Nein') == u'Nein'
95     assert v.from_python(u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456'
96
97
98 def test_MaskedEmail():
99     v = wrpylib.wrvalidators.MaskedEmail()
100     assert v.to_python(u'') == (None, None)
101     assert v.to_python(u'abc.def@example.com') == (u'abc.def@example.com', False)
102     assert v.to_python(u'abc.def(at)example.com') == (u'abc.def@example.com', True)
103     assert v.from_python((None, None)) == u''
104     assert v.from_python((u'abc.def@example.com', False)) == u'abc.def@example.com'
105     assert v.from_python((u'abc.def@example.com', True)) == u'abc.def(at)example.com'
106
107
108 def test_EmailCommentListNeinLoopNone():
109     v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone()
110     assert v.to_python(u'') == None
111     assert v.to_python(u'Nein') == u'Nein'
112     assert v.to_python(u'first@example.com') == u'first@example.com'
113     assert v.to_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com'
114     assert v.from_python(None) == u''
115     assert v.from_python(u'Nein') == u'Nein'
116     assert v.from_python(u'first@example.com') == u'first@example.com'
117     assert v.from_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com'
118     testvalue = u'abc.def(at)example.com (comment)'
119     try:
120         v.to_python(testvalue)
121         assert False
122     except formencode.Invalid:
123         pass
124     try:
125         v.from_python(testvalue)
126         assert False
127     except formencode.Invalid:
128         pass
129     v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True)
130     assert v.to_python(testvalue) == testvalue
131     assert v.from_python(testvalue) == testvalue
132
133
134 def test_GermanLift():
135     v = wrpylib.wrvalidators.GermanLift()
136     assert v.to_python(u'') == (None, None)
137     assert v.to_python(u'Nein') == (False, None)
138     assert v.to_python(u'Sessellift (4 Euro)') == (True, u'Sessellift (4 Euro)')
139     assert v.from_python((None, None)) == u''
140     assert v.from_python((False, None)) == u'Nein'
141     assert v.from_python((True, u'Sessellift (4 Euro)')) == u'Sessellift (4 Euro)'
142
143
144 def test_WikiPageListLoopNone():
145     v = wrpylib.wrvalidators.WikiPageListLoopNone()
146     assert v.to_python(u'') == None
147     assert v.to_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]'
148     assert v.from_python(None) == u''
149     assert v.from_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]'
150
151
152 def test_BoolUnicodeTupleValidator():
153     v = wrpylib.wrvalidators.BoolUnicodeTupleValidator()
154     assert v.to_python(u'') == (None, None)
155     assert v.to_python(u'Nein') == (False, None)
156     assert v.to_python(u'any text') == (True, u'any text')
157     assert v.from_python((None, None)) == u''
158     assert v.from_python((False, None)) == u'Nein'
159     assert v.from_python((True, u'any text')) == u'any text'
160
161
162 def test_SledRental():
163     v = wrpylib.wrvalidators.SledRental()
164     assert v.to_python(u'') == (None, None)
165     assert v.to_python(u'Nein') == (False, None)
166     assert v.to_python(u'Ja') == (True, u'Ja')
167     assert v.to_python(u'Talstation (nur mit Ticket); Schneealm') == (True, u'Talstation (nur mit Ticket); Schneealm')
168     assert v.from_python((None, None)) == u''
169     assert v.from_python((False, None)) == u'Nein'
170     assert v.from_python((True, u'Talstation (nur mit Ticket); Schneealm')) == u'Talstation (nur mit Ticket); Schneealm'
171     assert v.from_python((True, u'Ja')) == u'Ja'