async def test_get_item_with_projection(client: Client, table: TableName): item = { "h": "hkv", "r": "rkv", "string-key": "this is a string", "number-key": 123, "list-key": ["hello", "world"], "nested": { "nested": "key" }, } await client.put_item(table, item) db_item = await client.get_item(table, { "h": "hkv", "r": "rkv" }, projection=F("string-key") & F("list-key", 1)) assert db_item == {"string-key": "this is a string", "list-key": ["world"]} db_item = await client.get_item(table, { "h": "hkv", "r": "rkv" }, projection=F("string-key")) assert db_item == {"string-key": "this is a string"}
async def test_put_item_with_condition_with_no_values(client: Client, table: TableName): await client.put_item( table, {"h": "h", "r": "1"}, condition=F("h").does_not_exist() ) with pytest.raises(errors.ConditionalCheckFailed): await client.put_item( table, {"h": "h", "r": "1"}, condition=F("h").does_not_exist() )
async def test_update_item_with_broken_update_expression( client: Client, table: TableName ): item = {"h": "h", "r": "r", "f": 1} await client.put_item(table, item) with pytest.raises(errors.ValidationException): await client.update_item( table, {"h": "h", "r": "r"}, F("f").set(2) & F("f").set(3) )
async def test_comparison_condition_expression(client: Client, table: TableName): key = {"h": "h", "r": "r"} await client.put_item(table, {**key, "v": "initial", "n": 5, "c": 6}) with pytest.raises(errors.ConditionalCheckFailed): await client.update_item( table, key, update_expression=F("v").set("unchanged"), condition=F("n").equals(F("c")), ) item = await client.get_item(table, key) assert item["v"] == "initial" await client.update_item( table, key, update_expression=F("v").set("changed"), condition=F("n").lt(F("c")), ) item = await client.get_item(table, key) assert item["v"] == "changed" await client.update_item( table, key, update_expression=F("v").set("final"), condition=F("n").gte(5), ) item = await client.get_item(table, key) assert item["v"] == "final"
async def test_empty_string(client: Client, table: TableName, real_dynamo: bool): if not real_dynamo: pytest.xfail("empty strings not supported by dynalite yet") key = {"h": "h", "r": "r"} await client.put_item(table, {**key, "s": ""}) assert await client.get_item(table, key) == {"h": "h", "r": "r", "s": ""} assert ( await client.update_item( table, key, F("foo").set("") & F("bar").set("baz"), return_values=ReturnValues.all_new, ) == {"h": "h", "r": "r", "bar": "baz", "s": ""} )
async def test_delete_item_with_conditions(client: Client, table: TableName): await client.put_item(table, {"h": "h", "r": "1", "d": "x"}) with pytest.raises(errors.ConditionalCheckFailed): await client.delete_item( table, {"h": "h", "r": "1"}, condition=F("d").does_not_exist() ) assert await client.get_item(table, {"h": "h", "r": "1"})
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"}]
async def test_update_item(client: Client, table: TableName): item = { "h": "hkv", "r": "rkv", "string-key": "this is a string", "number-key": 123, "list-key": ["hello", "world"], "set-key-one": {"hello", "world"}, "set-key-two": {"hello", "world"}, "dead-key": "foo", } await client.put_item(table, item) ue = ( F("string-key").set("new value") & F("number-key").change(-12) & F("list-key").append(["!"]) & F("set-key-one").add({"hoge"}) & F("dead-key").remove() & F("set-key-two").delete({"hello"}) ) resp = await client.update_item( table, {"h": "hkv", "r": "rkv"}, ue, return_values=ReturnValues.all_new ) assert resp == { "h": "hkv", "r": "rkv", "string-key": "new value", "number-key": 111, "list-key": ["hello", "world", "!"], "set-key-one": {"hello", "world", "hoge"}, "set-key-two": {"world"}, }
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())
async def test_empty_list(client: Client, table: TableName): key = {"h": "h", "r": "r"} await client.put_item(table, {**key, "l": [1]}) await client.update_item(table, key, F("l").set([])) assert await client.get_item(table, key) == {"h": "h", "r": "r", "l": []}
import pytest from aiodynamo.expressions import F, HashKey, Parameters @pytest.mark.parametrize( "pe,expression,names", [ (F("foo") & F("bar"), "#n0,#n1", { "#n0": "foo", "#n1": "bar" }), (F("foo", 0, "bar") & F("bar"), "#n0[0].#n1,#n1", { "#n0": "foo", "#n1": "bar" }), ( F("foo", "12", "bar") & F("bar"), "#n0.#n1.#n2,#n2", { "#n0": "foo", "#n1": "12", "#n2": "bar" }, ), ], ) def test_project(pe, expression, names): params = Parameters() assert pe.encode(params) == expression payload = params.to_request_payload()