Table listing now supports limits and continuations
This commit is contained in:
parent
9e9e057289
commit
c196e15cf7
@ -1,5 +1,7 @@
|
||||
import datetime
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
from moto.core import BaseBackend
|
||||
from .utils import unix_time
|
||||
|
||||
@ -48,7 +50,7 @@ class Table(object):
|
||||
class DynamoDBBackend(BaseBackend):
|
||||
|
||||
def __init__(self):
|
||||
self.tables = {}
|
||||
self.tables = OrderedDict()
|
||||
|
||||
def create_table(self, name, **params):
|
||||
self.tables[name] = Table(name, **params)
|
||||
|
@ -32,12 +32,24 @@ class DynamoHandler(object):
|
||||
return "", dict(status=404)
|
||||
|
||||
def ListTables(self, uri, body, headers):
|
||||
tables = dynamodb_backend.tables.keys()
|
||||
limit = body.get('Limit')
|
||||
if body.get("ExclusiveStartTableName"):
|
||||
last = body.get("ExclusiveStartTableName")
|
||||
start = dynamodb_backend.tables.keys().index(last) + 1
|
||||
else:
|
||||
start = 0
|
||||
all_tables = dynamodb_backend.tables.keys()
|
||||
if limit:
|
||||
tables = all_tables[start:start + limit]
|
||||
else:
|
||||
tables = all_tables[start:]
|
||||
response = {"TableNames": tables}
|
||||
if limit and len(all_tables) > start + limit:
|
||||
response["LastEvaluatedTableName"] = tables[-1]
|
||||
return json.dumps(response)
|
||||
|
||||
def DescribeTable(self, uri, body, headers):
|
||||
name = json.loads(body)['TableName']
|
||||
name = body['TableName']
|
||||
try:
|
||||
table = dynamodb_backend.tables[name]
|
||||
except KeyError:
|
||||
@ -47,4 +59,5 @@ class DynamoHandler(object):
|
||||
|
||||
|
||||
def handler(uri, body, headers):
|
||||
body = json.loads(body or '{}')
|
||||
return DynamoHandler(uri, body, headers_to_dict(headers)).dispatch()
|
||||
|
@ -16,6 +16,20 @@ def test_list_tables():
|
||||
assert conn.list_tables() == ['TestTable']
|
||||
|
||||
|
||||
@mock_dynamodb
|
||||
def test_list_tables_layer_1():
|
||||
dynamodb_backend.create_table("test_1")
|
||||
dynamodb_backend.create_table("test_2")
|
||||
conn = boto.connect_dynamodb('the_key', 'the_secret')
|
||||
res = conn.layer1.list_tables(limit=1)
|
||||
expected = {"TableNames": ["test_1"], "LastEvaluatedTableName": "test_1"}
|
||||
res.should.equal(expected)
|
||||
|
||||
res = conn.layer1.list_tables(limit=1, start_table="test_1")
|
||||
expected = {"TableNames": ["test_2"]}
|
||||
res.should.equal(expected)
|
||||
|
||||
|
||||
@mock_dynamodb
|
||||
def test_describe_missing_table():
|
||||
conn = boto.connect_dynamodb('the_key', 'the_secret')
|
||||
|
Loading…
Reference in New Issue
Block a user