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 import time |
| 27 import base64 |
| 28 |
| 29 from tests.unit import unittest |
| 30 from boto.dynamodb.exceptions import DynamoDBKeyNotFoundError |
| 31 from boto.dynamodb.exceptions import DynamoDBConditionalCheckFailedError |
| 32 from boto.dynamodb.exceptions import DynamoDBValidationError |
| 33 from boto.dynamodb.layer1 import Layer1 |
| 34 |
| 35 |
| 36 class DynamoDBLayer1Test(unittest.TestCase): |
| 37 dynamodb = True |
| 38 |
| 39 def setUp(self): |
| 40 self.dynamodb = Layer1() |
| 41 self.table_name = 'test-%d' % int(time.time()) |
| 42 self.hash_key_name = 'forum_name' |
| 43 self.hash_key_type = 'S' |
| 44 self.range_key_name = 'subject' |
| 45 self.range_key_type = 'S' |
| 46 self.read_units = 5 |
| 47 self.write_units = 5 |
| 48 self.schema = {'HashKeyElement': {'AttributeName': self.hash_key_name, |
| 49 'AttributeType': self.hash_key_type}, |
| 50 'RangeKeyElement': {'AttributeName': self.range_key_name, |
| 51 'AttributeType': self.range_key_type}
} |
| 52 self.provisioned_throughput = {'ReadCapacityUnits': self.read_units, |
| 53 'WriteCapacityUnits': self.write_units} |
| 54 |
| 55 def tearDown(self): |
| 56 pass |
| 57 |
| 58 def create_table(self, table_name, schema, provisioned_throughput): |
| 59 result = self.dynamodb.create_table(table_name, schema, provisioned_thro
ughput) |
| 60 self.addCleanup(self.dynamodb.delete_table, table_name) |
| 61 return result |
| 62 |
| 63 def test_layer1_basic(self): |
| 64 print '--- running DynamoDB Layer1 tests ---' |
| 65 |
| 66 c = self.dynamodb |
| 67 |
| 68 # First create a table |
| 69 table_name = self.table_name |
| 70 hash_key_name = self.hash_key_name |
| 71 hash_key_type = self.hash_key_type |
| 72 range_key_name = self.range_key_name |
| 73 range_key_type = self.range_key_type |
| 74 read_units = self.read_units |
| 75 write_units = self.write_units |
| 76 schema = self.schema |
| 77 provisioned_throughput = self.provisioned_throughput |
| 78 |
| 79 result = self.create_table(table_name, schema, provisioned_throughput) |
| 80 assert result['TableDescription']['TableName'] == table_name |
| 81 result_schema = result['TableDescription']['KeySchema'] |
| 82 assert result_schema['HashKeyElement']['AttributeName'] == hash_key_name |
| 83 assert result_schema['HashKeyElement']['AttributeType'] == hash_key_type |
| 84 assert result_schema['RangeKeyElement']['AttributeName'] == range_key_na
me |
| 85 assert result_schema['RangeKeyElement']['AttributeType'] == range_key_ty
pe |
| 86 result_thruput = result['TableDescription']['ProvisionedThroughput'] |
| 87 assert result_thruput['ReadCapacityUnits'] == read_units |
| 88 assert result_thruput['WriteCapacityUnits'] == write_units |
| 89 |
| 90 # Wait for table to become active |
| 91 result = c.describe_table(table_name) |
| 92 while result['Table']['TableStatus'] != 'ACTIVE': |
| 93 time.sleep(5) |
| 94 result = c.describe_table(table_name) |
| 95 |
| 96 # List tables and make sure new one is there |
| 97 result = c.list_tables() |
| 98 assert table_name in result['TableNames'] |
| 99 |
| 100 # Update the tables ProvisionedThroughput |
| 101 new_read_units = 10 |
| 102 new_write_units = 5 |
| 103 new_provisioned_throughput = {'ReadCapacityUnits': new_read_units, |
| 104 'WriteCapacityUnits': new_write_units} |
| 105 result = c.update_table(table_name, new_provisioned_throughput) |
| 106 |
| 107 # Wait for table to be updated |
| 108 result = c.describe_table(table_name) |
| 109 while result['Table']['TableStatus'] == 'UPDATING': |
| 110 time.sleep(5) |
| 111 result = c.describe_table(table_name) |
| 112 |
| 113 result_thruput = result['Table']['ProvisionedThroughput'] |
| 114 assert result_thruput['ReadCapacityUnits'] == new_read_units |
| 115 assert result_thruput['WriteCapacityUnits'] == new_write_units |
| 116 |
| 117 # Put an item |
| 118 item1_key = 'Amazon DynamoDB' |
| 119 item1_range = 'DynamoDB Thread 1' |
| 120 item1_data = { |
| 121 hash_key_name: {hash_key_type: item1_key}, |
| 122 range_key_name: {range_key_type: item1_range}, |
| 123 'Message': {'S': 'DynamoDB thread 1 message text'}, |
| 124 'LastPostedBy': {'S': 'User A'}, |
| 125 'Views': {'N': '0'}, |
| 126 'Replies': {'N': '0'}, |
| 127 'Answered': {'N': '0'}, |
| 128 'Tags': {'SS': ["index", "primarykey", "table"]}, |
| 129 'LastPostDateTime': {'S': '12/9/2011 11:36:03 PM'} |
| 130 } |
| 131 result = c.put_item(table_name, item1_data) |
| 132 |
| 133 # Now do a consistent read and check results |
| 134 key1 = {'HashKeyElement': {hash_key_type: item1_key}, |
| 135 'RangeKeyElement': {range_key_type: item1_range}} |
| 136 result = c.get_item(table_name, key=key1, consistent_read=True) |
| 137 for name in item1_data: |
| 138 assert name in result['Item'] |
| 139 |
| 140 # Try to get an item that does not exist. |
| 141 invalid_key = {'HashKeyElement': {hash_key_type: 'bogus_key'}, |
| 142 'RangeKeyElement': {range_key_type: item1_range}} |
| 143 self.assertRaises(DynamoDBKeyNotFoundError, |
| 144 c.get_item, table_name, key=invalid_key) |
| 145 |
| 146 # Try retrieving only select attributes |
| 147 attributes = ['Message', 'Views'] |
| 148 result = c.get_item(table_name, key=key1, consistent_read=True, |
| 149 attributes_to_get=attributes) |
| 150 for name in result['Item']: |
| 151 assert name in attributes |
| 152 |
| 153 # Try to delete the item with the wrong Expected value |
| 154 expected = {'Views': {'Value': {'N': '1'}}} |
| 155 self.assertRaises(DynamoDBConditionalCheckFailedError, |
| 156 c.delete_item, table_name, key=key1, |
| 157 expected=expected) |
| 158 |
| 159 # Now update the existing object |
| 160 attribute_updates = {'Views': {'Value': {'N': '5'}, |
| 161 'Action': 'PUT'}, |
| 162 'Tags': {'Value': {'SS': ['foobar']}, |
| 163 'Action': 'ADD'}} |
| 164 result = c.update_item(table_name, key=key1, |
| 165 attribute_updates=attribute_updates) |
| 166 |
| 167 # Try and update an item, in a fashion which makes it too large. |
| 168 # The new message text is the item size limit minus 32 bytes and |
| 169 # the current object is larger than 32 bytes. |
| 170 item_size_overflow_text = 'Text to be padded'.zfill(64*1024-32) |
| 171 attribute_updates = {'Message': {'Value': {'S': item_size_overflow_text}
, |
| 172 'Action': 'PUT'}} |
| 173 self.assertRaises(DynamoDBValidationError, |
| 174 c.update_item, table_name, key=key1, |
| 175 attribute_updates=attribute_updates) |
| 176 |
| 177 |
| 178 # Put a few more items into the table |
| 179 item2_key = 'Amazon DynamoDB' |
| 180 item2_range = 'DynamoDB Thread 2' |
| 181 item2_data = { |
| 182 hash_key_name: {hash_key_type: item2_key}, |
| 183 range_key_name: {range_key_type: item2_range}, |
| 184 'Message': {'S': 'DynamoDB thread 2 message text'}, |
| 185 'LastPostedBy': {'S': 'User A'}, |
| 186 'Views': {'N': '0'}, |
| 187 'Replies': {'N': '0'}, |
| 188 'Answered': {'N': '0'}, |
| 189 'Tags': {'SS': ["index", "primarykey", "table"]}, |
| 190 'LastPostDateTime': {'S': '12/9/2011 11:36:03 PM'} |
| 191 } |
| 192 result = c.put_item(table_name, item2_data) |
| 193 key2 = {'HashKeyElement': {hash_key_type: item2_key}, |
| 194 'RangeKeyElement': {range_key_type: item2_range}} |
| 195 |
| 196 item3_key = 'Amazon S3' |
| 197 item3_range = 'S3 Thread 1' |
| 198 item3_data = { |
| 199 hash_key_name: {hash_key_type: item3_key}, |
| 200 range_key_name: {range_key_type: item3_range}, |
| 201 'Message': {'S': 'S3 Thread 1 message text'}, |
| 202 'LastPostedBy': {'S': 'User A'}, |
| 203 'Views': {'N': '0'}, |
| 204 'Replies': {'N': '0'}, |
| 205 'Answered': {'N': '0'}, |
| 206 'Tags': {'SS': ['largeobject', 'multipart upload']}, |
| 207 'LastPostDateTime': {'S': '12/9/2011 11:36:03 PM'} |
| 208 } |
| 209 result = c.put_item(table_name, item3_data) |
| 210 key3 = {'HashKeyElement': {hash_key_type: item3_key}, |
| 211 'RangeKeyElement': {range_key_type: item3_range}} |
| 212 |
| 213 # Try a few queries |
| 214 result = c.query(table_name, {'S': 'Amazon DynamoDB'}, |
| 215 {'AttributeValueList': [{'S': 'DynamoDB'}], |
| 216 'ComparisonOperator': 'BEGINS_WITH'}) |
| 217 assert 'Count' in result |
| 218 assert result['Count'] == 2 |
| 219 |
| 220 # Try a few scans |
| 221 result = c.scan(table_name, |
| 222 {'Tags': {'AttributeValueList':[{'S': 'table'}], |
| 223 'ComparisonOperator': 'CONTAINS'}}) |
| 224 assert 'Count' in result |
| 225 assert result['Count'] == 2 |
| 226 |
| 227 # Now delete the items |
| 228 result = c.delete_item(table_name, key=key1) |
| 229 result = c.delete_item(table_name, key=key2) |
| 230 result = c.delete_item(table_name, key=key3) |
| 231 |
| 232 print '--- tests completed ---' |
| 233 |
| 234 def test_binary_attributes(self): |
| 235 c = self.dynamodb |
| 236 result = self.create_table(self.table_name, self.schema, |
| 237 self.provisioned_throughput) |
| 238 # Wait for table to become active |
| 239 result = c.describe_table(self.table_name) |
| 240 while result['Table']['TableStatus'] != 'ACTIVE': |
| 241 time.sleep(5) |
| 242 result = c.describe_table(self.table_name) |
| 243 |
| 244 # Put an item |
| 245 item1_key = 'Amazon DynamoDB' |
| 246 item1_range = 'DynamoDB Thread 1' |
| 247 item1_data = { |
| 248 self.hash_key_name: {self.hash_key_type: item1_key}, |
| 249 self.range_key_name: {self.range_key_type: item1_range}, |
| 250 'Message': {'S': 'DynamoDB thread 1 message text'}, |
| 251 'LastPostedBy': {'S': 'User A'}, |
| 252 'Views': {'N': '0'}, |
| 253 'Replies': {'N': '0'}, |
| 254 'BinaryData': {'B': base64.b64encode(bytes('\x01\x02\x03\x04'))}, |
| 255 'Answered': {'N': '0'}, |
| 256 'Tags': {'SS': ["index", "primarykey", "table"]}, |
| 257 'LastPostDateTime': {'S': '12/9/2011 11:36:03 PM'} |
| 258 } |
| 259 result = c.put_item(self.table_name, item1_data) |
| 260 |
| 261 # Now do a consistent read and check results |
| 262 key1 = {'HashKeyElement': {self.hash_key_type: item1_key}, |
| 263 'RangeKeyElement': {self.range_key_type: item1_range}} |
| 264 result = c.get_item(self.table_name, key=key1, consistent_read=True) |
| 265 self.assertEqual(result['Item']['BinaryData'], |
| 266 {'B': base64.b64encode(bytes('\x01\x02\x03\x04'))}) |
OLD | NEW |