Expose 'is_base' schema property attribute

Changeset I49255255 has added an 'is_base' attribute for Image Schema
properties, thus allowing to differentiate between base and custom image
properties, but the client hasn't make any use of it.

This patch adds appropriate attribute to SchemaProperty class and a
helper method which allows to validate if the given property is base or
not.

The added helper method (is_base_property) should not be confused with
the existing is_core_property: the latter just checks if the property is
known to the schema, regardless of its being base or not.

Change-Id: I7c397196dad9ae5494ed2f8f3aacef3fc1ce70d8
Partial-Bug: #1323660
This commit is contained in:
Alexander Tivelkov 2015-03-27 17:53:46 +03:00
parent 13b5a5ade1
commit 90407d9e47
2 changed files with 38 additions and 1 deletions

View File

@ -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):

View File

@ -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):