async def high_throughput_table( client: Client, table_factory: Callable[[Optional[Throughput]], Awaitable[str]] ): name = await table_factory(Throughput(1000, 2500)) try: yield name finally: await client.delete_table(name)
async def factory(throughput: Throughput = Throughput(5, 5)) -> str: name = table_name_prefix + str(uuid.uuid4()) await client.create_table( name, throughput, KeySchema(KeySpec("h", KeyType.string), KeySpec("r", KeyType.string)), wait_for_active=WaitConfig(max_attempts=25, retry_delay=5), ) return name
async def test_exists(client: Client, table_factory): throughput = Throughput(5, 5) key_schema = KeySchema(KeySpec("h", KeyType.string), KeySpec("r", KeyType.string)) attrs = {"h": KeyType.string, "r": KeyType.string} name = await table_factory() try: assert await client.table_exists(name) desc = await client.describe_table(name) assert desc.throughput == throughput assert desc.status is TableStatus.active assert desc.attributes == attrs assert desc.key_schema == key_schema assert desc.item_count == 0 finally: await client.delete_table(name) assert await client.table_exists(name) == False with pytest.raises(TableNotFound): await client.describe_table(name)
async def example(): async with ClientSession() as session: client = Client(AIOHTTP(session), Credentials.auto(), "us-east-1") table = client.table("my-table") # Create table if it doesn't exist if not await table.exists(): await table.create( Throughput(read=10, write=10), KeySchema(hash_key=KeySpec("key", KeyType.string)), ) # Create or override an item await table.put_item({"key": "my-item", "value": 1}) # Get an item item = await table.get_item({"key": "my-item"}) print(item) # Update an item, if it exists. await table.update_item({"key": "my-item"}, F("value").add(1), condition=F("key").exists())