From 7e3aa7c8eea6d6bbe5fb02e84863c5e6632f71fc Mon Sep 17 00:00:00 2001 From: zmsmith Date: Tue, 19 Feb 2013 20:39:04 -0500 Subject: [PATCH] Adding describe table end point --- moto/dynamodb/models.py | 47 ++++++++++++++++++++++++++-- moto/dynamodb/responses.py | 6 ++++ moto/dynamodb/utils.py | 6 ++++ tests/test_dynamodb/test_dynamodb.py | 43 ++++++++++++++++++++++++- 4 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 moto/dynamodb/utils.py diff --git a/moto/dynamodb/models.py b/moto/dynamodb/models.py index 46199e625..880e73a8d 100644 --- a/moto/dynamodb/models.py +++ b/moto/dynamodb/models.py @@ -1,4 +1,47 @@ +import datetime + from moto.core import BaseBackend +from .utils import unix_time + + +class Table(object): + + def __init__(self, name, hash_key_attr=None, hash_key_type=None, + range_key_attr=None, range_key_type=None, read_capacity=None, + write_capacity=None): + self.name = name + self.hash_key_attr = hash_key_attr + self.hash_key_type = hash_key_type + self.range_key_attr = range_key_attr + self.range_key_type = range_key_type + self.read_capacity = read_capacity + self.write_capacity = write_capacity + self.created_at = datetime.datetime.now() + + @property + def describe(self): + return {"Table": { + "CreationDateTime": unix_time(self.created_at), + "KeySchema": { + "HashKeyElement": { + "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, + "WriteCapacityUnits": self.write_capacity + }, + "TableName": self.name, + "TableStatus": "ACTIVE", + "ItemCount": 0, + "TableSizeBytes": 0, + } + } class DynamoDBBackend(BaseBackend): @@ -6,7 +49,7 @@ class DynamoDBBackend(BaseBackend): def __init__(self): self.tables = {} - def create_table(self, name): - self.tables[name] = None + def create_table(self, name, **params): + self.tables[name] = Table(name, **params) dynamodb_backend = DynamoDBBackend() diff --git a/moto/dynamodb/responses.py b/moto/dynamodb/responses.py index 9fc616741..74417c4dc 100644 --- a/moto/dynamodb/responses.py +++ b/moto/dynamodb/responses.py @@ -1,5 +1,6 @@ import re import json + from .models import dynamodb_backend @@ -28,6 +29,11 @@ class DynamoHandler(object): response = {"TableNames": tables} return json.dumps(response) + def DescribeTable(self, uri, body, headers): + name = json.loads(body)['TableName'] + table = dynamodb_backend.tables[name] + return json.dumps(table.describe) + def handler(uri, body, headers): return DynamoHandler(uri, body, headers).dispatch() diff --git a/moto/dynamodb/utils.py b/moto/dynamodb/utils.py new file mode 100644 index 000000000..0c11c10cf --- /dev/null +++ b/moto/dynamodb/utils.py @@ -0,0 +1,6 @@ +import datetime + +def unix_time(dt): + epoch = datetime.datetime.utcfromtimestamp(0) + delta = dt - epoch + return delta.total_seconds() diff --git a/tests/test_dynamodb/test_dynamodb.py b/tests/test_dynamodb/test_dynamodb.py index fc815bfdf..837090aa9 100644 --- a/tests/test_dynamodb/test_dynamodb.py +++ b/tests/test_dynamodb/test_dynamodb.py @@ -1,12 +1,53 @@ import boto +from freezegun import freeze_time + from moto import mock_dynamodb from moto.dynamodb import dynamodb_backend @mock_dynamodb def test_list_tables(): - name = "TestTable" + name = 'TestTable' dynamodb_backend.create_table(name) conn = boto.connect_dynamodb('the_key', 'the_secret') assert conn.list_tables() == ['TestTable'] + + +@freeze_time("2012-01-14") +@mock_dynamodb +def test_describe_table(): + dynamodb_backend.create_table('messages', + hash_key_attr='forum_name', + hash_key_type='S', + range_key_attr='subject', + range_key_type='S', + read_capacity=10, + write_capacity=10, + ) + conn = boto.connect_dynamodb('the_key', 'the_secret') + expected = { + 'Table': { + 'CreationDateTime': 1326499200.0, + 'ItemCount': 0, + 'KeySchema': { + 'HashKeyElement': { + 'AttributeName': 'forum_name', + 'AttributeType': 'S' + }, + 'RangeKeyElement': { + 'AttributeName': 'subject', + 'AttributeType': 'S' + } + }, + 'ProvisionedThroughput': { + 'ReadCapacityUnits': 10, + 'WriteCapacityUnits': 10 + }, + 'TableName': 'messages', + 'TableSizeBytes': 0, + 'TableStatus': 'ACTIVE' + } + } + import pdb; pdb.set_trace() + assert conn.describe_table('messages') == expected