Better error messaging for dynamodb table gets for range key tables without range keys used. cc #28
This commit is contained in:
parent
755fe6563b
commit
549cb23b7f
@ -101,6 +101,10 @@ class Table(object):
|
|||||||
self.created_at = datetime.datetime.now()
|
self.created_at = datetime.datetime.now()
|
||||||
self.items = defaultdict(dict)
|
self.items = defaultdict(dict)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def has_range_key(self):
|
||||||
|
return self.range_key_attr is not None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def describe(self):
|
def describe(self):
|
||||||
results = {
|
results = {
|
||||||
@ -122,7 +126,7 @@ class Table(object):
|
|||||||
"TableSizeBytes": 0,
|
"TableSizeBytes": 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if self.range_key_attr:
|
if self.has_range_key:
|
||||||
results["Table"]["KeySchema"]["RangeKeyElement"] = {
|
results["Table"]["KeySchema"]["RangeKeyElement"] = {
|
||||||
"AttributeName": self.range_key_attr,
|
"AttributeName": self.range_key_attr,
|
||||||
"AttributeType": self.range_key_type
|
"AttributeType": self.range_key_type
|
||||||
@ -132,7 +136,7 @@ class Table(object):
|
|||||||
def __len__(self):
|
def __len__(self):
|
||||||
count = 0
|
count = 0
|
||||||
for key, value in self.items.iteritems():
|
for key, value in self.items.iteritems():
|
||||||
if self.range_key_attr:
|
if self.has_range_key:
|
||||||
count += len(value)
|
count += len(value)
|
||||||
else:
|
else:
|
||||||
count += 1
|
count += 1
|
||||||
@ -143,7 +147,7 @@ class Table(object):
|
|||||||
|
|
||||||
def put_item(self, item_attrs):
|
def put_item(self, item_attrs):
|
||||||
hash_value = DynamoType(item_attrs.get(self.hash_key_attr))
|
hash_value = DynamoType(item_attrs.get(self.hash_key_attr))
|
||||||
if self.range_key_attr:
|
if self.has_range_key:
|
||||||
range_value = DynamoType(item_attrs.get(self.range_key_attr))
|
range_value = DynamoType(item_attrs.get(self.range_key_attr))
|
||||||
else:
|
else:
|
||||||
range_value = None
|
range_value = None
|
||||||
@ -157,6 +161,8 @@ class Table(object):
|
|||||||
return item
|
return item
|
||||||
|
|
||||||
def get_item(self, hash_key, range_key):
|
def get_item(self, hash_key, range_key):
|
||||||
|
if self.has_range_key and not range_key:
|
||||||
|
raise ValueError("Table has a range key, but no range key was passed into get_item")
|
||||||
try:
|
try:
|
||||||
if range_key:
|
if range_key:
|
||||||
return self.items[hash_key][range_key]
|
return self.items[hash_key][range_key]
|
||||||
|
@ -188,12 +188,17 @@ class DynamoHandler(BaseResponse):
|
|||||||
hash_key = key['HashKeyElement']
|
hash_key = key['HashKeyElement']
|
||||||
range_key = key.get('RangeKeyElement')
|
range_key = key.get('RangeKeyElement')
|
||||||
attrs_to_get = self.body.get('AttributesToGet')
|
attrs_to_get = self.body.get('AttributesToGet')
|
||||||
item = dynamodb_backend.get_item(name, hash_key, range_key)
|
try:
|
||||||
|
item = dynamodb_backend.get_item(name, hash_key, range_key)
|
||||||
|
except ValueError:
|
||||||
|
er = 'com.amazon.coral.validate#ValidationException'
|
||||||
|
return self.error(er, status=400)
|
||||||
if item:
|
if item:
|
||||||
item_dict = item.describe_attrs(attrs_to_get)
|
item_dict = item.describe_attrs(attrs_to_get)
|
||||||
item_dict['ConsumedCapacityUnits'] = 0.5
|
item_dict['ConsumedCapacityUnits'] = 0.5
|
||||||
return dynamo_json_dump(item_dict)
|
return dynamo_json_dump(item_dict)
|
||||||
else:
|
else:
|
||||||
|
# Item not found
|
||||||
er = 'com.amazonaws.dynamodb.v20111205#ResourceNotFoundException'
|
er = 'com.amazonaws.dynamodb.v20111205#ResourceNotFoundException'
|
||||||
return self.error(er, status=404)
|
return self.error(er, status=404)
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ from moto import mock_dynamodb
|
|||||||
from moto.dynamodb import dynamodb_backend
|
from moto.dynamodb import dynamodb_backend
|
||||||
|
|
||||||
from boto.dynamodb import condition
|
from boto.dynamodb import condition
|
||||||
from boto.dynamodb.exceptions import DynamoDBKeyNotFoundError
|
from boto.dynamodb.exceptions import DynamoDBKeyNotFoundError, DynamoDBValidationError
|
||||||
from boto.exception import DynamoDBResponseError
|
from boto.exception import DynamoDBResponseError
|
||||||
|
|
||||||
|
|
||||||
@ -154,7 +154,7 @@ def test_get_missing_item():
|
|||||||
hash_key='tester',
|
hash_key='tester',
|
||||||
range_key='other',
|
range_key='other',
|
||||||
).should.throw(DynamoDBKeyNotFoundError)
|
).should.throw(DynamoDBKeyNotFoundError)
|
||||||
table.has_item("foobar").should.equal(False)
|
table.has_item("foobar", "more").should.equal(False)
|
||||||
|
|
||||||
|
|
||||||
@mock_dynamodb
|
@mock_dynamodb
|
||||||
@ -170,6 +170,30 @@ def test_get_item_with_undeclared_table():
|
|||||||
).should.throw(DynamoDBKeyNotFoundError)
|
).should.throw(DynamoDBKeyNotFoundError)
|
||||||
|
|
||||||
|
|
||||||
|
@mock_dynamodb
|
||||||
|
def test_get_item_without_range_key():
|
||||||
|
conn = boto.connect_dynamodb()
|
||||||
|
message_table_schema = conn.create_schema(
|
||||||
|
hash_key_name="test_hash",
|
||||||
|
hash_key_proto_value=int,
|
||||||
|
range_key_name="test_range",
|
||||||
|
range_key_proto_value=int,
|
||||||
|
)
|
||||||
|
table = conn.create_table(
|
||||||
|
name='messages',
|
||||||
|
schema=message_table_schema,
|
||||||
|
read_units=10,
|
||||||
|
write_units=10
|
||||||
|
)
|
||||||
|
|
||||||
|
hash_key = 3241526475
|
||||||
|
range_key = 1234567890987
|
||||||
|
new_item = table.new_item(hash_key=hash_key, range_key=range_key)
|
||||||
|
new_item.put()
|
||||||
|
|
||||||
|
table.get_item.when.called_with(hash_key=hash_key).should.throw(DynamoDBValidationError)
|
||||||
|
|
||||||
|
|
||||||
@mock_dynamodb
|
@mock_dynamodb
|
||||||
def test_delete_item():
|
def test_delete_item():
|
||||||
conn = boto.connect_dynamodb()
|
conn = boto.connect_dynamodb()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user