Skip to content

Commit 4bdb755

Browse files
author
Zach Moody
authored
Merge pull request #366 from digitalocean/get-behavior
Re-implements ValueError for .get()
2 parents 35adedd + 252fa3e commit 4bdb755

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

pynetbox/core/endpoint.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,19 @@ def get(self, *args, **kwargs):
133133
key = None
134134

135135
if not key:
136-
return next(self.filter(**kwargs), None)
136+
resp = self.filter(**kwargs)
137+
ret = next(resp, None)
138+
if not ret:
139+
return ret
140+
try:
141+
next(resp)
142+
raise ValueError(
143+
"get() returned more than one result. "
144+
"Check that the kwarg(s) passed are valid for this "
145+
"endpoint or use filter() or all() instead."
146+
)
147+
except StopIteration:
148+
return ret
137149

138150
req = Request(
139151
key=key,

pynetbox/core/response.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ def __init__(self, endpoint, request, **kwargs):
7878
self._response_cache = []
7979

8080
def __iter__(self):
81+
return self
82+
83+
def __next__(self):
8184
if self._response_cache:
82-
yield self.endpoint.return_obj(
85+
return self.endpoint.return_obj(
8386
self._response_cache.pop(), self.endpoint.api, self.endpoint
8487
)
85-
for i in self.response:
86-
yield self.endpoint.return_obj(i, self.endpoint.api, self.endpoint)
87-
88-
def __next__(self):
89-
for i in self:
90-
return i
88+
return self.endpoint.return_obj(
89+
next(self.response), self.endpoint.api, self.endpoint
90+
)
9191

9292
def __len__(self):
9393
try:

tests/unit/test_endpoint.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,36 @@ def test_choices(self):
5959
choices = test_obj.choices()
6060
self.assertEqual(choices["letter"][1]["display_name"], "B")
6161
self.assertEqual(choices["letter"][1]["value"], 2)
62+
63+
def test_get_with_filter(self):
64+
with patch(
65+
"pynetbox.core.query.Request._make_call", return_value=Mock()
66+
) as mock:
67+
mock.return_value = [{"id": 123}]
68+
api = Mock(base_url="http://localhost:8000/api")
69+
app = Mock(name="test")
70+
test_obj = Endpoint(api, app, "test")
71+
test = test_obj.get(name="test")
72+
self.assertEqual(test.id, 123)
73+
74+
def test_get_greater_than_one(self):
75+
with patch(
76+
"pynetbox.core.query.Request._make_call", return_value=Mock()
77+
) as mock:
78+
mock.return_value = [{"id": 123}, {"id": 321}]
79+
api = Mock(base_url="http://localhost:8000/api")
80+
app = Mock(name="test")
81+
test_obj = Endpoint(api, app, "test")
82+
with self.assertRaises(ValueError) as _:
83+
test_obj.get(name="test")
84+
85+
def test_get_no_results(self):
86+
with patch(
87+
"pynetbox.core.query.Request._make_call", return_value=Mock()
88+
) as mock:
89+
mock.return_value = []
90+
api = Mock(base_url="http://localhost:8000/api")
91+
app = Mock(name="test")
92+
test_obj = Endpoint(api, app, "test")
93+
test = test_obj.get(name="test")
94+
self.assertIsNone(test)

0 commit comments

Comments
 (0)