Skip to content

Commit

Permalink
Merge pull request #201 from markkuleinio/custom-choices
Browse files Browse the repository at this point in the history
Add method to return custom choices
  • Loading branch information
Zach Moody authored Nov 20, 2019
2 parents bc70a8a + 4e4dd35 commit 4fa86a8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pynetbox/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@ def choices(self):

return self._choices

def custom_choices(self):
""" Returns _custom_field_choices response from app
:Returns: Raw response from NetBox's _custom_field_choices endpoint.
:Raises: :py:class:`.RequestError` if called for an invalid endpoint.
:Example:
>>> nb.extras.custom_choices()
{'Testfield1': {'Testvalue2': 2, 'Testvalue1': 1},
'Testfield2': {'Othervalue2': 4, 'Othervalue1': 3}}
"""
custom_field_choices = Request(
base="{}/{}/_custom_field_choices/".format(
self.api.base_url,
self.name,
),
token=self.api.token,
private_key=self.api.private_key,
ssl_verify=self.api.ssl_verify,
http_session=self.api.http_session,
).get()
return custom_field_choices


class Api(object):
""" The API object is the point of entry to pynetbox.
Expand Down
35 changes: 35 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import unittest

import six

import pynetbox

if six.PY3:
from unittest.mock import patch
else:
from mock import patch

host = "http://localhost:8000"

def_kwargs = {
'token': 'abc123',
}


class AppCustomChoicesTestCase(unittest.TestCase):

@patch(
'pynetbox.core.query.Request.get',
return_value={
"Testfield1": {"TF1_1": 1, "TF1_2": 2},
"Testfield2": {"TF2_1": 3, "TF2_2": 4},
}
)
def test_custom_choices(self, *_):
api = pynetbox.api(
host,
**def_kwargs
)
choices = api.extras.custom_choices()
self.assertEqual(len(choices), 2)
self.assertEqual(sorted(choices.keys()), ["Testfield1", "Testfield2"])

0 comments on commit 4fa86a8

Please sign in to comment.