def test_errors() -> None:
        filter_expression_mock = MagicMock()
        projection_expression_mock = MagicMock()
        table_resource_mock = MagicMock()
        query = DynamoQuery.build_query(
            key_condition_expression=ConditionExpression("key", "contains"),
            index_name="my_index",
            filter_expression=filter_expression_mock,
            projection_expression=projection_expression_mock,
            limit=100,
        ).table(table=table_resource_mock, table_keys=("pk", "sk"))

        with pytest.raises(DynamoQueryError):
            query.execute_dict({"key": "value"})

        query = DynamoQuery.build_query(
            key_condition_expression=ConditionExpression("key"),
            index_name="my_index",
            filter_expression=filter_expression_mock,
            projection_expression=projection_expression_mock,
            limit=100,
        ).table(table=table_resource_mock, table_keys=("pk", "sk"))

        with pytest.raises(DynamoQueryError):
            query.execute_dict({"key1": "value"})

        with pytest.raises(DynamoQueryError):
            query.execute(DataTable({"key": [1, 2], "b": [3]}))

        with pytest.raises(DynamoQueryError):
            query.execute(DataTable({"key": [3, DataTable.NOT_SET]}))

        with pytest.raises(DynamoQueryError):
            DynamoQuery.build_batch_get_item().table(
                table=table_resource_mock, table_keys=("pk", "sk")).execute(
                    DataTable({
                        "pk": ["test"],
                        "sk": [DataTable.NOT_SET]
                    }))

        with pytest.raises(DynamoQueryError):
            DynamoQuery.build_batch_get_item().table(
                table=table_resource_mock,
                table_keys=("pk", "sk")).execute(DataTable({"pk": ["test"]}))
Пример #2
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_methods() -> None:
        table_resource_mock = MagicMock()
        query = DynamoQuery.build_query(
            key_condition_expression=ConditionExpression("key"),
            index_name="my_index",
            filter_expression=ConditionExpression("test"),
            limit=100,
        ).projection("return")

        with pytest.raises(DynamoQueryError):
            _ = query.table_resource

        with pytest.raises(DynamoQueryError):
            _ = query.table_keys

        query.table(table=table_resource_mock)
        assert str(query) == "<DynamoQuery type=query>"
        assert query.table_resource == table_resource_mock
        assert not query.was_executed()
        assert query.has_more_results()

        query.execute_dict({"key": "value", "test": "data"})
        assert query.was_executed()
        assert query.has_more_results()
        assert query.get_last_evaluated_key() == table_resource_mock.query(
        ).get()
        assert query.get_raw_responses() == [table_resource_mock.query()]

        table_resource_mock.key_schema = [
            {
                "AttributeName": "key"
            },
            {
                "NotKey": "not_key"
            },
        ]
        assert query.get_table_keys(table_resource_mock) == {"key"}
        assert query.limit(10)

        with pytest.raises(DynamoQueryError):
            DynamoQuery.build_batch_get_item().limit(10)
    def test_query() -> None:
        table_resource_mock = MagicMock()
        query = (DynamoQuery.build_query(
            key_condition_expression=ConditionExpression("pk"),
            index_name="my_index",
            filter_expression=ConditionExpression("test"),
            projection_expression=ProjectionExpression("test2"),
            exclusive_start_key={
                "pk": "my_pk",
                "sk": "my_sk"
            },
            limit=100,
        ).table(table=table_resource_mock,
                table_keys=("pk", "sk")).projection("test"))
        result = query.execute_dict({"pk": "pk_value", "test": "data"})
        table_resource_mock.query.assert_called_with(
            ConsistentRead=False,
            ExclusiveStartKey={
                "pk": "my_pk",
                "sk": "my_sk"
            },
            ExpressionAttributeNames={
                "#aaa": "pk",
                "#aab": "test"
            },
            ExpressionAttributeValues={
                ":aaa": "pk_value",
                ":aab": "data"
            },
            FilterExpression="#aab = :aab",
            IndexName="my_index",
            KeyConditionExpression="#aaa = :aaa",
            Limit=100,
            ProjectionExpression="#aab",
            ScanIndexForward=True,
        )
        assert list(result.get_records()) == []
        query.reset_start_key().execute(DataTable().add_record(
            {
                "pk": "pk_value",
                "test": "data"
            }, {
                "pk": "pk_value2",
                "test": "data"
            }))
        table_resource_mock.query.assert_called_with(
            ConsistentRead=False,
            ExclusiveStartKey=ANY,
            ExpressionAttributeNames={
                "#aaa": "pk",
                "#aab": "test"
            },
            ExpressionAttributeValues={
                ":aaa": "pk_value2",
                ":aab": "data"
            },
            FilterExpression="#aab = :aab",
            IndexName="my_index",
            KeyConditionExpression="#aaa = :aaa",
            Limit=100,
            ProjectionExpression="#aab",
            ScanIndexForward=True,
        )

        with pytest.raises(DynamoQueryError):
            query = (DynamoQuery.build_query(
                key_condition_expression=ConditionExpression("pk"),
                index_name="my_index",
                exclusive_start_key={
                    "pk": "my_pk"
                },
                limit=100,
            ).table(table=table_resource_mock,
                    table_keys=("pk",
                                "sk")).projection("test").execute_dict({}))