aletrn commited on
Commit
9205f9c
·
1 Parent(s): 142642e

[feat] add test case for get_response

Browse files
src/utilities/constants.py CHANGED
@@ -4,6 +4,7 @@ OUTPUT_CRS_STRING = "EPSG:3857"
4
  ROOT = "/tmp"
5
  CUSTOM_RESPONSE_MESSAGES = {
6
  200: "ok",
 
7
  422: "Missing required parameter",
8
  500: "Internal server error"
9
  }
 
4
  ROOT = "/tmp"
5
  CUSTOM_RESPONSE_MESSAGES = {
6
  200: "ok",
7
+ 400: "Bad Request",
8
  422: "Missing required parameter",
9
  500: "Internal server error"
10
  }
tests/events/get_response.json ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "OK": {
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]]]}}]}",
6
+ "n_shapes_geojson": 2
7
+ },
8
+ "output": {
9
+ "statusCode": 200,
10
+ "header": {
11
+ "Content-Type": "application/json"
12
+ },
13
+ "body": "{\"n_predictions\": 1, \"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]]]}}]}\", \"n_shapes_geojson\": 2, \"duration_run\": 108, \"message\": \"ok\", \"request_id\": \"test_invoke_id\"}",
14
+ "isBase64Encoded": false
15
+ }
16
+ },
17
+ "400": {
18
+ "input": {},
19
+ "output": "{\"statusCode\": 400, \"header\": {\"Content-Type\": \"application/json\"}, \"body\": \"{\\\"duration_run\\\": 108, \\\"message\\\": \\\"Bad Request\\\", \\\"request_id\\\": \\\"test_invoke_id\\\"}\", \"isBase64Encoded\": false}"
20
+ },
21
+ "422": {
22
+ "input": {},
23
+ "output": "{\"statusCode\": 422, \"header\": {\"Content-Type\": \"application/json\"}, \"body\": \"{\\\"duration_run\\\": 108, \\\"message\\\": \\\"Missing required parameter\\\", \\\"request_id\\\": \\\"test_invoke_id\\\"}\", \"isBase64Encoded\": false}"
24
+ },
25
+ "500": {
26
+ "input": {},
27
+ "output": "{\"statusCode\": 500, \"header\": {\"Content-Type\": \"application/json\"}, \"body\": \"{\\\"duration_run\\\": 108, \\\"message\\\": \\\"Internal server error\\\", \\\"request_id\\\": \\\"test_invoke_id\\\"}\", \"isBase64Encoded\": false}"
28
+ }
29
+ }
tests/io/test_lambda_helpers.py CHANGED
@@ -1,11 +1,37 @@
1
  import json
2
-
3
- from src.io.lambda_helpers import get_parsed_bbox_points, get_parsed_request_body
 
 
4
  from src.utilities.type_hints import RawRequestInput
5
- from src.utilities.utilities import base64_encode
6
  from tests import TEST_EVENTS_FOLDER
7
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def test_get_parsed_bbox_points():
10
  with open(TEST_EVENTS_FOLDER / "get_parsed_bbox_points.json") as tst_json:
11
  inputs_outputs = json.load(tst_json)
@@ -42,6 +68,6 @@ def test_get_parsed_request_body():
42
  output = get_parsed_request_body(input_event_str)
43
  assert output == RawRequestInput.model_validate(expected_output_dict)
44
 
45
- event = {"body": base64_encode(input_event_str).decode("utf-8")}
46
  output = get_parsed_request_body(event)
47
  assert output == RawRequestInput.model_validate(expected_output_dict)
 
1
  import json
2
+ import time
3
+ from http import HTTPStatus
4
+ from unittest.mock import patch
5
+ from src.io.lambda_helpers import get_parsed_bbox_points, get_parsed_request_body, get_response
6
  from src.utilities.type_hints import RawRequestInput
7
+ from src.utilities import utilities
8
  from tests import TEST_EVENTS_FOLDER
9
 
10
 
11
+ @patch.object(time, "time")
12
+ def test_get_response(time_mocked):
13
+ time_diff = 108
14
+ end_run = 1000
15
+ time_mocked.return_value = end_run
16
+ start_time = end_run-time_diff
17
+ aws_request_id = "test_invoke_id"
18
+
19
+ with open(TEST_EVENTS_FOLDER / "get_response.json") as tst_json:
20
+ inputs_outputs = json.load(tst_json)
21
+ body_response = inputs_outputs["OK"]["input"]
22
+ output = get_response(HTTPStatus.OK.value, start_time, aws_request_id, body_response)
23
+ assert json.loads(output) == inputs_outputs["OK"]["output"]
24
+
25
+ response_400 = get_response(HTTPStatus.BAD_REQUEST.value, start_time, aws_request_id, {})
26
+ assert response_400 == inputs_outputs["400"]["output"]
27
+
28
+ response_422 = get_response(HTTPStatus.UNPROCESSABLE_ENTITY.value, start_time, aws_request_id, {})
29
+ assert response_422 == inputs_outputs["422"]["output"]
30
+
31
+ response_500 = get_response(HTTPStatus.INTERNAL_SERVER_ERROR.value, start_time, aws_request_id, {})
32
+ assert response_500 == inputs_outputs["500"]["output"]
33
+
34
+
35
  def test_get_parsed_bbox_points():
36
  with open(TEST_EVENTS_FOLDER / "get_parsed_bbox_points.json") as tst_json:
37
  inputs_outputs = json.load(tst_json)
 
68
  output = get_parsed_request_body(input_event_str)
69
  assert output == RawRequestInput.model_validate(expected_output_dict)
70
 
71
+ event = {"body": utilities.base64_encode(input_event_str).decode("utf-8")}
72
  output = get_parsed_request_body(event)
73
  assert output == RawRequestInput.model_validate(expected_output_dict)