async def test_count(client: Client, table: TableName): assert await client.count(table, HashKey("h", "h1")) == 0 assert await client.count(table, HashKey("h", "h2")) == 0 await client.put_item(table, {"h": "h1", "r": "r1"}) assert await client.count(table, HashKey("h", "h1")) == 1 assert await client.count(table, HashKey("h", "h2")) == 0 await client.put_item(table, {"h": "h2", "r": "r2"}) assert await client.count(table, HashKey("h", "h1")) == 1 assert await client.count(table, HashKey("h", "h2")) == 1 await client.put_item(table, {"h": "h2", "r": "r1"}) assert await client.count(table, HashKey("h", "h2")) == 2 assert await client.count(table, HashKey("h", "h1")) == 1 assert (await client.count( table, HashKey("h", "h1") & RangeKey("r").begins_with("x")) == 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))
async def test_query(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.query(table, HashKey("h", "h")): assert item == items[index] index += 1 assert index == 2
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) )
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
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"
}, ), ], ) def test_update_expression(exp, ue, ean, eav): params = Parameters() assert exp.encode(params) == ue payload = params.to_request_payload() assert payload["ExpressionAttributeNames"] == ean assert payload["ExpressionAttributeValues"] == eav @pytest.mark.parametrize( "hash_key,encoded,ean,eav", [ (HashKey("h", "v"), "#n0 = :v0", { "#n0": "h" }, { ":v0": { "S": "v" } }), ( HashKey("hash_key", "value"), "#n0 = :v0", { "#n0": "hash_key" }, { ":v0": { "S": "value"