Beispiel #1
0
def test_resolver_multiple_mappings():
    # GIVEN
    app = AppSyncResolver()

    @app.resolver(field_name="listLocations")
    @app.resolver(field_name="locations")
    def get_locations(name: str, description: str = ""):
        return name + description

    # WHEN
    mock_event1 = {
        "typeName": "Query",
        "fieldName": "listLocations",
        "arguments": {
            "name": "value"
        }
    }
    mock_event2 = {
        "typeName": "Merchant",
        "fieldName": "locations",
        "arguments": {
            "name": "value2",
            "description": "description"
        },
    }
    result1 = app.resolve(mock_event1, LambdaContext())
    result2 = app.resolve(mock_event2, LambdaContext())

    # THEN
    assert result1 == "value"
    assert result2 == "value2description"
Beispiel #2
0
def test_resolver_value_error():
    # GIVEN no defined field resolver
    app = AppSyncResolver()

    # WHEN
    with pytest.raises(ValueError) as exp:
        event = {"typeName": "type", "fieldName": "field", "arguments": {}}
        app.resolve(event, LambdaContext())

    # THEN
    assert exp.value.args[0] == "No resolver found for 'type.field'"
Beispiel #3
0
def test_resolver_no_params():
    # GIVEN
    app = AppSyncResolver()

    @app.resolver(type_name="Query", field_name="noParams")
    def no_params():
        return "no_params has no params"

    event = {"typeName": "Query", "fieldName": "noParams", "arguments": {}}

    # WHEN
    result = app.resolve(event, LambdaContext())

    # THEN
    assert result == "no_params has no params"
Beispiel #4
0
def test_resolver_include_event():
    # GIVEN
    app = AppSyncResolver()

    mock_event = {"typeName": "Query", "fieldName": "field", "arguments": {}}

    @app.resolver(field_name="field", include_event=True)
    def get_value(event: AppSyncResolverEvent):
        return event

    # WHEN
    result = app.resolve(mock_event, LambdaContext())

    # THEN
    assert result._data == mock_event
    assert isinstance(result, AppSyncResolverEvent)
Beispiel #5
0
def test_resolver_include_context():
    # GIVEN
    app = AppSyncResolver()

    mock_event = {"typeName": "Query", "fieldName": "field", "arguments": {}}

    @app.resolver(field_name="field", include_context=True)
    def get_value(context: LambdaContext):
        return context

    # WHEN
    mock_context = LambdaContext()
    result = app.resolve(mock_event, mock_context)

    # THEN
    assert result == mock_context
Beispiel #6
0
def test_resolver_yield():
    # GIVEN
    app = AppSyncResolver()

    mock_event = {
        "typeName": "Customer",
        "fieldName": "field",
        "arguments": {}
    }

    @app.resolver(field_name="field")
    def func_yield():
        yield "value"

    # WHEN
    mock_context = LambdaContext()
    result = app.resolve(mock_event, mock_context)

    # THEN
    assert next(result) == "value"
Beispiel #7
0
def test_resolver_async():
    # GIVEN
    app = AppSyncResolver()

    mock_event = {
        "typeName": "Customer",
        "fieldName": "field",
        "arguments": {}
    }

    @app.resolver(field_name="field")
    async def get_async():
        await asyncio.sleep(0.0001)
        return "value"

    # WHEN
    mock_context = LambdaContext()
    result = app.resolve(mock_event, mock_context)

    # THEN
    assert asyncio.run(result) == "value"
Beispiel #8
0
def test_direct_resolver():
    # Check whether we can handle an example appsync direct resolver
    mock_event = load_event("appSyncDirectResolver.json")

    app = AppSyncResolver()

    @app.resolver(field_name="createSomething", include_context=True)
    def create_something(context, id: str):  # noqa AA03 VNE003
        assert context == {}
        return id

    def handler(event, context):
        return app.resolve(event, context)

    result = handler(mock_event, {})
    assert result == "my identifier"
Beispiel #9
0
def test_amplify_resolver():
    # Check whether we can handle an example appsync resolver
    mock_event = load_event("appSyncResolverEvent.json")

    app = AppSyncResolver()

    @app.resolver(type_name="Merchant",
                  field_name="locations",
                  include_event=True)
    def get_location(event: AppSyncResolverEvent, page: int, size: int,
                     name: str):
        assert event is not None
        assert page == 2
        assert size == 1
        return name

    def handler(event, context):
        return app.resolve(event, context)

    result = handler(mock_event, {})
    assert result == "value"