Skip to content
Open
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
4 changes: 2 additions & 2 deletions dapr/clients/grpc/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def to_bytes(data: Union[str, bytes]) -> bytes:
elif isinstance(data, str):
return data.encode('utf-8')
else:
raise f'invalid data type {type(data)}'
raise TypeError(f'invalid data type {type(data)}')


def to_str(data: Union[str, bytes]) -> str:
Expand All @@ -113,7 +113,7 @@ def to_str(data: Union[str, bytes]) -> str:
elif isinstance(data, bytes):
return data.decode('utf-8')
else:
raise f'invalid data type {type(data)}'
raise TypeError(f'invalid data type {type(data)}')


# Data validation helpers
Expand Down
24 changes: 24 additions & 0 deletions tests/clients/test_dapr_grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from dapr.clients.grpc._helpers import (
convert_dict_to_grpc_dict_of_any,
convert_value_to_struct,
to_bytes,
to_str,
)


Expand Down Expand Up @@ -185,5 +187,27 @@ def test_unsupported_type_raises_value_error(self):
convert_dict_to_grpc_dict_of_any({'bad': [1, 2, 3]})


class TestToBytesToStr(unittest.TestCase):
def test_to_bytes_passthrough(self):
self.assertEqual(to_bytes(b'abc'), b'abc')

def test_to_bytes_encodes_str(self):
self.assertEqual(to_bytes('abc'), b'abc')

def test_to_bytes_rejects_other_types(self):
with self.assertRaisesRegex(TypeError, 'invalid data type'):
to_bytes(123) # type: ignore[arg-type]

def test_to_str_passthrough(self):
self.assertEqual(to_str('abc'), 'abc')

def test_to_str_decodes_bytes(self):
self.assertEqual(to_str(b'abc'), 'abc')

def test_to_str_rejects_other_types(self):
with self.assertRaisesRegex(TypeError, 'invalid data type'):
to_str(123) # type: ignore[arg-type]


if __name__ == '__main__':
unittest.main(verbosity=2)