Add int_or_none adapter function

Add a function that allows a value to either be translated
to an int or left as None if the json value is null to account
for null values without throwing an error.

Change-Id: I03c76d01740cfb36648f09112611111a3b316467
Closes-bug: #1720443
This commit is contained in:
Nate Potter 2017-09-29 14:31:01 -07:00
parent 53e0725b5d
commit 2aa31ddf4f
4 changed files with 23 additions and 1 deletions

View File

@ -21,6 +21,7 @@ from sushy.resources import common
from sushy.resources.system import constants as sys_cons
from sushy.resources.system import mappings as sys_maps
from sushy.resources.system import processor
from sushy import utils
LOG = logging.getLogger(__name__)
@ -52,7 +53,7 @@ class MemorySummaryField(base.CompositeField):
This signifies health state of memory along with its dependent resources.
"""
size_gib = base.Field('TotalSystemMemoryGiB', adapter=int)
size_gib = base.Field('TotalSystemMemoryGiB', adapter=utils.int_or_none)
"""The size of memory of the system in GiB.
This signifies the total installed, operating system-accessible memory

View File

@ -79,6 +79,11 @@ class SystemTestCase(base.TestCase):
'attribute Actions/#ComputerSystem.Reset/target',
self.sys_inst._parse_attributes)
def test__parse_attributes_null_memory_capacity(self):
self.sys_inst.json['MemorySummary']['TotalSystemMemoryGiB'] = None
self.sys_inst._parse_attributes()
self.assertIsNone(self.sys_inst.memory_summary.size_gib)
def test_get__reset_action_element(self):
value = self.sys_inst._get_reset_action_element()
self.assertEqual("/redfish/v1/Systems/437XR1138R2/Actions/"

View File

@ -35,3 +35,7 @@ class UtilsTestCase(base.TestCase):
expected = ('/redfish/v1/Systems/FOO', '/redfish/v1/Systems/BAR')
self.assertEqual(expected, utils.get_members_identities(members))
self.assertEqual(1, log_mock.call_count)
def test_int_or_none(self):
self.assertEqual(1, utils.int_or_none('1'))
self.assertIsNone(None, utils.int_or_none(None))

View File

@ -45,3 +45,15 @@ def get_members_identities(members):
members_list.append(path.rstrip('/'))
return tuple(members_list)
def int_or_none(x):
"""Given a value x it cast as int or None
:param x: The value to transform and return
:returns: Either None or x cast to an int
"""
if x is None:
return None
return int(x)