def test_connection_integration(ddb_url): table_name = 'pynamodb-ci-connection' # For use with a fake dynamodb connection # See: http://aws.amazon.com/dynamodb/developer-resources/ conn = Connection(host=ddb_url) print(conn) print("conn.describe_table...") table = None try: table = conn.describe_table(table_name) except TableDoesNotExist: params = { 'read_capacity_units': 1, 'write_capacity_units': 1, 'attribute_definitions': [{ 'attribute_type': STRING, 'attribute_name': 'Forum' }, { 'attribute_type': STRING, 'attribute_name': 'Thread' }, { 'attribute_type': STRING, 'attribute_name': 'AltKey' }, { 'attribute_type': NUMBER, 'attribute_name': 'number' }], 'key_schema': [{ 'key_type': HASH, 'attribute_name': 'Forum' }, { 'key_type': RANGE, 'attribute_name': 'Thread' }], 'global_secondary_indexes': [{ 'index_name': 'alt-index', 'key_schema': [{ 'KeyType': 'HASH', 'AttributeName': 'AltKey' }], 'projection': { 'ProjectionType': 'KEYS_ONLY' }, 'provisioned_throughput': { 'ReadCapacityUnits': 1, 'WriteCapacityUnits': 1, } }], 'local_secondary_indexes': [{ 'index_name': 'view-index', 'key_schema': [{ 'KeyType': 'HASH', 'AttributeName': 'Forum' }, { 'KeyType': 'RANGE', 'AttributeName': 'AltKey' }], 'projection': { 'ProjectionType': 'KEYS_ONLY' } }] } print("conn.create_table...") conn.create_table(table_name, **params) while table is None: time.sleep(1) table = conn.describe_table(table_name) while table['TableStatus'] == 'CREATING': time.sleep(2) table = conn.describe_table(table_name) print("conn.list_tables") conn.list_tables() print("conn.update_table...") conn.update_table(table_name, read_capacity_units=table.get( PROVISIONED_THROUGHPUT).get(READ_CAPACITY_UNITS) + 1, write_capacity_units=2) table = conn.describe_table(table_name) while table['TableStatus'] != 'ACTIVE': time.sleep(2) table = conn.describe_table(table_name) print("conn.put_item") conn.put_item( table_name, 'item1-hash', range_key='item1-range', attributes={'foo': { 'S': 'bar' }}, condition=NotExists(Path('Forum')), ) conn.get_item(table_name, 'item1-hash', range_key='item1-range') conn.delete_item(table_name, 'item1-hash', range_key='item1-range') items = [] for i in range(10): items.append({"Forum": "FooForum", "Thread": f"thread-{i}"}) print("conn.batch_write_items...") conn.batch_write_item(table_name, put_items=items) print("conn.batch_get_items...") data = conn.batch_get_item(table_name, items) print("conn.query...") conn.query( table_name, "FooForum", range_key_condition=(BeginsWith(Path('Thread'), Value('thread'))), ) print("conn.scan...") conn.scan(table_name, ) print("conn.delete_table...") conn.delete_table(table_name)
def test_create_table(self): """ Connection.create_table """ conn = Connection(self.region) kwargs = { 'read_capacity_units': 1, 'write_capacity_units': 1, } self.assertRaises(ValueError, conn.create_table, self.test_table_name, **kwargs) kwargs['attribute_definitions'] = [ { 'attribute_name': 'key1', 'attribute_type': 'S' }, { 'attribute_name': 'key2', 'attribute_type': 'S' } ] self.assertRaises(ValueError, conn.create_table, self.test_table_name, **kwargs) kwargs['key_schema'] = [ { 'attribute_name': 'key1', 'key_type': 'hash' }, { 'attribute_name': 'key2', 'key_type': 'range' } ] params = { 'table_name': 'ci-table', 'provisioned_throughput': { 'WriteCapacityUnits': 1, 'ReadCapacityUnits': 1 }, 'attribute_definitions': [ { 'AttributeType': 'S', 'AttributeName': 'key1' }, { 'AttributeType': 'S', 'AttributeName': 'key2' } ], 'key_schema': [ { 'KeyType': 'HASH', 'AttributeName': 'key1' }, { 'KeyType': 'RANGE', 'AttributeName': 'key2' } ] } with patch(PATCH_METHOD) as req: req.return_value = HttpBadRequest(), None self.assertRaises(TableError, conn.create_table, self.test_table_name, **kwargs) with patch(PATCH_METHOD) as req: req.return_value = HttpOK(), None conn.create_table( self.test_table_name, **kwargs ) self.assertEqual(req.call_args[1], params) kwargs['global_secondary_indexes'] = [ { 'index_name': 'alt-index', 'key_schema': [ { 'KeyType': 'HASH', 'AttributeName': 'AltKey' } ], 'projection': { 'ProjectionType': 'KEYS_ONLY' }, 'provisioned_throughput': { 'ReadCapacityUnits': 1, 'WriteCapacityUnits': 1, }, } ] params['global_secondary_indexes'] = [{'IndexName': 'alt-index', 'Projection': {'ProjectionType': 'KEYS_ONLY'}, 'KeySchema': [{'AttributeName': 'AltKey', 'KeyType': 'HASH'}], 'ProvisionedThroughput': {'ReadCapacityUnits': 1, 'WriteCapacityUnits': 1}}] with patch(PATCH_METHOD) as req: req.return_value = HttpOK(), None conn.create_table( self.test_table_name, **kwargs ) self.assertEqual(req.call_args[1], params) del(kwargs['global_secondary_indexes']) del(params['global_secondary_indexes']) kwargs['local_secondary_indexes'] = [ { 'index_name': 'alt-index', 'projection': { 'ProjectionType': 'KEYS_ONLY' }, 'key_schema': [ { 'AttributeName': 'AltKey', 'KeyType': 'HASH' } ], 'provisioned_throughput': { 'ReadCapacityUnits': 1, 'WriteCapacityUnits': 1 } } ] params['local_secondary_indexes'] = [ { 'Projection': { 'ProjectionType': 'KEYS_ONLY' }, 'KeySchema': [ { 'KeyType': 'HASH', 'AttributeName': 'AltKey' } ], 'IndexName': 'alt-index' } ] with patch(PATCH_METHOD) as req: req.return_value = HttpOK(), None conn.create_table( self.test_table_name, **kwargs ) self.assertEqual(req.call_args[1], params)
'KeyType': 'HASH', 'AttributeName': 'Forum' }, { 'KeyType': 'RANGE', 'AttributeName': 'AltKey' } ], 'projection': { 'ProjectionType': 'KEYS_ONLY' } } ] } print("conn.create_table...") conn.create_table(table_name, **params) while table is None: time.sleep(2) table = conn.describe_table(table_name) while table['TableStatus'] == 'CREATING': time.sleep(2) table = conn.describe_table(table_name) print("conn.list_tables") conn.list_tables() print("conn.update_table...") conn.update_table( table_name, read_capacity_units=table.get(PROVISIONED_THROUGHPUT).get(READ_CAPACITY_UNITS) + 1,
'attribute_definitions': [{ 'attribute_type': 'S', 'attribute_name': 'key1' }, { 'attribute_type': 'S', 'attribute_name': 'key2' }], 'key_schema': [{ 'key_type': 'HASH', 'attribute_name': 'key1' }, { 'key_type': 'RANGE', 'attribute_name': 'key2' }] } conn.create_table('table_name', **kwargs) # You can also use update_table to change the Provisioned Throughput capacity of Global Secondary Indexes: kwargs = { 'global_secondary_index_updates': [{ 'index_name': 'index_name', 'read_capacity_units': 10, 'write_capacity_units': 10 }] } conn.update_table('table_name', **kwargs) # Modifying items # The low level API can perform item operations too, such as getting an item:
'index_name': 'view-index', 'key_schema': [{ 'KeyType': 'HASH', 'AttributeName': 'Forum' }, { 'KeyType': 'RANGE', 'AttributeName': 'AltKey' }], 'projection': { 'ProjectionType': 'KEYS_ONLY' } }] } print("conn.create_table...") conn.create_table(table_name, **params) while table is None: time.sleep(2) table = conn.describe_table(table_name) while table['TableStatus'] == 'CREATING': time.sleep(2) table = conn.describe_table(table_name) print("conn.list_tables") conn.list_tables() print("conn.update_table...") conn.update_table(table_name, read_capacity_units=table.get(PROVISIONED_THROUGHPUT).get( READ_CAPACITY_UNITS) + 1,