Merge "Expose 'is_base' schema property attribute"
This commit is contained in:
@@ -65,6 +65,7 @@ class SchemaProperty(object):
|
||||
def __init__(self, name, **kwargs):
|
||||
self.name = name
|
||||
self.description = kwargs.get('description')
|
||||
self.is_base = kwargs.get('is_base', True)
|
||||
|
||||
|
||||
def translate_schema_properties(schema_properties):
|
||||
@@ -86,9 +87,27 @@ class Schema(object):
|
||||
self.properties = translate_schema_properties(raw_properties)
|
||||
|
||||
def is_core_property(self, property_name):
|
||||
"""Checks if a property with a given name is known to the schema,
|
||||
i.e. is either a base property or a custom one registered in
|
||||
schema-image.json file
|
||||
|
||||
:param property_name: name of the property
|
||||
:returns: True if the property is known, False otherwise
|
||||
"""
|
||||
return self._check_property(property_name, True)
|
||||
|
||||
def is_base_property(self, property_name):
|
||||
"""Checks if a property with a given name is a base property
|
||||
|
||||
:param property_name: name of the property
|
||||
:returns: True if the property is base, False otherwise
|
||||
"""
|
||||
return self._check_property(property_name, False)
|
||||
|
||||
def _check_property(self, property_name, allow_non_base):
|
||||
for prop in self.properties:
|
||||
if property_name == prop.name:
|
||||
return True
|
||||
return prop.is_base or allow_non_base
|
||||
return False
|
||||
|
||||
def raw(self):
|
||||
|
||||
@@ -74,6 +74,14 @@ class TestSchemaProperty(testtools.TestCase):
|
||||
self.assertEqual('size', prop.name)
|
||||
self.assertEqual('some quantity', prop.description)
|
||||
|
||||
def test_property_is_base(self):
|
||||
prop1 = schemas.SchemaProperty('name')
|
||||
prop2 = schemas.SchemaProperty('foo', is_base=False)
|
||||
prop3 = schemas.SchemaProperty('foo', is_base=True)
|
||||
self.assertTrue(prop1.is_base)
|
||||
self.assertFalse(prop2.is_base)
|
||||
self.assertTrue(prop3.is_base)
|
||||
|
||||
|
||||
class TestSchema(testtools.TestCase):
|
||||
def test_schema_minimum(self):
|
||||
@@ -93,6 +101,16 @@ class TestSchema(testtools.TestCase):
|
||||
schema = schemas.Schema(raw_schema)
|
||||
self.assertEqual(raw_schema, schema.raw())
|
||||
|
||||
def test_property_is_base(self):
|
||||
raw_schema = {'name': 'Country',
|
||||
'properties': {
|
||||
'size': {},
|
||||
'population': {'is_base': False}}}
|
||||
schema = schemas.Schema(raw_schema)
|
||||
self.assertTrue(schema.is_base_property('size'))
|
||||
self.assertFalse(schema.is_base_property('population'))
|
||||
self.assertFalse(schema.is_base_property('foo'))
|
||||
|
||||
|
||||
class TestController(testtools.TestCase):
|
||||
def setUp(self):
|
||||
|
||||
Reference in New Issue
Block a user