From 6e3d87993e0911ad4f840107b44814ee97135d8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Flagothier?= <29896153+srfwx@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:56:34 +0100 Subject: [PATCH] Fix return object lookup and improve tests --- pynetbox/core/endpoint.py | 10 +++++----- tests/integration/test_dcim.py | 10 ++++++++++ tests/test_circuits.py | 3 +++ tests/test_users.py | 3 +++ tests/test_virtualization.py | 3 +++ tests/test_wireless.py | 2 ++ 6 files changed, 26 insertions(+), 5 deletions(-) diff --git a/pynetbox/core/endpoint.py b/pynetbox/core/endpoint.py index dfd989e..5b8f40c 100644 --- a/pynetbox/core/endpoint.py +++ b/pynetbox/core/endpoint.py @@ -42,8 +42,8 @@ class Endpoint: """ def __init__(self, api, app, name, model=None): - self.return_obj = self._lookup_ret_obj(name, model) self.name = name.replace("_", "-") + self.return_obj = self._lookup_ret_obj(model) self.api = api self.base_url = api.base_url self.token = api.token @@ -54,7 +54,7 @@ def __init__(self, api, app, name, model=None): ) self._choices = None - def _lookup_ret_obj(self, name, model): + def _lookup_ret_obj(self, model): """Loads unique Response objects. This method loads a unique response object for an endpoint if @@ -67,7 +67,7 @@ def _lookup_ret_obj(self, name, model): :Returns: Record (obj) """ if model: - name = name.title().replace("_", "") + name = self.name.title().replace("-", "") ret = getattr(model, name, Record) else: ret = Record @@ -636,8 +636,8 @@ def count(self, *args, **kwargs): if any(i in RESERVED_KWARGS for i in kwargs): raise ValueError( - "A reserved {} kwarg was passed. Please remove it " - "try again.".format(RESERVED_KWARGS) + "A reserved kwarg was passed ({}). Please remove it " + "and try again.".format(RESERVED_KWARGS) ) ret = Request( diff --git a/tests/integration/test_dcim.py b/tests/integration/test_dcim.py index 3a208f4..735b9de 100644 --- a/tests/integration/test_dcim.py +++ b/tests/integration/test_dcim.py @@ -2,6 +2,7 @@ from packaging import version import pynetbox +import pynetbox.models.dcim @pytest.fixture(scope="module") @@ -181,6 +182,7 @@ def interface(self, api, device): ret = api.dcim.interfaces.create( name="test-interface", type="1000base-t", device=device.id ) + assert isinstance(ret, pynetbox.models.dcim.Interfaces) yield ret ret.delete() @@ -213,12 +215,14 @@ def power_outlet(self, api, device_type, role, site): site=site.id, ) outlet = api.dcim.power_outlets.create(name="outlet", device=pdu.id) + assert isinstance(outlet, pynetbox.models.dcim.PowerOutlets) yield outlet pdu.delete() @pytest.fixture(scope="class") def power_port(self, api, device): ret = api.dcim.power_ports.create(name="PSU1", device=device.id) + assert isinstance(ret, pynetbox.models.dcim.PowerPorts) yield ret @pytest.fixture(scope="class") @@ -231,6 +235,7 @@ def power_cable(self, api, power_outlet, power_port): {"object_type": "dcim.poweroutlet", "object_id": power_outlet.id}, ], ) + assert isinstance(cable, pynetbox.models.dcim.Cables) yield cable cable.delete() @@ -263,12 +268,14 @@ def console_server_port(self, api, device_type, role, site): site=site.id, ) ret = api.dcim.console_server_ports.create(name="Port 1", device=device.id) + assert isinstance(ret, pynetbox.models.dcim.ConsoleServerPorts) yield ret device.delete() @pytest.fixture(scope="class") def console_port(self, api, device): ret = api.dcim.console_ports.create(name="Console", device=device.id) + assert isinstance(ret, pynetbox.models.dcim.ConsolePorts) yield ret @pytest.fixture(scope="class") @@ -284,6 +291,7 @@ def console_cable(self, api, console_port, console_server_port): }, ], ) + assert isinstance(ret, pynetbox.models.dcim.Cables) yield ret ret.delete() @@ -318,6 +326,7 @@ def interface_b(self, api, device_type, role, site): ret = api.dcim.interfaces.create( name="Ethernet1", type="1000base-t", device=device.id ) + assert isinstance(ret, pynetbox.models.dcim.Interfaces) yield ret device.delete() @@ -338,6 +347,7 @@ def interface_cable(self, api, interface_a, interface_b): {"object_type": "dcim.interface", "object_id": interface_b.id}, ], ) + assert isinstance(ret, pynetbox.models.dcim.Cables) yield ret ret.delete() diff --git a/tests/test_circuits.py b/tests/test_circuits.py index 16076a3..2fbbe88 100644 --- a/tests/test_circuits.py +++ b/tests/test_circuits.py @@ -2,6 +2,7 @@ from unittest.mock import patch import pynetbox +import pynetbox.models.circuits from .util import Response @@ -76,6 +77,7 @@ def test_get(self): class CircuitsTestCase(Generic.Tests): name = "circuits" + ret = pynetbox.models.circuits.Circuits @patch( "requests.sessions.Session.get", @@ -96,6 +98,7 @@ class CircuitTypeTestCase(Generic.Tests): class CircuitTerminationsTestCase(Generic.Tests): name = "circuit_terminations" + ret = pynetbox.models.circuits.CircuitTerminations @patch( "requests.sessions.Session.get", diff --git a/tests/test_users.py b/tests/test_users.py index 08a3175..b5e86b9 100644 --- a/tests/test_users.py +++ b/tests/test_users.py @@ -2,6 +2,7 @@ from unittest.mock import patch import pynetbox +import pynetbox.models.users from .util import Response @@ -76,6 +77,7 @@ def test_get(self): class UsersTestCase(Generic.Tests): name = "users" + ret = pynetbox.models.users.Users @patch( "requests.sessions.Session.get", @@ -93,6 +95,7 @@ class GroupsTestCase(Generic.Tests): class PermissionsTestCase(Generic.Tests): name = "permissions" + ret = pynetbox.models.users.Permissions @patch( "requests.sessions.Session.get", diff --git a/tests/test_virtualization.py b/tests/test_virtualization.py index b3d2484..1c52d0a 100644 --- a/tests/test_virtualization.py +++ b/tests/test_virtualization.py @@ -2,6 +2,8 @@ from unittest.mock import patch import pynetbox +import pynetbox.models +import pynetbox.models.virtualization from .util import Response @@ -88,6 +90,7 @@ class ClustersTestCase(Generic.Tests): class VirtualMachinesTestCase(Generic.Tests): name = "virtual_machines" + ret = pynetbox.models.virtualization.VirtualMachines class InterfacesTestCase(Generic.Tests): diff --git a/tests/test_wireless.py b/tests/test_wireless.py index 5fe719f..b08fb4f 100644 --- a/tests/test_wireless.py +++ b/tests/test_wireless.py @@ -2,6 +2,7 @@ from unittest.mock import patch import pynetbox +import pynetbox.models.wireless from .util import Response @@ -74,6 +75,7 @@ def test_get(self): class WirelessLansTestCase(Generic.Tests): name = "wireless_lans" + ret = pynetbox.models.wireless.WirelessLans @patch( "requests.sessions.Session.get",