Skip to content

Commit

Permalink
Merge pull request #240 from tsm1th/develop
Browse files Browse the repository at this point in the history
Changed all() method to call filter() method in Endpoint class
  • Loading branch information
joewesch authored Oct 10, 2024
2 parents 38b92ba + 668e603 commit b848c07
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
20 changes: 2 additions & 18 deletions pynautobot/core/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _lookup_ret_obj(self, name, model):
ret = Record
return ret

def all(self, api_version=None, limit=None, offset=None):
def all(self, *args, **kwargs):
"""Queries the 'ListView' of a given endpoint.
Returns all objects from an endpoint.
Expand All @@ -101,21 +101,7 @@ def all(self, api_version=None, limit=None, offset=None):
>>> nb.dcim.devices.all()
[test1-a3-oobsw2, test1-a3-oobsw3, test1-a3-oobsw4]
"""
if not limit and offset is not None:
raise ValueError("offset requires a positive limit value")
api_version = api_version or self.api.api_version
req = Request(
base="{}/".format(self.url),
token=self.token,
http_session=self.api.http_session,
threading=self.api.threading,
max_workers=self.api.max_workers,
api_version=api_version,
limit=limit,
offset=offset,
)

return response_loader(req.get(), self.return_obj, self)
return self.filter(*args, **kwargs)

def get(self, *args, **kwargs):
"""Queries the DetailsView of a given endpoint.
Expand Down Expand Up @@ -222,8 +208,6 @@ def filter(self, *args, api_version=None, **kwargs):
if args:
kwargs.update({"q": args[0]})

if not kwargs:
raise ValueError("filter must be passed kwargs. Perhaps use all() instead.")
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))
limit = kwargs.pop("limit") if "limit" in kwargs else None
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/test_dcim.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def test_fetching_vc_success(self, nb_client):
role={"name": "Leaf Switch"},
location=location.id,
status={"name": "Active"},
local_config_context_data={"foo": "bar"},
)
vc = nb_client.dcim.virtual_chassis.create(name="VC1", master=dev1.id)
nb_client.dcim.devices.create(
Expand All @@ -193,6 +194,7 @@ def test_fetching_vc_success(self, nb_client):
status={"name": "Active"},
virtual_chassis=vc.id,
vc_position=2,
local_config_context_data={"foo": "bar"},
)
all_vcs = nb_client.dcim.virtual_chassis.all()
assert len(all_vcs) == 1
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/test_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
class TestEndpoint:
"""Verify different methods on an endpoint."""

def test_get_all_devices(self, nb_client):
devices = nb_client.dcim.devices.all()
assert len(devices) == 8

def test_get_all_devices_include_context(self, nb_client):
devices = nb_client.dcim.devices.all(name="dev-1", include=["config_context"])
assert devices[0].config_context == {"foo": "bar"}

def test_get_filtered_devices(self, nb_client):
devices = nb_client.dcim.devices.filter(device_type="DCS-7050TX3-48C8")
assert len(devices) == 6


class TestPagination:
"""Verify we can limit and offset results on an endpoint."""

Expand Down
23 changes: 16 additions & 7 deletions tests/unit/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ def test_filter(self):
test = test_obj.filter(test="test")
self.assertEqual(len(test), 2)

def test_filter_empty_kwargs(self):
api = Mock(base_url="http://localhost:8000/api")
app = Mock(name="test")
test_obj = Endpoint(api, app, "test")
with self.assertRaises(ValueError) as _:
test_obj.filter()

def test_filter_reserved_kwargs(self):
api = Mock(base_url="http://localhost:8000/api")
app = Mock(name="test")
Expand All @@ -38,6 +31,22 @@ def test_all_none_limit_offset(self):
with self.assertRaises(ValueError) as _:
test_obj.all(limit=None, offset=1)

def test_all_equals_filter_empty_kwargs(self):
with patch("pynautobot.core.query.Request.get", return_value=Mock()) as mock:
api = Mock(base_url="http://localhost:8000/api")
app = Mock(name="test")
mock.return_value = [{"id": 123}, {"id": 321}]
test_obj = Endpoint(api, app, "test")
self.assertEqual(test_obj.all(), test_obj.filter())

def test_all_accepts_kwargs(self):
with patch("pynautobot.core.endpoint.Endpoint.filter", return_value=Mock()) as mock:
api = Mock(base_url="http://localhost:8000/api")
app = Mock(name="test")
test_obj = Endpoint(api, app, "test")
test_obj.all(include=["config_context"])
mock.assert_called_with(include=["config_context"])

def test_filter_zero_limit_offset(self):
with patch("pynautobot.core.query.Request.get", return_value=Mock()) as mock:
api = Mock(base_url="http://localhost:8000/api")
Expand Down

0 comments on commit b848c07

Please sign in to comment.