def test_table_eq(self): """Tables should be equal""" field = DynamoKey("foo") a, b = Table("a", field), Table("a", field) self.assertEqual(a, b) self.assertEqual(hash(a), hash(b)) self.assertFalse(a != b)
def test_create_hash_table(self): """ Create a table with just a hash key """ hash_key = DynamoKey('id', data_type=STRING) table = Table('foobar', hash_key) self.dynamo.create_table('foobar', hash_key=hash_key) desc = self.dynamo.describe_table('foobar') self.assertEqual(desc, table)
def test_create_hash_range_table(self): """ Create a table with a hash and range key """ hash_key = DynamoKey('id', data_type=STRING) range_key = DynamoKey('num', data_type=NUMBER) table = Table('foobar', hash_key, range_key) self.dynamo.create_table('foobar', hash_key, range_key) desc = self.dynamo.describe_table('foobar') self.assertEqual(desc, table)
def test_create_table_throughput(self): """Create a table and set throughput""" hash_key = DynamoKey("id", data_type=STRING) throughput = Throughput(8, 2) table = Table("foobar", hash_key, throughput=throughput) self.dynamo.create_table("foobar", hash_key=hash_key, throughput=throughput) desc = self.dynamo.describe_table("foobar") self.assertEqual(desc, table)
def test_create_global_keys_index(self): """ Create a table with a global KeysOnly index """ hash_key = DynamoKey('id', data_type=STRING) index_field = DynamoKey('name') index = GlobalIndex.keys('name-index', index_field) table = Table('foobar', hash_key, global_indexes=[index]) self.dynamo.create_table('foobar', hash_key, global_indexes=[index]) desc = self.dynamo.describe_table('foobar') self.assertEqual(desc, table)
def test_create_global_index(self): """Create a table with a global index""" hash_key = DynamoKey("id", data_type=STRING) index_field = DynamoKey("name") index = GlobalIndex.all("name-index", index_field) table = Table("foobar", hash_key, global_indexes=[index]) self.dynamo.create_table("foobar", hash_key, global_indexes=[index]) desc = self.dynamo.describe_table("foobar") self.assertEqual(desc, table)
def test_create_global_hash_range_index(self): """ Create a global index with a hash and range key """ hash_key = DynamoKey('id', data_type=STRING) index_hash = DynamoKey('foo') index_range = DynamoKey('bar') index = GlobalIndex.all('foo-index', index_hash, index_range) table = Table('foobar', hash_key, global_indexes=[index]) self.dynamo.create_table('foobar', hash_key, global_indexes=[index]) desc = self.dynamo.describe_table('foobar') self.assertEqual(desc, table)
def test_create_local_includes_index(self): """Create a table with a local Includes index""" hash_key = DynamoKey("id", data_type=STRING) range_key = DynamoKey("num", data_type=NUMBER) index_field = DynamoKey("name") index = LocalIndex.include("name-index", index_field, includes=["foo", "bar"]) table = Table("foobar", hash_key, range_key, [index]) self.dynamo.create_table("foobar", hash_key, range_key, indexes=[index]) desc = self.dynamo.describe_table("foobar") self.assertEqual(desc, table)
def test_create_global_index_throughput(self): """Create a table and set throughput on global index""" hash_key = DynamoKey("id", data_type=STRING) throughput = Throughput(8, 2) index_field = DynamoKey("name") index = GlobalIndex.all("name-index", index_field, throughput=throughput) table = Table("foobar", hash_key, global_indexes=[index], throughput=throughput) self.dynamo.create_table( "foobar", hash_key=hash_key, global_indexes=[index], throughput=throughput ) desc = self.dynamo.describe_table("foobar") self.assertEqual(desc, table)
def test_create_local_index(self): """ Create a table with a local index """ hash_key = DynamoKey('id', data_type=STRING) range_key = DynamoKey('num', data_type=NUMBER) index_field = DynamoKey('name') index = LocalIndex.all('name-index', index_field) table = Table('foobar', hash_key, range_key, [index]) self.dynamo.create_table('foobar', hash_key, range_key, indexes=[index]) desc = self.dynamo.describe_table('foobar') self.assertEqual(desc, table)
def from_description(cls, table: Table) -> "TableMeta": """Factory method that uses the dynamo3 'describe' return value""" attrs = {} for attr in table.attribute_definitions: field = TableField(attr.name, TYPES_REV[attr.data_type]) attrs[field.name] = field for data in table.get("KeySchema", []): name = data["AttributeName"] attrs[name].key_type = data["KeyType"] for index in table.get("LocalSecondaryIndexes", []): for data in index["KeySchema"]: if data["KeyType"] == "RANGE": name = data["AttributeName"] index_type = index["Projection"]["ProjectionType"] includes = index["Projection"].get("NonKeyAttributes") attrs[name] = attrs[name].to_index(index_type, index["IndexName"], includes) break global_indexes = {} for index in table.global_indexes: global_indexes[index.name] = GlobalIndexMeta(index) return cls(table, attrs, global_indexes)
def test_describe_during_delete(self): """ Describing a table during a delete operation should not crash """ response = { 'ItemCount': 0, 'ProvisionedThroughput': { 'NumberOfDecreasesToday': 0, 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 }, 'TableName': 'myTableName', 'TableSizeBytes': 0, 'TableStatus': 'DELETING' } table = Table.from_response(response) self.assertEqual(table.status, 'DELETING')
def test_describe_during_delete(self): """ Describing a table during a delete operation should not crash """ response = { 'ItemCount': 0, 'ProvisionedThroughput': { 'NumberOfDecreasesToday': 0, 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 }, 'TableName': 'myTableName', 'TableSizeBytes': 0, 'TableStatus': 'DELETING' } table = Table.from_response(response) self.assertEqual(table.status, 'DELETING')
def test_describe_during_delete(self): """Describing a table during a delete operation should not crash""" response = { "ItemCount": 0, "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 5, "WriteCapacityUnits": 5, }, "TableName": "myTableName", "TableSizeBytes": 0, "TableStatus": "DELETING", } table = Table.from_response(response) self.assertEqual(table.status, "DELETING")