Skip to content

Commit

Permalink
Merge branch 'Issue_3' into devel
Browse files Browse the repository at this point in the history
* Issue_3:
  Correctly deserialize complex key types. Fixes #3
  Tests for refreshing objects with complex key types
  Added test data for issue 3
  • Loading branch information
jlafon committed Feb 4, 2014
2 parents d472899 + a8fbe47 commit b3e23f1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
9 changes: 7 additions & 2 deletions pynamodb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,16 @@ def from_raw_data(cls, data):
hash_keyname = cls.meta().hash_keyname
range_keyname = cls.meta().range_keyname
hash_key_type = cls.meta().get_attribute_type(hash_keyname)
args = (mutable_data.pop(hash_keyname).get(hash_key_type),)
hash_key = mutable_data.pop(hash_keyname).get(hash_key_type)
hash_key_attr = cls.get_attributes().get(hash_keyname)
hash_key = hash_key_attr.deserialize(hash_key)
args = (hash_key,)
kwargs = {}
if range_keyname:
range_key_attr = cls.get_attributes().get(range_keyname)
range_key_type = cls.meta().get_attribute_type(range_keyname)
kwargs['range_key'] = mutable_data.pop(range_keyname).get(range_key_type)
range_key = mutable_data.pop(range_keyname).get(range_key_type)
kwargs['range_key'] = range_key_attr.deserialize(range_key)
for name, value in mutable_data.items():
attr = cls.get_attributes().get(name, None)
if attr:
Expand Down
45 changes: 45 additions & 0 deletions pynamodb/tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@
}
}
}
COMPLEX_ITEM_DATA = {
"ConsumedCapacity": {
"CapacityUnits": 1,
"TableName": "Thread"
},
'Item': {
'date_created': {
'S': '2014-02-03T23:58:10.963333+0000'
},
'name': {
'S': 'bar'
}
}
}

GET_ITEM_DATA = {
"ConsumedCapacity": {
Expand Down Expand Up @@ -231,3 +245,34 @@
]
}
}

COMPLEX_TABLE_DATA = {
'Table': {
'ItemCount': 0, 'TableName': 'ComplexKey',
'ProvisionedThroughput': {
'ReadCapacityUnits': 2,
'WriteCapacityUnits': 2,
'NumberOfDecreasesToday': 0
},
'CreationDateTime': 1391471876.86,
'TableStatus': 'ACTIVE',
'AttributeDefinitions': [
{
'AttributeName': 'date_created', 'AttributeType': 'S'
},
{
'AttributeName': 'name', 'AttributeType': 'S'
}
],
'KeySchema': [
{
'AttributeName': 'name', 'KeyType': 'HASH'
},
{
'AttributeName': 'date_created', 'KeyType': 'RANGE'
}
],
'TableSizeBytes': 0
}
}

28 changes: 26 additions & 2 deletions pynamodb/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import copy
import six
from datetime import datetime
from pynamodb.throttle import Throttle
from pynamodb.connection.util import pythonic
from pynamodb.connection.exceptions import TableError
Expand All @@ -17,13 +18,15 @@
IncludeProjection, KeysOnlyProjection, Index
)
from pynamodb.attributes import (
UnicodeAttribute, NumberAttribute, BinaryAttribute,
UnicodeAttribute, NumberAttribute, BinaryAttribute, UTCDateTimeAttribute,
UnicodeSetAttribute, NumberSetAttribute, BinarySetAttribute)
from unittest import TestCase
from .response import HttpOK, HttpBadRequest
from .data import (
MODEL_TABLE_DATA, GET_MODEL_ITEM_DATA, SIMPLE_MODEL_TABLE_DATA,
BATCH_GET_ITEMS, SIMPLE_BATCH_GET_ITEMS)
BATCH_GET_ITEMS, SIMPLE_BATCH_GET_ITEMS, COMPLEX_TABLE_DATA,
COMPLEX_ITEM_DATA
)

# Py2/3
if six.PY3:
Expand Down Expand Up @@ -117,6 +120,15 @@ class UserModel(Model):
callable_field = NumberAttribute(default=lambda: 42)


class ComplexKeyModel(Model):
"""
This model has a key that must be serialized/deserialized properly
"""
table_name = 'ComplexKey'
name = UnicodeAttribute(hash_key=True)
date_created = UTCDateTimeAttribute(default=datetime.utcnow)


class ModelTestCase(TestCase):
"""
Tests for the models API
Expand Down Expand Up @@ -235,6 +247,18 @@ def test_refresh(self):
item.user_name,
GET_MODEL_ITEM_DATA.get(ITEM).get('user_name').get(STRING_SHORT))

def test_complex_key(self):
"""
Model with complex key
"""
with patch(PATCH_METHOD) as req:
req.return_value = HttpOK(), COMPLEX_TABLE_DATA
item = ComplexKeyModel('test')

with patch(PATCH_METHOD) as req:
req.return_value = HttpOK(COMPLEX_ITEM_DATA), COMPLEX_ITEM_DATA
item.refresh()

def test_delete(self):
"""
Model.delete
Expand Down

0 comments on commit b3e23f1

Please sign in to comment.