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