Skip to content

Commit 2e59d20

Browse files
committed
fix: handle string id when checking has_id_or_name
The function did not handle the case of string ids (e.g. "2345"), we now handle this edge case.
1 parent 198f549 commit 2e59d20

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

hcloud/core/domain.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,22 @@ def has_id_or_name(self, id_or_name: int | str) -> bool:
5252
the comparison will not work as expected (e.g. the domains are the same but
5353
cannot be equal, if one provides an id and the other the name).
5454
"""
55-
values: list[int | str] = []
55+
result = None
56+
5657
if self.id is not None:
57-
values.append(self.id)
58+
value = id_or_name
59+
if isinstance(id_or_name, str) and id_or_name.isnumeric():
60+
value = int(id_or_name)
61+
62+
result = result or self.id == value
63+
5864
if self.name is not None:
59-
values.append(self.name)
60-
if not values:
65+
result = result or self.name == str(id_or_name)
66+
67+
if result is None:
6168
raise ValueError("id or name must be set")
6269

63-
return id_or_name in values
70+
return result
6471

6572

6673
class Pagination(BaseDomain):

tests/unit/core/test_domain.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,19 @@ def test_id_or_name_exception(self):
7171
assert str(error) == "id or name must be set"
7272

7373
@pytest.mark.parametrize(
74-
"other, expected",
74+
"id_or_name, expected",
7575
[
76-
(SomeDomain(id=1), True),
77-
(SomeDomain(name="name1"), True),
78-
(SomeDomain(id=1, name="name1"), True),
79-
(SomeDomain(id=2), False),
80-
(SomeDomain(name="name2"), False),
81-
(SomeDomain(id=2, name="name2"), False),
76+
(1, True),
77+
("1", True),
78+
("name1", True),
79+
(2, False),
80+
("2", False),
81+
("name2", False),
8282
],
8383
)
84-
def test_has_id_or_name_exception(self, other, expected):
84+
def test_has_id_or_name(self, id_or_name, expected):
8585
domain = SomeDomain(id=1, name="name1")
86-
assert domain.has_id_or_name(other.id_or_name) == expected
86+
assert domain.has_id_or_name(id_or_name) == expected
8787

8888

8989
class ActionDomain(BaseDomain, DomainIdentityMixin):

0 commit comments

Comments
 (0)