Skip to content

Commit 8ab34d0

Browse files
betamooharleyk
authored andcommitted
Use attr.get_value(value) when deserialize (pynamodb#450)
change update & update_item to use attr.get_value(value) in deserilazation
1 parent 9f94288 commit 8ab34d0

File tree

3 files changed

+139
-3
lines changed

3 files changed

+139
-3
lines changed

pynamodb/attributes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def get_value(self, value):
468468
# previously, BOOL was serialized as N
469469
value_to_deserialize = super(BooleanAttribute, self).get_value(value)
470470
if value_to_deserialize is None:
471-
value_to_deserialize = json.loads(value.get(NUMBER_SHORT, 0))
471+
value_to_deserialize = json.loads(value.get(NUMBER_SHORT, '0'))
472472
return value_to_deserialize
473473

474474

pynamodb/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ def update_item(self, attribute, value=None, action=None, condition=None, condit
389389
attr_name = self._dynamo_to_python_attr(name)
390390
attr = self._get_attributes().get(attr_name)
391391
if attr:
392-
setattr(self, attr_name, attr.deserialize(value.get(ATTR_TYPE_MAP[attr.attr_type])))
392+
setattr(self, attr_name, attr.deserialize(attr.get_value(value)))
393393
return data
394394

395395
def update(self, attributes=None, actions=None, condition=None, conditional_operator=None, **expected_values):
@@ -441,7 +441,7 @@ def update(self, attributes=None, actions=None, condition=None, conditional_oper
441441
attr_name = self._dynamo_to_python_attr(name)
442442
attr = self._get_attributes().get(attr_name)
443443
if attr:
444-
setattr(self, attr_name, attr.deserialize(value.get(ATTR_TYPE_MAP[attr.attr_type])))
444+
setattr(self, attr_name, attr.deserialize(attr.get_value(value)))
445445
return data
446446

447447
def save(self, condition=None, conditional_operator=None, **expected_values):

pynamodb/tests/integration/test_migration.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,139 @@ class Meta:
154154
BAModel.create_table(read_capacity_units=1, write_capacity_units=1)
155155
BAModel('pkey', flag=True).save()
156156
assert (0, 0) == migrate_boolean_attributes(BAModel, ['flag'], allow_rate_limited_scan_without_consumed_capacity=True)
157+
158+
159+
@pytest.mark.parametrize("flag_value",[True, False, None])
160+
@pytest.mark.ddblocal
161+
def test_legacy_boolean_attribute_deserialization_in_update(ddb_url, flag_value):
162+
class BAModel(Model):
163+
class Meta:
164+
table_name = 'lba_deserialization_test'
165+
host = ddb_url
166+
id = UnicodeAttribute(hash_key=True)
167+
flag = BooleanAttribute(null=True)
168+
value = UnicodeAttribute(null=True)
169+
170+
class LBAModel(Model):
171+
class Meta:
172+
table_name = 'lba_deserialization_test'
173+
host = ddb_url
174+
id = UnicodeAttribute(hash_key=True)
175+
flag = LegacyBooleanAttribute(null=True)
176+
value = UnicodeAttribute(null=True)
177+
178+
BAModel.create_table(read_capacity_units=1, write_capacity_units=1)
179+
180+
# Create objects with a BooleanAttribute flag
181+
BAModel('pkey', flag=flag_value, value = 'value').save()
182+
183+
# Check we are able to read the flag with LegacyBooleanAttribute
184+
assert flag_value == LBAModel.get('pkey').flag
185+
186+
# Update a value in the model causing LegacyBooleanAttribute to be deserialized
187+
LBAModel.get('pkey').update(actions=[LBAModel.value.set('new value')])
188+
189+
# Check we are able to read the flag with LegacyBooleanAttribute
190+
assert flag_value == LBAModel.get('pkey').flag
191+
192+
193+
@pytest.mark.parametrize("flag_value",[True, False, None])
194+
@pytest.mark.ddblocal
195+
def test_legacy_boolean_attribute_deserialization_in_update_item(ddb_url, flag_value):
196+
class BAModel(Model):
197+
class Meta:
198+
table_name = 'lba_deserialization_test'
199+
host = ddb_url
200+
id = UnicodeAttribute(hash_key=True)
201+
flag = BooleanAttribute(null=True)
202+
value = UnicodeAttribute(null=True)
203+
204+
class LBAModel(Model):
205+
class Meta:
206+
table_name = 'lba_deserialization_test'
207+
host = ddb_url
208+
id = UnicodeAttribute(hash_key=True)
209+
flag = LegacyBooleanAttribute(null=True)
210+
value = UnicodeAttribute(null=True)
211+
212+
BAModel.create_table(read_capacity_units=1, write_capacity_units=1)
213+
214+
# Create objects with a BooleanAttribute flag
215+
BAModel('pkey', flag=flag_value, value = 'value').save()
216+
217+
# Check we are able to read the flag with LegacyBooleanAttribute
218+
assert flag_value == LBAModel.get('pkey').flag
219+
220+
# Update a value in the model causing LegacyBooleanAttribute to be deserialized
221+
LBAModel.get('pkey').update_item('value', 'new value', 'PUT')
222+
223+
# Check we are able to read the flag with LegacyBooleanAttribute
224+
assert flag_value == LBAModel.get('pkey').flag
225+
226+
227+
@pytest.mark.parametrize("flag_value",[True, False, None])
228+
@pytest.mark.ddblocal
229+
def test_boolean_attribute_deserialization_in_update(ddb_url, flag_value):
230+
class BAModel(Model):
231+
class Meta:
232+
table_name = 'ba_deserialization_test'
233+
host = ddb_url
234+
id = UnicodeAttribute(hash_key=True)
235+
flag = BooleanAttribute(null=True)
236+
value = UnicodeAttribute(null=True)
237+
238+
class LBAModel(Model):
239+
class Meta:
240+
table_name = 'ba_deserialization_test'
241+
host = ddb_url
242+
id = UnicodeAttribute(hash_key=True)
243+
flag = LegacyBooleanAttribute(null=True)
244+
value = UnicodeAttribute(null=True)
245+
246+
LBAModel.create_table(read_capacity_units=1, write_capacity_units=1)
247+
248+
# Create an object with a LegacyBooleanAttribute flag
249+
LBAModel('pkey', flag=flag_value, value = 'value').save()
250+
251+
# Check we are able to read the flag with BooleanAttribute
252+
assert flag_value == BAModel.get('pkey').flag
253+
254+
# Update a value in the model causing BooleanAttribute to be deserialized
255+
BAModel.get('pkey').update(actions=[BAModel.value.set('new value')])
256+
257+
# Check we are able to read the flag with BooleanAttribute
258+
assert flag_value == BAModel.get('pkey').flag
259+
260+
261+
@pytest.mark.parametrize("flag_value",[True, False, None])
262+
@pytest.mark.ddblocal
263+
def test_boolean_attribute_deserialization_in_update_item(ddb_url, flag_value):
264+
class BAModel(Model):
265+
class Meta:
266+
table_name = 'ba_deserialization_test'
267+
host = ddb_url
268+
id = UnicodeAttribute(hash_key=True)
269+
flag = BooleanAttribute(null=True)
270+
value = UnicodeAttribute(null=True)
271+
272+
class LBAModel(Model):
273+
class Meta:
274+
table_name = 'ba_deserialization_test'
275+
host = ddb_url
276+
id = UnicodeAttribute(hash_key=True)
277+
flag = LegacyBooleanAttribute(null=True)
278+
value = UnicodeAttribute(null=True)
279+
280+
LBAModel.create_table(read_capacity_units=1, write_capacity_units=1)
281+
282+
# Create an object with a LegacyBooleanAttribute flag
283+
LBAModel('pkey', flag=flag_value, value = 'value').save()
284+
285+
# Check we are able to read the flag with BooleanAttribute
286+
assert flag_value == BAModel.get('pkey').flag
287+
288+
# Update a value in the model causing BooleanAttribute to be deserialized
289+
BAModel.get('pkey').update_item('value', 'new value', 'PUT')
290+
291+
# Check we are able to read the flag with BooleanAttribute
292+
assert flag_value == BAModel.get('pkey').flag

0 commit comments

Comments
 (0)