Skip to content

Commit 2287a7b

Browse files
fix: added boolean_to_string converter (#753)
1 parent f0a2548 commit 2287a7b

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

tests/unit/base/test_serialize.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,28 @@ def test_list(self):
9090
self.assertEqual({}, actual)
9191

9292

93+
class BooleanTestCase(unittest.TestCase):
94+
def test_none(self):
95+
value = None
96+
actual = serialize.boolean_to_string(value)
97+
self.assertIsNone(actual)
98+
99+
def test_string(self):
100+
value = "True"
101+
actual = serialize.boolean_to_string(value)
102+
self.assertEqual("true", actual)
103+
104+
def test_bool_true(self):
105+
value = True
106+
actual = serialize.boolean_to_string(value)
107+
self.assertEqual("true", actual)
108+
109+
def test_bool_false(self):
110+
value = False
111+
actual = serialize.boolean_to_string(value)
112+
self.assertEqual("false", actual)
113+
114+
93115
class ObjectTestCase(unittest.TestCase):
94116
def test_object(self):
95117
actual = serialize.object({"twilio": "rocks"})

twilio/base/serialize.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ def flatten_dict(d, result=None, prv_keys=None):
6161
return {}
6262

6363

64+
def boolean_to_string(bool_or_str):
65+
if bool_or_str is None:
66+
return bool_or_str
67+
68+
if isinstance(bool_or_str, str):
69+
return bool_or_str.lower()
70+
71+
return "true" if bool_or_str else "false"
72+
73+
6474
def object(obj):
6575
"""
6676
Return a jsonified string represenation of obj if obj is jsonifiable else

0 commit comments

Comments
 (0)