Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add __repr__ to domain classes #246

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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