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'"
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"
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"
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"
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"