def test_create_table(self):
     """
     TableConnection.create_table
     """
     conn = TableConnection(self.test_table_name)
     kwargs = {"read_capacity_units": 1, "write_capacity_units": 1}
     self.assertRaises(ValueError, conn.create_table, **kwargs)
     kwargs["attribute_definitions"] = [
         {"attribute_name": "key1", "attribute_type": "S"},
         {"attribute_name": "key2", "attribute_type": "S"},
     ]
     self.assertRaises(ValueError, conn.create_table, **kwargs)
     kwargs["key_schema"] = [
         {"attribute_name": "key1", "key_type": "hash"},
         {"attribute_name": "key2", "key_type": "range"},
     ]
     params = {
         "TableName": "ci-table",
         "ProvisionedThroughput": {"WriteCapacityUnits": 1, "ReadCapacityUnits": 1},
         "AttributeDefinitions": [
             {"AttributeType": "S", "AttributeName": "key1"},
             {"AttributeType": "S", "AttributeName": "key2"},
         ],
         "KeySchema": [{"KeyType": "HASH", "AttributeName": "key1"}, {"KeyType": "RANGE", "AttributeName": "key2"}],
     }
     with patch(PATCH_METHOD) as req:
         req.return_value = {}
         conn.create_table(**kwargs)
         kwargs = req.call_args[0][1]
         self.assertEqual(kwargs, params)
Esempio n. 2
0
 def test_create_table_with_tags(self):
     conn = TableConnection(self.test_table_name)
     kwargs = {
         'read_capacity_units':
         1,
         'write_capacity_units':
         1,
         'attribute_definitions': [{
             'attribute_name': 'key1',
             'attribute_type': 'S'
         }, {
             'attribute_name': 'key2',
             'attribute_type': 'S'
         }],
         'key_schema': [{
             'attribute_name': 'key1',
             'key_type': 'hash'
         }, {
             'attribute_name': 'key2',
             'key_type': 'range'
         }],
         'tags': {
             'tag-key1': 'tag-value1',
             'tag-key2': 'tag-value2',
         }
     }
     params = {
         'TableName':
         'ci-table',
         'ProvisionedThroughput': {
             'WriteCapacityUnits': 1,
             'ReadCapacityUnits': 1
         },
         'AttributeDefinitions': [{
             'AttributeType': 'S',
             'AttributeName': 'key1'
         }, {
             'AttributeType': 'S',
             'AttributeName': 'key2'
         }],
         'KeySchema': [{
             'KeyType': 'HASH',
             'AttributeName': 'key1'
         }, {
             'KeyType': 'RANGE',
             'AttributeName': 'key2'
         }],
         'Tags': [{
             'Key': 'tag-key1',
             'Value': 'tag-value1'
         }, {
             'Key': 'tag-key2',
             'Value': 'tag-value2'
         }]
     }
     with patch(PATCH_METHOD) as req:
         req.return_value = {}
         conn.create_table(**kwargs)
         kwargs = req.call_args[0][1]
         self.assertEqual(kwargs, params)
Esempio n. 3
0
 def test_create_table(self):
     """
     TableConnection.create_table
     """
     conn = TableConnection(self.test_table_name)
     kwargs = {
         'read_capacity_units': 1,
         'write_capacity_units': 1,
     }
     self.assertRaises(ValueError, conn.create_table, **kwargs)
     kwargs['attribute_definitions'] = [{
         'attribute_name': 'key1',
         'attribute_type': 'S'
     }, {
         'attribute_name': 'key2',
         'attribute_type': 'S'
     }]
     self.assertRaises(ValueError, conn.create_table, **kwargs)
     kwargs['key_schema'] = [{
         'attribute_name': 'key1',
         'key_type': 'hash'
     }, {
         'attribute_name': 'key2',
         'key_type': 'range'
     }]
     params = {
         'TableName':
         'ci-table',
         'BillingMode':
         PROVISIONED_BILLING_MODE,
         'ProvisionedThroughput': {
             'WriteCapacityUnits': 1,
             'ReadCapacityUnits': 1
         },
         'AttributeDefinitions': [{
             'AttributeType': 'S',
             'AttributeName': 'key1'
         }, {
             'AttributeType': 'S',
             'AttributeName': 'key2'
         }],
         'KeySchema': [{
             'KeyType': 'HASH',
             'AttributeName': 'key1'
         }, {
             'KeyType': 'RANGE',
             'AttributeName': 'key2'
         }]
     }
     with patch(PATCH_METHOD) as req:
         req.return_value = {}
         conn.create_table(**kwargs)
         kwargs = req.call_args[0][1]
         self.assertEqual(kwargs, params)
