Skip to content
Newer
Older
100644 798 lines (675 sloc) 27 KB
db2b5e4 @reedobrien 80 char max line legnth
reedobrien authored Mar 14, 2011
1 # -*- coding: utf-8 -*-
2
599dbbb @reedobrien add converter_date and date_header tests for descriptors
reedobrien authored Mar 14, 2011
3 from datetime import tzinfo
4 from datetime import timedelta
5
6f4f52f @reedobrien add test_descriptors with initial tests
reedobrien authored Mar 14, 2011
6 from nose.tools import eq_
7 from nose.tools import ok_
8 from nose.tools import assert_raises
9
10 from webob import Request
11
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
12
599dbbb @reedobrien add converter_date and date_header tests for descriptors
reedobrien authored Mar 14, 2011
13 class GMT(tzinfo):
14 """UTC"""
15 ZERO = timedelta(0)
16 def utcoffset(self, dt):
17 return self.ZERO
18
19 def tzname(self, dt):
20 return "UTC"
21
22 def dst(self, dt):
23 return self.ZERO
6f4f52f @reedobrien add test_descriptors with initial tests
reedobrien authored Mar 14, 2011
24
00de86b @reedobrien moar coverage for descriptors
reedobrien authored Mar 15, 2011
25
26 class MockDescriptor:
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
27 _val = 'avalue'
00de86b @reedobrien moar coverage for descriptors
reedobrien authored Mar 15, 2011
28 def __get__(self, obj, type=None):
29 return self._val
30 def __set__(self, obj, val):
31 self._val = val
32 def __delete__(self, obj):
33 self._val = None
34
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
35
36 def test_environ_getter_docstring():
6f4f52f @reedobrien add test_descriptors with initial tests
reedobrien authored Mar 14, 2011
37 from webob.descriptors import environ_getter
38 desc = environ_getter('akey')
39 eq_(desc.__doc__, "Gets and sets the 'akey' key in the environment.")
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
40
41 def test_environ_getter_nodefault_keyerror():
42 from webob.descriptors import environ_getter
43 req = Request.blank('/')
44 desc = environ_getter('akey')
6f4f52f @reedobrien add test_descriptors with initial tests
reedobrien authored Mar 14, 2011
45 assert_raises(KeyError, desc.fget, req)
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
46
47 def test_environ_getter_nodefault_fget():
48 from webob.descriptors import environ_getter
49 req = Request.blank('/')
50 desc = environ_getter('akey')
6f4f52f @reedobrien add test_descriptors with initial tests
reedobrien authored Mar 14, 2011
51 desc.fset(req, 'bar')
52 eq_(req.environ['akey'], 'bar')
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
53
54 def test_environ_getter_nodefault_fdel():
55 from webob.descriptors import environ_getter
56 desc = environ_getter('akey')
6f4f52f @reedobrien add test_descriptors with initial tests
reedobrien authored Mar 14, 2011
57 eq_(desc.fdel, None)
58
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
59 def test_environ_getter_default_fget():
6f4f52f @reedobrien add test_descriptors with initial tests
reedobrien authored Mar 14, 2011
60 from webob.descriptors import environ_getter
61 req = Request.blank('/')
62 desc = environ_getter('akey', default='the_default')
63 eq_(desc.fget(req), 'the_default')
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
64
65 def test_environ_getter_default_fset():
66 from webob.descriptors import environ_getter
67 req = Request.blank('/')
68 desc = environ_getter('akey', default='the_default')
6f4f52f @reedobrien add test_descriptors with initial tests
reedobrien authored Mar 14, 2011
69 desc.fset(req, 'bar')
70 eq_(req.environ['akey'], 'bar')
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
71
72 def test_environ_getter_default_fset_none():
73 from webob.descriptors import environ_getter
74 req = Request.blank('/')
75 desc = environ_getter('akey', default='the_default')
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
76 desc.fset(req, 'baz')
6f4f52f @reedobrien add test_descriptors with initial tests
reedobrien authored Mar 14, 2011
77 desc.fset(req, None)
78 ok_('akey' not in req.environ)
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
79
80 def test_environ_getter_default_fdel():
81 from webob.descriptors import environ_getter
82 req = Request.blank('/')
83 desc = environ_getter('akey', default='the_default')
6f4f52f @reedobrien add test_descriptors with initial tests
reedobrien authored Mar 14, 2011
84 desc.fset(req, 'baz')
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
85 assert 'akey' in req.environ
6f4f52f @reedobrien add test_descriptors with initial tests
reedobrien authored Mar 14, 2011
86 desc.fdel(req)
87 ok_('akey' not in req.environ)
88
89 def test_environ_getter_rfc_section():
90 from webob.descriptors import environ_getter
91 desc = environ_getter('akey', rfc_section='14.3')
db2b5e4 @reedobrien 80 char max line legnth
reedobrien authored Mar 14, 2011
92 eq_(desc.__doc__, "Gets and sets the 'akey' key in the environment. For "
93 "more information on akey see `section 14.3 "
94 "<http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3>`_.")
23eb217 @reedobrien add test_upath_property for descriptors
reedobrien authored Mar 14, 2011
95
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
96 def test_upath_property_fget():
23eb217 @reedobrien add test_upath_property for descriptors
reedobrien authored Mar 14, 2011
97 from webob.descriptors import upath_property
98 req = Request.blank('/')
99 desc = upath_property('akey')
100 eq_(desc.fget(req), '')
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
101
102 def test_upath_property_fset():
103 from webob.descriptors import upath_property
104 req = Request.blank('/')
105 desc = upath_property('akey')
23eb217 @reedobrien add test_upath_property for descriptors
reedobrien authored Mar 14, 2011
106 desc.fset(req, 'avalue')
107 eq_(desc.fget(req), 'avalue')
f24f2cc @reedobrien add header_getter test for descriptors
reedobrien authored Mar 14, 2011
108
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
109 def test_header_getter_doc():
f24f2cc @reedobrien add header_getter test for descriptors
reedobrien authored Mar 14, 2011
110 from webob.descriptors import header_getter
111 desc = header_getter('AHEADER', '14.3')
112 eq_(desc.__doc__, "Gets and sets and deletes the AHEADER header. For "
113 "more information on AHEADER see `section 14.3 "
114 "<http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3>`_.")
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
115
116 def test_header_getter_fget():
117 from webob.descriptors import header_getter
118 from webob import Response
119 resp = Response('aresp')
120 desc = header_getter('AHEADER', '14.3')
f24f2cc @reedobrien add header_getter test for descriptors
reedobrien authored Mar 14, 2011
121 eq_(desc.fget(resp), None)
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
122
123 def test_header_getter_fset():
124 from webob.descriptors import header_getter
125 from webob import Response
126 resp = Response('aresp')
127 desc = header_getter('AHEADER', '14.3')
f24f2cc @reedobrien add header_getter test for descriptors
reedobrien authored Mar 14, 2011
128 desc.fset(resp, 'avalue')
129 eq_(desc.fget(resp), 'avalue')
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
130
131 def test_header_getter_fset_none():
132 from webob.descriptors import header_getter
133 from webob import Response
134 resp = Response('aresp')
135 desc = header_getter('AHEADER', '14.3')
136 desc.fset(resp, 'avalue')
f24f2cc @reedobrien add header_getter test for descriptors
reedobrien authored Mar 14, 2011
137 desc.fset(resp, None)
138 eq_(desc.fget(resp), None)
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
139
140 def test_header_getter_fdel():
141 from webob.descriptors import header_getter
142 from webob import Response
143 resp = Response('aresp')
144 desc = header_getter('AHEADER', '14.3')
f24f2cc @reedobrien add header_getter test for descriptors
reedobrien authored Mar 14, 2011
145 desc.fset(resp, 'avalue2')
146 desc.fdel(resp)
147 eq_(desc.fget(resp), None)
fd795d4 @reedobrien another header_getter and add converter tests for descriptors
reedobrien authored Mar 14, 2011
148
149 def test_header_getter_unicode():
150 from webob.descriptors import header_getter
151 desc = header_getter('AHEADER', '14.3')
152 eq_(desc.__doc__, "Gets and sets and deletes the AHEADER header. For "
153 "more information on AHEADER see `section 14.3 "
154 "<http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3>`_.")
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
155
156 def test_header_getter_unicode_fget_none():
157 from webob.descriptors import header_getter
158 from webob import Response
159 resp = Response('aresp')
160 desc = header_getter('AHEADER', '14.3')
fd795d4 @reedobrien another header_getter and add converter tests for descriptors
reedobrien authored Mar 14, 2011
161 eq_(desc.fget(resp), None)
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
162
163 def test_header_getter_unicode_fget():
164 from webob.descriptors import header_getter
165 from webob import Response
166 resp = Response('aresp')
167 desc = header_getter('AHEADER', '14.3')
fd795d4 @reedobrien another header_getter and add converter tests for descriptors
reedobrien authored Mar 14, 2011
168 desc.fset(resp, u'avalue')
169 eq_(desc.fget(resp), u'avalue')
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
170
171 def test_header_getter_unicode_fset_none():
172 from webob.descriptors import header_getter
173 from webob import Response
174 resp = Response('aresp')
175 desc = header_getter('AHEADER', '14.3')
fd795d4 @reedobrien another header_getter and add converter tests for descriptors
reedobrien authored Mar 14, 2011
176 desc.fset(resp, None)
177 eq_(desc.fget(resp), None)
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
178
179 def test_header_getter_unicode_fset():
180 from webob.descriptors import header_getter
181 from webob import Response
182 resp = Response('aresp')
183 desc = header_getter('AHEADER', '14.3')
fd795d4 @reedobrien another header_getter and add converter tests for descriptors
reedobrien authored Mar 14, 2011
184 desc.fset(resp, u'avalue2')
185 eq_(desc.fget(resp), u'avalue2')
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
186
187 def test_header_getter_unicode_fdel():
188 from webob.descriptors import header_getter
189 from webob import Response
190 resp = Response('aresp')
191 desc = header_getter('AHEADER', '14.3')
192 desc.fset(resp, u'avalue3')
fd795d4 @reedobrien another header_getter and add converter tests for descriptors
reedobrien authored Mar 14, 2011
193 desc.fdel(resp)
194 eq_(desc.fget(resp), None)
195
196 def test_converter_not_prop():
197 from webob.descriptors import converter
198 from webob.descriptors import parse_int_safe
199 from webob.descriptors import serialize_int
200 assert_raises(AssertionError,converter,
1a3142b @reedobrien don't use a literal set contructor since python<2.7 doesn't support it
reedobrien authored Mar 14, 2011
201 ('CONTENT_LENGTH', None, '14.13'),
fd795d4 @reedobrien another header_getter and add converter tests for descriptors
reedobrien authored Mar 14, 2011
202 parse_int_safe, serialize_int, 'int')
203
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
204 def test_converter_with_name_docstring():
fd795d4 @reedobrien another header_getter and add converter tests for descriptors
reedobrien authored Mar 14, 2011
205 from webob.descriptors import converter
206 from webob.descriptors import environ_getter
207 from webob.descriptors import parse_int_safe
208 from webob.descriptors import serialize_int
209 desc = converter(
210 environ_getter('CONTENT_LENGTH', '666', '14.13'),
211 parse_int_safe, serialize_int, 'int')
212 eq_(desc.__doc__, "Gets and sets the 'CONTENT_LENGTH' key in the "
213 "environment. For more information on CONTENT_LENGTH see `section 14.13 "
214 "<http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13>`_. "
711f21e @tseaver Fix exposing object pointers in docstring output.
tseaver authored Mar 14, 2011
215 "Converts it using int.")
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
216
217 def test_converter_with_name_fget():
218 from webob.descriptors import converter
219 from webob.descriptors import environ_getter
220 from webob.descriptors import parse_int_safe
221 from webob.descriptors import serialize_int
222 req = Request.blank('/')
223 desc = converter(
224 environ_getter('CONTENT_LENGTH', '666', '14.13'),
225 parse_int_safe, serialize_int, 'int')
fd795d4 @reedobrien another header_getter and add converter tests for descriptors
reedobrien authored Mar 14, 2011
226 eq_(desc.fget(req), 666)
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
227
228 def test_converter_with_name_fset():
229 from webob.descriptors import converter
230 from webob.descriptors import environ_getter
231 from webob.descriptors import parse_int_safe
232 from webob.descriptors import serialize_int
233 req = Request.blank('/')
234 desc = converter(
235 environ_getter('CONTENT_LENGTH', '666', '14.13'),
236 parse_int_safe, serialize_int, 'int')
fd795d4 @reedobrien another header_getter and add converter tests for descriptors
reedobrien authored Mar 14, 2011
237 desc.fset(req, '999')
238 eq_(desc.fget(req), 999)
239
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
240 def test_converter_without_name_fget():
fd795d4 @reedobrien another header_getter and add converter tests for descriptors
reedobrien authored Mar 14, 2011
241 from webob.descriptors import converter
242 from webob.descriptors import environ_getter
243 from webob.descriptors import parse_int_safe
244 from webob.descriptors import serialize_int
245 req = Request.blank('/')
246 desc = converter(
247 environ_getter('CONTENT_LENGTH', '666', '14.13'),
248 parse_int_safe, serialize_int)
249 eq_(desc.fget(req), 666)
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
250
251 def test_converter_without_name_fset():
252 from webob.descriptors import converter
253 from webob.descriptors import environ_getter
254 from webob.descriptors import parse_int_safe
255 from webob.descriptors import serialize_int
256 req = Request.blank('/')
257 desc = converter(
258 environ_getter('CONTENT_LENGTH', '666', '14.13'),
259 parse_int_safe, serialize_int)
fd795d4 @reedobrien another header_getter and add converter tests for descriptors
reedobrien authored Mar 14, 2011
260 desc.fset(req, '999')
261 eq_(desc.fget(req), 999)
262
263 def test_converter_none_for_wrong_type():
264 from webob.descriptors import converter
265 from webob.descriptors import environ_getter
266 from webob.descriptors import parse_int_safe
267 from webob.descriptors import serialize_int
268 req = Request.blank('/')
269 desc = converter(
270 ## XXX: Should this fail if the type is wrong?
271 environ_getter('CONTENT_LENGTH', 'sixsixsix', '14.13'),
272 parse_int_safe, serialize_int, 'int')
273 eq_(desc.fget(req), None)
274
275 def test_converter_delete():
276 from webob.descriptors import converter
277 from webob.descriptors import environ_getter
278 from webob.descriptors import parse_int_safe
279 from webob.descriptors import serialize_int
280 req = Request.blank('/')
281 desc = converter(
282 ## XXX: Should this fail if the type is wrong?
283 environ_getter('CONTENT_LENGTH', '666', '14.13'),
284 parse_int_safe, serialize_int, 'int')
285 assert_raises(KeyError, desc.fdel, req)
9c655f2 @reedobrien add parse_list and serialize_list tests to descriptors
reedobrien authored Mar 14, 2011
286
287 def test_list_header():
288 from webob.descriptors import list_header
289 desc = list_header('CONTENT_LENGTH', '14.13')
290 eq_(type(desc), property)
291
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
292 def test_parse_list_single():
9c655f2 @reedobrien add parse_list and serialize_list tests to descriptors
reedobrien authored Mar 14, 2011
293 from webob.descriptors import parse_list
294 result = parse_list('avalue')
295 eq_(result, ('avalue',))
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
296
297 def test_parse_list_multiple():
298 from webob.descriptors import parse_list
9c655f2 @reedobrien add parse_list and serialize_list tests to descriptors
reedobrien authored Mar 14, 2011
299 result = parse_list('avalue,avalue2')
300 eq_(result, ('avalue', 'avalue2'))
301
c6cb2f8 @reedobrien 100% coverage for descritors
reedobrien authored Mar 15, 2011
302 def test_parse_list_none():
303 from webob.descriptors import parse_list
304 result = parse_list(None)
305 eq_(result, None)
306
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
307 def test_parse_list_unicode_single():
9c655f2 @reedobrien add parse_list and serialize_list tests to descriptors
reedobrien authored Mar 14, 2011
308 from webob.descriptors import parse_list
309 result = parse_list(u'avalue')
310 eq_(result, ('avalue',))
1a95104 @reedobrien break descriptors tests into individual units
reedobrien authored Mar 15, 2011
311
312 def test_parse_list_unicode_multiple():
313 from webob.descriptors import parse_list
9c655f2 @reedobrien add parse_list and serialize_list tests to descriptors
reedobrien authored Mar 14, 2011
314 result = parse_list(u'avalue,avalue2')
315 eq_(result, ('avalue', 'avalue2'))
316
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
317 def test_serialize_list():
9c655f2 @reedobrien add parse_list and serialize_list tests to descriptors
reedobrien authored Mar 14, 2011
318 from webob.descriptors import serialize_list
319 result = serialize_list(('avalue', 'avalue2'))
320 eq_(result, 'avalue, avalue2')
321
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
322 def test_serialize_list_string():
c6cb2f8 @reedobrien 100% coverage for descritors
reedobrien authored Mar 15, 2011
323 from webob.descriptors import serialize_list
324 result = serialize_list('avalue')
325 eq_(result, 'avalue')
326
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
327 def test_serialize_list_unicode():
9c655f2 @reedobrien add parse_list and serialize_list tests to descriptors
reedobrien authored Mar 14, 2011
328 from webob.descriptors import serialize_list
c6cb2f8 @reedobrien 100% coverage for descritors
reedobrien authored Mar 15, 2011
329 result = serialize_list(u'avalue')
330 eq_(result, u'avalue')
599dbbb @reedobrien add converter_date and date_header tests for descriptors
reedobrien authored Mar 14, 2011
331
332 def test_converter_date():
333 import datetime
334 from webob.descriptors import converter_date
335 from webob.descriptors import environ_getter
336 req = Request.blank('/')
337 UTC = GMT()
338 desc = converter_date(environ_getter(
339 "HTTP_DATE", "Tue, 15 Nov 1994 08:12:31 GMT", "14.8"))
340 eq_(desc.fget(req),
341 datetime.datetime(1994, 11, 15, 8, 12, 31, tzinfo=UTC))
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
342
343 def test_converter_date_docstring():
344 from webob.descriptors import converter_date
345 from webob.descriptors import environ_getter
346 desc = converter_date(environ_getter(
347 "HTTP_DATE", "Tue, 15 Nov 1994 08:12:31 GMT", "14.8"))
599dbbb @reedobrien add converter_date and date_header tests for descriptors
reedobrien authored Mar 14, 2011
348 eq_(desc.__doc__, "Gets and sets the 'HTTP_DATE' key in the environment. "
349 "For more information on Date see `section 14.8 "
350 "<http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.8>`_. "
351 "Converts it using HTTP date.")
352
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
353 def test_date_header_fget_none():
354 from webob import Response
355 from webob.descriptors import date_header
356 resp = Response('aresponse')
357 desc = date_header('HTTP_DATE', "14.8")
358 eq_(desc.fget(resp), None)
359
360 def test_date_header_fset_fget():
599dbbb @reedobrien add converter_date and date_header tests for descriptors
reedobrien authored Mar 14, 2011
361 import datetime
362 from webob import Response
363 from webob.descriptors import date_header
364 resp = Response('aresponse')
365 UTC = GMT()
366 desc = date_header('HTTP_DATE', "14.8")
367 desc.fset(resp, "Tue, 15 Nov 1994 08:12:31 GMT")
368 eq_(desc.fget(resp), datetime.datetime(1994, 11, 15, 8, 12, 31, tzinfo=UTC))
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
369
370 def test_date_header_fdel():
371 from webob import Response
372 from webob.descriptors import date_header
373 resp = Response('aresponse')
374 desc = date_header('HTTP_DATE', "14.8")
375 desc.fset(resp, "Tue, 15 Nov 1994 08:12:31 GMT")
599dbbb @reedobrien add converter_date and date_header tests for descriptors
reedobrien authored Mar 14, 2011
376 desc.fdel(resp)
377 eq_(desc.fget(resp), None)
378
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
379 def test_deprecated_property_ctor_prop():
87e366c @reedobrien add deprecated_property tests for descriptors
reedobrien authored Mar 14, 2011
380 from webob.descriptors import deprecated_property
381 prop = property()
382 dep = deprecated_property(prop,
383 'deprecated_property',
384 "Don't use it",
385 warning=False)
386 eq_(dep.descriptor, prop)
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
387
388 def test_deprecated_property_ctor_attr():
389 from webob.descriptors import deprecated_property
390 prop = property()
391 dep = deprecated_property(prop,
392 'deprecated_property',
393 "Don't use it",
394 warning=False)
87e366c @reedobrien add deprecated_property tests for descriptors
reedobrien authored Mar 15, 2011
395 eq_(dep.attr, 'deprecated_property')
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
396
397 def test_deprecated_property_ctor_message():
398 from webob.descriptors import deprecated_property
399 prop = property()
400 dep = deprecated_property(prop,
401 'deprecated_property',
402 "Don't use it",
403 warning=False)
87e366c @reedobrien add deprecated_property tests for descriptors
reedobrien authored Mar 15, 2011
404 eq_(dep.message, "Don't use it")
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
405
406 def test_deprecated_property_ctor_raises():
407 from webob.descriptors import deprecated_property
408 prop = property()
409 dep = deprecated_property(prop,
410 'deprecated_property',
411 "Don't use it",
412 warning=False)
87e366c @reedobrien add deprecated_property tests for descriptors
reedobrien authored Mar 15, 2011
413 assert_raises(DeprecationWarning, dep.warn)
414
415 def test_deprecated_property_get():
416 from webob.descriptors import deprecated_property
417 dep = deprecated_property(deprecated_property,
418 'deprecated_property',
419 'DEPRECATED',
420 warning=False)
421 assert_raises(DeprecationWarning, dep.__get__, dep)
422
c6cb2f8 @reedobrien 100% coverage for descritors
reedobrien authored Mar 15, 2011
423 def test_deprecated_property_get_none():
424 from webob.descriptors import deprecated_property
425 dep = deprecated_property(None,
426 'deprecated_property',
427 'DEPRECATED',
428 warning=False)
429 eq_(dep.__get__(None), dep)
430
87e366c @reedobrien add deprecated_property tests for descriptors
reedobrien authored Mar 15, 2011
431 def test_deprecated_property_set():
432 from webob.descriptors import deprecated_property
433 dep = deprecated_property(deprecated_property,
434 'deprecated_property',
435 'DEPRECATED',
436 warning=False)
437 assert_raises(DeprecationWarning, dep.__set__, dep, 'avalue')
438
439 def test_deprecated_property_delete():
440 from webob.descriptors import deprecated_property
441 dep = deprecated_property(deprecated_property,
442 'deprecated_property',
443 'DEPRECATED',
444 warning=False)
445 assert_raises(DeprecationWarning, dep.__delete__, dep)
c1c9d75 @reedobrien add parse and seralize etag tests for descriptors
reedobrien authored Mar 14, 2011
446
00de86b @reedobrien moar coverage for descriptors
reedobrien authored Mar 15, 2011
447 def test_deprecated_property_repr():
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
448 import warnings
449 from webob.descriptors import deprecated_property
450 mock = MockDescriptor()
451 try:
452 warnings.simplefilter('ignore')
453 dep = deprecated_property(mock,
454 'mock_property',
455 'DEPRECATED')
456 assert dep.__repr__().startswith(
457 "<Deprecated attribute mock_property: "
458 "<tests.test_descriptors.MockDescriptor instance at")
459 finally:
460 warnings.resetwarnings()
461
462 def test_deprecated_property_warn_get():
463 import warnings
00de86b @reedobrien moar coverage for descriptors
reedobrien authored Mar 15, 2011
464 from webob.descriptors import deprecated_property
465 mock = MockDescriptor()
466 dep = deprecated_property(mock,
467 'mock_property',
468 'DEPRECATED')
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
469 try:
470 warnings.simplefilter('error')
471 assert_raises(DeprecationWarning, dep.__get__, mock)
472 finally:
473 warnings.resetwarnings()
00de86b @reedobrien moar coverage for descriptors
reedobrien authored Mar 15, 2011
474
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
475 def test_deprecated_property_warn_set():
7cd1b17 @reedobrien Don't use context maanagers in tests as they aren't supported in 2.5
reedobrien authored Mar 15, 2011
476 import warnings
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
477 from webob.descriptors import deprecated_property
7cd1b17 @reedobrien Don't use context maanagers in tests as they aren't supported in 2.5
reedobrien authored Mar 15, 2011
478 mock = MockDescriptor()
479 dep = deprecated_property(mock,
480 'mock_property',
481 'DEPRECATED')
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
482 try:
483 warnings.simplefilter('error')
484 assert_raises(DeprecationWarning, dep.__set__, mock, 'avalue')
485 finally:
486 warnings.resetwarnings()
00de86b @reedobrien moar coverage for descriptors
reedobrien authored Mar 15, 2011
487
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
488 def test_deprecated_property_warn_delete():
489 import warnings
7cd1b17 @reedobrien Don't use context maanagers in tests as they aren't supported in 2.5
reedobrien authored Mar 15, 2011
490 from webob.descriptors import deprecated_property
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
491 mock = MockDescriptor()
492 dep = deprecated_property(mock,
493 'mock_property',
494 'DEPRECATED')
495 try:
496 warnings.simplefilter('error')
497 assert_raises(DeprecationWarning, dep.__delete__, mock)
498 finally:
499 warnings.resetwarnings()
500
501 def test_deprecated_property_warn_get_call():
7cd1b17 @reedobrien Don't use context maanagers in tests as they aren't supported in 2.5
reedobrien authored Mar 15, 2011
502 import warnings
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
503 from webob.descriptors import deprecated_property
7cd1b17 @reedobrien Don't use context maanagers in tests as they aren't supported in 2.5
reedobrien authored Mar 15, 2011
504 mock = MockDescriptor()
505 dep = deprecated_property(mock,
506 'mock_property',
507 'DEPRECATED')
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
508 try:
509 warnings.simplefilter('ignore')
510 eq_(dep.__get__(mock), 'avalue')
511 finally:
512 warnings.resetwarnings()
7cd1b17 @reedobrien Don't use context maanagers in tests as they aren't supported in 2.5
reedobrien authored Mar 15, 2011
513
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
514 def test_deprecated_property_warn_set_call():
515 import warnings
7cd1b17 @reedobrien Don't use context maanagers in tests as they aren't supported in 2.5
reedobrien authored Mar 15, 2011
516 from webob.descriptors import deprecated_property
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
517 mock = MockDescriptor()
518 dep = deprecated_property(mock,
519 'mock_property',
520 'DEPRECATED')
521 try:
522 warnings.simplefilter('ignore')
523 dep.__set__(mock, 'avalue2')
524 eq_(dep.__get__(mock), 'avalue2')
525 finally:
526 warnings.resetwarnings()
527
528 def test_deprecated_property_warn_delete_call():
7cd1b17 @reedobrien Don't use context maanagers in tests as they aren't supported in 2.5
reedobrien authored Mar 15, 2011
529 import warnings
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
530 from webob.descriptors import deprecated_property
7cd1b17 @reedobrien Don't use context maanagers in tests as they aren't supported in 2.5
reedobrien authored Mar 15, 2011
531 mock = MockDescriptor()
532 dep = deprecated_property(mock,
533 'mock_property',
f58c6b1 @reedobrien rewire deprecated property tests to not screw up warnings filter stat…
reedobrien authored Mar 15, 2011
534 'DEPRECATED')
535 try:
536 warnings.simplefilter('ignore')
537 dep.__delete__(mock)
538 eq_(dep.__get__(mock), None)
539 finally:
540 warnings.resetwarnings()
00de86b @reedobrien moar coverage for descriptors
reedobrien authored Mar 15, 2011
541
c1c9d75 @reedobrien add parse and seralize etag tests for descriptors
reedobrien authored Mar 15, 2011
542 def test_parse_etag_response():
543 from webob.descriptors import parse_etag_response
544 etresp = parse_etag_response("etag")
545 eq_(etresp, "etag")
546
547 def test_parse_etag_response_quoted():
548 from webob.descriptors import parse_etag_response
549 etresp = parse_etag_response('"etag"')
550 eq_(etresp, "etag")
551
552 def test_parse_etag_response_is_none():
553 from webob.descriptors import parse_etag_response
554 etresp = parse_etag_response(None)
555 eq_(etresp, None)
556
557 def test_serialize_etag_response():
558 from webob.descriptors import serialize_etag_response
559 etresp = serialize_etag_response("etag")
560 eq_(etresp, '"etag"')
561
0726866 @reedobrien add parse and seralize etag tests for descriptors
reedobrien authored Mar 14, 2011
562 def test_parse_if_range_is_None():
563 from webob.descriptors import parse_if_range
564 from webob.descriptors import NoIfRange
de7834d @reedobrien add moar parse and seralize etag tests for descriptors
reedobrien authored Mar 14, 2011
565 eq_(NoIfRange, parse_if_range(None))
566
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
567 def test_parse_if_range_date_ifr():
de7834d @reedobrien add moar parse and seralize etag tests for descriptors
reedobrien authored Mar 15, 2011
568 from webob.descriptors import parse_if_range
569 from webob.descriptors import IfRange
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
570 ifr = parse_if_range("2011-03-15 01:24:43.272409")
de7834d @reedobrien add moar parse and seralize etag tests for descriptors
reedobrien authored Mar 15, 2011
571 eq_(type(ifr), IfRange)
572
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
573 def test_parse_if_range_date_etagmatcher():
de7834d @reedobrien add moar parse and seralize etag tests for descriptors
reedobrien authored Mar 15, 2011
574 from webob.descriptors import parse_if_range
575 from webob.etag import ETagMatcher
576 ifr = parse_if_range("2011-03-15 01:24:43.272409")
577 eq_(type(ifr.etag), ETagMatcher)
578
b2323d7 @reedobrien add more tests
reedobrien authored Mar 15, 2011
579 def test_serialize_if_range_string():
580 from webob.descriptors import serialize_if_range
581 val = serialize_if_range("avalue")
582 eq_(val, "avalue")
0726866 @reedobrien add parse and seralize etag tests for descriptors
reedobrien authored Mar 15, 2011
583
b2323d7 @reedobrien add more tests
reedobrien authored Mar 15, 2011
584 def test_serialize_if_range_unicode():
585 from webob.descriptors import serialize_if_range
586 val = serialize_if_range(u"avalue")
587 eq_(val, u"avalue")
588
589 def test_serialize_if_range_datetime():
590 import datetime
591 from webob.descriptors import serialize_if_range
592 UTC = GMT()
593 val = serialize_if_range(datetime.datetime(1994, 11, 15, 8, 12, 31, tzinfo=UTC))
594 eq_(val, "Tue, 15 Nov 1994 08:12:31 GMT")
595
596 def test_serialize_if_range_other():
597 from webob.descriptors import serialize_if_range
598 val = serialize_if_range(123456)
599 eq_(val, '123456')
600
601 def test_parse_range_none():
602 from webob.descriptors import parse_range
603 val = parse_range(None)
604 eq_(val, None)
605
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
606 def test_parse_range_type():
b2323d7 @reedobrien add more tests
reedobrien authored Mar 15, 2011
607 from webob.byterange import Range
608 from webob.descriptors import parse_range
609 val = parse_range("bytes=1-500")
610 eq_(type(val), type(Range.parse("bytes=1-500")))
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
611
612 def test_parse_range_values():
613 from webob.byterange import Range
614 from webob.descriptors import parse_range
615 val = parse_range("bytes=1-500")
b2323d7 @reedobrien add more tests
reedobrien authored Mar 15, 2011
616 eq_(val.ranges, Range.parse("bytes=1-500").ranges)
617
618 def test_serialize_range_none():
619 from webob.descriptors import serialize_range
620 val = serialize_range(None)
621 eq_(val, None)
622
623 def test_serialize_range():
624 from webob.descriptors import serialize_range
625 val = serialize_range((1,500))
626 eq_(val, 'bytes=1-499')
627
628 def test_serialize_invalid_len():
629 from webob.descriptors import serialize_range
630 assert_raises(ValueError, serialize_range, (1,))
631
632 def test_parse_int_none():
633 from webob.descriptors import parse_int
634 val = parse_int(None)
635 eq_(val, None)
636
637 def test_parse_int_emptystr():
638 from webob.descriptors import parse_int
639 val = parse_int('')
640 eq_(val, None)
641
642 def test_parse_int():
643 from webob.descriptors import parse_int
644 val = parse_int('123')
645 eq_(val, 123)
646
647 def test_parse_int_invalid():
648 from webob.descriptors import parse_int
649 assert_raises(ValueError, parse_int, 'abc')
650
651 def test_parse_int_safe_none():
652 from webob.descriptors import parse_int_safe
653 eq_(parse_int_safe(None), None)
654
655 def test_parse_int_safe_emptystr():
656 from webob.descriptors import parse_int_safe
657 eq_(parse_int_safe(''), None)
658
659 def test_parse_int_safe():
660 from webob.descriptors import parse_int_safe
661 eq_(parse_int_safe('123'), 123)
662
663 def test_parse_int_safe_invalid():
664 from webob.descriptors import parse_int_safe
665 eq_(parse_int_safe('abc'), None)
666
667 def test_serialize_int():
668 from webob.descriptors import serialize_int
669 assert serialize_int is str
5280d21 @reedobrien add more tests to descriptors
reedobrien authored Mar 15, 2011
670
671 def test_parse_content_range_none():
672 from webob.descriptors import parse_content_range
673 eq_(parse_content_range(None), None)
674
675 def test_parse_content_range_emptystr():
676 from webob.descriptors import parse_content_range
677 eq_(parse_content_range(' '), None)
678
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
679 def test_parse_content_range_length():
5280d21 @reedobrien add more tests to descriptors
reedobrien authored Mar 15, 2011
680 from webob.byterange import ContentRange
681 from webob.descriptors import parse_content_range
682 val = parse_content_range("bytes 0-499/1234")
683 eq_(val.length, ContentRange.parse("bytes 0-499/1234").length)
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
684
685 def test_parse_content_range_start():
686 from webob.byterange import ContentRange
687 from webob.descriptors import parse_content_range
688 val = parse_content_range("bytes 0-499/1234")
5280d21 @reedobrien add more tests to descriptors
reedobrien authored Mar 15, 2011
689 eq_(val.start, ContentRange.parse("bytes 0-499/1234").start)
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
690
691 def test_parse_content_range_stop():
692 from webob.byterange import ContentRange
693 from webob.descriptors import parse_content_range
694 val = parse_content_range("bytes 0-499/1234")
5280d21 @reedobrien add more tests to descriptors
reedobrien authored Mar 15, 2011
695 eq_(val.stop, ContentRange.parse("bytes 0-499/1234").stop)
696
697 def test_serialize_content_range_none():
698 from webob.descriptors import serialize_content_range
699 eq_(serialize_content_range(None), 'None') ### XXX: Seems wrong
700
701 def test_serialize_content_range_emptystr():
702 from webob.descriptors import serialize_content_range
703 eq_(serialize_content_range(''), None)
704
705 def test_serialize_content_range_invalid():
706 from webob.descriptors import serialize_content_range
707 assert_raises(ValueError, serialize_content_range, (1,))
708
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
709 def test_serialize_content_range_asterisk():
54db262 @reedobrien more tests for descriptors
reedobrien authored Mar 15, 2011
710 from webob.descriptors import serialize_content_range
711 eq_(serialize_content_range((0, 500)), 'bytes 0-499/*')
36afd55 @reedobrien split the rest of the tests out into their own funtions
reedobrien authored Mar 15, 2011
712
713 def test_serialize_content_range_defined():
714 from webob.descriptors import serialize_content_range
54db262 @reedobrien more tests for descriptors
reedobrien authored Mar 15, 2011
715 eq_(serialize_content_range((0, 500, 1234)), 'bytes 0-499/1234')
716
00de86b @reedobrien moar coverage for descriptors
reedobrien authored Mar 15, 2011
717 def test_parse_auth_params_leading_capital_letter():
54db262 @reedobrien more tests for descriptors
reedobrien authored Mar 15, 2011
718 from webob.descriptors import parse_auth_params
719 val = parse_auth_params('Basic Realm=WebOb')
720 eq_(val, {'ealm': 'WebOb'})
00de86b @reedobrien moar coverage for descriptors
reedobrien authored Mar 15, 2011
721
722 def test_parse_auth_params_trailing_capital_letter():
723 from webob.descriptors import parse_auth_params
54db262 @reedobrien more tests for descriptors
reedobrien authored Mar 15, 2011
724 val = parse_auth_params('Basic realM=WebOb')
725 eq_(val, {})
00de86b @reedobrien moar coverage for descriptors
reedobrien authored Mar 15, 2011
726
727 def test_parse_auth_params_doublequotes():
728 from webob.descriptors import parse_auth_params
54db262 @reedobrien more tests for descriptors
reedobrien authored Mar 15, 2011
729 val = parse_auth_params('Basic realm="Web Object"')
730 eq_(val, {'realm': 'Web Object'})
00de86b @reedobrien moar coverage for descriptors
reedobrien authored Mar 15, 2011
731
732 def test_parse_auth_params_multiple_values():
733 from webob.descriptors import parse_auth_params
54db262 @reedobrien more tests for descriptors
reedobrien authored Mar 15, 2011
734 val = parse_auth_params("foo='blah &&234', qop=foo, nonce='qwerty1234'")
735 eq_(val, {'nonce': "'qwerty1234'", 'foo': "'blah &&234'", 'qop': 'foo'})
00de86b @reedobrien moar coverage for descriptors
reedobrien authored Mar 15, 2011
736
737 def test_parse_auth_params_truncate_on_comma():
738 from webob.descriptors import parse_auth_params
54db262 @reedobrien more tests for descriptors
reedobrien authored Mar 15, 2011
739 val = parse_auth_params("Basic realm=WebOb,this_will_truncate")
740 eq_(val, {'realm': 'WebOb'})
741
742 def test_parse_auth_params_emptystr():
743 from webob.descriptors import parse_auth_params
744 eq_(parse_auth_params(''), {})
00de86b @reedobrien moar coverage for descriptors
reedobrien authored Mar 15, 2011
745
746 def test_parse_auth_none():
747 from webob.descriptors import parse_auth
748 eq_(parse_auth(None), None)
749
750 def test_parse_auth_emptystr():
751 from webob.descriptors import parse_auth
752 assert_raises(ValueError, parse_auth, '')
753
754 def test_parse_auth_basic():
755 from webob.descriptors import parse_auth
756 eq_(parse_auth("Basic realm=WebOb"), ('Basic', 'realm=WebOb'))
757
758 def test_parse_auth_basic_quoted():
759 from webob.descriptors import parse_auth
760 eq_(parse_auth('Basic realm="Web Ob"'), ('Basic', {'realm': 'Web Ob'}))
761
762 def test_parse_auth_basic_quoted_multiple_unknown():
763 from webob.descriptors import parse_auth
764 eq_(parse_auth("foo='blah &&234', qop=foo, nonce='qwerty1234'"),
765 ("foo='blah", "&&234', qop=foo, nonce='qwerty1234'"))
766
767 def test_parse_auth_basic_quoted_known_multiple():
768 from webob.descriptors import parse_auth
769 eq_(parse_auth("Basic realm='blah &&234', qop=foo, nonce='qwerty1234'"),
770 ('Basic', "realm='blah &&234', qop=foo, nonce='qwerty1234'"))
771
772 def test_serialize_auth_none():
773 from webob.descriptors import serialize_auth
774 eq_(serialize_auth(None), None)
775
776 def test_serialize_auth_emptystr():
777 from webob.descriptors import serialize_auth
778 eq_(serialize_auth(''), '')
779
780 def test_serialize_auth_basic_quoted():
781 from webob.descriptors import serialize_auth
782 val = serialize_auth(('Basic', 'realm="WebOb"'))
783 eq_(val, 'Basic realm="WebOb"')
784
785 def test_serialize_auth_digest_multiple():
786 from webob.descriptors import serialize_auth
787 val = serialize_auth(('Digest', 'realm="WebOb", nonce=abcde12345, qop=foo'))
7027672 @mcdonc make descriptor tests pass on jython
mcdonc authored Mar 16, 2011
788 flags = val[len('Digest'):]
789 result = sorted([ x.strip() for x in flags.split(',') ])
790 eq_(result, ['nonce=abcde12345', 'qop=foo', 'realm="WebOb"'])
00de86b @reedobrien moar coverage for descriptors
reedobrien authored Mar 15, 2011
791
792 def test_serialize_auth_digest_tuple():
793 from webob.descriptors import serialize_auth
794 val = serialize_auth(('Digest', {'realm':'"WebOb"', 'nonce':'abcde12345', 'qop':'foo'}))
7027672 @mcdonc make descriptor tests pass on jython
mcdonc authored Mar 16, 2011
795 flags = val[len('Digest'):]
796 result = sorted([ x.strip() for x in flags.split(',') ])
797 eq_(result, ['nonce="abcde12345"', 'qop="foo"', 'realm=""WebOb""'])
Something went wrong with that request. Please try again.