Skip to content

Commit 2226a66

Browse files
committed
Implement __eq__ on BoundModelBase
1 parent 0411e26 commit 2226a66

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ 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+
# Comparing a bound model with a base domain
90+
assert bound_model_a == bound_model_a.data_model
91+
92+
# Identical bound models
93+
assert bound_model_a == bound_model_b
94+
assert bound_model_a == bound_model_b.data_model
95+
96+
# Differing bound models
97+
bound_model_b.data_model.name = "changed_name"
98+
assert bound_model_a != bound_model_b
99+
assert bound_model_a != bound_model_b.data_model
100+
84101

85102
class TestClientEntityBase:
86103
@pytest.fixture()

0 commit comments

Comments
 (0)