Repository: sosw/sosw

Issue:
DynamoDB - exception on wrong type in row_mapper
When row_mapper type is of one type (for example 'S'), and real type from DB is of another type (for example 'N')
This exception happens:

<img width="834" alt="Screen Shot 2020-07-08 at 13 59 53" src="https://user-images.githubusercontent.com/11838450/86911422-f7956280-c123-11ea-83cf-d9f207ed3c1c.png">

Options (choose one)
1. Raise informative exception
2. Convert to the expected type if possible

I prefer the 1st option.

Discussion:

Patch:
diff --git a/sosw/components/dynamo_db.py b/sosw/components/dynamo_db.py
index ba52847..d7686c6 100644
--- a/sosw/components/dynamo_db.py
+++ b/sosw/components/dynamo_db.py
@@ -261,6 +261,11 @@ class DynamoDbClient:
                 if val_dict:
                     val = val_dict.get(key_type)  # Ex: 1234 or "myvalue"
 
+                    if val is None and key_type not in val_dict:
+                        real_type = list(val_dict.keys())[0]
+                        raise ValueError(f"'{key}' is expected to be of type '{key_type}' in row_mapper, "
+                                         f"but real value is of type '{real_type}'")
+
                     # type_deserializer.deserialize() parses 'N' to `Decimal` type but it cant be parsed to a datetime
                     # so we cast it to either an integer or a float.
                     if key_type == 'N':


Test patch:
diff --git a/sosw/components/test/unit/test_dynamo_db.py b/sosw/components/test/unit/test_dynamo_db.py
index 30dace2..e9022ee 100644
--- a/sosw/components/test/unit/test_dynamo_db.py
+++ b/sosw/components/test/unit/test_dynamo_db.py
@@ -201,6 +201,21 @@ class dynamodb_client_UnitTestCase(unittest.TestCase):
         self.assertDictEqual(res, expected)
 
 
+    def test_dynamo_to_dict__mapping_doesnt_match__raises(self):
+        # If the value type in the DB doesn't match the expected type in row_mapper - raise ValueError
+
+        dynamo_row = {
+            'hash_col':   {'S': 'aaa'}, 'range_col': {'N': '123'},
+            'other_col': {'N': '111'}  # In the row_mapper, other_col is of type 'S'
+        }
+
+        with self.assertRaises(ValueError) as e:
+            dict_row = self.dynamo_client.dynamo_to_dict(dynamo_row)
+
+        self.assertEqual("'other_col' is expected to be of type 'S' in row_mapper, but real value is of type 'N'",
+                         str(e.exception))
+
+
     def test_get_by_query__validates_comparison(self):
         self.assertRaises(AssertionError, self.dynamo_client.get_by_query, keys={'k': '1'},
                           comparisons={'k': 'unsupported'})