Skip to content

Commit

Permalink
Refactor & tests update
Browse files Browse the repository at this point in the history
  • Loading branch information
pszulczewski committed Aug 31, 2023
1 parent 28694d3 commit 8eb7556
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
10 changes: 5 additions & 5 deletions pynautobot/core/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,11 @@ def choices(self, api_version=None):
post_data = req["schema"]["properties"]
except (KeyError, TypeError):
raise ValueError(f"Unexpected format in the OPTIONS response at {self.url}")
self._choices = {}
for prop in post_data:
if "choices" in post_data[prop]:
self._choices[prop] = post_data[prop]["choices"]

self._choices = {
prop: [{"value": x, "display": y} for x, y in zip(post_data[prop]["enum"], post_data[prop]["enumNames"])]
for prop in post_data
if "enum" in post_data[prop]
}
return self._choices

def count(self, *args, api_version=None, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_api_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_api_versioning_at_api_level(self, ResponseLoader):
)

# Test choices request includes version=1.3 in its headers
self.api.http_session.options.return_value.json.return_value = {"actions": {"POST": []}}
self.api.http_session.options.return_value.json.return_value = {"schema": {"properties": {}}}
self.test_obj.choices()
self.api.http_session.options.assert_called_with(
"http://localhost:8000/api/test/test/",
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_api_versioning_at_per_request_level(self, ResponseLoader):
)

# Test choices request overrides Api level versioning
self.api.http_session.options.return_value.json.return_value = {"actions": {"POST": []}}
self.api.http_session.options.return_value.json.return_value = {"schema": {"properties": {}}}
self.test_obj.choices(api_version=1.2)
self.api.http_session.options.assert_called_with(
"http://localhost:8000/api/test/test/",
Expand Down
13 changes: 5 additions & 8 deletions tests/unit/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,18 @@ def test_choices(self):
api = Mock(base_url="http://localhost:8000/api")
app = Mock(name="test")
mock.return_value = {
"actions": {
"POST": {
"schema": {
"properties": {
"letter": {
"choices": [
{"display_name": "A", "value": 1},
{"display_name": "B", "value": 2},
{"display_name": "C", "value": 3},
]
"enum": [1, 2, 3],
"enumNames": ["A", "B", "C"],
}
}
}
}
test_obj = Endpoint(api, app, "test")
choices = test_obj.choices()
self.assertEqual(choices["letter"][1]["display_name"], "B")
self.assertEqual(choices["letter"][1]["display"], "B")
self.assertEqual(choices["letter"][1]["value"], 2)


Expand Down

0 comments on commit 8eb7556

Please sign in to comment.