Skip to content

Commit 1399f91

Browse files
betamooharleyk
authored andcommitted
Fixing binary data deserialization (pynamodb#459)
* fix Binary Data deserialization
1 parent 6f13d8a commit 1399f91

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

pynamodb/connection/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
PUT_ITEM, SELECT, ACTION, EXISTS, VALUE, LIMIT, QUERY, SCAN, ITEM, LOCAL_SECONDARY_INDEXES,
3434
KEYS, KEY, EQ, SEGMENT, TOTAL_SEGMENTS, CREATE_TABLE, PROVISIONED_THROUGHPUT, READ_CAPACITY_UNITS,
3535
WRITE_CAPACITY_UNITS, GLOBAL_SECONDARY_INDEXES, PROJECTION, EXCLUSIVE_START_TABLE_NAME, TOTAL,
36-
DELETE_TABLE, UPDATE_TABLE, LIST_TABLES, GLOBAL_SECONDARY_INDEX_UPDATES,
36+
DELETE_TABLE, UPDATE_TABLE, LIST_TABLES, GLOBAL_SECONDARY_INDEX_UPDATES, ATTRIBUTES,
3737
CONSUMED_CAPACITY, CAPACITY_UNITS, QUERY_FILTER, QUERY_FILTER_VALUES, CONDITIONAL_OPERATOR,
3838
CONDITIONAL_OPERATORS, NULL, NOT_NULL, SHORT_ATTR_TYPES, DELETE, PUT,
3939
ITEMS, DEFAULT_ENCODING, BINARY_SHORT, BINARY_SET_SHORT, LAST_EVALUATED_KEY, RESPONSES, UNPROCESSED_KEYS,
@@ -447,6 +447,9 @@ def _handle_binary_attributes(data):
447447
for item in six.itervalues(item_mapping):
448448
for attr in six.itervalues(item):
449449
_convert_binary(attr)
450+
if ATTRIBUTES in data:
451+
for attr in six.itervalues(data[ATTRIBUTES]):
452+
_convert_binary(attr)
450453
return data
451454

452455
@property
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pytest
2+
3+
from pynamodb.attributes import UnicodeAttribute, BinaryAttribute
4+
from pynamodb.models import Model
5+
6+
7+
@pytest.mark.ddblocal
8+
def test_update(ddb_url):
9+
class DataModel(Model):
10+
class Meta:
11+
table_name = 'binary_attr_update'
12+
host = ddb_url
13+
pkey = UnicodeAttribute(hash_key=True)
14+
data = BinaryAttribute()
15+
16+
DataModel.create_table(read_capacity_units=1, write_capacity_units=1)
17+
data = b'\x00hey\xfb'
18+
pkey = 'pkey'
19+
DataModel(pkey, data=data).save()
20+
m = DataModel.get(pkey)
21+
assert m.data == data
22+
23+
new_data = b'\xff'
24+
m.update(actions=[DataModel.data.set(new_data)])
25+
assert new_data == m.data
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
3+
import pytest
4+
5+
6+
@pytest.fixture
7+
def ddb_url():
8+
"""Obtain the URL of a local DynamoDB instance.
9+
10+
This is meant to be used with something like DynamoDB Local:
11+
12+
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html
13+
14+
It must be set up "out of band"; we merely assume it exists on
15+
http://localhost:8000 or a URL specified though the
16+
PYNAMODB_INTEGRATION_TEST_DDB_URL environment variable.
17+
"""
18+
ddb_url = os.getenv("PYNAMODB_INTEGRATION_TEST_DDB_URL")
19+
return "http://localhost:8000" if ddb_url is None else ddb_url

pynamodb/tests/integration/test_migration.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import pytest
32

43
from pynamodb.attributes import BooleanAttribute, LegacyBooleanAttribute, UnicodeAttribute
@@ -7,22 +6,6 @@
76
from pynamodb.models import Model
87

98

10-
@pytest.fixture()
11-
def ddb_url():
12-
"""Obtain the URL of a local DynamoDB instance.
13-
14-
This is meant to be used with something like DynamoDB Local:
15-
16-
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html
17-
18-
It must be set up "out of band"; we merely assume it exists on
19-
http://localhost:8000 or a URL specified though the
20-
PYNAMODB_INTEGRATION_TEST_DDB_URL environment variable.
21-
"""
22-
ddb_url = os.getenv("PYNAMODB_INTEGRATION_TEST_DDB_URL")
23-
return "http://localhost:8000" if ddb_url is None else ddb_url
24-
25-
269
@pytest.mark.ddblocal
2710
def test_migrate_boolean_attributes_upgrade_path(ddb_url):
2811
class BAModel(Model):

0 commit comments

Comments
 (0)