From 295fa7af9eceb2fe5b262bd11eac2b1785e91859 Mon Sep 17 00:00:00 2001 From: Justin Phillips Date: Fri, 18 Nov 2016 14:30:44 -0800 Subject: [PATCH] Allow longs as members of maps + lists in python 2 (#200) --- pynamodb/attributes.py | 6 ++++-- pynamodb/tests/data.py | 2 +- pynamodb/tests/test_attributes.py | 4 +++- pynamodb/tests/test_model.py | 17 +++++++++-------- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/pynamodb/attributes.py b/pynamodb/attributes.py index 136151d51..31e38472f 100644 --- a/pynamodb/attributes.py +++ b/pynamodb/attributes.py @@ -542,7 +542,7 @@ def deserialize(self, values): bool: BooleanAttribute(), float: NumberAttribute(), int: NumberAttribute(), - str: UnicodeAttribute() + str: UnicodeAttribute(), } @@ -553,10 +553,12 @@ def deserialize(self, values): bool: BOOLEAN, float: NUMBER_SHORT, int: NUMBER_SHORT, - str: STRING_SHORT + str: STRING_SHORT, } if six.PY2: SERIALIZE_CLASS_MAP[unicode] = UnicodeAttribute() + SERIALIZE_CLASS_MAP[long] = NumberAttribute() SERIALIZE_KEY_MAP[unicode] = STRING_SHORT + SERIALIZE_KEY_MAP[long] = NUMBER_SHORT diff --git a/pynamodb/tests/data.py b/pynamodb/tests/data.py index c61901e27..f3c9ef5e4 100644 --- a/pynamodb/tests/data.py +++ b/pynamodb/tests/data.py @@ -1233,7 +1233,7 @@ 'bool_type': {'BOOL': True}, 'other_b_type': {'BOOL': False}, 'floaty': {'N': '1.2'}, - 'listy': {'L': [{'N': '1'}, {'N': '2'}, {'N': '3'}]}, + 'listy': {'L': [{'N': '1'}, {'N': '2'}, {'N': '12345678909876543211234234324234'}]}, 'mapy': {'M': {'baz': {'S': 'bongo'}}} } } diff --git a/pynamodb/tests/test_attributes.py b/pynamodb/tests/test_attributes.py index fdc9661a1..24515e1c7 100644 --- a/pynamodb/tests/test_attributes.py +++ b/pynamodb/tests/test_attributes.py @@ -258,6 +258,7 @@ def test_number_serialize(self): attr = NumberAttribute() self.assertEqual(attr.serialize(3.141), '3.141') self.assertEqual(attr.serialize(1), '1') + self.assertEqual(attr.serialize(12345678909876543211234234324234), '12345678909876543211234234324234') def test_number_deserialize(self): """ @@ -266,6 +267,7 @@ def test_number_deserialize(self): attr = NumberAttribute() self.assertEqual(attr.deserialize('1'), 1) self.assertEqual(attr.deserialize('3.141'), 3.141) + self.assertEqual(attr.deserialize('12345678909876543211234234324234'), 12345678909876543211234234324234) def test_number_set_deserialize(self): """ @@ -518,7 +520,7 @@ class MapAttributeTestCase(TestCase): def test_attribute_children(self): person_attribute = { 'name': 'Justin', - 'age': 31, + 'age': 12345678909876543211234234324234, 'height': 187.96 } attr = MapAttribute() diff --git a/pynamodb/tests/test_model.py b/pynamodb/tests/test_model.py index ebb5cd53c..18b4febad 100644 --- a/pynamodb/tests/test_model.py +++ b/pynamodb/tests/test_model.py @@ -3053,13 +3053,13 @@ def test_explicit_raw_map_serialize_pass(self): def test_raw_map_serialize_fun_one(self): map_native = { - 'foo': 'bar', 'num': 1, 'bool_type': True, + 'foo': 'bar', 'num': 12345678909876543211234234324234, 'bool_type': True, 'other_b_type': False, 'floaty': 1.2, 'listy': [1,2,3], 'mapy': {'baz': 'bongo'} } expected = {'M': {'foo': {'S': u'bar'}, 'listy': {'L': [{'N': '1'}, {'N': '2'}, {'N': '3'}]}, - 'num': {'N': '1'}, 'other_b_type': {'BOOL': False}, + 'num': {'N': '12345678909876543211234234324234'}, 'other_b_type': {'BOOL': False}, 'floaty': {'N': '1.2'}, 'mapy': {'M': {'baz': {'S': u'bongo'}}}, 'bool_type': {'BOOL': True}}} @@ -3071,17 +3071,17 @@ def test_raw_map_serialize_fun_one(self): def test_raw_map_deserializes(self): map_native = { 'foo': 'bar', 'num': 1, 'bool_type': True, - 'other_b_type': False, 'floaty': 1.2, 'listy': [1, 2, 3], + 'other_b_type': False, 'floaty': 1.2, 'listy': [1, 2, 12345678909876543211234234324234], 'mapy': {'baz': 'bongo'} } map_serialized = { 'M': { 'foo': {'S': 'bar'}, - 'num': {'N': 1}, + 'num': {'N': '1'}, 'bool_type': {'BOOL': True}, 'other_b_type': {'BOOL': False}, - 'floaty': {'N': 1.2}, - 'listy': {'L': [{'N': 1}, {'N': 2}, {'N': 3}]}, + 'floaty': {'N': '1.2'}, + 'listy': {'L': [{'N': '1'}, {'N': '2'}, {'N': '12345678909876543211234234324234'}]}, 'mapy': {'M': {'baz': {'S': 'bongo'}}} } } @@ -3094,7 +3094,7 @@ def test_raw_map_deserializes(self): def test_raw_map_from_raw_data_works(self): map_native = { 'foo': 'bar', 'num': 1, 'bool_type': True, - 'other_b_type': False, 'floaty': 1.2, 'listy': [1, 2, 3], + 'other_b_type': False, 'floaty': 1.2, 'listy': [1, 2, 12345678909876543211234234324234], 'mapy': {'baz': 'bongo'} } map_serialized = { @@ -3104,7 +3104,7 @@ def test_raw_map_from_raw_data_works(self): 'bool_type': {'BOOL': True}, 'other_b_type': {'BOOL': False}, 'floaty': {'N': 1.2}, - 'listy': {'L': [{'N': 1}, {'N': 2}, {'N': 3}]}, + 'listy': {'L': [{'N': 1}, {'N': 2}, {'N': 1234567890987654321}]}, 'mapy': {'M': {'baz': {'S': 'bongo'}}} } } @@ -3117,6 +3117,7 @@ def test_raw_map_from_raw_data_works(self): with patch(PATCH_METHOD, new=fake_db) as req: item = ExplicitRawMapModel.get(123) actual = item.map_attr + self.assertEqual(map_native.get('listy')[2], actual.get('listy')[2]) for k, v in six.iteritems(map_native): self.assertEqual(v, actual[k])