Turn normalize_status into a class

For better readability, make normalize_status a class that has constants
for each of the status values.

While in there, add a test for unknown status values.

Co-Authored-By: Eric Fried <efried@us.ibm.com>
Change-Id: I93ee971125bc0c7a497e1fb839df38ebd38340e1
This commit is contained in:
Monty Taylor 2018-04-25 07:26:21 +01:00
parent 57f9736329
commit 4629e3c944
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
2 changed files with 42 additions and 21 deletions

View File

@ -131,26 +131,6 @@ def get_version_data(session, url, authenticated=None):
'returned: %s' % err_text)
def normalize_status(raw_status):
"""Turn a status into a canonical status value.
If the status from the version discovery document does not match one
of the known values, it will be set to 'UNKNOWN'.
:param str raw_status: Status value from a discovery document.
:returns: A canonicalized version of the status. Valid values
are CURRENT, SUPPORTED, DEPRECATED, EXPERIMENTAL and UNKNOWN
:rtype: str
"""
status = raw_status.upper()
if status == 'STABLE':
status = 'CURRENT'
if status not in ('CURRENT', 'SUPPORTED', 'DEPRECATED', 'EXPERIMENTAL'):
status = 'UNKNOWN'
return status
def normalize_version_number(version):
"""Turn a version representation into a tuple.
@ -411,6 +391,35 @@ def _version_from_url(url):
return None
class Status(object):
CURRENT = 'CURRENT'
SUPPORTED = 'SUPPORTED'
DEPRECATED = 'DEPRECATED'
EXPERIMENTAL = 'EXPERIMENTAL'
UNKNOWN = 'UNKNOWN'
KNOWN = (CURRENT, SUPPORTED, DEPRECATED, EXPERIMENTAL)
@classmethod
def normalize(cls, raw_status):
"""Turn a status into a canonical status value.
If the status from the version discovery document does not match one
of the known values, it will be set to 'UNKNOWN'.
:param str raw_status: Status value from a discovery document.
:returns: A canonicalized version of the status. Valid values
are CURRENT, SUPPORTED, DEPRECATED, EXPERIMENTAL and UNKNOWN
:rtype: str
"""
status = raw_status.upper()
if status == 'STABLE':
status = 'CURRENT'
if status not in cls.KNOWN:
status = cls.UNKNOWN
return status
class Discover(object):
CURRENT_STATUSES = ('stable', 'current', 'supported')
@ -537,7 +546,7 @@ class Discover(object):
max_microversion=max_microversion,
next_min_version=next_min_version,
not_before=not_before,
status=normalize_status(v['status']),
status=Status.normalize(v['status']),
raw_status=v['status']))
versions.sort(key=lambda v: v['version'], reverse=reverse)

View File

@ -480,6 +480,18 @@ class VersionDataTests(utils.TestCase):
self.assertTrue(mock.called_once)
def test_version_data_unknown(self):
discovery_fixture = fixture.V3Discovery(V3_URL)
discovery_fixture.status = 'hungry'
discovery_doc = _create_single_version(discovery_fixture)
self.requests_mock.get(V3_URL, status_code=200, json=discovery_doc)
disc = discover.Discover(self.session, V3_URL)
clean_data = disc.version_data(allow_unknown=True)
self.assertEqual(discover.Status.UNKNOWN, clean_data[0]['status'])
def test_version_data_individual(self):
mock = self.requests_mock.get(V3_URL,
status_code=200,