Skip to content

Commit 0411e26

Browse files
committed
Add equality checks to Domains
This allows easier introspection of API responses. Very useful for unit-testing behaviour of wrappers of hcloud.
1 parent 125a2ba commit 0411e26

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

hcloud/core/domain.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from typing import Any
4+
35

46
class BaseDomain:
57
__api_properties__: tuple
@@ -16,6 +18,15 @@ def __repr__(self) -> str:
1618
kwargs = [f"{key}={getattr(self, key)!r}" for key in self.__api_properties__] # type: ignore[var-annotated]
1719
return f"{self.__class__.__qualname__}({', '.join(kwargs)})"
1820

21+
def __eq__(self, other: Any) -> bool:
22+
"""Compare a domain object with another of the same type."""
23+
if not isinstance(other, self.__class__):
24+
return NotImplemented
25+
for key in self.__api_properties__:
26+
if getattr(self, key) != getattr(other, key):
27+
return False
28+
return True
29+
1930

2031
class DomainIdentityMixin:
2132

tests/unit/core/test_domain.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,10 @@ def test_from_dict_ok(self, data_dict, expected_result):
156156
)
157157
def test_repr_ok(self, data, expected):
158158
assert data.__repr__() == expected
159+
160+
def test__eq__(self):
161+
a1 = ActionDomain(id=1, name="action")
162+
assert a1 == ActionDomain(id=1, name="action")
163+
assert a1 != ActionDomain(id=2, name="action")
164+
assert a1 != ActionDomain(id=1, name="something")
165+
assert a1 != SomeOtherDomain(id=1, name="action")

0 commit comments

Comments
 (0)