Debian Codegraph @

tests/pyutils/test_frozen_dict.py

raw
1 from copy import copy, deepcopy
2
3 from graphql.pyutils import FrozenDict, FrozenError
4 from pytest import raises
5
6
7 def describe_frozen_list():
8 def can_read():
9 fd = FrozenDict({1: 2, 3: 4})
10 assert fd == {1: 2, 3: 4}
11 assert list(i for i in fd) == [1, 3]
12 assert fd.copy() == fd
13 assert 3 in fd
14 assert 2 not in fd
15 assert fd[1] == 2
16 with raises(KeyError):
17 # noinspection PyStatementEffect
18 fd[2]
19 assert len(fd) == 2
20 assert fd.get(1) == 2
21 assert fd.get(2, 5) == 5
22 assert list(fd.items()) == [(1, 2), (3, 4)]
23 assert list(fd.keys()) == [1, 3]
24 assert list(fd.values()) == [2, 4]
25
26 def cannot_write():
27 fd = FrozenDict({1: 2, 3: 4})
28 with raises(FrozenError):
29 fd[1] = 2
30 with raises(FrozenError):
31 fd[4] = 5
32 with raises(FrozenError):
33 del fd[1]
34 with raises(FrozenError):
35 del fd[3]
36 with raises(FrozenError):
37 fd.clear()
38 with raises(FrozenError):
39 fd.pop(1)
40 with raises(FrozenError):
41 fd.pop(4, 5)
42 with raises(FrozenError):
43 fd.popitem()
44 with raises(FrozenError):
45 fd.setdefault(1, 2)
46 with raises(FrozenError):
47 fd.setdefault(4, 5)
48 with raises(FrozenError):
49 fd.update({1: 2})
50 with raises(FrozenError):
51 fd.update({4: 5})
52 with raises(FrozenError):
53 fd += {4: 5}
54 assert fd == {1: 2, 3: 4}
55
56 def can_hash():
57 fd1 = FrozenDict({1: 2, 3: 4})
58 fd2 = FrozenDict({1: 2, 3: 4})
59 assert fd2 == fd1
60 assert fd2 is not fd1
61 assert hash(fd2) == hash(fd1)
62 fd3 = FrozenDict({1: 2, 3: 5})
63 assert fd3 != fd1
64 assert hash(fd3) != hash(fd1)
65
66 def can_copy():
67 fd1 = FrozenDict({1: 2, 3: 4})
68 fd2 = fd1.copy()
69 assert isinstance(fd2, FrozenDict)
70 assert fd2 == fd1
71 assert hash(fd2) == hash(fd1)
72 assert fd2 is not fd1
73 fd3 = copy(fd1)
74 assert isinstance(fd3, FrozenDict)
75 assert fd3 == fd1
76 assert hash(fd3) == hash(fd1)
77 assert fd3 is not fd1
78
79 def can_deep_copy():
80 fd11 = FrozenDict({1: 2, 3: 4})
81 fd12 = FrozenDict({2: 1, 4: 3})
82 fd1 = FrozenDict({1: fd11, 2: fd12})
83 assert fd1[1] is fd11
84 assert fd1[2] is fd12
85 fd2 = deepcopy(fd1)
86 assert isinstance(fd2, FrozenDict)
87 assert fd2 == fd1
88 assert hash(fd2) == hash(fd1)
89 fd21 = fd2[1]
90 fd22 = fd2[2]
91 assert isinstance(fd21, FrozenDict)
92 assert isinstance(fd22, FrozenDict)
93 assert fd21 == fd11
94 assert fd21 is not fd11
95 assert fd22 == fd12
96 assert fd22 is not fd12