Skip to content

Commit 04daf2d

Browse files
committed
Implement __eq__ on BoundModelBase
1 parent 0411e26 commit 04daf2d

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

hcloud/core/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,9 @@ def __repr__(self) -> str:
9696
# models, as they will generate a lot of API call trying to print all the fields
9797
# of the model.
9898
return object.__repr__(self)
99+
100+
def __eq__(self, other: Any) -> bool:
101+
"""Compare a bound model object with another of the same type."""
102+
if not isinstance(other, self.__class__):
103+
return NotImplemented
104+
return self.data_model == other.data_model

tests/unit/core/test_client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ def test_get_non_exists_model_attribute_incomplete_model(
8181
client.get_by_id.assert_not_called()
8282
assert bound_model.complete is False
8383

84+
def test_equality(self, bound_model_class, client):
85+
data = {"id": 1, "name": "name", "description": "my_description"}
86+
bound_model_a = bound_model_class(client=client, data=data)
87+
bound_model_b = bound_model_class(client=client, data=data)
88+
89+
assert bound_model_a == bound_model_b
90+
bound_model_b.data_model.name = "changed_name"
91+
assert bound_model_a != bound_model_b
92+
8493

8594
class TestClientEntityBase:
8695
@pytest.fixture()

0 commit comments

Comments
 (0)