Add method is_valid_boolstr

method is_valid_boolstr is defined in Cinder[1] and Ironic[2],
It's good for oslo.utils to adopt it.
[1]aeba18101c/cinder/utils.py (L271)
[2]55410888de/ironic/common/utils.py (L150)

Change-Id: Id0a95ad31f8cb7ea444e2cafecb48d79bb9c1a1b
This commit is contained in:
ChangBo Guo(gcb) 2016-09-29 16:14:55 +08:00
parent c28347d9d1
commit feb3a1baf4
2 changed files with 28 additions and 0 deletions

View File

@ -145,6 +145,19 @@ def bool_from_string(subject, strict=False, default=False):
return default
def is_valid_boolstr(value):
"""Check if the provided string is a valid bool string or not.
:param value: value to verify
:type value: string
:returns: true if value is boolean string, false otherwise
.. versionadded:: 3.17
"""
boolstrs = TRUE_STRINGS + FALSE_STRINGS
return str(value).lower() in boolstrs
def string_to_bytes(text, unit_system='IEC', return_int=False):
"""Converts a string into an float representation of bytes.

View File

@ -150,6 +150,21 @@ class StrUtilsTest(test_base.BaseTestCase):
self.assertEqual(1, strutils.int_from_bool_as_string(True))
self.assertEqual(0, strutils.int_from_bool_as_string(False))
def test_is_valid_boolstr(self):
self.assertTrue(strutils.is_valid_boolstr('true'))
self.assertTrue(strutils.is_valid_boolstr('false'))
self.assertTrue(strutils.is_valid_boolstr('yes'))
self.assertTrue(strutils.is_valid_boolstr('no'))
self.assertTrue(strutils.is_valid_boolstr('y'))
self.assertTrue(strutils.is_valid_boolstr('n'))
self.assertTrue(strutils.is_valid_boolstr('1'))
self.assertTrue(strutils.is_valid_boolstr('0'))
self.assertTrue(strutils.is_valid_boolstr(1))
self.assertTrue(strutils.is_valid_boolstr(0))
self.assertFalse(strutils.is_valid_boolstr('maybe'))
self.assertFalse(strutils.is_valid_boolstr('only on tuesdays'))
def test_slugify(self):
to_slug = strutils.to_slug
self.assertRaises(TypeError, to_slug, True)