File: test_frozen_dict.py

package info (click to toggle)
graphql-core 3.2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,384 kB
  • sloc: python: 45,812; makefile: 26; sh: 13
file content (96 lines) | stat: -rw-r--r-- 2,854 bytes parent folder | download | duplicates (2)
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
from copy import copy, deepcopy

from graphql.pyutils import FrozenDict, FrozenError
from pytest import raises


def describe_frozen_list():
    def can_read():
        fd = FrozenDict({1: 2, 3: 4})
        assert fd == {1: 2, 3: 4}
        assert list(i for i in fd) == [1, 3]
        assert fd.copy() == fd
        assert 3 in fd
        assert 2 not in fd
        assert fd[1] == 2
        with raises(KeyError):
            # noinspection PyStatementEffect
            fd[2]
        assert len(fd) == 2
        assert fd.get(1) == 2
        assert fd.get(2, 5) == 5
        assert list(fd.items()) == [(1, 2), (3, 4)]
        assert list(fd.keys()) == [1, 3]
        assert list(fd.values()) == [2, 4]

    def cannot_write():
        fd = FrozenDict({1: 2, 3: 4})
        with raises(FrozenError):
            fd[1] = 2
        with raises(FrozenError):
            fd[4] = 5
        with raises(FrozenError):
            del fd[1]
        with raises(FrozenError):
            del fd[3]
        with raises(FrozenError):
            fd.clear()
        with raises(FrozenError):
            fd.pop(1)
        with raises(FrozenError):
            fd.pop(4, 5)
        with raises(FrozenError):
            fd.popitem()
        with raises(FrozenError):
            fd.setdefault(1, 2)
        with raises(FrozenError):
            fd.setdefault(4, 5)
        with raises(FrozenError):
            fd.update({1: 2})
        with raises(FrozenError):
            fd.update({4: 5})
        with raises(FrozenError):
            fd += {4: 5}
        assert fd == {1: 2, 3: 4}

    def can_hash():
        fd1 = FrozenDict({1: 2, 3: 4})
        fd2 = FrozenDict({1: 2, 3: 4})
        assert fd2 == fd1
        assert fd2 is not fd1
        assert hash(fd2) == hash(fd1)
        fd3 = FrozenDict({1: 2, 3: 5})
        assert fd3 != fd1
        assert hash(fd3) != hash(fd1)

    def can_copy():
        fd1 = FrozenDict({1: 2, 3: 4})
        fd2 = fd1.copy()
        assert isinstance(fd2, FrozenDict)
        assert fd2 == fd1
        assert hash(fd2) == hash(fd1)
        assert fd2 is not fd1
        fd3 = copy(fd1)
        assert isinstance(fd3, FrozenDict)
        assert fd3 == fd1
        assert hash(fd3) == hash(fd1)
        assert fd3 is not fd1

    def can_deep_copy():
        fd11 = FrozenDict({1: 2, 3: 4})
        fd12 = FrozenDict({2: 1, 4: 3})
        fd1 = FrozenDict({1: fd11, 2: fd12})
        assert fd1[1] is fd11
        assert fd1[2] is fd12
        fd2 = deepcopy(fd1)
        assert isinstance(fd2, FrozenDict)
        assert fd2 == fd1
        assert hash(fd2) == hash(fd1)
        fd21 = fd2[1]
        fd22 = fd2[2]
        assert isinstance(fd21, FrozenDict)
        assert isinstance(fd22, FrozenDict)
        assert fd21 == fd11
        assert fd21 is not fd11
        assert fd22 == fd12
        assert fd22 is not fd12