Skip to content

Commit

Permalink
Allow models to be iterable.
Browse files Browse the repository at this point in the history
Signed-off-by: Lee Trager <lee.trager@canonical.com>
  • Loading branch information
ltrager committed May 19, 2020
1 parent bf38396 commit aaff301
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
4 changes: 4 additions & 0 deletions pylxd/models/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ def __setattr__(self, name, value):
self.__dirty__.add(name)
return super(Model, self).__setattr__(name, value)

def __iter__(self):
for attr in self.__attributes__.keys():
yield attr, getattr(self, attr)

@property
def dirty(self):
return len(self.__dirty__) > 0
Expand Down
57 changes: 31 additions & 26 deletions pylxd/tests/mock_lxd.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,23 +208,25 @@ def snapshot_DELETE(request, context):
'metadata': {
"server_name": "an-member",
"enabled": 'true',
"member_config": [{
"entity": "storage-pool",
"name": "local",
"key": "source",
"value": "",
"description":
"\"source\" property for storage pool \"local\""
},
{
"entity": "storage-pool",
"name": "local",
"key": "volatile.initial_source",
"value": "",
"description":
"\"volatile.initial_source\" property for"
" storage pool \"local\""
}]
"member_config": [
{
"entity": "storage-pool",
"name": "local",
"key": "source",
"value": "",
"description":
"\"source\" property for storage pool \"local\""
},
{
"entity": "storage-pool",
"name": "local",
"key": "volatile.initial_source",
"value": "",
"description":
"\"volatile.initial_source\" property for"
" storage pool \"local\""
},
]
}
}),
'method': 'GET',
Expand Down Expand Up @@ -333,9 +335,10 @@ def snapshot_DELETE(request, context):
'stateful': False,
'status': "Running",
'status_code': 103,
'unsupportedbypylxd': "This attribute is not supported by "\
"pylxd. We want to test whether the mere presence of it "\
"makes it crash."
'unsupportedbypylxd': (
"This attribute is not supported by "
"pylxd. We want to test whether the mere presence of it "
"makes it crash.")
}},
'method': 'GET',
'url': r'^http://pylxd2.test/1.0/instances/an-instance$',
Expand Down Expand Up @@ -381,9 +384,10 @@ def snapshot_DELETE(request, context):
'stateful': False,
'status': "Running",
'status_code': 103,
'unsupportedbypylxd': "This attribute is not supported by "\
"pylxd. We want to test whether the mere presence of it "\
"makes it crash."
'unsupportedbypylxd': (
"This attribute is not supported by "
"pylxd. We want to test whether the mere presence of it "
"makes it crash.")
}},
'method': 'GET',
'url': r'^http://pylxd.test/1.0/instances/an-instance$',
Expand Down Expand Up @@ -437,9 +441,10 @@ def snapshot_DELETE(request, context):
'location': "an-remote",
'status': "Running",
'status_code': 103,
'unsupportedbypylxd': "This attribute is not supported by "\
"pylxd. We want to test whether the mere presence of it "\
"makes it crash."
'unsupportedbypylxd': (
"This attribute is not supported by "
"pylxd. We want to test whether the mere presence of it "
"makes it crash.")
}},
'method': 'GET',
'url': r'^http://pylxd.test/1.0/instances/an-new-remote-instance$',
Expand Down
8 changes: 8 additions & 0 deletions pylxd/tests/models/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ def test_unset_attribute_sync(self):

self.assertEqual(1000, item.age)

def test_iter(self):
"""Test models can be iterated over."""
item = Item(self.client, name='an-item')

self.assertDictEqual(
{'name': 'an-item', 'age': 1000, 'data': {'key': 'val'}},
dict(item))

def test_sync(self):
"""A sync will update attributes from the server."""
item = Item(self.client, name='an-item')
Expand Down

0 comments on commit aaff301

Please sign in to comment.