1# Copyright (c) 2013 Amazon.com, Inc. or its affiliates. 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""" 24Tests for Layer1 of DynamoDB v2 25""" 26import time 27 28from tests.unit import unittest 29from boto.dynamodb2 import exceptions 30from boto.dynamodb2.layer1 import DynamoDBConnection 31 32 33class DynamoDBv2Layer1Test(unittest.TestCase): 34 dynamodb = True 35 36 def setUp(self): 37 self.dynamodb = DynamoDBConnection() 38 self.table_name = 'test-%d' % int(time.time()) 39 self.hash_key_name = 'username' 40 self.hash_key_type = 'S' 41 self.range_key_name = 'date_joined' 42 self.range_key_type = 'N' 43 self.read_units = 5 44 self.write_units = 5 45 self.attributes = [ 46 { 47 'AttributeName': self.hash_key_name, 48 'AttributeType': self.hash_key_type, 49 }, 50 { 51 'AttributeName': self.range_key_name, 52 'AttributeType': self.range_key_type, 53 } 54 ] 55 self.schema = [ 56 { 57 'AttributeName': self.hash_key_name, 58 'KeyType': 'HASH', 59 }, 60 { 61 'AttributeName': self.range_key_name, 62 'KeyType': 'RANGE', 63 }, 64 ] 65 self.provisioned_throughput = { 66 'ReadCapacityUnits': self.read_units, 67 'WriteCapacityUnits': self.write_units, 68 } 69 self.lsi = [ 70 { 71 'IndexName': 'MostRecentIndex', 72 'KeySchema': [ 73 { 74 'AttributeName': self.hash_key_name, 75 'KeyType': 'HASH', 76 }, 77 { 78 'AttributeName': self.range_key_name, 79 'KeyType': 'RANGE', 80 }, 81 ], 82 'Projection': { 83 'ProjectionType': 'KEYS_ONLY', 84 } 85 } 86 ] 87 88 def create_table(self, table_name, attributes, schema, 89 provisioned_throughput, lsi=None, wait=True): 90 # Note: This is a slightly different ordering that makes less sense. 91 result = self.dynamodb.create_table( 92 attributes, 93 table_name, 94 schema, 95 provisioned_throughput, 96 local_secondary_indexes=lsi 97 ) 98 self.addCleanup(self.dynamodb.delete_table, table_name) 99 if wait: 100 while True: 101 description = self.dynamodb.describe_table(table_name) 102 if description['Table']['TableStatus'].lower() == 'active': 103 return result 104 else: 105 time.sleep(5) 106 else: 107 return result 108 109 def test_integrated(self): 110 result = self.create_table( 111 self.table_name, 112 self.attributes, 113 self.schema, 114 self.provisioned_throughput, 115 self.lsi 116 ) 117 self.assertEqual( 118 result['TableDescription']['TableName'], 119 self.table_name 120 ) 121 122 description = self.dynamodb.describe_table(self.table_name) 123 self.assertEqual(description['Table']['ItemCount'], 0) 124 125 # Create some records. 126 record_1_data = { 127 'username': {'S': 'johndoe'}, 128 'first_name': {'S': 'John'}, 129 'last_name': {'S': 'Doe'}, 130 'date_joined': {'N': '1366056668'}, 131 'friend_count': {'N': '3'}, 132 'friends': {'SS': ['alice', 'bob', 'jane']}, 133 } 134 r1_result = self.dynamodb.put_item(self.table_name, record_1_data) 135 136 # Get the data. 137 record_1 = self.dynamodb.get_item(self.table_name, key={ 138 'username': {'S': 'johndoe'}, 139 'date_joined': {'N': '1366056668'}, 140 }, consistent_read=True) 141 self.assertEqual(record_1['Item']['username']['S'], 'johndoe') 142 self.assertEqual(record_1['Item']['first_name']['S'], 'John') 143 self.assertEqual(record_1['Item']['friends']['SS'], [ 144 'alice', 'bob', 'jane' 145 ]) 146 147 # Now in a batch. 148 self.dynamodb.batch_write_item({ 149 self.table_name: [ 150 { 151 'PutRequest': { 152 'Item': { 153 'username': {'S': 'jane'}, 154 'first_name': {'S': 'Jane'}, 155 'last_name': {'S': 'Doe'}, 156 'date_joined': {'N': '1366056789'}, 157 'friend_count': {'N': '1'}, 158 'friends': {'SS': ['johndoe']}, 159 }, 160 }, 161 }, 162 ] 163 }) 164 165 # Now a query. 166 lsi_results = self.dynamodb.query( 167 self.table_name, 168 index_name='MostRecentIndex', 169 key_conditions={ 170 'username': { 171 'AttributeValueList': [ 172 {'S': 'johndoe'}, 173 ], 174 'ComparisonOperator': 'EQ', 175 }, 176 }, 177 consistent_read=True 178 ) 179 self.assertEqual(lsi_results['Count'], 1) 180 181 results = self.dynamodb.query(self.table_name, key_conditions={ 182 'username': { 183 'AttributeValueList': [ 184 {'S': 'jane'}, 185 ], 186 'ComparisonOperator': 'EQ', 187 }, 188 'date_joined': { 189 'AttributeValueList': [ 190 {'N': '1366050000'} 191 ], 192 'ComparisonOperator': 'GT', 193 } 194 }, consistent_read=True) 195 self.assertEqual(results['Count'], 1) 196 197 # Now a scan. 198 results = self.dynamodb.scan(self.table_name) 199 self.assertEqual(results['Count'], 2) 200 s_items = sorted([res['username']['S'] for res in results['Items']]) 201 self.assertEqual(s_items, ['jane', 'johndoe']) 202 203 self.dynamodb.delete_item(self.table_name, key={ 204 'username': {'S': 'johndoe'}, 205 'date_joined': {'N': '1366056668'}, 206 }) 207 208 results = self.dynamodb.scan(self.table_name) 209 self.assertEqual(results['Count'], 1) 210 211 # Parallel scan (minus client-side threading). 212 self.dynamodb.batch_write_item({ 213 self.table_name: [ 214 { 215 'PutRequest': { 216 'Item': { 217 'username': {'S': 'johndoe'}, 218 'first_name': {'S': 'Johann'}, 219 'last_name': {'S': 'Does'}, 220 'date_joined': {'N': '1366058000'}, 221 'friend_count': {'N': '1'}, 222 'friends': {'SS': ['jane']}, 223 }, 224 }, 225 'PutRequest': { 226 'Item': { 227 'username': {'S': 'alice'}, 228 'first_name': {'S': 'Alice'}, 229 'last_name': {'S': 'Expert'}, 230 'date_joined': {'N': '1366056800'}, 231 'friend_count': {'N': '2'}, 232 'friends': {'SS': ['johndoe', 'jane']}, 233 }, 234 }, 235 }, 236 ] 237 }) 238 time.sleep(20) 239 results = self.dynamodb.scan(self.table_name, segment=0, total_segments=2) 240 self.assertTrue(results['Count'] in [1, 2]) 241 results = self.dynamodb.scan(self.table_name, segment=1, total_segments=2) 242 self.assertTrue(results['Count'] in [1, 2]) 243 244 def test_without_range_key(self): 245 result = self.create_table( 246 self.table_name, 247 [ 248 { 249 'AttributeName': self.hash_key_name, 250 'AttributeType': self.hash_key_type, 251 }, 252 ], 253 [ 254 { 255 'AttributeName': self.hash_key_name, 256 'KeyType': 'HASH', 257 }, 258 ], 259 self.provisioned_throughput 260 ) 261 self.assertEqual( 262 result['TableDescription']['TableName'], 263 self.table_name 264 ) 265 266 description = self.dynamodb.describe_table(self.table_name) 267 self.assertEqual(description['Table']['ItemCount'], 0) 268 269 # Create some records. 270 record_1_data = { 271 'username': {'S': 'johndoe'}, 272 'first_name': {'S': 'John'}, 273 'last_name': {'S': 'Doe'}, 274 'date_joined': {'N': '1366056668'}, 275 'friend_count': {'N': '3'}, 276 'friends': {'SS': ['alice', 'bob', 'jane']}, 277 } 278 r1_result = self.dynamodb.put_item(self.table_name, record_1_data) 279 280 # Now try a range-less get. 281 johndoe = self.dynamodb.get_item(self.table_name, key={ 282 'username': {'S': 'johndoe'}, 283 }, consistent_read=True) 284 self.assertEqual(johndoe['Item']['username']['S'], 'johndoe') 285 self.assertEqual(johndoe['Item']['first_name']['S'], 'John') 286 self.assertEqual(johndoe['Item']['friends']['SS'], [ 287 'alice', 'bob', 'jane' 288 ]) 289 290 def test_throughput_exceeded_regression(self): 291 tiny_tablename = 'TinyThroughput' 292 tiny = self.create_table( 293 tiny_tablename, 294 self.attributes, 295 self.schema, 296 { 297 'ReadCapacityUnits': 1, 298 'WriteCapacityUnits': 1, 299 } 300 ) 301 302 self.dynamodb.put_item(tiny_tablename, { 303 'username': {'S': 'johndoe'}, 304 'first_name': {'S': 'John'}, 305 'last_name': {'S': 'Doe'}, 306 'date_joined': {'N': '1366056668'}, 307 }) 308 self.dynamodb.put_item(tiny_tablename, { 309 'username': {'S': 'jane'}, 310 'first_name': {'S': 'Jane'}, 311 'last_name': {'S': 'Doe'}, 312 'date_joined': {'N': '1366056669'}, 313 }) 314 self.dynamodb.put_item(tiny_tablename, { 315 'username': {'S': 'alice'}, 316 'first_name': {'S': 'Alice'}, 317 'last_name': {'S': 'Expert'}, 318 'date_joined': {'N': '1366057000'}, 319 }) 320 time.sleep(20) 321 322 for i in range(100): 323 # This would cause an exception due to a non-existant instance variable. 324 self.dynamodb.scan(tiny_tablename) 325 326 def test_recursive(self): 327 result = self.create_table( 328 self.table_name, 329 self.attributes, 330 self.schema, 331 self.provisioned_throughput, 332 self.lsi 333 ) 334 self.assertEqual( 335 result['TableDescription']['TableName'], 336 self.table_name 337 ) 338 339 description = self.dynamodb.describe_table(self.table_name) 340 self.assertEqual(description['Table']['ItemCount'], 0) 341 342 # Create some records with one being a recursive shape. 343 record_1_data = { 344 'username': {'S': 'johndoe'}, 345 'first_name': {'S': 'John'}, 346 'last_name': {'S': 'Doe'}, 347 'date_joined': {'N': '1366056668'}, 348 'friend_count': {'N': '3'}, 349 'friend_data': {'M': {'username': {'S': 'alice'}, 350 'friend_count': {'N': '4'}}} 351 } 352 r1_result = self.dynamodb.put_item(self.table_name, record_1_data) 353 354 # Get the data. 355 record_1 = self.dynamodb.get_item(self.table_name, key={ 356 'username': {'S': 'johndoe'}, 357 'date_joined': {'N': '1366056668'}, 358 }, consistent_read=True) 359 self.assertEqual(record_1['Item']['username']['S'], 'johndoe') 360 self.assertEqual(record_1['Item']['first_name']['S'], 'John') 361 recursive_data = record_1['Item']['friend_data']['M'] 362 self.assertEqual(recursive_data['username']['S'], 'alice') 363 self.assertEqual(recursive_data['friend_count']['N'], '4') 364