add table delete and update. add item ops, including querying.

This commit is contained in:
Steve Pulec 2013-03-13 09:06:45 -04:00
commit 621ac79f33
3 changed files with 382 additions and 16 deletions

View file

@ -1,10 +1,11 @@
import boto
import sure # flake8: noqa
from freezegun import freeze_time
from moto import mock_dynamodb
from moto.dynamodb import dynamodb_backend
from boto.dynamodb.condition import GT
from boto.exception import DynamoDBResponseError
@ -36,19 +37,29 @@ def test_describe_missing_table():
conn.describe_table.when.called_with('messages').should.throw(DynamoDBResponseError)
def create_table(conn):
message_table_schema = conn.create_schema(
hash_key_name='forum_name',
hash_key_proto_value=str,
range_key_name='subject',
range_key_proto_value=str
)
table = conn.create_table(
name='messages',
schema=message_table_schema,
read_units=10,
write_units=10
)
return table
@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')
def test_create_table():
conn = boto.connect_dynamodb()
create_table(conn)
expected = {
'Table': {
'CreationDateTime': 1326499200.0,
@ -72,4 +83,147 @@ def test_describe_table():
'TableStatus': 'ACTIVE'
}
}
assert conn.describe_table('messages') == expected
conn.describe_table('messages').should.equal(expected)
@mock_dynamodb
def test_delete_table():
conn = boto.connect_dynamodb()
create_table(conn)
conn.list_tables().should.have.length_of(1)
conn.layer1.delete_table('messages')
conn.list_tables().should.have.length_of(0)
conn.layer1.delete_table.when.called_with('messages').should.throw(DynamoDBResponseError)
@mock_dynamodb
def test_update_table_throughput():
conn = boto.connect_dynamodb()
table = create_table(conn)
table.read_units.should.equal(10)
table.write_units.should.equal(10)
table.update_throughput(5, 6)
table.refresh()
table.read_units.should.equal(5)
table.write_units.should.equal(6)
@mock_dynamodb
def test_item_add_and_describe_and_update():
conn = boto.connect_dynamodb()
table = create_table(conn)
item_data = {
'Body': 'http://url_to_lolcat.gif',
'SentBy': 'User A',
'ReceivedTime': '12/9/2011 11:36:03 PM',
}
item = table.new_item(
hash_key='LOLCat Forum',
range_key='Check this out!',
attrs=item_data,
)
item.put()
returned_item = table.get_item(
hash_key='LOLCat Forum',
range_key='Check this out!',
attributes_to_get=['Body', 'SentBy']
)
dict(returned_item).should.equal({
'forum_name': 'LOLCat Forum',
'subject': 'Check this out!',
'Body': 'http://url_to_lolcat.gif',
'SentBy': 'User A',
})
item['SentBy'] = 'User B'
item.put()
returned_item = table.get_item(
hash_key='LOLCat Forum',
range_key='Check this out!',
attributes_to_get=['Body', 'SentBy']
)
dict(returned_item).should.equal({
'forum_name': 'LOLCat Forum',
'subject': 'Check this out!',
'Body': 'http://url_to_lolcat.gif',
'SentBy': 'User B',
})
@mock_dynamodb
def test_delete_item():
conn = boto.connect_dynamodb()
table = create_table(conn)
item_data = {
'Body': 'http://url_to_lolcat.gif',
'SentBy': 'User A',
'ReceivedTime': '12/9/2011 11:36:03 PM',
}
item = table.new_item(
hash_key='LOLCat Forum',
range_key='Check this out!',
attrs=item_data,
)
item.put()
table.refresh()
table.item_count.should.equal(1)
item.delete()
table.refresh()
table.item_count.should.equal(0)
item.delete.when.called_with().should.throw(DynamoDBResponseError)
@mock_dynamodb
def test_query():
conn = boto.connect_dynamodb()
table = create_table(conn)
item_data = {
'Body': 'http://url_to_lolcat.gif',
'SentBy': 'User A',
'ReceivedTime': '12/9/2011 11:36:03 PM',
}
item = table.new_item(
hash_key='the-key',
range_key='456',
attrs=item_data,
)
item.put()
item = table.new_item(
hash_key='the-key',
range_key='123',
attrs=item_data,
)
item.put()
item = table.new_item(
hash_key='the-key',
range_key='789',
attrs=item_data,
)
item.put()
results = table.query(hash_key='the-key', range_key_condition=GT('1'))
results.response['Items'].should.have.length_of(3)
results = table.query(hash_key='the-key', range_key_condition=GT('234'))
results.response['Items'].should.have.length_of(2)
results = table.query(hash_key='the-key', range_key_condition=GT('9999'))
results.response['Items'].should.have.length_of(0)
# Batch read
# Batch write
# scan