aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/multidict/tests/test_mutable_multidict.py
blob: 3f66e279ad092f07b8c37656d4353018392b1904 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import string
import sys

import pytest


class TestMutableMultiDict:
    @pytest.fixture
    def cls(self, _multidict):
        return _multidict.MultiDict

    @pytest.fixture
    def proxy_cls(self, _multidict):
        return _multidict.MultiDictProxy

    @pytest.fixture
    def istr(self, _multidict):
        return _multidict.istr

    def test_copy(self, cls):
        d1 = cls(key="value", a="b")

        d2 = d1.copy()
        assert d1 == d2
        assert d1 is not d2

    def test__repr__(self, cls):
        d = cls()
        assert str(d) == "<%s()>" % cls.__name__

        d = cls([("key", "one"), ("key", "two")])

        expected = "<%s('key': 'one', 'key': 'two')>" % cls.__name__
        assert str(d) == expected

    def test_getall(self, cls):
        d = cls([("key", "value1")], key="value2")
        assert len(d) == 2

        assert d.getall("key") == ["value1", "value2"]

        with pytest.raises(KeyError, match="some_key"):
            d.getall("some_key")

        default = object()
        assert d.getall("some_key", default) is default
        assert d.getall(key="some_key", default=default) is default

    def test_add(self, cls):
        d = cls()

        assert d == {}
        d["key"] = "one"
        assert d == {"key": "one"}
        assert d.getall("key") == ["one"]

        d["key"] = "two"
        assert d == {"key": "two"}
        assert d.getall("key") == ["two"]

        d.add("key", "one")
        assert 2 == len(d)
        assert d.getall("key") == ["two", "one"]

        d.add("foo", "bar")
        assert 3 == len(d)
        assert d.getall("foo") == ["bar"]

    def test_extend(self, cls):
        d = cls()
        assert d == {}

        d.extend([("key", "one"), ("key", "two")], key=3, foo="bar")
        assert d != {"key": "one", "foo": "bar"}
        assert 4 == len(d)
        itms = d.items()
        # we can't guarantee order of kwargs
        assert ("key", "one") in itms
        assert ("key", "two") in itms
        assert ("key", 3) in itms
        assert ("foo", "bar") in itms

        other = cls(bar="baz")
        assert other == {"bar": "baz"}

        d.extend(other)
        assert ("bar", "baz") in d.items()

        d.extend({"foo": "moo"})
        assert ("foo", "moo") in d.items()

        d.extend()
        assert 6 == len(d)

        with pytest.raises(TypeError):
            d.extend("foo", "bar")

    def test_extend_from_proxy(self, cls, proxy_cls):
        d = cls([("a", "a"), ("b", "b")])
        proxy = proxy_cls(d)

        d2 = cls()
        d2.extend(proxy)

        assert [("a", "a"), ("b", "b")] == list(d2.items())

    def test_clear(self, cls):
        d = cls([("key", "one")], key="two", foo="bar")

        d.clear()
        assert d == {}
        assert list(d.items()) == []

    def test_del(self, cls):
        d = cls([("key", "one"), ("key", "two")], foo="bar")
        assert list(d.keys()) == ["key", "key", "foo"]

        del d["key"]
        assert d == {"foo": "bar"}
        assert list(d.items()) == [("foo", "bar")]

        with pytest.raises(KeyError, match="key"):
            del d["key"]

    def test_set_default(self, cls):
        d = cls([("key", "one"), ("key", "two")], foo="bar")
        assert "one" == d.setdefault("key", "three")
        assert "three" == d.setdefault(key="otherkey", default="three")
        assert "otherkey" in d
        assert "three" == d["otherkey"]

    def test_popitem(self, cls):
        d = cls()
        d.add("key", "val1")
        d.add("key", "val2")

        assert ("key", "val1") == d.popitem()
        assert [("key", "val2")] == list(d.items())

    def test_popitem_empty_multidict(self, cls):
        d = cls()

        with pytest.raises(KeyError):
            d.popitem()

    def test_pop(self, cls):
        d = cls()
        d.add("key", "val1")
        d.add("key", "val2")

        assert "val1" == d.pop("key")
        assert {"key": "val2"} == d

    def test_pop2(self, cls):
        d = cls()
        d.add("key", "val1")
        d.add("key2", "val2")
        d.add("key", "val3")

        assert "val1" == d.pop("key")
        assert [("key2", "val2"), ("key", "val3")] == list(d.items())

    def test_pop_default(self, cls):
        d = cls(other="val")

        assert "default" == d.pop("key", "default")
        assert "default" == d.pop(key="key", default="default")
        assert "other" in d

    def test_pop_raises(self, cls):
        d = cls(other="val")

        with pytest.raises(KeyError, match="key"):
            d.pop("key")

        assert "other" in d

    def test_replacement_order(self, cls):
        d = cls()
        d.add("key1", "val1")
        d.add("key2", "val2")
        d.add("key1", "val3")
        d.add("key2", "val4")

        d["key1"] = "val"

        expected = [("key1", "val"), ("key2", "val2"), ("key2", "val4")]

        assert expected == list(d.items())

    def test_nonstr_key(self, cls):
        d = cls()
        with pytest.raises(TypeError):
            d[1] = "val"

    def test_istr_key(self, cls, istr):
        d = cls()
        d[istr("1")] = "val"
        assert type(list(d.keys())[0]) is istr

    def test_str_derived_key(self, cls):
        class A(str):
            pass

        d = cls()
        d[A("1")] = "val"
        assert type(list(d.keys())[0]) is A

    def test_istr_key_add(self, cls, istr):
        d = cls()
        d.add(istr("1"), "val")
        assert type(list(d.keys())[0]) is istr

    def test_str_derived_key_add(self, cls):
        class A(str):
            pass

        d = cls()
        d.add(A("1"), "val")
        assert type(list(d.keys())[0]) is A

    def test_popall(self, cls):
        d = cls()
        d.add("key1", "val1")
        d.add("key2", "val2")
        d.add("key1", "val3")
        ret = d.popall("key1")
        assert ["val1", "val3"] == ret
        assert {"key2": "val2"} == d

    def test_popall_default(self, cls):
        d = cls()
        assert "val" == d.popall("key", "val")
        assert "val" == d.popall(key="key", default="val")

    def test_popall_key_error(self, cls):
        d = cls()
        with pytest.raises(KeyError, match="key"):
            d.popall("key")

    def test_large_multidict_resizing(self, cls):
        SIZE = 1024
        d = cls()
        for i in range(SIZE):
            d["key" + str(i)] = i

        for i in range(SIZE - 1):
            del d["key" + str(i)]

        assert {"key" + str(SIZE - 1): SIZE - 1} == d


