Skip to content

Commit

Permalink
Merge pull request #223 from digitalocean/fix-nb27-choice
Browse files Browse the repository at this point in the history
Fixes #221 - Account for changes in choices field
  • Loading branch information
Zach Moody authored Feb 11, 2020
2 parents 6a9190a + f58e7ee commit 2523bd9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pynetbox/core/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def get_return(lookup, return_fields=None):
"""Returns simple representations for items passed to lookup.
Used to return a "simple" representation of objects and collections
sent to it via lookup. If lookup is an IPNetwork object immediately
return the string representation. Otherwise, we look to see if
sent to it via lookup. Otherwise, we look to see if
lookup is a "choices" field (dict with only 'id' and 'value')
or a nested_return. Finally, we check if it's a Record, if
so simply return a string. Order is important due to nested_return
Expand All @@ -41,6 +40,10 @@ def get_return(lookup, return_fields=None):
return lookup[i]
else:
if hasattr(lookup, i):
# check if this is a "choices" field record
# from a NetBox 2.7 server.
if sorted(dict(lookup)) == sorted(["id", "value", "label"]):
return getattr(lookup, "value")
return getattr(lookup, i)

if isinstance(lookup, Record):
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,43 @@ def test_dict(self):
test = Record(test_values, None, None)
self.assertEqual(dict(test), test_values)

def test_choices_idempotence_prev27(self):
test_values = {
"id": 123,
"choices_test": {
"value": 1,
"label": "test",
},
}
test = Record(test_values, None, None)
test.choices_test = 1
self.assertFalse(test._diff())

def test_choices_idempotence_v27(self):
test_values = {
"id": 123,
"choices_test": {
"value": "test",
"label": "test",
"id": 1,
},
}
test = Record(test_values, None, None)
test.choices_test = "test"
self.assertFalse(test._diff())

def test_choices_idempotence_v28(self):
test_values = {
"id": 123,
"choices_test": {
"value": "test",
"label": "test",
},
}
test = Record(test_values, None, None)
test.choices_test = "test"
self.assertFalse(test._diff())

def test_hash(self):
endpoint = Mock()
endpoint.name = "test-endpoint"
Expand Down

0 comments on commit 2523bd9

Please sign in to comment.