Make warlock filtering match dict filtering
For obj_to_dict, we filter out keys that start with _ and values that are not normal values. Do the same for our warlock conversion. Change-Id: Idbf4303c1e4151494d1ea814be6ca5f86e76b16d
This commit is contained in:
parent
59848071e4
commit
a417f3882f
|
@ -186,6 +186,7 @@ def warlock_to_dict(obj):
|
||||||
# deep black magic to attribute look up to support validation things that
|
# deep black magic to attribute look up to support validation things that
|
||||||
# means we cannot use normal obj_to_dict
|
# means we cannot use normal obj_to_dict
|
||||||
obj_dict = {}
|
obj_dict = {}
|
||||||
for key in obj.keys():
|
for (key, value) in obj.items():
|
||||||
obj_dict[key] = obj[key]
|
if isinstance(value, NON_CALLABLES) and not key.startswith('_'):
|
||||||
|
obj_dict[key] = value
|
||||||
return obj_dict
|
return obj_dict
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
import testtools
|
import testtools
|
||||||
|
import warlock
|
||||||
|
|
||||||
from shade import exc
|
from shade import exc
|
||||||
from shade import meta
|
from shade import meta
|
||||||
|
@ -26,6 +27,7 @@ class FakeCloud(object):
|
||||||
region_name = 'test-region'
|
region_name = 'test-region'
|
||||||
name = 'test-name'
|
name = 'test-name'
|
||||||
private = False
|
private = False
|
||||||
|
_unused = "useless"
|
||||||
|
|
||||||
def get_flavor_name(self, id):
|
def get_flavor_name(self, id):
|
||||||
return 'test-flavor-name'
|
return 'test-flavor-name'
|
||||||
|
@ -173,3 +175,29 @@ class TestMeta(testtools.TestCase):
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
FakeException,
|
FakeException,
|
||||||
meta.get_hostvars_from_server, mock_cloud, FakeServer())
|
meta.get_hostvars_from_server, mock_cloud, FakeServer())
|
||||||
|
|
||||||
|
def test_obj_to_dict(self):
|
||||||
|
cloud = FakeCloud()
|
||||||
|
cloud.server = FakeServer()
|
||||||
|
cloud_dict = meta.obj_to_dict(cloud)
|
||||||
|
self.assertEqual(FakeCloud.name, cloud_dict['name'])
|
||||||
|
self.assertNotIn('_unused', cloud_dict)
|
||||||
|
self.assertNotIn('get_flavor_name', cloud_dict)
|
||||||
|
self.assertNotIn('server', cloud_dict)
|
||||||
|
|
||||||
|
def test_warlock_to_dict(self):
|
||||||
|
schema = {
|
||||||
|
'name': 'Test',
|
||||||
|
'properties': {
|
||||||
|
'id': {'type': 'string'},
|
||||||
|
'name': {'type': 'string'},
|
||||||
|
'_unused': {'type': 'string'},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
test_model = warlock.model_factory(schema)
|
||||||
|
test_obj = test_model(
|
||||||
|
id='471c2475-da2f-47ac-aba5-cb4aa3d546f5',
|
||||||
|
name='test-image')
|
||||||
|
test_dict = meta.warlock_to_dict(test_obj)
|
||||||
|
self.assertNotIn('_unused', test_dict)
|
||||||
|
self.assertEqual('test-image', test_dict['name'])
|
||||||
|
|
|
@ -10,3 +10,4 @@ sphinx>=1.1.2,!=1.2.0,!=1.3b1,<1.3
|
||||||
testrepository>=0.0.17
|
testrepository>=0.0.17
|
||||||
testscenarios>=0.4,<0.5
|
testscenarios>=0.4,<0.5
|
||||||
testtools>=0.9.32
|
testtools>=0.9.32
|
||||||
|
warlock>=1.0.1,<2
|
||||||
|
|
Loading…
Reference in New Issue