Skip to content

Commit

Permalink
feat: add __repr__ method to domains (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Jul 19, 2023
1 parent 2ce71e9 commit 4c22765
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions hcloud/core/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def from_dict(cls, data):
supported_data = {k: v for k, v in data.items() if k in cls.__slots__}
return cls(**supported_data)

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


class DomainIdentityMixin:
__slots__ = ()
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/core/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ def __init__(self, id, name="name1", started=None):
self.started = isoparse(started) if started else None


class SomeOtherDomain(BaseDomain):
__slots__ = ("id", "name", "child")

def __init__(self, id=None, name=None, child=None):
self.id = id
self.name = name
self.child = child


class TestBaseDomain:
@pytest.mark.parametrize(
"data_dict,expected_result",
Expand Down Expand Up @@ -134,3 +143,23 @@ def test_from_dict_ok(self, data_dict, expected_result):
model = ActionDomain.from_dict(data_dict)
for k, v in expected_result.items():
assert getattr(model, k) == v

@pytest.mark.parametrize(
"data,expected",
[
(
SomeOtherDomain(id=1, name="name1"),
"SomeOtherDomain(id=1, name='name1', child=None)",
),
(
SomeOtherDomain(
id=2,
name="name2",
child=SomeOtherDomain(id=3, name="name3"),
),
"SomeOtherDomain(id=2, name='name2', child=SomeOtherDomain(id=3, name='name3', child=None))",
),
],
)
def test_repr_ok(self, data, expected):
assert data.__repr__() == expected

0 comments on commit 4c22765

Please sign in to comment.