Skip to content

Commit

Permalink
core/endpoint: Add unit tests for endpoint choices
Browse files Browse the repository at this point in the history
Make sure we can get choices from the `PUT` action when the API doesn't
allow `POST`, and that the `POST` data takes precedence over the `PUT`
data.

Signed-off-by: Benoît Knecht <bknecht@protonmail.ch>
  • Loading branch information
BenoitKnecht committed Sep 30, 2024
1 parent aa86d3d commit 0f9a463
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions tests/unit/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,59 @@ def test_choices(self):
self.assertEqual(choices["letter"][1]["display_name"], "B")
self.assertEqual(choices["letter"][1]["value"], 2)

def test_choices_put(self):
with patch("pynetbox.core.query.Request.options", return_value=Mock()) as mock:
api = Mock(base_url="http://localhost:8000/api")
app = Mock(name="test")
mock.return_value = {
"actions": {
"PUT": {
"letter": {
"choices": [
{"display_name": "A", "value": 1},
{"display_name": "B", "value": 2},
{"display_name": "C", "value": 3},
]
}
}
}
}
test_obj = Endpoint(api, app, "test")
choices = test_obj.choices()
self.assertEqual(choices["letter"][0]["display_name"], "A")
self.assertEqual(choices["letter"][0]["value"], 1)

def test_choices_precedence(self):
with patch("pynetbox.core.query.Request.options", return_value=Mock()) as mock:
api = Mock(base_url="http://localhost:8000/api")
app = Mock(name="test")
mock.return_value = {
"actions": {
"POST": {
"letter": {
"choices": [
{"display_name": "A", "value": 1},
{"display_name": "B", "value": 2},
{"display_name": "C", "value": 3},
]
}
},
"PUT": {
"letter": {
"choices": [
{"display_name": "D", "value": 4},
{"display_name": "E", "value": 5},
{"display_name": "F", "value": 6},
]
}
},
}
}
test_obj = Endpoint(api, app, "test")
choices = test_obj.choices()
self.assertEqual(choices["letter"][2]["display_name"], "C")
self.assertEqual(choices["letter"][2]["value"], 3)

def test_get_with_filter(self):
with patch(
"pynetbox.core.query.Request._make_call", return_value=Mock()
Expand Down

0 comments on commit 0f9a463

Please sign in to comment.