2 # -*- coding: iso-8859-15 -*-
3 import wrpylib.wrvalidators
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''
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''
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)
29 assert v.from_python((lat, lon)) == coord
30 assert v.from_python((None, None)) == u''
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'
45 def tes_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'
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); value2 (test (not easy))') == [(u'value1', u'comment1'), (u'value2', u'test (not easy)')]
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'
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'
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'
98 def test_EmailCommentListNeinLoopNone():
99 v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone()
100 assert v.to_python(u'') == None
101 assert v.to_python(u'Nein') == u'Nein'
102 assert v.to_python(u'first@example.com') == u'first@example.com'
103 assert v.to_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com'
104 assert v.from_python(None) == u''
105 assert v.from_python(u'Nein') == u'Nein'
106 assert v.from_python(u'first@example.com') == u'first@example.com'
107 assert v.from_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com'
110 def test_GermanLift():
111 v = wrpylib.wrvalidators.GermanLift()
112 assert v.to_python(u'') == (None, None)
113 assert v.to_python(u'Nein') == (False, None)
114 assert v.to_python(u'Sessellift (4 Euro)') == (True, u'Sessellift (4 Euro)')
115 assert v.from_python((None, None)) == u''
116 assert v.from_python((False, None)) == u'Nein'
117 assert v.from_python((True, u'Sessellift (4 Euro)')) == u'Sessellift (4 Euro)'
120 def test_WikiPageListLoopNone():
121 v = wrpylib.wrvalidators.WikiPageListLoopNone()
122 assert v.to_python(u'') == None
123 assert v.to_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]'
124 assert v.from_python(None) == u''
125 assert v.from_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]'
128 def test_BoolUnicodeTupleValidator():
129 v = wrpylib.wrvalidators.BoolUnicodeTupleValidator()
130 assert v.to_python(u'') == (None, None)
131 assert v.to_python(u'Nein') == (False, None)
132 assert v.to_python(u'any text') == (True, u'any text')
133 assert v.from_python((None, None)) == u''
134 assert v.from_python((False, None)) == u'Nein'
135 assert v.from_python((True, u'any text')) == u'any text'
138 def test_SledRental():
139 v = wrpylib.wrvalidators.SledRental()
140 assert v.to_python(u'') == (None, None)
141 assert v.to_python(u'Nein') == (False, None)
142 assert v.to_python(u'Ja') == (True, u'Ja')
143 assert v.to_python(u'Talstation (nur mit Ticket); Schneealm') == (True, u'Talstation (nur mit Ticket); Schneealm')
144 assert v.from_python((None, None)) == u''
145 assert v.from_python((False, None)) == u'Nein'
146 assert v.from_python((True, u'Talstation (nur mit Ticket); Schneealm')) == u'Talstation (nur mit Ticket); Schneealm'
147 assert v.from_python((True, u'Ja')) == u'Ja'