Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix return object lookup and improve tests #661

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pynetbox/core/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/test_dcim.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from packaging import version

import pynetbox
import pynetbox.models.dcim


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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")
Expand All @@ -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()

Expand Down Expand Up @@ -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")
Expand All @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand Down
3 changes: 3 additions & 0 deletions tests/test_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import patch

import pynetbox
import pynetbox.models.circuits

from .util import Response

Expand Down Expand Up @@ -76,6 +77,7 @@ def test_get(self):

class CircuitsTestCase(Generic.Tests):
name = "circuits"
ret = pynetbox.models.circuits.Circuits

@patch(
"requests.sessions.Session.get",
Expand All @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import patch

import pynetbox
import pynetbox.models.users

from .util import Response

Expand Down Expand Up @@ -76,6 +77,7 @@ def test_get(self):

class UsersTestCase(Generic.Tests):
name = "users"
ret = pynetbox.models.users.Users

@patch(
"requests.sessions.Session.get",
Expand All @@ -93,6 +95,7 @@ class GroupsTestCase(Generic.Tests):

class PermissionsTestCase(Generic.Tests):
name = "permissions"
ret = pynetbox.models.users.Permissions

@patch(
"requests.sessions.Session.get",
Expand Down
3 changes: 3 additions & 0 deletions tests/test_virtualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from unittest.mock import patch

import pynetbox
import pynetbox.models
import pynetbox.models.virtualization

from .util import Response

Expand Down Expand Up @@ -88,6 +90,7 @@ class ClustersTestCase(Generic.Tests):

class VirtualMachinesTestCase(Generic.Tests):
name = "virtual_machines"
ret = pynetbox.models.virtualization.VirtualMachines


class InterfacesTestCase(Generic.Tests):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_wireless.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import patch

import pynetbox
import pynetbox.models.wireless

from .util import Response

Expand Down Expand Up @@ -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",
Expand Down
Loading