Esempio n. 4
0
def test_table_integration(ddb_url):
    table_name = 'pynamodb-ci-table'

    # For use with a fake dynamodb connection
    # See: http://aws.amazon.com/dynamodb/developer-resources/
    conn = TableConnection(table_name, host=ddb_url)
    print(conn)

    print("conn.describe_table...")
    table = None
    try:
        table = conn.describe_table()
    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(**params)

    while table is None:
        time.sleep(2)
        table = conn.describe_table()
    while table['TableStatus'] == 'CREATING':
        time.sleep(5)
        print(table['TableStatus'])
        table = conn.describe_table()
    print("conn.update_table...")

    conn.update_table(read_capacity_units=table.get(
        PROVISIONED_THROUGHPUT).get(READ_CAPACITY_UNITS) + 1,
                      write_capacity_units=2)

    table = conn.describe_table()
    while table['TableStatus'] != 'ACTIVE':
        time.sleep(2)
        table = conn.describe_table()

    print("conn.put_item")
    conn.put_item(
        'item1-hash',
        range_key='item1-range',
        attributes={'foo': {
            'S': 'bar'
        }},
        condition=NotExists(Path('Forum')),
    )
    conn.get_item('item1-hash', range_key='item1-range')
    conn.delete_item('item1-hash', range_key='item1-range')

    items = []
    for i in range(10):
        items.append({"Forum": "FooForum", "Thread": "thread-{}".format(i)})
    print("conn.batch_write_items...")
    conn.batch_write_item(put_items=items)
    print("conn.batch_get_items...")
    data = conn.batch_get_item(items)
    print("conn.query...")
    conn.query(
        "FooForum",
        range_key_condition=(BeginsWith(Path('Thread'), Value('thread'))),
    )
    print("conn.scan...")
    conn.scan()
    print("conn.delete_table...")
    conn.delete_table()
Esempio n. 5
0
                        'KeyType': 'HASH',
                        'AttributeName': 'Forum'
                    },
                    {
                        'KeyType': 'RANGE',
                        'AttributeName': 'AltKey'
                    }
                ],
                'projection': {
                    'ProjectionType': 'KEYS_ONLY'
                }
            }
        ]
    }
    print("conn.create_table...")
    conn.create_table(**params)

while table is None:
    time.sleep(2)
    table = conn.describe_table()
while table['TableStatus'] == 'CREATING':
    time.sleep(5)
    print(table['TableStatus'])
    table = conn.describe_table()
print("conn.update_table...")

conn.update_table(
    read_capacity_units=table.get(PROVISIONED_THROUGHPUT).get(READ_CAPACITY_UNITS) + 1,
    write_capacity_units=2
)
Esempio n. 6
0
 def test_create_table(self):
     """
     TableConnection.create_table
     """
     conn = TableConnection(self.test_table_name)
     kwargs = {
         'read_capacity_units': 1,
         'write_capacity_units': 1,
     }
     self.assertRaises(ValueError, conn.create_table, **kwargs)
     kwargs['attribute_definitions'] = [
         {
             'attribute_name': 'key1',
             'attribute_type': 'S'
         },
         {
             'attribute_name': 'key2',
             'attribute_type': 'S'
         }
     ]
     self.assertRaises(ValueError, conn.create_table, **kwargs)
     kwargs['key_schema'] = [
         {
             'attribute_name': 'key1',
             'key_type': 'hash'
         },
         {
             'attribute_name': 'key2',
             'key_type': 'range'
         }
     ]
     params = {
         'TableName': 'ci-table',
         'ProvisionedThroughput': {
             'WriteCapacityUnits': 1,
             'ReadCapacityUnits': 1
         },
         'AttributeDefinitions': [
             {
                 'AttributeType': 'S',
                 'AttributeName': 'key1'
             },
             {
                 'AttributeType': 'S',
                 'AttributeName': 'key2'
             }
         ],
         'KeySchema': [
             {
                 'KeyType': 'HASH',
                 'AttributeName': 'key1'
             },
             {
                 'KeyType': 'RANGE',
                 'AttributeName': 'key2'
             }
         ]
     }
     with patch(PATCH_METHOD) as req:
         req.return_value = {}
         conn.create_table(
             **kwargs
         )
         kwargs = req.call_args[0][1]
         self.assertEqual(kwargs, params)
Esempio n. 7
0
            '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(**params)

while table is None:
    time.sleep(2)
    table = conn.describe_table()
while table['TableStatus'] == 'CREATING':
    time.sleep(5)
    print(table['TableStatus'])
    table = conn.describe_table()
print("conn.update_table...")

conn.update_table(read_capacity_units=table.get(PROVISIONED_THROUGHPUT).get(
    READ_CAPACITY_UNITS) + 1,
                  write_capacity_units=2)

table = conn.describe_table()