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
« prev ^ index » next coverage.py v7.6.1, created at 2024-10-09 01:48 -0600
1from __future__ import annotations
3import pytest
5from muutils.misc import freeze
8def test_freeze_basic_types():
9 freeze(True)
10 freeze(123)
11 freeze(45.67)
12 freeze("hello")
13 freeze(b"bytes")
15 assert True # No exceptions should be raised
18def test_freeze_list():
19 lst = [1, 2, 3]
20 lst = freeze(lst)
22 with pytest.raises(AttributeError):
23 lst[0] = 4
25 with pytest.raises(AttributeError):
26 lst.append(4)
28 with pytest.raises(AttributeError):
29 lst.extend([4, 5])
31 with pytest.raises(AttributeError):
32 lst.pop()
34 with pytest.raises(AttributeError):
35 lst.clear()
38def test_freeze_tuple():
39 tpl = (1, 2, 3)
40 frozen_tpl = freeze(tpl)
42 assert frozen_tpl == (1, 2, 3)
43 assert isinstance(frozen_tpl, tuple)
46def test_freeze_set():
47 st = {1, 2, 3}
48 frozen_st = freeze(st)
50 assert frozen_st == frozenset({1, 2, 3})
51 assert isinstance(frozen_st, frozenset)
54def test_freeze_dict():
55 dct = {"key1": 1, "key2": 2}
56 dct = freeze(dct)
58 with pytest.raises(AttributeError):
59 dct["key1"] = 3
61 with pytest.raises(AttributeError):
62 del dct["key2"]
65def test_freeze_nested_structures():
66 nested = {"key1": [1, 2, 3], "key2": {"subkey": 4}}
67 freeze(nested)
69 with pytest.raises(AttributeError):
70 nested["key1"][0] = 4
72 with pytest.raises(AttributeError):
73 nested["key2"]["subkey"] = 5
76def test_freeze_custom_class():
77 class CustomClass:
78 def __init__(self, value):
79 self.value = value
81 obj = CustomClass(10)
82 freeze(obj)
84 with pytest.raises(AttributeError):
85 obj.value = 20
88class CustomClass:
89 def __init__(self, value):
90 self.value = value
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})
101 obj = NestedClass()
102 obj = freeze(obj)
104 with pytest.raises(AttributeError):
105 obj.lst[0] = 10
107 with pytest.raises(AttributeError):
108 obj.lst[2]["key"] = 30
110 with pytest.raises(AttributeError):
111 obj.lst[3] = (40, 50)
113 with pytest.raises(AttributeError):
114 obj.dct["key1"] = {100, 200}
116 with pytest.raises(AttributeError):
117 obj.dct["key2"][1] = 70
119 with pytest.raises(AttributeError):
120 obj.st.add((13, 14))
122 with pytest.raises(AttributeError):
123 obj.tpl[1][0] = 140
125 with pytest.raises(AttributeError):
126 obj.tpl[2]["key3"] = 160
129def test_freeze_lists_with_classes_and_nested_structures():
130 lst = [CustomClass(1), [2, 3], {"key": (4, 5)}]
131 freeze(lst)
133 with pytest.raises(AttributeError):
134 lst[0].value = 10
136 with pytest.raises(AttributeError):
137 lst[1][1] = 30
139 with pytest.raises(AttributeError):
140 lst[2]["key"] = (40, 50)
143def test_freeze_dicts_with_classes_and_nested_structures():
144 dct = {"class": CustomClass(6), "lst": [7, 8], "set": {9, (10, 11)}}
145 freeze(dct)
147 with pytest.raises(AttributeError):
148 dct["class"].value = 60
150 with pytest.raises(AttributeError):
151 dct["lst"][0] = 70
153 with pytest.raises(AttributeError):
154 dct["set"].add(12)
157def test_freeze_sets_with_classes_and_nested_structures():
158 st = {CustomClass(1), frozenset({2, 3}), (4, 5)}
159 freeze(st)
161 for item in st:
162 if isinstance(item, CustomClass):
163 with pytest.raises(AttributeError):
164 item.value = 10
167def test_freeze_tuples_with_classes_and_nested_structures():
168 tpl = (CustomClass(1), [2, 3], {"key": 4})
169 frozen_tpl = freeze(tpl)
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