-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Helpers build_messages_attributes (#243)
- Loading branch information
1 parent
6433c48
commit cbcd45f
Showing
3 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from typing import Dict | ||
|
||
|
||
def build_message_attributes(attributes: Dict[str, str]) -> Dict[str, Dict[str, str]]: | ||
attrs = {} | ||
|
||
for key, value in attributes.items(): | ||
if isinstance(value, str): | ||
attrs[key] = {"stringValue": value, "dataType": "String"} | ||
elif isinstance(value, (int, float)): | ||
attrs[key] = {"stringValue": str(value), "dataType": "Number"} | ||
elif isinstance(value, bytes): | ||
attrs[key] = {"BinaryValue": str(value), "dataType": "Binary"} | ||
else: | ||
raise TypeError(f"{value} of type {type(value).__name__} is not supported") | ||
|
||
return attrs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import unittest | ||
|
||
from faster_sam import helpers | ||
|
||
|
||
class TestHelpers(unittest.TestCase): | ||
def teste_empty_attributes(self): | ||
result = helpers.build_message_attributes({}) | ||
self.assertEqual(result, {}) | ||
|
||
def test_string_attributes(self): | ||
attributes = {"key1": "value1", "key2": "value2"} | ||
expected_result = { | ||
"key1": {"stringValue": "value1", "dataType": "String"}, | ||
"key2": {"stringValue": "value2", "dataType": "String"}, | ||
} | ||
result = helpers.build_message_attributes(attributes) | ||
self.assertEqual(result, expected_result) | ||
|
||
def test_number_attributes(self): | ||
attributes = {"key1": 123, "key2": 12.3} | ||
expected_result = { | ||
"key1": {"stringValue": "123", "dataType": "Number"}, | ||
"key2": {"stringValue": "12.3", "dataType": "Number"}, | ||
} | ||
result = helpers.build_message_attributes(attributes) | ||
self.assertEqual(result, expected_result) | ||
|
||
def test_bytes_attributes(self): | ||
attributes = {"key1": b"test_bytes"} | ||
expected_result = {"key1": {"BinaryValue": str(b"test_bytes"), "dataType": "Binary"}} | ||
result = helpers.build_message_attributes(attributes) | ||
self.assertEqual(result, expected_result) | ||
|
||
def test_multi_attributes(self): | ||
attributes = {"key1": 123, "key2": "value1"} | ||
expected_result = { | ||
"key1": {"stringValue": "123", "dataType": "Number"}, | ||
"key2": {"stringValue": "value1", "dataType": "String"}, | ||
} | ||
result = helpers.build_message_attributes(attributes) | ||
self.assertEqual(result, expected_result) | ||
|
||
def test_unsupported_type(self): | ||
attributes = {"key1": "value1", "key2": [1, 2, 3]} | ||
with self.assertRaises(TypeError): | ||
helpers.build_message_attributes(attributes) |