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

Changed all() method to call filter() method in Endpoint class #240

Merged
merged 2 commits into from
Oct 10, 2024
Merged
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
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):
joewesch marked this conversation as resolved.
Show resolved Hide resolved
"""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