class TestCIMutableMultiDict:
    @pytest.fixture
    def cls(self, _multidict):
        return _multidict.CIMultiDict

    @pytest.fixture
    def proxy_cls(self, _multidict):
        return _multidict.CIMultiDictProxy

    @pytest.fixture
    def istr(self, _multidict):
        return _multidict.istr

    def test_getall(self, cls):
        d = cls([("KEY", "value1")], KEY="value2")

        assert d != {"KEY": "value1"}
        assert len(d) == 2

        assert d.getall("key") == ["value1", "value2"]

        with pytest.raises(KeyError, match="some_key"):
            d.getall("some_key")

    def test_ctor(self, cls):
        d = cls(k1="v1")
        assert "v1" == d["K1"]
        assert ("k1", "v1") in d.items()

    def test_setitem(self, cls):
        d = cls()
        d["k1"] = "v1"
        assert "v1" == d["K1"]
        assert ("k1", "v1") in d.items()

    def test_delitem(self, cls):
        d = cls()
        d["k1"] = "v1"
        assert "K1" in d
        del d["k1"]
        assert "K1" not in d

    def test_copy(self, cls):
        d1 = cls(key="KEY", a="b")

        d2 = d1.copy()
        assert d1 == d2
        assert d1.items() == d2.items()
        assert d1 is not d2

    def test__repr__(self, cls):
        d = cls()
        assert str(d) == "<%s()>" % cls.__name__

        d = cls([("KEY", "one"), ("KEY", "two")])

        expected = "<%s('KEY': 'one', 'KEY': 'two')>" % cls.__name__
        assert str(d) == expected

    def test_add(self, cls):
        d = cls()

        assert d == {}
        d["KEY"] = "one"
        assert ("KEY", "one") in d.items()
        assert d == cls({"Key": "one"})
        assert d.getall("key") == ["one"]

        d["KEY"] = "two"
        assert ("KEY", "two") in d.items()
        assert d == cls({"Key": "two"})
        assert d.getall("key") == ["two"]

        d.add("KEY", "one")
        assert ("KEY", "one") in d.items()
        assert 2 == len(d)
        assert d.getall("key") == ["two", "one"]

        d.add("FOO", "bar")
        assert ("FOO", "bar") in d.items()
        assert 3 == len(d)
        assert d.getall("foo") == ["bar"]

        d.add(key="test", value="test")
        assert ("test", "test") in d.items()
        assert 4 == len(d)
        assert d.getall("test") == ["test"]

    def test_extend(self, cls):
        d = cls()
        assert d == {}

        d.extend([("KEY", "one"), ("key", "two")], key=3, foo="bar")
        assert 4 == len(d)
        itms = d.items()
        # we can't guarantee order of kwargs
        assert ("KEY", "one") in itms
        assert ("key", "two") in itms
        assert ("key", 3) in itms
        assert ("foo", "bar") in itms

        other = cls(Bar="baz")
        assert other == {"Bar": "baz"}

        d.extend(other)
        assert ("Bar", "baz") in d.items()
        assert "bar" in d

        d.extend({"Foo": "moo"})
        assert ("Foo", "moo") in d.items()
        assert "foo" in d

        d.extend()
        assert 6 == len(d)

        with pytest.raises(TypeError):
            d.extend("foo", "bar")

    def test_extend_from_proxy(self, cls, proxy_cls):
        d = cls([("a", "a"), ("b", "b")])
        proxy = proxy_cls(d)

        d2 = cls()
        d2.extend(proxy)

        assert [("a", "a"), ("b", "b")] == list(d2.items())

    def test_clear(self, cls):
        d = cls([("KEY", "one")], key="two", foo="bar")

        d.clear()
        assert d == {}
        assert list(d.items()) == []

    def test_del(self, cls):
        d = cls([("KEY", "one"), ("key", "two")], foo="bar")

        del d["key"]
        assert d == {"foo": "bar"}
        assert list(d.items()) == [("foo", "bar")]

        with pytest.raises(KeyError, match="key"):
            del d["key"]

    def test_set_default(self, cls):
        d = cls([("KEY", "one"), ("key", "two")], foo="bar")
        assert "one" == d.setdefault("key", "three")
        assert "three" == d.setdefault("otherkey", "three")
        assert "otherkey" in d
        assert ("otherkey", "three") in d.items()
        assert "three" == d["OTHERKEY"]

    def test_popitem(self, cls):
        d = cls()
        d.add("KEY", "val1")
        d.add("key", "val2")

        pair = d.popitem()
        assert ("KEY", "val1") == pair
        assert isinstance(pair[0], str)
        assert [("key", "val2")] == list(d.items())

    def test_popitem_empty_multidict(self, cls):
        d = cls()

        with pytest.raises(KeyError):
            d.popitem()

    def test_pop(self, cls):
        d = cls()
        d.add("KEY", "val1")
        d.add("key", "val2")

        assert "val1" == d.pop("KEY")
        assert {"key": "val2"} == d

    def test_pop_lowercase(self, cls):
        d = cls()
        d.add("KEY", "val1")
        d.add("key", "val2")

        assert "val1" == d.pop("key")
        assert {"key": "val2"} == d

    def test_pop_default(self, cls):
        d = cls(OTHER="val")

        assert "default" == d.pop("key", "default")
        assert "other" in d

    def test_pop_raises(self, cls):
        d = cls(OTHER="val")

        with pytest.raises(KeyError, match="KEY"):
            d.pop("KEY")

        assert "other" in d

    def test_extend_with_istr(self, cls, istr):
        us = istr("aBc")
        d = cls()

        d.extend([(us, "val")])
        assert [("aBc", "val")] == list(d.items())

    def test_copy_istr(self, cls, istr):
        d = cls({istr("Foo"): "bar"})
        d2 = d.copy()
        assert d == d2

    def test_eq(self, cls):
        d1 = cls(Key="val")
        d2 = cls(KEY="val")

        assert d1 == d2

    @pytest.mark.skipif(
        sys.implementation.name == "pypy",
        reason="getsizeof() is not implemented on PyPy",
    )
    def test_sizeof(self, cls):
        md = cls()
        s1 = sys.getsizeof(md)
        for i in string.ascii_lowercase:
            for j in string.ascii_uppercase:
                md[i + j] = i + j
        # multidict should be resized
        s2 = sys.getsizeof(md)
        assert s2 > s1

    @pytest.mark.skipif(
        sys.implementation.name == "pypy",
        reason="getsizeof() is not implemented on PyPy",
    )
    def test_min_sizeof(self, cls):
        md = cls()
        assert sys.getsizeof(md) < 1024

    def test_issue_620_items(self, cls):
        # https://github.com/aio-libs/multidict/issues/620
        d = cls({"a": "123, 456", "b": "789"})
        before_mutation_items = d.items()
        d["c"] = "000"
        # This causes an error on pypy.
        list(before_mutation_items)

    def test_issue_620_keys(self, cls):
        # https://github.com/aio-libs/multidict/issues/620
        d = cls({"a": "123, 456", "b": "789"})
        before_mutation_keys = d.keys()
        d["c"] = "000"
        # This causes an error on pypy.
        list(before_mutation_keys)

    def test_issue_620_values(self, cls):
        # https://github.com/aio-libs/multidict/issues/620
        d = cls({"a": "123, 456", "b": "789"})
        before_mutation_values = d.values()
        d["c"] = "000"
        # This causes an error on pypy.
        list(before_mutation_values)