add tests for tables without range keys. tests back to 100%
This commit is contained in:
parent
cc23453d77
commit
11c1a2a4c1
5 changed files with 976 additions and 390 deletions
|
|
@ -36,7 +36,7 @@ class Item(object):
|
|||
|
||||
class Table(object):
|
||||
|
||||
def __init__(self, name, hash_key_attr=None, hash_key_type=None,
|
||||
def __init__(self, name, hash_key_attr, hash_key_type,
|
||||
range_key_attr=None, range_key_type=None, read_capacity=None,
|
||||
write_capacity=None):
|
||||
self.name = name
|
||||
|
|
@ -51,7 +51,7 @@ class Table(object):
|
|||
|
||||
@property
|
||||
def describe(self):
|
||||
return {
|
||||
results = {
|
||||
"Table": {
|
||||
"CreationDateTime": unix_time(self.created_at),
|
||||
"KeySchema": {
|
||||
|
|
@ -59,10 +59,6 @@ class Table(object):
|
|||
"AttributeName": self.hash_key_attr,
|
||||
"AttributeType": self.hash_key_type
|
||||
},
|
||||
"RangeKeyElement": {
|
||||
"AttributeName": self.range_key_attr,
|
||||
"AttributeType": self.range_key_type
|
||||
}
|
||||
},
|
||||
"ProvisionedThroughput": {
|
||||
"ReadCapacityUnits": self.read_capacity,
|
||||
|
|
@ -74,11 +70,20 @@ class Table(object):
|
|||
"TableSizeBytes": 0,
|
||||
}
|
||||
}
|
||||
if self.range_key_attr:
|
||||
results["Table"]["KeySchema"]["RangeKeyElement"] = {
|
||||
"AttributeName": self.range_key_attr,
|
||||
"AttributeType": self.range_key_type
|
||||
}
|
||||
return results
|
||||
|
||||
def __len__(self):
|
||||
count = 0
|
||||
for key, value in self.items.iteritems():
|
||||
count += len(value)
|
||||
if self.range_key_attr:
|
||||
count += len(value)
|
||||
else:
|
||||
count += 1
|
||||
return count
|
||||
|
||||
def __nonzero__(self):
|
||||
|
|
@ -86,14 +91,24 @@ class Table(object):
|
|||
|
||||
def put_item(self, item_attrs):
|
||||
hash_value = item_attrs.get(self.hash_key_attr).values()[0]
|
||||
range_value = item_attrs.get(self.range_key_attr).values()[0]
|
||||
if self.range_key_attr:
|
||||
range_value = item_attrs.get(self.range_key_attr).values()[0]
|
||||
else:
|
||||
range_value = None
|
||||
item = Item(hash_value, self.hash_key_type, range_value, self.range_key_type, item_attrs)
|
||||
self.items[hash_value][range_value] = item
|
||||
|
||||
if range_value:
|
||||
self.items[hash_value][range_value] = item
|
||||
else:
|
||||
self.items[hash_value] = item
|
||||
return item
|
||||
|
||||
def get_item(self, hash_key, range_key):
|
||||
try:
|
||||
return self.items[hash_key][range_key]
|
||||
if range_key:
|
||||
return self.items[hash_key][range_key]
|
||||
else:
|
||||
return self.items[hash_key]
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
|
|
@ -101,17 +116,24 @@ class Table(object):
|
|||
results = []
|
||||
last_page = True # Once pagination is implemented, change this
|
||||
|
||||
possible_results = self.items.get(hash_key, [])
|
||||
comparison_func = get_comparison_func(range_comparison)
|
||||
for result in possible_results.values():
|
||||
if comparison_func(result.range_key, *range_values):
|
||||
results.append(result)
|
||||
possible_results = list(self.all_items())
|
||||
if range_comparison:
|
||||
comparison_func = get_comparison_func(range_comparison)
|
||||
for result in possible_results:
|
||||
if comparison_func(result.range_key, *range_values):
|
||||
results.append(result)
|
||||
else:
|
||||
# If we're not filtering on range key, return all values
|
||||
results = possible_results
|
||||
return results, last_page
|
||||
|
||||
def all_items(self):
|
||||
for hash_set in self.items.values():
|
||||
for item in hash_set.values():
|
||||
yield item
|
||||
if self.range_key_attr:
|
||||
for item in hash_set.values():
|
||||
yield item
|
||||
else:
|
||||
yield hash_set
|
||||
|
||||
def scan(self, filters):
|
||||
results = []
|
||||
|
|
@ -146,7 +168,10 @@ class Table(object):
|
|||
|
||||
def delete_item(self, hash_key, range_key):
|
||||
try:
|
||||
return self.items[hash_key].pop(range_key)
|
||||
if range_key:
|
||||
return self.items[hash_key].pop(range_key)
|
||||
else:
|
||||
return self.items.pop(hash_key)
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
|
|
@ -187,14 +212,14 @@ class DynamoDBBackend(BaseBackend):
|
|||
def query(self, table_name, hash_key, range_comparison, range_values):
|
||||
table = self.tables.get(table_name)
|
||||
if not table:
|
||||
return None
|
||||
return None, None
|
||||
|
||||
return table.query(hash_key, range_comparison, range_values)
|
||||
|
||||
def scan(self, table_name, filters):
|
||||
table = self.tables.get(table_name)
|
||||
if not table:
|
||||
return None
|
||||
return None, None, None
|
||||
|
||||
return table.scan(filters)
|
||||
|
||||
|
|
|
|||
|
|
@ -57,9 +57,9 @@ class DynamoHandler(object):
|
|||
hash_key_attr = hash_hey['AttributeName']
|
||||
hash_key_type = hash_hey['AttributeType']
|
||||
|
||||
range_hey = key_schema['RangeKeyElement']
|
||||
range_key_attr = range_hey['AttributeName']
|
||||
range_key_type = range_hey['AttributeType']
|
||||
range_hey = key_schema.get('RangeKeyElement', {})
|
||||
range_key_attr = range_hey.get('AttributeName')
|
||||
range_key_type = range_hey.get('AttributeType')
|
||||
|
||||
throughput = body["ProvisionedThroughput"]
|
||||
read_units = throughput["ReadCapacityUnits"]
|
||||
|
|
@ -106,9 +106,13 @@ class DynamoHandler(object):
|
|||
name = body['TableName']
|
||||
item = body['Item']
|
||||
result = dynamodb_backend.put_item(name, item)
|
||||
item_dict = result.describe
|
||||
item_dict['ConsumedCapacityUnits'] = 1
|
||||
return json.dumps(item_dict)
|
||||
if result:
|
||||
item_dict = result.describe
|
||||
item_dict['ConsumedCapacityUnits'] = 1
|
||||
return json.dumps(item_dict)
|
||||
else:
|
||||
er = 'com.amazonaws.dynamodb.v20111205#ResourceNotFoundException'
|
||||
return self.error(er)
|
||||
|
||||
def BatchWriteItem(self, uri, body, headers):
|
||||
table_batches = body['RequestItems']
|
||||
|
|
@ -143,8 +147,9 @@ class DynamoHandler(object):
|
|||
|
||||
def GetItem(self, uri, body, headers):
|
||||
name = body['TableName']
|
||||
hash_key = body['Key']['HashKeyElement'].values()[0]
|
||||
range_key = body['Key']['RangeKeyElement'].values()[0]
|
||||
key = body['Key']
|
||||
hash_key = key['HashKeyElement'].values()[0]
|
||||
range_key = value_from_dynamo_type(key.get('RangeKeyElement'))
|
||||
attrs_to_get = body.get('AttributesToGet')
|
||||
item = dynamodb_backend.get_item(name, hash_key, range_key)
|
||||
if item:
|
||||
|
|
@ -181,23 +186,31 @@ class DynamoHandler(object):
|
|||
def Query(self, uri, body, headers):
|
||||
name = body['TableName']
|
||||
hash_key = body['HashKeyValue'].values()[0]
|
||||
range_condition = body['RangeKeyCondition']
|
||||
range_comparison = range_condition['ComparisonOperator']
|
||||
range_values = values_from_dynamo_types(range_condition['AttributeValueList'])
|
||||
range_condition = body.get('RangeKeyCondition')
|
||||
if range_condition:
|
||||
range_comparison = range_condition['ComparisonOperator']
|
||||
range_values = values_from_dynamo_types(range_condition['AttributeValueList'])
|
||||
else:
|
||||
range_comparison = range_values = None
|
||||
|
||||
items, last_page = dynamodb_backend.query(name, hash_key, range_comparison, range_values)
|
||||
|
||||
if items is None:
|
||||
er = 'com.amazonaws.dynamodb.v20111205#ResourceNotFoundException'
|
||||
return self.error(er)
|
||||
|
||||
result = {
|
||||
"Count": len(items),
|
||||
"Items": [item.attrs for item in items],
|
||||
"ConsumedCapacityUnits": 1,
|
||||
}
|
||||
|
||||
if not last_page:
|
||||
result["LastEvaluatedKey"] = {
|
||||
"HashKeyElement": items[-1].hash_key,
|
||||
"RangeKeyElement": items[-1].range_key,
|
||||
}
|
||||
# Implement this when we do pagination
|
||||
# if not last_page:
|
||||
# result["LastEvaluatedKey"] = {
|
||||
# "HashKeyElement": items[-1].hash_key,
|
||||
# "RangeKeyElement": items[-1].range_key,
|
||||
# }
|
||||
return json.dumps(result)
|
||||
|
||||
def Scan(self, uri, body, headers):
|
||||
|
|
@ -216,6 +229,10 @@ class DynamoHandler(object):
|
|||
|
||||
items, scanned_count, last_page = dynamodb_backend.scan(name, filters)
|
||||
|
||||
if items is None:
|
||||
er = 'com.amazonaws.dynamodb.v20111205#ResourceNotFoundException'
|
||||
return self.error(er)
|
||||
|
||||
result = {
|
||||
"Count": len(items),
|
||||
"Items": [item.attrs for item in items],
|
||||
|
|
@ -223,17 +240,19 @@ class DynamoHandler(object):
|
|||
"ScannedCount": scanned_count
|
||||
}
|
||||
|
||||
if not last_page:
|
||||
result["LastEvaluatedKey"] = {
|
||||
"HashKeyElement": items[-1].hash_key,
|
||||
"RangeKeyElement": items[-1].range_key,
|
||||
}
|
||||
# Implement this when we do pagination
|
||||
# if not last_page:
|
||||
# result["LastEvaluatedKey"] = {
|
||||
# "HashKeyElement": items[-1].hash_key,
|
||||
# "RangeKeyElement": items[-1].range_key,
|
||||
# }
|
||||
return json.dumps(result)
|
||||
|
||||
def DeleteItem(self, uri, body, headers):
|
||||
name = body['TableName']
|
||||
hash_key = body['Key']['HashKeyElement'].values()[0]
|
||||
range_key = body['Key']['RangeKeyElement'].values()[0]
|
||||
key = body['Key']
|
||||
hash_key = value_from_dynamo_type(key['HashKeyElement'])
|
||||
range_key = value_from_dynamo_type(key.get('RangeKeyElement'))
|
||||
return_values = body.get('ReturnValues', '')
|
||||
item = dynamodb_backend.delete_item(name, hash_key, range_key)
|
||||
if item:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue