Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added boolean_to_string converter #753

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading