Skip to content

Commit c1ac2ac

Browse files
jooolalukasmetzner
andcommitted
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. Co-authored-by: Lukas Metzner <[email protected]>
1 parent 198f549 commit c1ac2ac

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
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: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,25 @@ 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+
"domain, 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+
(SomeDomain(id=1, name="name1"), 1, True),
77+
(SomeDomain(id=1, name="name1"), "1", True),
78+
(SomeDomain(id=1, name="name1"), "name1", True),
79+
(SomeDomain(id=1, name="name1"), 2, False),
80+
(SomeDomain(id=1, name="name1"), "2", False),
81+
(SomeDomain(id=1, name="name1"), "name2", False),
82+
(SomeDomain(id=1, name="3"), 3, True),
83+
(SomeDomain(id=3, name="1"), "3", True),
8284
],
8385
)
84-
def test_has_id_or_name_exception(self, other, expected):
85-
domain = SomeDomain(id=1, name="name1")
86-
assert domain.has_id_or_name(other.id_or_name) == expected
86+
def test_has_id_or_name(
87+
self,
88+
domain: SomeDomain,
89+
id_or_name: str | int,
90+
expected: bool,
91+
):
92+
assert domain.has_id_or_name(id_or_name) == expected
8793

8894

8995
class ActionDomain(BaseDomain, DomainIdentityMixin):

0 commit comments

Comments
 (0)