ale
commited on
Commit
•
bf5d95f
1
Parent(s):
3ed3617
[test] add test case for lambda_handler
Browse files- tests/events/get_response.json +1 -1
- tests/io/test_lambda_helpers.py +14 -9
- tests/test_app.py +145 -0
tests/events/get_response.json
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
{
|
2 |
-
"
|
3 |
"input": {
|
4 |
"n_predictions": 1,
|
5 |
"geojson": "{\"type\": \"FeatureCollection\", \"features\": [{\"id\": \"0\", \"type\": \"Feature\", \"properties\": {\"raster_val\": 255.0}, \"geometry\": {\"type\": \"Polygon\", \"coordinates\": [[[148.359375, -40.4469470596005], [148.447265625, -40.4469470596005], [148.447265625, -40.51379915504414], [148.359375, -40.4469470596005]]]}}]}",
|
|
|
1 |
{
|
2 |
+
"200": {
|
3 |
"input": {
|
4 |
"n_predictions": 1,
|
5 |
"geojson": "{\"type\": \"FeatureCollection\", \"features\": [{\"id\": \"0\", \"type\": \"Feature\", \"properties\": {\"raster_val\": 255.0}, \"geometry\": {\"type\": \"Polygon\", \"coordinates\": [[[148.359375, -40.4469470596005], [148.447265625, -40.4469470596005], [148.447265625, -40.51379915504414], [148.359375, -40.4469470596005]]]}}]}",
|
tests/io/test_lambda_helpers.py
CHANGED
@@ -18,18 +18,23 @@ def test_get_response(time_mocked):
|
|
18 |
|
19 |
with open(TEST_EVENTS_FOLDER / "get_response.json") as tst_json:
|
20 |
inputs_outputs = json.load(tst_json)
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
response_400 = get_response(HTTPStatus.BAD_REQUEST.value, start_time, aws_request_id, {})
|
26 |
-
assert response_400 == inputs_outputs[
|
27 |
-
|
|
|
28 |
response_422 = get_response(HTTPStatus.UNPROCESSABLE_ENTITY.value, start_time, aws_request_id, {})
|
29 |
-
assert response_422 == inputs_outputs[
|
30 |
-
|
|
|
31 |
response_500 = get_response(HTTPStatus.INTERNAL_SERVER_ERROR.value, start_time, aws_request_id, {})
|
32 |
-
assert response_500 == inputs_outputs[
|
33 |
|
34 |
|
35 |
def test_get_parsed_bbox_points():
|
|
|
18 |
|
19 |
with open(TEST_EVENTS_FOLDER / "get_response.json") as tst_json:
|
20 |
inputs_outputs = json.load(tst_json)
|
21 |
+
|
22 |
+
response_type = "200"
|
23 |
+
body_response = inputs_outputs[response_type]["input"]
|
24 |
+
output = get_response(HTTPStatus.OK.value, start_time, aws_request_id, body_response)
|
25 |
+
assert json.loads(output) == inputs_outputs[response_type]["output"]
|
26 |
+
|
27 |
+
response_type = "400"
|
28 |
response_400 = get_response(HTTPStatus.BAD_REQUEST.value, start_time, aws_request_id, {})
|
29 |
+
assert response_400 == inputs_outputs[response_type]["output"]
|
30 |
+
|
31 |
+
response_type = "422"
|
32 |
response_422 = get_response(HTTPStatus.UNPROCESSABLE_ENTITY.value, start_time, aws_request_id, {})
|
33 |
+
assert response_422 == inputs_outputs[response_type]["output"]
|
34 |
+
|
35 |
+
response_type = "500"
|
36 |
response_500 = get_response(HTTPStatus.INTERNAL_SERVER_ERROR.value, start_time, aws_request_id, {})
|
37 |
+
assert response_500 == inputs_outputs[response_type]["output"]
|
38 |
|
39 |
|
40 |
def test_get_parsed_bbox_points():
|
tests/test_app.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import time
|
3 |
+
import unittest
|
4 |
+
from unittest.mock import patch
|
5 |
+
|
6 |
+
from awslambdaric.lambda_context import LambdaContext
|
7 |
+
|
8 |
+
from src import app
|
9 |
+
|
10 |
+
|
11 |
+
class TestAppFailures(unittest.TestCase):
|
12 |
+
@patch.object(time, "time")
|
13 |
+
@patch.object(app, "samexporter_predict")
|
14 |
+
@patch.object(app, "get_parsed_bbox_points")
|
15 |
+
@patch.object(app, "get_parsed_request_body")
|
16 |
+
def test_lambda_handler_400(
|
17 |
+
self,
|
18 |
+
get_parsed_request_body_mocked,
|
19 |
+
get_parsed_bbox_points_mocked,
|
20 |
+
samexporter_predict_mocked,
|
21 |
+
time_mocked
|
22 |
+
):
|
23 |
+
from src.app import lambda_handler
|
24 |
+
|
25 |
+
time_mocked.return_value = 0
|
26 |
+
get_parsed_request_body_mocked.value = {}
|
27 |
+
get_parsed_bbox_points_mocked.return_value = {"bbox": "bbox_object", "prompt": "prompt_object", "zoom": 1}
|
28 |
+
samexporter_predict_mocked.side_effect = ValueError("I raise a value error!")
|
29 |
+
|
30 |
+
event = {"body": {}, "version": 1.0}
|
31 |
+
lambda_context = LambdaContext(
|
32 |
+
invoke_id="test_invoke_id",
|
33 |
+
client_context=None,
|
34 |
+
cognito_identity=None,
|
35 |
+
epoch_deadline_time_in_ms=time.time()
|
36 |
+
)
|
37 |
+
expected_response_400 = '{"statusCode": 400, "header": {"Content-Type": "application/json"}, '
|
38 |
+
expected_response_400 += '"body": "{\\"duration_run\\": 0, \\"message\\": \\"Bad Request\\", '
|
39 |
+
expected_response_400 += '\\"request_id\\": \\"test_invoke_id\\"}", "isBase64Encoded": false}'
|
40 |
+
|
41 |
+
response_400 = lambda_handler(event, lambda_context)
|
42 |
+
assert response_400 == expected_response_400
|
43 |
+
|
44 |
+
@patch.object(time, "time")
|
45 |
+
@patch.object(app, "get_parsed_request_body")
|
46 |
+
def test_lambda_handler_500(self, get_parsed_request_body_mocked, time_mocked):
|
47 |
+
from src.app import lambda_handler
|
48 |
+
|
49 |
+
time_mocked.return_value = 0
|
50 |
+
get_parsed_request_body_mocked.return_value = {}
|
51 |
+
|
52 |
+
event = {"body": {}, "version": 1.0}
|
53 |
+
lambda_context = LambdaContext(
|
54 |
+
invoke_id="test_invoke_id",
|
55 |
+
client_context=None,
|
56 |
+
cognito_identity=None,
|
57 |
+
epoch_deadline_time_in_ms=time.time()
|
58 |
+
)
|
59 |
+
|
60 |
+
response_500 = lambda_handler(event, lambda_context)
|
61 |
+
check_500 = response_500 == (
|
62 |
+
'{"statusCode": 500, "header": {"Content-Type": "application/json"}, '
|
63 |
+
'"body": "{\\"duration_run\\": 0, \\"message\\": \\"Internal server error\\", '
|
64 |
+
'\\"request_id\\": \\"test_invoke_id\\"}", "isBase64Encoded": false}')
|
65 |
+
print(f"test_lambda_handler_422:{check_500}.")
|
66 |
+
assert check_500
|
67 |
+
print("check_500")
|
68 |
+
|
69 |
+
@patch.object(time, "time")
|
70 |
+
def test_lambda_handler_422(self, time_mocked):
|
71 |
+
from src.app import lambda_handler
|
72 |
+
|
73 |
+
time_mocked.return_value = 0
|
74 |
+
event = {"body": {}, "version": 1.0}
|
75 |
+
lambda_context = LambdaContext(
|
76 |
+
invoke_id="test_invoke_id",
|
77 |
+
client_context=None,
|
78 |
+
cognito_identity=None,
|
79 |
+
epoch_deadline_time_in_ms=time.time()
|
80 |
+
)
|
81 |
+
|
82 |
+
response_422 = lambda_handler(event, lambda_context)
|
83 |
+
expected_response_422 = '{"statusCode": 422, "header": {"Content-Type": "application/json"}, '
|
84 |
+
expected_response_422 += '"body": "{\\"duration_run\\": 0, \\"message\\": \\"Missing required parameter\\", '
|
85 |
+
expected_response_422 += '\\"request_id\\": \\"test_invoke_id\\"}", "isBase64Encoded": false}'
|
86 |
+
|
87 |
+
assert response_422 == expected_response_422
|
88 |
+
|
89 |
+
@patch.object(time, "time")
|
90 |
+
@patch.object(app, "samexporter_predict")
|
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(
|
95 |
+
self,
|
96 |
+
get_parsed_request_body_mocked,
|
97 |
+
get_parsed_bbox_points_mocked,
|
98 |
+
get_response_mocked,
|
99 |
+
samexporter_predict_mocked,
|
100 |
+
time_mocked
|
101 |
+
):
|
102 |
+
from src.app import lambda_handler
|
103 |
+
from tests import TEST_EVENTS_FOLDER
|
104 |
+
|
105 |
+
time_mocked.return_value = 0
|
106 |
+
get_parsed_request_body_mocked.value = {}
|
107 |
+
get_parsed_bbox_points_mocked.return_value = {"bbox": "bbox_object", "prompt": "prompt_object", "zoom": 1}
|
108 |
+
|
109 |
+
response_type = "200"
|
110 |
+
with open(TEST_EVENTS_FOLDER / "get_response.json") as tst_json_get_response:
|
111 |
+
get_response_io = json.load(tst_json_get_response)
|
112 |
+
|
113 |
+
input_200 = {
|
114 |
+
"bbox": {
|
115 |
+
"ne": {"lat": 38.03932961278458, "lng": 15.36808069832851},
|
116 |
+
"sw": {"lat": 37.455509218936974, "lng": 14.632807441554068}
|
117 |
+
},
|
118 |
+
"prompt": [{
|
119 |
+
"type": "point",
|
120 |
+
"data": {"lat": 37.0, "lng": 15.0},
|
121 |
+
"label": 0
|
122 |
+
}],
|
123 |
+
"zoom": 10,
|
124 |
+
"source_type": "Satellite",
|
125 |
+
"debug": True
|
126 |
+
}
|
127 |
+
|
128 |
+
samexporter_predict_output = get_response_io[response_type]["input"]
|
129 |
+
samexporter_predict_mocked.return_value = samexporter_predict_output
|
130 |
+
samexporter_predict_mocked.side_effect = None
|
131 |
+
get_response_mocked.return_value = get_response_io[response_type]["output"]
|
132 |
+
|
133 |
+
event = {"body": input_200, "version": 1.0}
|
134 |
+
|
135 |
+
lambda_context = LambdaContext(
|
136 |
+
invoke_id="test_invoke_id",
|
137 |
+
client_context=None,
|
138 |
+
cognito_identity=None,
|
139 |
+
epoch_deadline_time_in_ms=time.time()
|
140 |
+
)
|
141 |
+
|
142 |
+
response_200 = lambda_handler(event, lambda_context)
|
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
|