Skip to content

Commit

Permalink
Merge pull request #128 from digitalocean/fix-list-of-records
Browse files Browse the repository at this point in the history
Fixes #127 - Record iter doesn't handle list of records
  • Loading branch information
Zach Moody authored Dec 7, 2018
2 parents 0547b3c + 2c4d4f4 commit 8d5d981
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@

v4.0.3 (2018-12-07)

## Bug Fixes
* [#127](https://github.com/digitalocean/pynetbox/issues/127) - Fixes `__iter__` method on Record object so that it properly return lists record objects. Like tagged_vlans on for Interfaces.

---

v4.0.2 (2018-12-06)

## Bug Fixes
Expand Down
4 changes: 4 additions & 0 deletions pynetbox/core/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ def __iter__(self):
cur_attr = getattr(self, i)
if isinstance(cur_attr, Record):
yield i, dict(cur_attr)
elif isinstance(cur_attr, list) and isinstance(
cur_attr[0], Record
):
yield i, [dict(x) for x in cur_attr]
else:
yield i, cur_attr

Expand Down
53 changes: 53 additions & 0 deletions tests/unit/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,56 @@ def test_diff(self):
test.nested_dict = 1
test.string_field = 'foobaz'
self.assertEqual(test._diff(), {'tags', 'nested_dict', 'string_field'})

def test_dict(self):
test_values = {
'id': 123,
'custom_fields': {
'foo': 'bar'
},
'string_field': 'foobar',
'int_field': 1,
"nested_dict": {
"id": 222,
"name": 'bar',
},
'tags': [
'foo',
'bar',
],
'int_list': [
123,
321,
231,
],
'record_list': [
{
'id': 123,
'name': 'Test',
'str_attr': 'foo',
'int_attr': 123,
'custom_fields': {
'foo': 'bar'
},
'tags': [
'foo',
'bar',
],
},
{
'id': 321,
'name': 'Test 1',
'str_attr': 'bar',
'int_attr': 321,
'custom_fields': {
'foo': 'bar'
},
'tags': [
'foo',
'bar',
],
},
]
}
test = Record(test_values, None, None)
self.assertEqual(dict(test), test_values)

1 comment on commit 8d5d981

@zachmoody
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uhg, of course it would. 🤦‍♂️

Should've stuck with the original idea of testing each item for Record(). Will get it fixed.

Please sign in to comment.