|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from aws_lambda_powertools.event_handler import AppSyncResolver |
| 4 | +from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent |
| 5 | +from tests.functional.utils import load_event |
| 6 | + |
| 7 | + |
| 8 | +def test_exception_handler_with_single_resolver(): |
| 9 | + # GIVEN a AppSyncResolver instance |
| 10 | + mock_event = load_event("appSyncDirectResolver.json") |
| 11 | + |
| 12 | + app = AppSyncResolver() |
| 13 | + |
| 14 | + # WHEN we configure exception handler for ValueError |
| 15 | + @app.exception_handler(ValueError) |
| 16 | + def handle_value_error(ex: ValueError): |
| 17 | + return {"message": "error"} |
| 18 | + |
| 19 | + @app.resolver(field_name="createSomething") |
| 20 | + def create_something(id: str): # noqa AA03 VNE003 |
| 21 | + raise ValueError("Error") |
| 22 | + |
| 23 | + # Call the implicit handler |
| 24 | + result = app(mock_event, {}) |
| 25 | + |
| 26 | + # THEN the return must be the Exception Handler error message |
| 27 | + assert result["message"] == "error" |
| 28 | + |
| 29 | + |
| 30 | +def test_exception_handler_with_batch_resolver_and_raise_exception(): |
| 31 | + |
| 32 | + # GIVEN a AppSyncResolver instance |
| 33 | + app = AppSyncResolver() |
| 34 | + |
| 35 | + event = [ |
| 36 | + { |
| 37 | + "typeName": "Query", |
| 38 | + "info": { |
| 39 | + "fieldName": "listLocations", |
| 40 | + "parentTypeName": "Post", |
| 41 | + }, |
| 42 | + "fieldName": "listLocations", |
| 43 | + "arguments": {}, |
| 44 | + "source": { |
| 45 | + "id": "1", |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + "typeName": "Query", |
| 50 | + "info": { |
| 51 | + "fieldName": "listLocations", |
| 52 | + "parentTypeName": "Post", |
| 53 | + }, |
| 54 | + "fieldName": "listLocations", |
| 55 | + "arguments": {}, |
| 56 | + "source": { |
| 57 | + "id": "2", |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + "typeName": "Query", |
| 62 | + "info": { |
| 63 | + "fieldName": "listLocations", |
| 64 | + "parentTypeName": "Post", |
| 65 | + }, |
| 66 | + "fieldName": "listLocations", |
| 67 | + "arguments": {}, |
| 68 | + "source": { |
| 69 | + "id": [3, 4], |
| 70 | + }, |
| 71 | + }, |
| 72 | + ] |
| 73 | + |
| 74 | + # WHEN we configure exception handler for ValueError |
| 75 | + @app.exception_handler(ValueError) |
| 76 | + def handle_value_error(ex: ValueError): |
| 77 | + return {"message": "error"} |
| 78 | + |
| 79 | + # WHEN the sync batch resolver for the 'listLocations' field is defined with raise_on_error=True |
| 80 | + @app.batch_resolver(field_name="listLocations", raise_on_error=True, aggregate=False) |
| 81 | + def create_something(event: AppSyncResolverEvent) -> Optional[list]: # noqa AA03 VNE003 |
| 82 | + raise ValueError |
| 83 | + |
| 84 | + # Call the implicit handler |
| 85 | + result = app(event, {}) |
| 86 | + |
| 87 | + # THEN the return must be the Exception Handler error message |
| 88 | + assert result["message"] == "error" |
| 89 | + |
| 90 | + |
| 91 | +def test_exception_handler_with_batch_resolver_and_no_raise_exception(): |
| 92 | + |
| 93 | + # GIVEN a AppSyncResolver instance |
| 94 | + app = AppSyncResolver() |
| 95 | + |
| 96 | + event = [ |
| 97 | + { |
| 98 | + "typeName": "Query", |
| 99 | + "info": { |
| 100 | + "fieldName": "listLocations", |
| 101 | + "parentTypeName": "Post", |
| 102 | + }, |
| 103 | + "fieldName": "listLocations", |
| 104 | + "arguments": {}, |
| 105 | + "source": { |
| 106 | + "id": "1", |
| 107 | + }, |
| 108 | + }, |
| 109 | + { |
| 110 | + "typeName": "Query", |
| 111 | + "info": { |
| 112 | + "fieldName": "listLocations", |
| 113 | + "parentTypeName": "Post", |
| 114 | + }, |
| 115 | + "fieldName": "listLocations", |
| 116 | + "arguments": {}, |
| 117 | + "source": { |
| 118 | + "id": "2", |
| 119 | + }, |
| 120 | + }, |
| 121 | + { |
| 122 | + "typeName": "Query", |
| 123 | + "info": { |
| 124 | + "fieldName": "listLocations", |
| 125 | + "parentTypeName": "Post", |
| 126 | + }, |
| 127 | + "fieldName": "listLocations", |
| 128 | + "arguments": {}, |
| 129 | + "source": { |
| 130 | + "id": [3, 4], |
| 131 | + }, |
| 132 | + }, |
| 133 | + ] |
| 134 | + |
| 135 | + # WHEN we configure exception handler for ValueError |
| 136 | + @app.exception_handler(ValueError) |
| 137 | + def handle_value_error(ex: ValueError): |
| 138 | + return {"message": "error"} |
| 139 | + |
| 140 | + # WHEN the sync batch resolver for the 'listLocations' field is defined with raise_on_error=False |
| 141 | + @app.batch_resolver(field_name="listLocations", raise_on_error=False, aggregate=False) |
| 142 | + def create_something(event: AppSyncResolverEvent) -> Optional[list]: # noqa AA03 VNE003 |
| 143 | + raise ValueError |
| 144 | + |
| 145 | + # Call the implicit handler |
| 146 | + result = app(event, {}) |
| 147 | + |
| 148 | + # THEN the return must not trigger the Exception Handler, but instead return from the resolver |
| 149 | + assert result == [None, None, None] |
0 commit comments