]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blob - tests/test_wrvalidators.py
19fca40e7259363f3774e6ac88e3c5ae1421023c
[philipp/winterrodeln/wrpylib.git] / tests / test_wrvalidators.py
1 #!/usr/bin/python2.7
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 # test_NeinValidator
14
15
16 # test_Unicode
17
18
19 # test_UnicodeNone
20
21
22 # test_Unsigned
23
24
25 def test_UnsignedNone():
26     v = wrpylib.wrvalidators.UnsignedNone()
27     assert v.to_python(u'42') == 42
28     assert v.to_python(u'') == None
29     assert v.from_python(42) == u'42'
30     assert v.from_python(None) == u''
31
32
33 # test_UnsignedNeinNone
34
35
36 # test_Loop
37
38
39 # test_DictValidator
40
41
42 # test_GermanBoolNone
43
44
45 def test_GermanTristateTuple():
46     v = wrpylib.wrvalidators.GermanTristateTuple()
47     assert v.to_python(u'') == (None, None)
48     assert v.to_python(u'Ja') == (True, False)
49     assert v.to_python(u'Nein') == (False, True)
50     assert v.to_python(u'Teilweise') == (True, True)
51     assert v.from_python((None, None)) == u''
52     assert v.from_python((False, True)) == u'Nein'
53     assert v.from_python((True, False)) == u'Ja'
54     assert v.from_python((True, True)) == u'Teilweise'
55
56
57 def test_GermanTristateFloat():
58     v = wrpylib.wrvalidators.GermanTristateFloat()
59     assert v.to_python(u'') == None
60     assert v.to_python(u'Ja') == 1.0
61     assert v.to_python(u'Nein') == 0.0
62     assert v.to_python(u'Teilweise') == 0.5
63     assert v.from_python(None) == u''
64     assert v.from_python(0.0) == u'Nein'
65     assert v.from_python(1.0) == u'Ja'
66     assert v.from_python(0.5) == u'Teilweise'
67
68
69 # test_ValueComment
70
71
72 # test_SemicolonList
73
74
75 def test_ValueCommentList():
76     v = wrpylib.wrvalidators.ValueCommentList()
77     assert v.to_python(u'abc') == [(u'abc', None)]
78     assert v.to_python(u'abc def') == [(u'abc def', None)]
79     assert v.to_python(u'value (comment)') == [(u'value', u'comment')]
80     assert v.to_python(u'value (comment)') == [(u'value', u'comment')]
81     assert v.to_python(u'value1 (comment); value2') == [(u'value1', u'comment'), (u'value2', None)]
82     assert v.to_python(u'value1 (comment1); value2; value3 (comment3)') == [(u'value1', u'comment1'), (u'value2', None), ('value3', 'comment3')]
83     assert v.to_python(u'value1 (comment1); [[link (linkcomment)]] (not easy)') == [(u'value1', u'comment1'), (u'[[link (linkcomment)]]', u'not easy')]
84
85
86 # test_GenericDateTime
87
88
89 # test_DateTimeNoSec
90
91
92 # test_DateNone
93
94
95 # test_Geo
96
97
98 def test_GeoNone():
99     coord = u'47.076207 N 11.453553 E'
100     v = wrpylib.wrvalidators.GeoNone()
101     (lat, lon) = v.to_python(coord)
102     assert lat == 47.076207
103     assert lon == 11.453553
104     assert v.to_python(u'') == (None, None)
105
106     assert v.from_python((lat, lon)) == coord
107     assert v.from_python((None, None)) == u''
108
109
110 # test_MultiGeo
111
112
113 # test_AustrianPhoneNumber
114
115
116 # test_AustrianPhoneNumberNone
117
118
119 # test_AustrianPhoneNumberCommentLoop
120
121
122 # test_GermanDifficulty
123
124
125 # test_GermanAvalanches
126
127
128 def test_GermanPublicTransport():
129     v = wrpylib.wrvalidators.GermanPublicTransport()
130     assert v.to_python(u'') is None
131     assert v.to_python(u'Sehr gut') == 1
132     assert v.to_python(u'Gut') == 2
133     assert v.to_python(u'Mittelmäßig') == 3
134     assert v.to_python(u'Schlecht') == 4
135     assert v.to_python(u'Nein') == 5
136     assert v.to_python(u'Ja') == 6
137
138     assert v.from_python(None) == u''
139     assert v.from_python(1) == u'Sehr gut'
140     assert v.from_python(2) == u'Gut'
141     assert v.from_python(3) == u'Mittelmäßig'
142     assert v.from_python(4) == u'Schlecht'
143     assert v.from_python(5) == u'Nein'
144     assert v.from_python(6) == u'Ja'
145
146
147 # test_GermanTristateFloatComment
148
149
150 # test_UnsignedCommentNone
151
152
153 # test_GermanCachet
154
155
156 # test_url
157
158
159 def test_UrlNeinNone():
160     v = wrpylib.wrvalidators.UrlNeinNone()
161     assert v.to_python(u'') == None
162     assert v.to_python(u'Nein') == u'Nein'
163     assert v.to_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at'
164     assert v.from_python(None) == u''
165     assert v.from_python(u'Nein') == u'Nein'
166     assert v.from_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at'
167
168
169 def test_ValueCommentListNeinLoopNone():
170     v = wrpylib.wrvalidators.ValueCommentListNeinLoopNone()
171     assert v.to_python(u'') == None
172     assert v.to_python(u'Nein') == u'Nein'
173     assert v.to_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1'
174     assert v.from_python(None) == u''
175     assert v.from_python(u'Nein') == u'Nein'
176     assert v.from_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1'
177
178
179 # test_PhoneNumber
180     
181
182 def test_PhoneCommentListNeinLoopNone():
183     v = wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True)
184     assert v.to_python(u'') == None
185     assert v.to_python(u'Nein') == u'Nein'
186     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'
187     assert v.from_python(None) == u''
188     assert v.from_python(u'Nein') == u'Nein'
189     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'
190
191
192 def test_MaskedEmail():
193     v = wrpylib.wrvalidators.MaskedEmail()
194     assert v.to_python(u'') == (None, None)
195     assert v.to_python(u'abc.def@example.com') == (u'abc.def@example.com', False)
196     assert v.to_python(u'abc.def(at)example.com') == (u'abc.def@example.com', True)
197     assert v.from_python((None, None)) == u''
198     assert v.from_python((u'abc.def@example.com', False)) == u'abc.def@example.com'
199     assert v.from_python((u'abc.def@example.com', True)) == u'abc.def(at)example.com'
200
201
202 def test_EmailCommentListNeinLoopNone():
203     v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone()
204     assert v.to_python(u'') == None
205     assert v.to_python(u'Nein') == u'Nein'
206     assert v.to_python(u'first@example.com') == u'first@example.com'
207     assert v.to_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com'
208     assert v.from_python(None) == u''
209     assert v.from_python(u'Nein') == u'Nein'
210     assert v.from_python(u'first@example.com') == u'first@example.com'
211     assert v.from_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com'
212     testvalue = u'abc.def(at)example.com (comment)'
213     try:
214         v.to_python(testvalue)
215         assert False
216     except formencode.Invalid:
217         pass
218     try:
219         v.from_python(testvalue)
220         assert False
221     except formencode.Invalid:
222         pass
223     v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True)
224     assert v.to_python(testvalue) == testvalue
225     assert v.from_python(testvalue) == testvalue
226
227
228 # test_WikiPage
229
230
231 # test_WikiPageList
232
233
234 def test_WikiPageListLoopNone():
235     v = wrpylib.wrvalidators.WikiPageListLoopNone()
236     assert v.to_python(u'') == None
237     assert v.to_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]'
238     assert v.from_python(None) == u''
239     assert v.from_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]'
240
241
242 # test_TupleSecondValidator
243
244
245 def test_BoolUnicodeTupleValidator():
246     v = wrpylib.wrvalidators.BoolUnicodeTupleValidator()
247     assert v.to_python(u'') == (None, None)
248     assert v.to_python(u'Nein') == (False, None)
249     assert v.to_python(u'any text') == (True, u'any text')
250     assert v.from_python((None, None)) == u''
251     assert v.from_python((False, None)) == u'Nein'
252     assert v.from_python((True, u'any text')) == u'any text'
253
254
255
256
257 def test_GermanLift():
258     v = wrpylib.wrvalidators.GermanLift()
259     assert v.to_python(u'') == (None, None)
260     assert v.to_python(u'Nein') == (False, None)
261     assert v.to_python(u'Sessellift (4 Euro)') == (True, u'Sessellift (4 Euro)')
262     assert v.from_python((None, None)) == u''
263     assert v.from_python((False, None)) == u'Nein'
264     assert v.from_python((True, u'Sessellift (4 Euro)')) == u'Sessellift (4 Euro)'
265
266
267 def test_SledRental():
268     v = wrpylib.wrvalidators.SledRental()
269     assert v.to_python(u'') == (None, None)
270     assert v.to_python(u'Nein') == (False, None)
271     assert v.to_python(u'Ja') == (True, u'Ja')
272     assert v.to_python(u'Talstation (nur mit Ticket); Schneealm') == (True, u'Talstation (nur mit Ticket); Schneealm')
273     assert v.from_python((None, None)) == u''
274     assert v.from_python((False, None)) == u'Nein'
275     assert v.from_python((True, u'Talstation (nur mit Ticket); Schneealm')) == u'Talstation (nur mit Ticket); Schneealm'
276     assert v.from_python((True, u'Ja')) == u'Ja'