|
1 | 1 | # Copyright (c) Microsoft Corporation. |
2 | 2 | # Licensed under the MIT License. |
3 | 3 |
|
| 4 | +import copy |
| 5 | +import pickle |
| 6 | + |
4 | 7 | import pytest |
5 | 8 | from durabletask.entities import EntityInstanceId |
6 | 9 |
|
@@ -51,3 +54,129 @@ def test_entity_id_parsing_failures(): |
51 | 54 | # Test multiple @ symbols |
52 | 55 | with pytest.raises(ValueError): |
53 | 56 | EntityInstanceId.parse("invalid@symbol@placement") |
| 57 | + |
| 58 | + |
| 59 | +def test_entity_instance_id_hash_and_equality(): |
| 60 | + id1 = EntityInstanceId("MyEntity", "instance1") |
| 61 | + id2 = EntityInstanceId("myentity", "instance1") |
| 62 | + id3 = EntityInstanceId("myentity", "instance2") |
| 63 | + id4 = EntityInstanceId("other", "instance1") |
| 64 | + |
| 65 | + assert hash(id1) == hash(id2) |
| 66 | + assert hash(id1) != hash(id3) |
| 67 | + assert hash(id1) != hash(id4) |
| 68 | + |
| 69 | + |
| 70 | +def test_entity_instance_id_in_set(): |
| 71 | + id1 = EntityInstanceId("MyEntity", "key1") |
| 72 | + id2 = EntityInstanceId("myentity", "key1") |
| 73 | + id3 = EntityInstanceId("myentity", "key2") |
| 74 | + |
| 75 | + s = {id1, id2, id3} |
| 76 | + assert len(s) == 2 |
| 77 | + assert EntityInstanceId("myentity", "key1") in s |
| 78 | + assert EntityInstanceId("myentity", "key2") in s |
| 79 | + |
| 80 | + |
| 81 | +def test_entity_instance_id_as_dict_key(): |
| 82 | + id1 = EntityInstanceId("Counter", "key1") |
| 83 | + id2 = EntityInstanceId("counter", "key1") |
| 84 | + |
| 85 | + mapping = {id1: "value1"} |
| 86 | + assert mapping[id2] == "value1" |
| 87 | + mapping[id2] = "value2" |
| 88 | + assert len(mapping) == 1 |
| 89 | + assert mapping[id1] == "value2" |
| 90 | + |
| 91 | + |
| 92 | +def test_entity_instance_id_repr(): |
| 93 | + eid = EntityInstanceId("MyEntity", "InstanceKey") |
| 94 | + expected = "EntityInstanceId(entity='myentity', key='InstanceKey')" |
| 95 | + assert repr(eid) == expected |
| 96 | + |
| 97 | + |
| 98 | +def test_entity_instance_id_immutable(): |
| 99 | + """Verify that entity and key cannot be reassigned or deleted after construction, |
| 100 | + ensuring hash stability when instances are stored in sets or dicts.""" |
| 101 | + eid = EntityInstanceId("Counter", "key1") |
| 102 | + |
| 103 | + with pytest.raises(AttributeError): |
| 104 | + eid.entity = "other" |
| 105 | + |
| 106 | + with pytest.raises(AttributeError): |
| 107 | + eid.key = "other_key" |
| 108 | + |
| 109 | + with pytest.raises(AttributeError): |
| 110 | + eid.custom_attr = "value" |
| 111 | + |
| 112 | + with pytest.raises(AttributeError): |
| 113 | + del eid.entity |
| 114 | + |
| 115 | + with pytest.raises(AttributeError): |
| 116 | + del eid.key |
| 117 | + |
| 118 | + # Verify the original values are unchanged |
| 119 | + assert eid.entity == "counter" |
| 120 | + assert eid.key == "key1" |
| 121 | + |
| 122 | + |
| 123 | +def test_entity_instance_id_immutable_dict_stability(): |
| 124 | + """Verify that immutability preserves dict/set lookup integrity.""" |
| 125 | + eid = EntityInstanceId("Counter", "key1") |
| 126 | + mapping = {eid: "value"} |
| 127 | + s = {eid} |
| 128 | + |
| 129 | + # Attempt mutation - should be rejected |
| 130 | + with pytest.raises(AttributeError): |
| 131 | + eid.entity = "other" |
| 132 | + |
| 133 | + # Lookups with an equal instance must still succeed |
| 134 | + lookup = EntityInstanceId("counter", "key1") |
| 135 | + assert mapping[lookup] == "value" |
| 136 | + assert lookup in s |
| 137 | + |
| 138 | + |
| 139 | +def test_entity_instance_id_copy_and_pickle(): |
| 140 | + """Verify that immutable EntityInstanceId instances can be copied and pickled.""" |
| 141 | + eid = EntityInstanceId("Counter", "key1") |
| 142 | + |
| 143 | + # Copying should preserve identity / immutability |
| 144 | + assert copy.copy(eid) is eid |
| 145 | + assert copy.deepcopy(eid) is eid |
| 146 | + |
| 147 | + # Pickling should preserve equality and hash |
| 148 | + pickled = pickle.loads(pickle.dumps(eid)) |
| 149 | + assert pickled == eid |
| 150 | + assert hash(pickled) == hash(eid) |
| 151 | + assert pickled.entity == "counter" |
| 152 | + assert pickled.key == "key1" |
| 153 | + |
| 154 | + |
| 155 | +class _SubclassEntityId(EntityInstanceId): |
| 156 | + @property |
| 157 | + def custom_name(self) -> str: |
| 158 | + return self.entity.upper() |
| 159 | + |
| 160 | + |
| 161 | +def test_entity_instance_id_subclass_pickle(): |
| 162 | + """Verify that unpickling a subclass of EntityInstanceId preserves the concrete type.""" |
| 163 | + custom = _SubclassEntityId("Counter", "key1") |
| 164 | + assert isinstance(custom, _SubclassEntityId) |
| 165 | + assert custom.custom_name == "COUNTER" |
| 166 | + |
| 167 | + restored = pickle.loads(pickle.dumps(custom)) |
| 168 | + assert type(restored) is _SubclassEntityId |
| 169 | + assert isinstance(restored, _SubclassEntityId) |
| 170 | + assert restored.custom_name == "COUNTER" |
| 171 | + assert restored.entity == "counter" |
| 172 | + assert restored.key == "key1" |
| 173 | + assert restored == custom |
| 174 | + assert hash(restored) == hash(custom) |
| 175 | + |
| 176 | + # Immutability guarantees remain intact after unpickling |
| 177 | + with pytest.raises(AttributeError): |
| 178 | + restored.entity = "other" |
| 179 | + with pytest.raises(AttributeError): |
| 180 | + restored.key = "other" |
| 181 | + with pytest.raises(AttributeError): |
| 182 | + del restored.entity |
0 commit comments