Skip to content

Commit 95940a2

Browse files
committed
test: use BoundModelTestCase for zones api
1 parent ede12f1 commit 95940a2

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

hcloud/zones/client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def update_rrset(
352352
DNS API is in beta, breaking changes may occur within minor releases.
353353
See https://docs.hetzner.cloud/changelog#2025-10-07-dns-beta for more details.
354354
"""
355-
return self._client.update_rrset(rrset, labels=labels)
355+
return self._client.update_rrset(rrset=rrset, labels=labels)
356356

357357
def delete_rrset(
358358
self,
@@ -369,7 +369,7 @@ def delete_rrset(
369369
DNS API is in beta, breaking changes may occur within minor releases.
370370
See https://docs.hetzner.cloud/changelog#2025-10-07-dns-beta for more details.
371371
"""
372-
return self._client.delete_rrset(rrset)
372+
return self._client.delete_rrset(rrset=rrset)
373373

374374
def change_rrset_protection(
375375
self,
@@ -389,7 +389,7 @@ def change_rrset_protection(
389389
DNS API is in beta, breaking changes may occur within minor releases.
390390
See https://docs.hetzner.cloud/changelog#2025-10-07-dns-beta for more details.
391391
"""
392-
return self._client.change_rrset_protection(rrset, change=change)
392+
return self._client.change_rrset_protection(rrset=rrset, change=change)
393393

394394
def change_rrset_ttl(
395395
self,
@@ -408,7 +408,7 @@ def change_rrset_ttl(
408408
DNS API is in beta, breaking changes may occur within minor releases.
409409
See https://docs.hetzner.cloud/changelog#2025-10-07-dns-beta for more details.
410410
"""
411-
return self._client.change_rrset_ttl(rrset, ttl=ttl)
411+
return self._client.change_rrset_ttl(rrset=rrset, ttl=ttl)
412412

413413
def add_rrset_records(
414414
self,
@@ -429,7 +429,7 @@ def add_rrset_records(
429429
DNS API is in beta, breaking changes may occur within minor releases.
430430
See https://docs.hetzner.cloud/changelog#2025-10-07-dns-beta for more details.
431431
"""
432-
return self._client.add_rrset_records(rrset, records=records, ttl=ttl)
432+
return self._client.add_rrset_records(rrset=rrset, records=records, ttl=ttl)
433433

434434
def remove_rrset_records(
435435
self,
@@ -448,7 +448,7 @@ def remove_rrset_records(
448448
DNS API is in beta, breaking changes may occur within minor releases.
449449
See https://docs.hetzner.cloud/changelog#2025-10-07-dns-beta for more details.
450450
"""
451-
return self._client.remove_rrset_records(rrset, records=records)
451+
return self._client.remove_rrset_records(rrset=rrset, records=records)
452452

453453
def set_rrset_records(
454454
self,
@@ -467,7 +467,7 @@ def set_rrset_records(
467467
DNS API is in beta, breaking changes may occur within minor releases.
468468
See https://docs.hetzner.cloud/changelog#2025-10-07-dns-beta for more details.
469469
"""
470-
return self._client.set_rrset_records(rrset, records=records)
470+
return self._client.set_rrset_records(rrset=rrset, records=records)
471471

472472

473473
class BoundZoneRRSet(BoundModelBase, ZoneRRSet):

tests/unit/conftest.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
import inspect
6-
from typing import Callable, ClassVar
6+
from typing import Callable, ClassVar, TypedDict
77
from unittest import mock
88

99
import pytest
@@ -166,13 +166,20 @@ def pytest_generate_tests(metafunc: pytest.Metafunc):
166166
metafunc.parametrize("bound_model_method", metafunc.cls.methods)
167167

168168

169+
class BoundModelTestOptions(TypedDict):
170+
sub_resource: bool
171+
172+
169173
class BoundModelTestCase:
170-
methods: ClassVar[list[Callable]]
174+
methods: ClassVar[list[Callable | tuple[Callable, BoundModelTestOptions]]]
171175

172176
def test_method_list(self, bound_model):
173177
"""
174178
Ensure the list of bound model methods is up to date.
175179
"""
180+
# Unpack methods
181+
methods = [m[0] if isinstance(m, tuple) else m for m in self.__class__.methods]
182+
176183
members_count = 0
177184
members_missing = []
178185
for name, member in inspect.getmembers(
@@ -184,7 +191,7 @@ def test_method_list(self, bound_model):
184191
if name in ("__init__", "get_actions", "get_actions_list"):
185192
continue
186193

187-
if member.__func__ in self.__class__.methods:
194+
if member.__func__ in methods:
188195
members_count += 1
189196
else:
190197
members_missing.append(member.__func__.__qualname__)
@@ -196,8 +203,12 @@ def test_method(
196203
self,
197204
resource_client,
198205
bound_model,
199-
bound_model_method: Callable,
206+
bound_model_method: Callable | tuple[Callable, BoundModelTestOptions],
200207
):
208+
options = BoundModelTestOptions()
209+
if isinstance(bound_model_method, tuple):
210+
bound_model_method, options = bound_model_method
211+
201212
# Check if the resource client has a method named after the bound model method.
202213
assert hasattr(resource_client, bound_model_method.__name__)
203214

@@ -214,6 +225,9 @@ def test_method(
214225
# Call the bound model method
215226
result = getattr(bound_model, bound_model_method.__name__)(**kwargs)
216227

217-
resource_client_method_mock.assert_called_with(bound_model, **kwargs)
228+
if options.get("sub_resource"):
229+
resource_client_method_mock.assert_called_with(**kwargs)
230+
else:
231+
resource_client_method_mock.assert_called_with(bound_model, **kwargs)
218232

219233
assert result is resource_client_method_mock.return_value

tests/unit/zones/test_client.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
ZonesClient,
2020
)
2121

22-
from ..conftest import assert_bound_action1
22+
from ..conftest import BoundModelTestCase, assert_bound_action1
2323

2424

2525
def assert_bound_zone1(o: BoundZone, client: ZonesClient):
@@ -847,13 +847,35 @@ def test_set_rrset_records(
847847
assert_bound_action1(action, resource_client._parent.actions)
848848

849849

850-
class TestBoundZone:
850+
class TestBoundZone(BoundModelTestCase):
851+
methods = [
852+
BoundZone.update,
853+
BoundZone.delete,
854+
BoundZone.import_zonefile,
855+
BoundZone.export_zonefile,
856+
BoundZone.change_primary_nameservers,
857+
BoundZone.change_ttl,
858+
BoundZone.change_protection,
859+
BoundZone.get_rrset_all,
860+
BoundZone.get_rrset_list,
861+
BoundZone.get_rrset,
862+
BoundZone.create_rrset,
863+
# With rrset sub resource
864+
(BoundZone.update_rrset, {"sub_resource": True}),
865+
(BoundZone.delete_rrset, {"sub_resource": True}),
866+
(BoundZone.change_rrset_protection, {"sub_resource": True}),
867+
(BoundZone.change_rrset_ttl, {"sub_resource": True}),
868+
(BoundZone.add_rrset_records, {"sub_resource": True}),
869+
(BoundZone.remove_rrset_records, {"sub_resource": True}),
870+
(BoundZone.set_rrset_records, {"sub_resource": True}),
871+
]
872+
851873
@pytest.fixture()
852874
def resource_client(self, client: Client):
853875
return client.zones
854876

855877
@pytest.fixture()
856-
def bound_model(self, resource_client, zone1):
878+
def bound_model(self, resource_client: ZonesClient, zone1):
857879
return BoundZone(resource_client, data=zone1)
858880

859881
def test_init(self, resource_client: ZonesClient, bound_model: BoundZone):
@@ -898,13 +920,23 @@ def test_init(self, resource_client: ZonesClient, bound_model: BoundZone):
898920
assert o.registrar == "hetzner"
899921

900922

901-
class TestBoundZoneRRSet:
923+
class TestBoundZoneRRSet(BoundModelTestCase):
924+
methods = [
925+
BoundZoneRRSet.update_rrset,
926+
BoundZoneRRSet.delete_rrset,
927+
BoundZoneRRSet.change_rrset_protection,
928+
BoundZoneRRSet.change_rrset_ttl,
929+
BoundZoneRRSet.add_rrset_records,
930+
BoundZoneRRSet.remove_rrset_records,
931+
BoundZoneRRSet.set_rrset_records,
932+
]
933+
902934
@pytest.fixture()
903935
def resource_client(self, client: Client):
904936
return client.zones
905937

906938
@pytest.fixture()
907-
def bound_model(self, resource_client, zone_rrset1):
939+
def bound_model(self, resource_client: ZonesClient, zone_rrset1):
908940
return BoundZoneRRSet(resource_client, data=zone_rrset1)
909941

910942
def test_init(self, resource_client: ZonesClient, bound_model: BoundZoneRRSet):

0 commit comments

Comments
 (0)