Skip to content

Commit

Permalink
Merge pull request #366 from digitalocean/get-behavior
Browse files Browse the repository at this point in the history
Re-implements ValueError for .get()
  • Loading branch information
Zach Moody authored Apr 9, 2021
2 parents 35adedd + 252fa3e commit 4bdb755
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
14 changes: 13 additions & 1 deletion pynetbox/core/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,19 @@ def get(self, *args, **kwargs):
key = None

if not key:
return next(self.filter(**kwargs), None)
resp = self.filter(**kwargs)
ret = next(resp, None)
if not ret:
return ret
try:
next(resp)
raise ValueError(
"get() returned more than one result. "
"Check that the kwarg(s) passed are valid for this "
"endpoint or use filter() or all() instead."
)
except StopIteration:
return ret

req = Request(
key=key,
Expand Down
14 changes: 7 additions & 7 deletions pynetbox/core/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ def __init__(self, endpoint, request, **kwargs):
self._response_cache = []

def __iter__(self):
return self

def __next__(self):
if self._response_cache:
yield self.endpoint.return_obj(
return self.endpoint.return_obj(
self._response_cache.pop(), self.endpoint.api, self.endpoint
)
for i in self.response:
yield self.endpoint.return_obj(i, self.endpoint.api, self.endpoint)

def __next__(self):
for i in self:
return i
return self.endpoint.return_obj(
next(self.response), self.endpoint.api, self.endpoint
)

def __len__(self):
try:
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,36 @@ def test_choices(self):
choices = test_obj.choices()
self.assertEqual(choices["letter"][1]["display_name"], "B")
self.assertEqual(choices["letter"][1]["value"], 2)

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

def test_get_greater_than_one(self):
with patch(
"pynetbox.core.query.Request._make_call", return_value=Mock()
) as mock:
mock.return_value = [{"id": 123}, {"id": 321}]
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.get(name="test")

def test_get_no_results(self):
with patch(
"pynetbox.core.query.Request._make_call", return_value=Mock()
) as mock:
mock.return_value = []
api = Mock(base_url="http://localhost:8000/api")
app = Mock(name="test")
test_obj = Endpoint(api, app, "test")
test = test_obj.get(name="test")
self.assertIsNone(test)

0 comments on commit 4bdb755

Please sign in to comment.