| OLD | NEW |
| (Empty) | |
| 1 # Copyright (c) 2012 Mitch Garnaat http://garnaat.org/ |
| 2 # All rights reserved. |
| 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: |
| 11 # |
| 12 # The above copyright notice and this permission notice shall be included |
| 13 # in all copies or substantial portions of the Software. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 # IN THE SOFTWARE. |
| 22 |
| 23 """ |
| 24 Tests for Layer1 of DynamoDB |
| 25 """ |
| 26 |
| 27 import unittest |
| 28 import time |
| 29 from boto.dynamodb.exceptions import DynamoDBKeyNotFoundError |
| 30 from boto.dynamodb.layer1 import Layer1 |
| 31 from boto.sts.credentials import Credentials |
| 32 |
| 33 json_doc = """{"access_key": "ASIAIV7R2NUUJ6SB7GKQ", "secret_key": "eIfijGxJlejH
DSQiaGr6b7U805U0GKWmllCTt2ZM", "request_id": "28c17897-4555-11e1-8bb1-2529f165f2
f0", "expiration": "2012-01-23T00:59:45.617Z", "session_token": "AQoDYXdzEPn////
//////wEasAGDXeGY8bx36NLRSA1v3dy2x00k3FNA2KVsMEXkQuKY08gPTtYs2tefZTBsTjgjC+O6j8i
eoB1on2bPyCq872+Yq3cipls8jna+PNSEcsXtC8CJBKai/FfYNg1XUHam6EUCtRiUHvqztOVgaGqUUS1
UbrBKB7kKSXzgKrJ9AT0bvqi4hZS0ayaU8969f2HIbN9psXhRBKpJyB9FUPuVYpYYZsz9NY3y2kGtK+d
gfrKvxyDxxfL4BA=="}""" |
| 34 |
| 35 class DynamoDBLayer1Test (unittest.TestCase): |
| 36 |
| 37 def test_layer1_basic(self): |
| 38 print '--- running DynamoDB Layer1 tests ---' |
| 39 |
| 40 # Create a Layer1 connection with an expired set of |
| 41 # credentials to test the automatic renewal of tokens |
| 42 |
| 43 bad_creds = Credentials.from_json(json_doc) |
| 44 c = Layer1(session_token=bad_creds) |
| 45 |
| 46 # First create a table |
| 47 table_name = 'test-%d' % int(time.time()) |
| 48 hash_key_name = 'forum_name' |
| 49 hash_key_type = 'S' |
| 50 range_key_name = 'subject' |
| 51 range_key_type = 'S' |
| 52 read_units = 5 |
| 53 write_units = 5 |
| 54 schema = {'HashKeyElement': {'AttributeName': hash_key_name, |
| 55 'AttributeType': hash_key_type}, |
| 56 'RangeKeyElement': {'AttributeName': range_key_name, |
| 57 'AttributeType': range_key_type}} |
| 58 provisioned_throughput = {'ReadCapacityUnits': read_units, |
| 59 'WriteCapacityUnits': write_units} |
| 60 |
| 61 result = c.create_table(table_name, schema, provisioned_throughput) |
| 62 assert result['TableDescription']['TableName'] == table_name |
| 63 result_schema = result['TableDescription']['KeySchema'] |
| 64 assert result_schema['HashKeyElement']['AttributeName'] == hash_key_name |
| 65 assert result_schema['HashKeyElement']['AttributeType'] == hash_key_type |
| 66 assert result_schema['RangeKeyElement']['AttributeName'] == range_key_na
me |
| 67 assert result_schema['RangeKeyElement']['AttributeType'] == range_key_ty
pe |
| 68 result_thruput = result['TableDescription']['ProvisionedThroughput'] |
| 69 assert result_thruput['ReadCapacityUnits'] == read_units |
| 70 assert result_thruput['WriteCapacityUnits'] == write_units |
| 71 |
| 72 # Wait for table to become active |
| 73 result = c.describe_table(table_name) |
| 74 while result['Table']['TableStatus'] != 'ACTIVE': |
| 75 time.sleep(5) |
| 76 result = c.describe_table(table_name) |
| 77 |
| 78 # List tables and make sure new one is there |
| 79 result = c.list_tables() |
| 80 assert table_name in result['TableNames'] |
| 81 |
| 82 # Update the tables ProvisionedThroughput |
| 83 new_read_units = 10 |
| 84 new_write_units = 5 |
| 85 new_provisioned_throughput = {'ReadCapacityUnits': new_read_units, |
| 86 'WriteCapacityUnits': new_write_units} |
| 87 result = c.update_table(table_name, new_provisioned_throughput) |
| 88 |
| 89 # Wait for table to be updated |
| 90 result = c.describe_table(table_name) |
| 91 while result['Table']['TableStatus'] == 'UPDATING': |
| 92 time.sleep(5) |
| 93 result = c.describe_table(table_name) |
| 94 |
| 95 result_thruput = result['Table']['ProvisionedThroughput'] |
| 96 assert result_thruput['ReadCapacityUnits'] == new_read_units |
| 97 assert result_thruput['WriteCapacityUnits'] == new_write_units |
| 98 |
| 99 # Put an item |
| 100 item1_key = 'Amazon DynamoDB' |
| 101 item1_range = 'DynamoDB Thread 1' |
| 102 item1_data = { |
| 103 hash_key_name: {hash_key_type: item1_key}, |
| 104 range_key_name: {range_key_type: item1_range}, |
| 105 'Message': {'S': 'DynamoDB thread 1 message text'}, |
| 106 'LastPostedBy': {'S': 'User A'}, |
| 107 'Views': {'N': '0'}, |
| 108 'Replies': {'N': '0'}, |
| 109 'Answered': {'N': '0'}, |
| 110 'Tags': {'SS': ["index", "primarykey", "table"]}, |
| 111 'LastPostDateTime': {'S': '12/9/2011 11:36:03 PM'} |
| 112 } |
| 113 result = c.put_item(table_name, item1_data) |
| 114 |
| 115 # Now do a consistent read and check results |
| 116 key1 = {'HashKeyElement': {hash_key_type: item1_key}, |
| 117 'RangeKeyElement': {range_key_type: item1_range}} |
| 118 result = c.get_item(table_name, key=key1, consistent_read=True) |
| 119 for name in item1_data: |
| 120 assert name in result['Item'] |
| 121 |
| 122 # Try to get an item that does not exist. |
| 123 invalid_key = {'HashKeyElement': {hash_key_type: 'bogus_key'}, |
| 124 'RangeKeyElement': {range_key_type: item1_range}} |
| 125 self.assertRaises(DynamoDBKeyNotFoundError, |
| 126 c.get_item, table_name, key=invalid_key) |
| 127 |
| 128 # Try retrieving only select attributes |
| 129 attributes = ['Message', 'Views'] |
| 130 result = c.get_item(table_name, key=key1, consistent_read=True, |
| 131 attributes_to_get=attributes) |
| 132 for name in result['Item']: |
| 133 assert name in attributes |
| 134 |
| 135 # Try to delete the item with the wrong Expected value |
| 136 expected = {'Views': {'Value': {'N': '1'}}} |
| 137 try: |
| 138 result = c.delete_item(table_name, key=key1, expected=expected) |
| 139 except c.ResponseError, e: |
| 140 assert e.error_code == 'ConditionalCheckFailedException' |
| 141 |
| 142 # Now update the existing object |
| 143 attribute_updates = {'Views': {'Value': {'N': '5'}, |
| 144 'Action': 'PUT'}, |
| 145 'Tags': {'Value': {'SS': ['foobar']}, |
| 146 'Action': 'ADD'}} |
| 147 result = c.update_item(table_name, key=key1, |
| 148 attribute_updates=attribute_updates) |
| 149 |
| 150 # Put a few more items into the table |
| 151 item2_key = 'Amazon DynamoDB' |
| 152 item2_range = 'DynamoDB Thread 2' |
| 153 item2_data = { |
| 154 hash_key_name: {hash_key_type: item2_key}, |
| 155 range_key_name: {range_key_type: item2_range}, |
| 156 'Message': {'S': 'DynamoDB thread 2 message text'}, |
| 157 'LastPostedBy': {'S': 'User A'}, |
| 158 'Views': {'N': '0'}, |
| 159 'Replies': {'N': '0'}, |
| 160 'Answered': {'N': '0'}, |
| 161 'Tags': {'SS': ["index", "primarykey", "table"]}, |
| 162 'LastPostDateTime': {'S': '12/9/2011 11:36:03 PM'} |
| 163 } |
| 164 result = c.put_item(table_name, item2_data) |
| 165 key2 = {'HashKeyElement': {hash_key_type: item2_key}, |
| 166 'RangeKeyElement': {range_key_type: item2_range}} |
| 167 |
| 168 item3_key = 'Amazon S3' |
| 169 item3_range = 'S3 Thread 1' |
| 170 item3_data = { |
| 171 hash_key_name: {hash_key_type: item3_key}, |
| 172 range_key_name: {range_key_type: item3_range}, |
| 173 'Message': {'S': 'S3 Thread 1 message text'}, |
| 174 'LastPostedBy': {'S': 'User A'}, |
| 175 'Views': {'N': '0'}, |
| 176 'Replies': {'N': '0'}, |
| 177 'Answered': {'N': '0'}, |
| 178 'Tags': {'SS': ['largeobject', 'multipart upload']}, |
| 179 'LastPostDateTime': {'S': '12/9/2011 11:36:03 PM'} |
| 180 } |
| 181 result = c.put_item(table_name, item3_data) |
| 182 key3 = {'HashKeyElement': {hash_key_type: item3_key}, |
| 183 'RangeKeyElement': {range_key_type: item3_range}} |
| 184 |
| 185 # Try a few queries |
| 186 result = c.query(table_name, {'S': 'Amazon DynamoDB'}, |
| 187 {'AttributeValueList': [{'S': 'DynamoDB'}], |
| 188 'ComparisonOperator': 'BEGINS_WITH'}) |
| 189 assert 'Count' in result |
| 190 assert result['Count'] == 2 |
| 191 |
| 192 # Try a few scans |
| 193 result = c.scan(table_name, |
| 194 {'Tags': {'AttributeValueList':[{'S': 'table'}], |
| 195 'ComparisonOperator': 'CONTAINS'}}) |
| 196 assert 'Count' in result |
| 197 assert result['Count'] == 2 |
| 198 |
| 199 # Now delete the items |
| 200 result = c.delete_item(table_name, key=key1) |
| 201 result = c.delete_item(table_name, key=key2) |
| 202 result = c.delete_item(table_name, key=key3) |
| 203 |
| 204 # Now delete the table |
| 205 result = c.delete_table(table_name) |
| 206 assert result['TableDescription']['TableStatus'] == 'DELETING' |
| 207 |
| 208 print '--- tests completed ---' |
| 209 |
| OLD | NEW |