[test] add lambda_handler "real" test case
Browse files- src/app.py +1 -1
- tests/events/lambda_handler.json +66 -0
- tests/test_app.py +32 -1
src/app.py
CHANGED
@@ -10,7 +10,7 @@ from src.io.lambda_helpers import get_parsed_request_body, get_parsed_bbox_point
|
|
10 |
from src.prediction_api.predictors import samexporter_predict
|
11 |
|
12 |
|
13 |
-
def lambda_handler(event: dict, context: LambdaContext):
|
14 |
"""
|
15 |
Handle the request for the serverless backend, dispatch the response.
|
16 |
|
|
|
10 |
from src.prediction_api.predictors import samexporter_predict
|
11 |
|
12 |
|
13 |
+
def lambda_handler(event: dict, context: LambdaContext) -> str:
|
14 |
"""
|
15 |
Handle the request for the serverless backend, dispatch the response.
|
16 |
|
tests/events/lambda_handler.json
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"input": {
|
3 |
+
"version": "2.0",
|
4 |
+
"routeKey": "POST /lambda-ml-fastsam-api",
|
5 |
+
"rawPath": "/prod/lambda-ml-fastsam-api",
|
6 |
+
"rawQueryString": "",
|
7 |
+
"headers": {
|
8 |
+
"accept-encoding": "gzip",
|
9 |
+
"authorization": "Bearer part1.part2.part3",
|
10 |
+
"cdn-loop": "cloudflare; subreqs=1",
|
11 |
+
"cf-connecting-ip": "2a06:98c0:3600::103",
|
12 |
+
"cf-ew-via": "15",
|
13 |
+
"cf-ray": "cf-ray-MXP",
|
14 |
+
"cf-worker": "ml-tornidor.pages.dev",
|
15 |
+
"content-length": "255",
|
16 |
+
"content-type": "application/json",
|
17 |
+
"host": "domain.execute-api.eu-west-1.amazonaws.com",
|
18 |
+
"x-amzn-trace-id": "Root=1-656316af-2acac2173efc7cf468ea20cc",
|
19 |
+
"x-forwarded-for": "2a06:98c0:3600::103, 172.71.114.177",
|
20 |
+
"x-forwarded-port": "443",
|
21 |
+
"x-forwarded-proto": "https"
|
22 |
+
},
|
23 |
+
"requestContext": {
|
24 |
+
"accountId": "686901913580",
|
25 |
+
"apiId": "domain",
|
26 |
+
"authorizer": {
|
27 |
+
"jwt": {
|
28 |
+
"claims": {
|
29 |
+
"aud": "[http://prod-ml-lambda/ https://domain.eu.auth0.com/userinfo]",
|
30 |
+
"azp": "v5hyXwmbFSm7pL0Y3p4xWNjYvw5pLIo2",
|
31 |
+
"exp": "1701078906",
|
32 |
+
"iat": "1700992506",
|
33 |
+
"iss": "https://domain.eu.auth0.com/",
|
34 |
+
"scope": "openid profile email",
|
35 |
+
"sub": "auth0|sub-auth0"
|
36 |
+
},
|
37 |
+
"scopes": null
|
38 |
+
}
|
39 |
+
},
|
40 |
+
"domainName": "domain.execute-api.eu-west-1.amazonaws.com",
|
41 |
+
"domainPrefix": "domain",
|
42 |
+
"http": {
|
43 |
+
"method": "POST",
|
44 |
+
"path": "/prod/lambda-ml-fastsam-api",
|
45 |
+
"protocol": "HTTP/1.1",
|
46 |
+
"sourceIp": "172.71.114.177",
|
47 |
+
"userAgent": ""
|
48 |
+
},
|
49 |
+
"requestId": "PAB7bgt0joEEJDw=",
|
50 |
+
"routeKey": "POST /lambda-ml-fastsam-api",
|
51 |
+
"stage": "prod",
|
52 |
+
"time": "26/Nov/2023:09:58:07 +0000",
|
53 |
+
"timeEpoch": 1700992687215
|
54 |
+
},
|
55 |
+
"body": "{\"bbox\": {\"ne\": {\"lat\": 38.839707613545144, \"lng\": 15.515441894531252},\"sw\": {\"lat\": 38.10430528370985, \"lng\": 14.109191894531252}},\"prompt\": [{\"type\": \"point\",\"data\": {\"lat\": 38.565347844885466, \"lng\": 14.82810974121094},\"label\": 0}],\"zoom\": 10, \"source_type\": \"Satellite\"\n}",
|
56 |
+
"isBase64Encoded": false
|
57 |
+
},
|
58 |
+
"output": {
|
59 |
+
"statusCode": 200,
|
60 |
+
"header": {
|
61 |
+
"Content-Type": "application/json"
|
62 |
+
},
|
63 |
+
"body": "{\"n_predictions\": 1, \"geojson\": \"{\\\"type\\\": \\\"FeatureCollection\\\", \\\"features\\\": [{\\\"id\\\": \\\"0\\\", \\\"type\\\": \\\"Feature\\\", \\\"properties\\\": {\\\"raster_val\\\": 255.0}, \\\"geometry\\\": {\\\"type\\\": \\\"Polygon\\\", \\\"coordinates\\\": [[[14.809570312500002, 38.58252615935333], [14.832916259765627, 38.58252615935333], [14.832916259765627, 38.58145263504286], [14.837036132812502, 38.58145263504286], [14.837036132812502, 38.58037909468592], [14.845275878906252, 38.58037909468592], [14.845275878906252, 38.58145263504286], [14.854888916015627, 38.58145263504286], [14.854888916015627, 38.58252615935333], [14.868621826171877, 38.58252615935333], [14.868621826171877, 38.58145263504286], [14.871368408203125, 38.58145263504286], [14.871368408203125, 38.57930553828263], [14.87274169921875, 38.57930553828263], [14.87274169921875, 38.57071650940461], [14.874114990234377, 38.57071650940461], [14.874114990234377, 38.55997877925586], [14.87274169921875, 38.55997877925586], [14.87274169921875, 38.55675714735221], [14.871368408203125, 38.55675714735221], [14.871368408203125, 38.5535353710587], [14.869995117187502, 38.5535353710587], [14.869995117187502, 38.54709138531695], [14.868621826171877, 38.54709138531695], [14.868621826171877, 38.54386917587615], [14.869995117187502, 38.54386917587615], [14.869995117187502, 38.54064682206035], [14.868621826171877, 38.54064682206035], [14.868621826171877, 38.53742432387328], [14.86724853515625, 38.53742432387328], [14.86724853515625, 38.53635012572906], [14.864501953125002, 38.53635012572906], [14.864501953125002, 38.53527591154415], [14.860382080078127, 38.53527591154415], [14.860382080078127, 38.53420168131867], [14.856262207031252, 38.53420168131867], [14.856262207031252, 38.53527591154415], [14.849395751953125, 38.53527591154415], [14.849395751953125, 38.53635012572906], [14.846649169921877, 38.53635012572906], [14.846649169921877, 38.53742432387328], [14.845275878906252, 38.53742432387328], [14.845275878906252, 38.53849850597666], [14.843902587890625, 38.53849850597666], [14.843902587890625, 38.53957267203906], [14.841156005859377, 38.53957267203906], [14.841156005859377, 38.54064682206035], [14.838409423828129, 38.54064682206035], [14.838409423828129, 38.541720956040386], [14.837036132812502, 38.541720956040386], [14.837036132812502, 38.54279507397902], [14.835662841796877, 38.54279507397902], [14.835662841796877, 38.54386917587615], [14.832916259765627, 38.54386917587615], [14.832916259765627, 38.5449432617316], [14.830169677734375, 38.5449432617316], [14.830169677734375, 38.54601733154525], [14.827423095703127, 38.54601733154525], [14.827423095703127, 38.54709138531695], [14.823303222656252, 38.54709138531695], [14.823303222656252, 38.54816542304658], [14.820556640625, 38.54816542304658], [14.820556640625, 38.54923944473399], [14.817810058593752, 38.54923944473399], [14.817810058593752, 38.55031345037904], [14.8150634765625, 38.55031345037904], [14.8150634765625, 38.55138743998161], [14.81231689453125, 38.55138743998161], [14.81231689453125, 38.55246141354154], [14.808197021484377, 38.55246141354154], [14.808197021484377, 38.5535353710587], [14.80682373046875, 38.5535353710587], [14.80682373046875, 38.55460931253297], [14.804077148437502, 38.55460931253297], [14.804077148437502, 38.55568323796418], [14.802703857421875, 38.55568323796418], [14.802703857421875, 38.55675714735221], [14.801330566406254, 38.55675714735221], [14.801330566406254, 38.557831040696925], [14.799957275390623, 38.557831040696925], [14.799957275390623, 38.558904917998184], [14.798583984375002, 38.558904917998184], [14.798583984375002, 38.561052624469795], [14.797210693359375, 38.561052624469795], [14.797210693359375, 38.568569091731305], [14.798583984375002, 38.568569091731305], [14.798583984375002, 38.571790194173666], [14.799957275390623, 38.571790194173666], [14.799957275390623, 38.57286386289749], [14.801330566406254, 38.57286386289749], [14.801330566406254, 38.57501115220886], [14.802703857421875, 38.57501115220886], [14.802703857421875, 38.57823196583315], [14.804077148437502, 38.57823196583315], [14.804077148437502, 38.57930553828263], [14.805450439453125, 38.57930553828263], [14.805450439453125, 38.58037909468592], [14.80682373046875, 38.58037909468592], [14.80682373046875, 38.58145263504286], [14.809570312500002, 38.58145263504286], [14.809570312500002, 38.58252615935333]]]}}, {\\\"id\\\": \\\"1\\\", \\\"type\\\": \\\"Feature\\\", \\\"properties\\\": {\\\"raster_val\\\": 0.0}, \\\"geometry\\\": {\\\"type\\\": \\\"Polygon\\\", \\\"coordinates\\\": [[[14.109191894531252, 38.839707613545166], [14.109191894531252, 38.10430528370981], [15.515441894531252, 38.10430528370981], [15.515441894531252, 38.839707613545166], [14.109191894531252, 38.839707613545166]], [[14.809570312500002, 38.58252615935333], [14.809570312500002, 38.58145263504286], [14.80682373046875, 38.58145263504286], [14.80682373046875, 38.58037909468592], [14.805450439453125, 38.58037909468592], [14.805450439453125, 38.57930553828263], [14.804077148437502, 38.57930553828263], [14.804077148437502, 38.57823196583315], [14.802703857421875, 38.57823196583315], [14.802703857421875, 38.57501115220886], [14.801330566406254, 38.57501115220886], [14.801330566406254, 38.57286386289749], [14.799957275390623, 38.57286386289749], [14.799957275390623, 38.571790194173666], [14.798583984375002, 38.571790194173666], [14.798583984375002, 38.568569091731305], [14.797210693359375, 38.568569091731305], [14.797210693359375, 38.561052624469795], [14.798583984375002, 38.561052624469795], [14.798583984375002, 38.558904917998184], [14.799957275390623, 38.558904917998184], [14.799957275390623, 38.557831040696925], [14.801330566406254, 38.557831040696925], [14.801330566406254, 38.55675714735221], [14.802703857421875, 38.55675714735221], [14.802703857421875, 38.55568323796418], [14.804077148437502, 38.55568323796418], [14.804077148437502, 38.55460931253297], [14.80682373046875, 38.55460931253297], [14.80682373046875, 38.5535353710587], [14.808197021484377, 38.5535353710587], [14.808197021484377, 38.55246141354154], [14.81231689453125, 38.55246141354154], [14.81231689453125, 38.55138743998161], [14.8150634765625, 38.55138743998161], [14.8150634765625, 38.55031345037904], [14.817810058593752, 38.55031345037904], [14.817810058593752, 38.54923944473399], [14.820556640625, 38.54923944473399], [14.820556640625, 38.54816542304658], [14.823303222656252, 38.54816542304658], [14.823303222656252, 38.54709138531695], [14.827423095703127, 38.54709138531695], [14.827423095703127, 38.54601733154525], [14.830169677734375, 38.54601733154525], [14.830169677734375, 38.5449432617316], [14.832916259765627, 38.5449432617316], [14.832916259765627, 38.54386917587615], [14.835662841796877, 38.54386917587615], [14.835662841796877, 38.54279507397902], [14.837036132812502, 38.54279507397902], [14.837036132812502, 38.541720956040386], [14.838409423828129, 38.541720956040386], [14.838409423828129, 38.54064682206035], [14.841156005859377, 38.54064682206035], [14.841156005859377, 38.53957267203906], [14.843902587890625, 38.53957267203906], [14.843902587890625, 38.53849850597666], [14.845275878906252, 38.53849850597666], [14.845275878906252, 38.53742432387328], [14.846649169921877, 38.53742432387328], [14.846649169921877, 38.53635012572906], [14.849395751953125, 38.53635012572906], [14.849395751953125, 38.53527591154415], [14.856262207031252, 38.53527591154415], [14.856262207031252, 38.53420168131867], [14.860382080078127, 38.53420168131867], [14.860382080078127, 38.53527591154415], [14.864501953125002, 38.53527591154415], [14.864501953125002, 38.53635012572906], [14.86724853515625, 38.53635012572906], [14.86724853515625, 38.53742432387328], [14.868621826171877, 38.53742432387328], [14.868621826171877, 38.54064682206035], [14.869995117187502, 38.54064682206035], [14.869995117187502, 38.54386917587615], [14.868621826171877, 38.54386917587615], [14.868621826171877, 38.54709138531695], [14.869995117187502, 38.54709138531695], [14.869995117187502, 38.5535353710587], [14.871368408203125, 38.5535353710587], [14.871368408203125, 38.55675714735221], [14.87274169921875, 38.55675714735221], [14.87274169921875, 38.55997877925586], [14.874114990234377, 38.55997877925586], [14.874114990234377, 38.57071650940461], [14.87274169921875, 38.57071650940461], [14.87274169921875, 38.57930553828263], [14.871368408203125, 38.57930553828263], [14.871368408203125, 38.58145263504286], [14.868621826171877, 38.58145263504286], [14.868621826171877, 38.58252615935333], [14.854888916015627, 38.58252615935333], [14.854888916015627, 38.58145263504286], [14.845275878906252, 38.58145263504286], [14.845275878906252, 38.58037909468592], [14.837036132812502, 38.58037909468592], [14.837036132812502, 38.58145263504286], [14.832916259765627, 38.58145263504286], [14.832916259765627, 38.58252615935333], [14.809570312500002, 38.58252615935333]]]}}]}\", \"n_shapes_geojson\": 2, \"duration_run\": 5.23, \"message\": \"ok\", \"request_id\": \"test_invoke_id\"}",
|
64 |
+
"isBase64Encoded": false
|
65 |
+
}
|
66 |
+
}
|
tests/test_app.py
CHANGED
@@ -91,7 +91,7 @@ class TestAppFailures(unittest.TestCase):
|
|
91 |
@patch.object(app, "get_response")
|
92 |
@patch.object(app, "get_parsed_bbox_points")
|
93 |
@patch.object(app, "get_parsed_request_body")
|
94 |
-
def
|
95 |
self,
|
96 |
get_parsed_request_body_mocked,
|
97 |
get_parsed_bbox_points_mocked,
|
@@ -143,3 +143,34 @@ class TestAppFailures(unittest.TestCase):
|
|
143 |
expected_response_200 = get_response_io[response_type]["output"]
|
144 |
print(f"types: response_200:{type(response_200)}, expected:{type(expected_response_200)}.")
|
145 |
assert response_200 == expected_response_200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
@patch.object(app, "get_response")
|
92 |
@patch.object(app, "get_parsed_bbox_points")
|
93 |
@patch.object(app, "get_parsed_request_body")
|
94 |
+
def test_lambda_handler_200_mocked(
|
95 |
self,
|
96 |
get_parsed_request_body_mocked,
|
97 |
get_parsed_bbox_points_mocked,
|
|
|
143 |
expected_response_200 = get_response_io[response_type]["output"]
|
144 |
print(f"types: response_200:{type(response_200)}, expected:{type(expected_response_200)}.")
|
145 |
assert response_200 == expected_response_200
|
146 |
+
|
147 |
+
def test_lambda_handler_200_real(self):
|
148 |
+
import shapely
|
149 |
+
|
150 |
+
from src.app import lambda_handler
|
151 |
+
from tests import TEST_EVENTS_FOLDER
|
152 |
+
|
153 |
+
name_fn = "lambda_handler"
|
154 |
+
invoke_id = "test_invoke_id"
|
155 |
+
|
156 |
+
with open(TEST_EVENTS_FOLDER / f"{name_fn}.json") as tst_json:
|
157 |
+
inputs_outputs = json.load(tst_json)
|
158 |
+
lambda_context = LambdaContext(
|
159 |
+
invoke_id=invoke_id,
|
160 |
+
client_context=None,
|
161 |
+
cognito_identity=None,
|
162 |
+
epoch_deadline_time_in_ms=time.time()
|
163 |
+
)
|
164 |
+
expected_response_dict = inputs_outputs["output"]
|
165 |
+
expected_response_body = json.loads(expected_response_dict["body"])
|
166 |
+
response = lambda_handler(event=inputs_outputs["input"], context=lambda_context)
|
167 |
+
|
168 |
+
response_dict = json.loads(response)
|
169 |
+
body_dict = json.loads(response_dict["body"])
|
170 |
+
assert body_dict["n_predictions"] == 1
|
171 |
+
assert body_dict["request_id"] == invoke_id
|
172 |
+
assert body_dict["message"] == "ok"
|
173 |
+
assert body_dict["n_shapes_geojson"] == expected_response_body["n_shapes_geojson"]
|
174 |
+
output_geojson = shapely.from_geojson(body_dict["geojson"])
|
175 |
+
expected_output_geojson = shapely.from_geojson(expected_response_body["geojson"])
|
176 |
+
assert shapely.equals_exact(output_geojson, expected_output_geojson, tolerance=0.000006)
|