Skip to content

Commit 4c22765

Browse files
authored
feat: add __repr__ method to domains (#246)
1 parent 2ce71e9 commit 4c22765

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

hcloud/core/domain.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def from_dict(cls, data):
99
supported_data = {k: v for k, v in data.items() if k in cls.__slots__}
1010
return cls(**supported_data)
1111

12+
def __repr__(self) -> str:
13+
kwargs = [f"{key}={getattr(self, key)!r}" for key in self.__slots__]
14+
return f"{self.__class__.__qualname__}({', '.join(kwargs)})"
15+
1216

1317
class DomainIdentityMixin:
1418
__slots__ = ()

tests/unit/core/test_domain.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ def __init__(self, id, name="name1", started=None):
104104
self.started = isoparse(started) if started else None
105105

106106

107+
class SomeOtherDomain(BaseDomain):
108+
__slots__ = ("id", "name", "child")
109+
110+
def __init__(self, id=None, name=None, child=None):
111+
self.id = id
112+
self.name = name
113+
self.child = child
114+
115+
107116
class TestBaseDomain:
108117
@pytest.mark.parametrize(
109118
"data_dict,expected_result",
@@ -134,3 +143,23 @@ def test_from_dict_ok(self, data_dict, expected_result):
134143
model = ActionDomain.from_dict(data_dict)
135144
for k, v in expected_result.items():
136145
assert getattr(model, k) == v
146+
147+
@pytest.mark.parametrize(
148+
"data,expected",
149+
[
150+
(
151+
SomeOtherDomain(id=1, name="name1"),
152+
"SomeOtherDomain(id=1, name='name1', child=None)",
153+
),
154+
(
155+
SomeOtherDomain(
156+
id=2,
157+
name="name2",
158+
child=SomeOtherDomain(id=3, name="name3"),
159+
),
160+
"SomeOtherDomain(id=2, name='name2', child=SomeOtherDomain(id=3, name='name3', child=None))",
161+
),
162+
],
163+
)
164+
def test_repr_ok(self, data, expected):
165+
assert data.__repr__() == expected

0 commit comments

Comments
 (0)