예제 #1
0
async def inner():
    async with ClientSession() as session:
        client = Client(AIOHTTP(session), Credentials.auto(), REGION_NAME)
        items = [
            item
            async for item in client.query(TABLE_NAME, Key(KEY_FIELD).eq(KEY_VALUE))
        ]
예제 #2
0
async def inner():
    async with AsyncClient() as http_client:
        client = Client(HTTPX(http_client), Credentials.auto(), REGION_NAME)
        items = [
            item async for item in client.query(TABLE_NAME,
                                                Key(KEY_FIELD).eq(KEY_VALUE))
        ]
예제 #3
0
async def test_scan_with_projection_only(client: Client, table: TableName):
    item1 = {"h": "h", "r": "1", "d": "x"}
    item2 = {"h": "h", "r": "2", "d": "y"}
    await client.put_item(table, item1)
    await client.put_item(table, item2)
    items = [item async for item in client.scan(table, projection=F("d"))]
    assert items == [{"d": "x"}, {"d": "y"}]
예제 #4
0
def client(http: HTTP, endpoint: URL, region: str) -> Client:
    yield Client(
        http,
        Credentials.auto(),
        region,
        endpoint,
    )
예제 #5
0
async def test_query_with_limit(client: Client,
                                high_throughput_table: TableName):
    big = "x" * 20_000

    await asyncio.gather(*(client.put_item(high_throughput_table, {
        "h": "h",
        "r": str(i),
        "big": big
    }) for i in range(100)))

    items = [
        item async for item in client.query(
            high_throughput_table, HashKey("h", "h"), limit=1)
    ]
    assert len(items) == 1
    assert items[0]["r"] == "0"
예제 #6
0
async def test_scan_with_limit(client: Client, table: TableName):
    item1 = {"h": "h", "r": "1", "d": "x"}
    item2 = {"h": "h", "r": "2", "d": "y"}
    await client.put_item(table, item1)
    await client.put_item(table, item2)
    items = [item async for item in client.scan(table, limit=1)]
    assert len(items) == 1
    assert items[0] == item1
예제 #7
0
async def test_no_credentials(http: HTTP, endpoint: URL, region: str):
    client = Client(
        http,
        ChainCredentials([]),
        region,
        endpoint,
        throttle_config=ExponentialBackoffThrottling(time_limit_secs=1),
    )
    with pytest.raises(NoCredentialsFound):
        await client.get_item("no-table", {"key": "no-key"})
예제 #8
0
async def test_scan(client: Client, table: TableName):
    item1 = {"h": "h", "r": "1", "d": "x"}
    item2 = {"h": "h", "r": "2", "d": "y"}
    items = [item1, item2]
    await client.put_item(table, item1)
    await client.put_item(table, item2)
    index = 0
    async for item in client.scan(table):
        assert item == items[index]
        index += 1
    assert index == 2
예제 #9
0
async def test_query_descending(client: Client, table: TableName):
    item1 = {"h": "h", "r": "1", "d": "x"}
    item2 = {"h": "h", "r": "2", "d": "y"}
    items = [item1, item2]
    await client.put_item(table, item1)
    await client.put_item(table, item2)
    rv = [
        item
        async for item in client.query(table, HashKey("h", "h"), scan_forward=False)
    ]
    assert rv == list(reversed(items))
예제 #10
0
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())
예제 #11
0
async def test_batch(client: Client, table: TableName):
    response = await client.batch_write(
        {
            table: BatchWriteRequest(
                items_to_put=[{"h": "h", "r": "1"}, {"h": "h", "r": "2"}]
            )
        }
    )
    assert not response
    assert len([item async for item in client.query(table, HashKey("h", "h"))]) == 2

    result = await client.batch_get(
        {table: BatchGetRequest(keys=[{"h": "h", "r": "1"}, {"h": "h", "r": "2"}])}
    )
    assert sorted(result.items[table], key=itemgetter("r")) == sorted(
        [{"h": "h", "r": "1"}, {"h": "h", "r": "2"}], key=itemgetter("r")
    )
    assert not result.unprocessed_keys

    response = await client.batch_write(
        {
            table: BatchWriteRequest(
                items_to_put=[{"h": "h", "r": "3"}],
                keys_to_delete=[{"h": "h", "r": "1"}],
            )
        }
    )
    assert not response
    assert len([item async for item in client.query(table, HashKey("h", "h"))]) == 2
    response = await client.batch_write(
        {
            table: BatchWriteRequest(
                keys_to_delete=[{"h": "h", "r": "2"}, {"h": "h", "r": "3"}],
            )
        }
    )
    assert not response
    assert len([item async for item in client.query(table, HashKey("h", "h"))]) == 0
예제 #12
0
async def test_query_and_scan_single_page(
    client: Client, high_throughput_table: TableName
):
    # query and scan are tested in the same method since creating all the items takes a long time
    big = "x" * 20_000

    await asyncio.gather(
        *(
            client.put_item(high_throughput_table, {"h": "h", "r": str(i), "big": big})
            for i in range(100)
        )
    )

    first_page = await client.query_single_page(
        high_throughput_table, HashKey("h", "h")
    )
    assert first_page.items
    assert first_page.last_evaluated_key
    assert not first_page.is_last_page
    second_page = await client.query_single_page(
        high_throughput_table,
        HashKey("h", "h"),
        start_key=first_page.last_evaluated_key,
    )
    assert not set(map(itemgetter("r"), first_page.items)) & set(
        map(itemgetter("r"), second_page.items)
    )

    first_page = await client.scan_single_page(high_throughput_table)
    assert first_page.items
    assert first_page.last_evaluated_key
    assert not first_page.is_last_page
    second_page = await client.scan_single_page(
        high_throughput_table,
        start_key=first_page.last_evaluated_key,
    )
    assert not set(map(itemgetter("r"), first_page.items)) & set(
        map(itemgetter("r"), second_page.items)
    )