Django

Code

root/django/trunk/tests/regressiontests/httpwrappers/tests.py

Revision 8705, 7.7 kB (checked in by jacob, 1 month ago)

Fixed #8278: fixed QueryDict.update(QueryDict). Thanks, julien.

  • Property svn:eol-style set to native
Line 
1 """
2 ###################
3 # Empty QueryDict #
4 ###################
5
6 >>> q = QueryDict('')
7
8 >>> q['foo']
9 Traceback (most recent call last):
10 ...
11 MultiValueDictKeyError: "Key 'foo' not found in <QueryDict: {}>"
12
13 >>> q['something'] = 'bar'
14 Traceback (most recent call last):
15 ...
16 AttributeError: This QueryDict instance is immutable
17
18 >>> q.get('foo', 'default')
19 'default'
20
21 >>> q.getlist('foo')
22 []
23
24 >>> q.setlist('foo', ['bar', 'baz'])
25 Traceback (most recent call last):
26 ...
27 AttributeError: This QueryDict instance is immutable
28
29 >>> q.appendlist('foo', ['bar'])
30 Traceback (most recent call last):
31 ...
32 AttributeError: This QueryDict instance is immutable
33
34 >>> q.has_key('foo')
35 False
36
37 >>> 'foo' in q
38 False
39
40 >>> q.items()
41 []
42
43 >>> q.lists()
44 []
45
46 >>> q.keys()
47 []
48
49 >>> q.values()
50 []
51
52 >>> len(q)
53 0
54
55 >>> q.update({'foo': 'bar'})
56 Traceback (most recent call last):
57 ...
58 AttributeError: This QueryDict instance is immutable
59
60 >>> q.pop('foo')
61 Traceback (most recent call last):
62 ...
63 AttributeError: This QueryDict instance is immutable
64
65 >>> q.popitem()
66 Traceback (most recent call last):
67 ...
68 AttributeError: This QueryDict instance is immutable
69
70 >>> q.clear()
71 Traceback (most recent call last):
72 ...
73 AttributeError: This QueryDict instance is immutable
74
75 >>> q.setdefault('foo', 'bar')
76 Traceback (most recent call last):
77 ...
78 AttributeError: This QueryDict instance is immutable
79
80 >>> q.urlencode()
81 ''
82
83 ###################################
84 # Mutable copy of empty QueryDict #
85 ###################################
86
87 >>> q = q.copy()
88
89 >>> q['foo']
90 Traceback (most recent call last):
91 ...
92 MultiValueDictKeyError: "Key 'foo' not found in <QueryDict: {}>"
93
94 >>> q['name'] = 'john'
95
96 >>> q['name']
97 u'john'
98
99 >>> del q['name']
100 >>> 'name' in q
101 False
102
103 >>> q['name'] = 'john'
104
105 >>> q.get('foo', 'default')
106 'default'
107
108 >>> q.get('name', 'default')
109 u'john'
110
111 >>> q.getlist('name')
112 [u'john']
113
114 >>> q.getlist('foo')
115 []
116
117 >>> q.setlist('foo', ['bar', 'baz'])
118
119 >>> q.get('foo', 'default')
120 u'baz'
121
122 >>> q.getlist('foo')
123 [u'bar', u'baz']
124
125 >>> q.appendlist('foo', 'another')
126
127 >>> q.getlist('foo')
128 [u'bar', u'baz', u'another']
129
130 >>> q['foo']
131 u'another'
132
133 >>> q.has_key('foo')
134 True
135
136 >>> 'foo' in q
137 True
138
139 >>> q.items()
140 [(u'foo', u'another'), (u'name', u'john')]
141
142 >>> q.lists()
143 [(u'foo', [u'bar', u'baz', u'another']), (u'name', [u'john'])]
144
145 >>> q.keys()
146 [u'foo', u'name']
147
148 >>> q.values()
149 [u'another', u'john']
150
151 >>> len(q)
152 2
153
154 >>> q.update({'foo': 'hello'})
155
156 # Displays last value
157 >>> q['foo']
158 u'hello'
159
160 >>> q.get('foo', 'not available')
161 u'hello'
162
163 >>> q.getlist('foo')
164 [u'bar', u'baz', u'another', u'hello']
165
166 >>> q.pop('foo')
167 [u'bar', u'baz', u'another', u'hello']
168
169 >>> q.pop('foo', 'not there')
170 'not there'
171
172 >>> q.get('foo', 'not there')
173 'not there'
174
175 >>> q.setdefault('foo', 'bar')
176 u'bar'
177
178 >>> q['foo']
179 u'bar'
180
181 >>> q.getlist('foo')
182 [u'bar']
183
184 >>> q.urlencode()
185 'foo=bar&name=john'
186
187 >>> q.clear()
188
189 >>> len(q)
190 0
191
192 #####################################
193 # QueryDict with one key/value pair #
194 #####################################
195
196 >>> q = QueryDict('foo=bar')
197
198 >>> q['foo']
199 u'bar'
200
201 >>> q['bar']
202 Traceback (most recent call last):
203 ...
204 MultiValueDictKeyError: "Key 'bar' not found in <QueryDict: {u'foo': [u'bar']}>"
205
206 >>> q['something'] = 'bar'
207 Traceback (most recent call last):
208 ...
209 AttributeError: This QueryDict instance is immutable
210
211 >>> q.get('foo', 'default')
212 u'bar'
213
214 >>> q.get('bar', 'default')
215 'default'
216
217 >>> q.getlist('foo')
218 [u'bar']
219
220 >>> q.getlist('bar')
221 []
222
223 >>> q.setlist('foo', ['bar', 'baz'])
224 Traceback (most recent call last):
225 ...
226 AttributeError: This QueryDict instance is immutable
227
228 >>> q.appendlist('foo', ['bar'])
229 Traceback (most recent call last):
230 ...
231 AttributeError: This QueryDict instance is immutable
232
233 >>> q.has_key('foo')
234 True
235
236 >>> 'foo' in q
237 True
238
239 >>> q.has_key('bar')
240 False
241
242 >>> 'bar' in q
243 False
244
245 >>> q.items()
246 [(u'foo', u'bar')]
247
248 >>> q.lists()
249 [(u'foo', [u'bar'])]
250
251 >>> q.keys()
252 [u'foo']
253
254 >>> q.values()
255 [u'bar']
256
257 >>> len(q)
258 1
259
260 >>> q.update({'foo': 'bar'})
261 Traceback (most recent call last):
262 ...
263 AttributeError: This QueryDict instance is immutable
264
265 >>> q.pop('foo')
266 Traceback (most recent call last):
267 ...
268 AttributeError: This QueryDict instance is immutable
269
270 >>> q.popitem()
271 Traceback (most recent call last):
272 ...
273 AttributeError: This QueryDict instance is immutable
274
275 >>> q.clear()
276 Traceback (most recent call last):
277 ...
278 AttributeError: This QueryDict instance is immutable
279
280 >>> q.setdefault('foo', 'bar')
281 Traceback (most recent call last):
282 ...
283 AttributeError: This QueryDict instance is immutable
284
285 >>> q.urlencode()
286 'foo=bar'
287
288 #####################################################
289 # QueryDict with two key/value pairs with same keys #
290 #####################################################
291
292 >>> q = QueryDict('vote=yes&vote=no')
293
294 >>> q['vote']
295 u'no'
296
297 >>> q['something'] = 'bar'
298 Traceback (most recent call last):
299 ...
300 AttributeError: This QueryDict instance is immutable
301
302 >>> q.get('vote', 'default')
303 u'no'
304
305 >>> q.get('foo', 'default')
306 'default'
307
308 >>> q.getlist('vote')
309 [u'yes', u'no']
310
311 >>> q.getlist('foo')
312 []
313
314 >>> q.setlist('foo', ['bar', 'baz'])
315 Traceback (most recent call last):
316 ...
317 AttributeError: This QueryDict instance is immutable
318
319 >>> q.appendlist('foo', ['bar'])
320 Traceback (most recent call last):
321 ...
322 AttributeError: This QueryDict instance is immutable
323
324 >>> q.has_key('vote')
325 True
326
327 >>> 'vote' in q
328 True
329
330 >>> q.has_key('foo')
331 False
332
333 >>> 'foo' in q
334 False
335
336 >>> q.items()
337 [(u'vote', u'no')]
338
339 >>> q.lists()
340 [(u'vote', [u'yes', u'no'])]
341
342 >>> q.keys()
343 [u'vote']
344
345 >>> q.values()
346 [u'no']
347
348 >>> len(q)
349 1
350
351 >>> q.update({'foo': 'bar'})
352 Traceback (most recent call last):
353 ...
354 AttributeError: This QueryDict instance is immutable
355
356 >>> q.pop('foo')
357 Traceback (most recent call last):
358 ...
359 AttributeError: This QueryDict instance is immutable
360
361 >>> q.popitem()
362 Traceback (most recent call last):
363 ...
364 AttributeError: This QueryDict instance is immutable
365
366 >>> q.clear()
367 Traceback (most recent call last):
368 ...
369 AttributeError: This QueryDict instance is immutable
370
371 >>> q.setdefault('foo', 'bar')
372 Traceback (most recent call last):
373 ...
374 AttributeError: This QueryDict instance is immutable
375
376 >>> q.urlencode()
377 'vote=yes&vote=no'
378
379 >>> del q['vote']
380 Traceback (most recent call last):
381 ...
382 AttributeError: This QueryDict instance is immutable
383
384 # QueryDicts must be able to handle invalid input encoding (in this case, bad
385 # UTF-8 encoding).
386 >>> q = QueryDict('foo=bar&foo=\xff')
387
388 >>> q['foo']
389 u'\ufffd'
390
391 >>> q.getlist('foo')
392 [u'bar', u'\ufffd']
393
394
395 ########################
396 # Pickling a QueryDict #
397 ########################
398 >>> import pickle
399 >>> q = QueryDict('a=b&c=d')
400 >>> q1 = pickle.loads(pickle.dumps(q, 2))
401 >>> q == q1
402 True
403
404 ######################################
405 # HttpResponse with Unicode headers  #
406 ######################################
407
408 >>> r = HttpResponse()
409
410 If we insert a unicode value it will be converted to an ascii
411 string. This makes sure we comply with the HTTP specifications.
412
413 >>> r['value'] = u'test value'
414 >>> isinstance(r['value'], str)
415 True
416
417 An error is raised When a unicode object with non-ascii is assigned.
418
419 >>> r['value'] = u't\xebst value' # doctest:+ELLIPSIS
420 Traceback (most recent call last):
421 ...
422 UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
423
424 The response also converts unicode keys to strings.
425
426 >>> r[u'test'] = 'testing key'
427 >>> l = list(r.items())
428 >>> l.sort()
429 >>> l[1]
430 ('test', 'testing key')
431
432 It will also raise errors for keys with non-ascii data.
433
434 >>> r[u't\xebst'] = 'testing key'  # doctest:+ELLIPSIS
435 Traceback (most recent call last):
436 ...
437 UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
438
439 #
440 # Regression test for #8278: QueryDict.update(QueryDict)
441 #
442 >>> x = QueryDict("a=1&a=2", mutable=True)
443 >>> y = QueryDict("a=3&a=4")
444 >>> x.update(y)
445 >>> x.getlist('a')
446 [u'1', u'2', u'3', u'4']
447 """
448
449 from django.http import QueryDict, HttpResponse
450
451 if __name__ == "__main__":
452     import doctest
453     doctest.testmod()
Note: See TracBrowser for help on using the browser.