Skip to content

Commit

Permalink
Allow longs as members of maps + lists in python 2 (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmphilli authored and danielhochman committed Nov 18, 2016
1 parent 9caef0e commit 295fa7a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
6 changes: 4 additions & 2 deletions pynamodb/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ def deserialize(self, values):
bool: BooleanAttribute(),
float: NumberAttribute(),
int: NumberAttribute(),
str: UnicodeAttribute()
str: UnicodeAttribute(),
}


Expand All @@ -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
2 changes: 1 addition & 1 deletion pynamodb/tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}}}
}
}
Expand Down
4 changes: 3 additions & 1 deletion pynamodb/tests/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 9 additions & 8 deletions pynamodb/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}

Expand All @@ -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'}}}
}
}
Expand All @@ -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 = {
Expand All @@ -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'}}}
}
}
Expand All @@ -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])

Expand Down

0 comments on commit 295fa7a

Please sign in to comment.