Coverage for tests\unit\misc\test_freeze.py: 100%

121 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-09 01:48 -0600

1from __future__ import annotations 

2 

3import pytest 

4 

5from muutils.misc import freeze 

6 

7 

8def test_freeze_basic_types(): 

9 freeze(True) 

10 freeze(123) 

11 freeze(45.67) 

12 freeze("hello") 

13 freeze(b"bytes") 

14 

15 assert True # No exceptions should be raised 

16 

17 

18def test_freeze_list(): 

19 lst = [1, 2, 3] 

20 lst = freeze(lst) 

21 

22 with pytest.raises(AttributeError): 

23 lst[0] = 4 

24 

25 with pytest.raises(AttributeError): 

26 lst.append(4) 

27 

28 with pytest.raises(AttributeError): 

29 lst.extend([4, 5]) 

30 

31 with pytest.raises(AttributeError): 

32 lst.pop() 

33 

34 with pytest.raises(AttributeError): 

35 lst.clear() 

36 

37 

38def test_freeze_tuple(): 

39 tpl = (1, 2, 3) 

40 frozen_tpl = freeze(tpl) 

41 

42 assert frozen_tpl == (1, 2, 3) 

43 assert isinstance(frozen_tpl, tuple) 

44 

45 

46def test_freeze_set(): 

47 st = {1, 2, 3} 

48 frozen_st = freeze(st) 

49 

50 assert frozen_st == frozenset({1, 2, 3}) 

51 assert isinstance(frozen_st, frozenset) 

52 

53 

54def test_freeze_dict(): 

55 dct = {"key1": 1, "key2": 2} 

56 dct = freeze(dct) 

57 

58 with pytest.raises(AttributeError): 

59 dct["key1"] = 3 

60 

61 with pytest.raises(AttributeError): 

62 del dct["key2"] 

63 

64 

65def test_freeze_nested_structures(): 

66 nested = {"key1": [1, 2, 3], "key2": {"subkey": 4}} 

67 freeze(nested) 

68 

69 with pytest.raises(AttributeError): 

70 nested["key1"][0] = 4 

71 

72 with pytest.raises(AttributeError): 

73 nested["key2"]["subkey"] = 5 

74 

75 

76def test_freeze_custom_class(): 

77 class CustomClass: 

78 def __init__(self, value): 

79 self.value = value 

80 

81 obj = CustomClass(10) 

82 freeze(obj) 

83 

84 with pytest.raises(AttributeError): 

85 obj.value = 20 

86 

87 

88class CustomClass: 

89 def __init__(self, value): 

90 self.value = value 

91 

92 

93def test_freeze_class_with_nested_structures(): 

94 class NestedClass: 

95 def __init__(self): 

96 self.lst = [1, 2, {"key": 3}, (4, 5)] 

97 self.dct = {"key1": {1, 2, 3}, "key2": [6, 7, 8]} 

98 self.st = {frozenset((9, 10)), (11, 12)} 

99 self.tpl = (CustomClass(13), [14, 15], {"key3": 16}) 

100 

101 obj = NestedClass() 

102 obj = freeze(obj) 

103 

104 with pytest.raises(AttributeError): 

105 obj.lst[0] = 10 

106 

107 with pytest.raises(AttributeError): 

108 obj.lst[2]["key"] = 30 

109 

110 with pytest.raises(AttributeError): 

111 obj.lst[3] = (40, 50) 

112 

113 with pytest.raises(AttributeError): 

114 obj.dct["key1"] = {100, 200} 

115 

116 with pytest.raises(AttributeError): 

117 obj.dct["key2"][1] = 70 

118 

119 with pytest.raises(AttributeError): 

120 obj.st.add((13, 14)) 

121 

122 with pytest.raises(AttributeError): 

123 obj.tpl[1][0] = 140 

124 

125 with pytest.raises(AttributeError): 

126 obj.tpl[2]["key3"] = 160 

127 

128 

129def test_freeze_lists_with_classes_and_nested_structures(): 

130 lst = [CustomClass(1), [2, 3], {"key": (4, 5)}] 

131 freeze(lst) 

132 

133 with pytest.raises(AttributeError): 

134 lst[0].value = 10 

135 

136 with pytest.raises(AttributeError): 

137 lst[1][1] = 30 

138 

139 with pytest.raises(AttributeError): 

140 lst[2]["key"] = (40, 50) 

141 

142 

143def test_freeze_dicts_with_classes_and_nested_structures(): 

144 dct = {"class": CustomClass(6), "lst": [7, 8], "set": {9, (10, 11)}} 

145 freeze(dct) 

146 

147 with pytest.raises(AttributeError): 

148 dct["class"].value = 60 

149 

150 with pytest.raises(AttributeError): 

151 dct["lst"][0] = 70 

152 

153 with pytest.raises(AttributeError): 

154 dct["set"].add(12) 

155 

156 

157def test_freeze_sets_with_classes_and_nested_structures(): 

158 st = {CustomClass(1), frozenset({2, 3}), (4, 5)} 

159 freeze(st) 

160 

161 for item in st: 

162 if isinstance(item, CustomClass): 

163 with pytest.raises(AttributeError): 

164 item.value = 10 

165 

166 

167def test_freeze_tuples_with_classes_and_nested_structures(): 

168 tpl = (CustomClass(1), [2, 3], {"key": 4}) 

169 frozen_tpl = freeze(tpl) 

170 

171 for item in frozen_tpl: 

172 if isinstance(item, CustomClass): 

173 with pytest.raises(AttributeError): 

174 item.value = 10 

175 elif isinstance(item, list): 

176 with pytest.raises(AttributeError): 

177 item[0] = 20 

178 elif isinstance(item, dict): 

179 with pytest.raises(AttributeError): 

180 item["key"] = 40