Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Batch - Testing your code examples has syntax errors #1211

Closed
michaelbrewer opened this issue May 17, 2022 · 1 comment
Closed

Docs: Batch - Testing your code examples has syntax errors #1211

michaelbrewer opened this issue May 17, 2022 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@michaelbrewer
Copy link
Contributor

Expected Behaviour

Batch -Testing your code examples should not contain python syntax errors

Current Behaviour

Missing closing quote ["] present in the code examples

Screen Shot 2022-05-17 at 2 48 49 PM

Code snippet

import json

from pathlib import Path
from dataclasses import dataclass

import pytest
from src.app import lambda_handler, processor


def load_event(path: Path):
    with path.open() as f:
        return json.load(f)


@pytest.fixture
def lambda_context():
    @dataclass
    class LambdaContext:
        function_name: str = "test"
        memory_limit_in_mb: int = 128
        invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
        aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"

    return LambdaContext()

@pytest.fixture()
def sqs_event():
    """Generates API GW Event"""
    return load_event(path=Path("events/sqs_event.json"))


def test_app_batch_partial_response(sqs_event, lambda_context):
    # GIVEN
    processor = app.processor  # access processor for additional assertions
    successful_record = sqs_event["Records"][0]
    failed_record = sqs_event["Records"][1]
    expected_response = {
        "batchItemFailures: [
            {
                "itemIdentifier": failed_record["messageId"]
            }
        ]
    }

    # WHEN
    ret = app.lambda_handler(sqs_event, lambda_context)

    # THEN
    assert ret == expected_response
    assert len(processor.fail_messages) == 1
    assert processor.success_messages[0] == successful_record

Possible Solution

Fix the code testing examples as in #1114 and deployed as an example here: https://gyft.github.io/aws-lambda-powertools-python/latest/utilities/batch/#testing-your-code

import json
from dataclasses import dataclass
from pathlib import Path

import pytest
from src.app import lambda_handler, processor


def load_event(path: Path):
    with path.open() as f:
        return json.load(f)


@pytest.fixture
def lambda_context():
    @dataclass
    class LambdaContext:
        function_name: str = "test"
        memory_limit_in_mb: int = 128
        invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
        aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"

    return LambdaContext()


@pytest.fixture()
def sqs_event():
    """Generates API GW Event"""
    return load_event(path=Path("events/sqs_event.json"))


def test_app_batch_partial_response(sqs_event, lambda_context):
    # GIVEN
    processor = app.processor  # access processor for additional assertions
    successful_record = sqs_event["Records"][0]
    failed_record = sqs_event["Records"][1]
    expected_response = {
        "batchItemFailures": [
            {
                "itemIdentifier": failed_record["messageId"],
            },
        ],
    }

    # WHEN
    ret = app.lambda_handler(sqs_event, lambda_context)

    # THEN
    assert ret == expected_response
    assert len(processor.fail_messages) == 1
    assert processor.success_messages[0] == successful_record

Steps to Reproduce

  1. Copy code example from https://awslabs.github.io/aws-lambda-powertools-python/latest/utilities/batch/#testing-your-code
  2. Try to compile this and it should result in a Missing closing quote ["] error

AWS Lambda Powertools for Python version

latest

AWS Lambda function runtime

3.9

Packaging format used

PyPi

Debugging logs

N/A
@michaelbrewer michaelbrewer added bug Something isn't working triage Pending triage from maintainers labels May 17, 2022
@michaelbrewer michaelbrewer changed the title Bug: Batch Testing your code examples Bug: Batch Testing your code examples has syntax errors May 17, 2022
@michaelbrewer michaelbrewer changed the title Bug: Batch Testing your code examples has syntax errors Bug: Batch, Testing your code examples has syntax errors May 18, 2022
@michaelbrewer michaelbrewer changed the title Bug: Batch, Testing your code examples has syntax errors Bug: Batch - Testing your code examples has syntax errors May 18, 2022
@sthulb sthulb changed the title Bug: Batch - Testing your code examples has syntax errors Docs: Batch - Testing your code examples has syntax errors May 18, 2022
@heitorlessa heitorlessa added documentation Improvements or additions to documentation and removed bug Something isn't working labels May 20, 2022
@sthulb sthulb closed this as completed Jun 14, 2022
@github-actions
Copy link
Contributor

Comments on closed issues are hard for our team to see.

@heitorlessa heitorlessa removed the triage Pending triage from maintainers label Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants