From cec9b6790815c935fecb2a7bf292c50ad37b3739 Mon Sep 17 00:00:00 2001 From: jo Date: Wed, 19 Jul 2023 09:40:49 +0200 Subject: [PATCH] feat: add __repr__ method to domains --- hcloud/core/domain.py | 4 ++++ tests/unit/core/test_domain.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/hcloud/core/domain.py b/hcloud/core/domain.py index 8d99f63..c4d3308 100644 --- a/hcloud/core/domain.py +++ b/hcloud/core/domain.py @@ -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__ = () diff --git a/tests/unit/core/test_domain.py b/tests/unit/core/test_domain.py index 9e71cee..57cb790 100644 --- a/tests/unit/core/test_domain.py +++ b/tests/unit/core/test_domain.py @@ -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", @@ -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