Ejemplo n.º 1
0
def main() -> None:
    table = boto3.resource("dynamodb").Table("test_dq_users_table")
    user_records = [
        {
            "pk": "*****@*****.**",
            "sk": "IBM",
            "email": "*****@*****.**",
            "company": "IBM",
            "name": "John",
            "age": 34,
        },
        {
            "pk": "*****@*****.**",
            "sk": "CiscoSystems",
            "email": "*****@*****.**",
            "company": "CiscoSystems",
            "name": "Mary",
            "age": 34,
        },
    ]
    DynamoQuery.build_batch_update_item().table(
        table, table_keys={"pk", "sk"}).execute(user_records)

    print("Get all records:")
    for record in DynamoQuery.build_scan().table(table,
                                                 table_keys={"pk", "sk"
                                                             }).execute_dict():
        print(record)

    print("Get John's record:")
    for record in (DynamoQuery.build_get_item().table(
            table, table_keys={"pk", "sk"}).execute_dict({
                "pk": "*****@*****.**",
                "sk": "IBM"
            })):
        print(record)

    print("Query by a specific index:")
    for record in (DynamoQuery.build_query(
            index_name="gsi_name_age",
            key_condition_expression=ConditionExpression("name")).table(
                table, table_keys={"pk", "sk"}).execute_dict({
                    "name": "Mary",
                    "age": 34
                })):
        print(record)
 def test_get_item() -> None:
     table_resource_mock = MagicMock()
     query = (DynamoQuery.build_get_item(
         projection_expression=ProjectionExpression("test2"), ).table(
             table=table_resource_mock,
             table_keys=("pk", "sk")).projection("test"))
     result = query.execute_dict({"pk": "value", "sk": "value"})
     table_resource_mock.get_item.assert_called_with(
         ConsistentRead=False,
         ExpressionAttributeNames={"#aaa": "test"},
         Key={
             "pk": "value",
             "sk": "value"
         },
         ProjectionExpression="#aaa",
         ReturnConsumedCapacity="NONE",
     )
     assert list(result.get_records()) == [{"pk": "value", "sk": "value"}]