Skip to content

Commit

Permalink
fix: added boolean_to_string converter (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiwarishubham635 authored Feb 9, 2024
1 parent f0a2548 commit 2287a7b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/unit/base/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ def test_list(self):
self.assertEqual({}, actual)


class BooleanTestCase(unittest.TestCase):
def test_none(self):
value = None
actual = serialize.boolean_to_string(value)
self.assertIsNone(actual)

def test_string(self):
value = "True"
actual = serialize.boolean_to_string(value)
self.assertEqual("true", actual)

def test_bool_true(self):
value = True
actual = serialize.boolean_to_string(value)
self.assertEqual("true", actual)

def test_bool_false(self):
value = False
actual = serialize.boolean_to_string(value)
self.assertEqual("false", actual)


class ObjectTestCase(unittest.TestCase):
def test_object(self):
actual = serialize.object({"twilio": "rocks"})
Expand Down
10 changes: 10 additions & 0 deletions twilio/base/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def flatten_dict(d, result=None, prv_keys=None):
return {}


def boolean_to_string(bool_or_str):
if bool_or_str is None:
return bool_or_str

if isinstance(bool_or_str, str):
return bool_or_str.lower()

return "true" if bool_or_str else "false"


def object(obj):
"""
Return a jsonified string represenation of obj if obj is jsonifiable else
Expand Down

0 comments on commit 2287a7b

Please sign in to comment.