Skip to content

Commit

Permalink
Added tests to verify changes made to all() method
Browse files Browse the repository at this point in the history
  • Loading branch information
tsm1th committed Oct 9, 2024
1 parent 1af940f commit 668e603
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
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 668e603

Please sign in to comment.