Spaces:
Running
Running
File size: 1,713 Bytes
67f4974 b41850c 67f4974 b41850c 67f4974 b41850c 67f4974 b41850c 67f4974 b41850c 67f4974 b41850c 67f4974 b41850c 67f4974 b41850c 67f4974 b41850c 67f4974 b41850c 67f4974 b41850c 67f4974 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
"""
Testing Lambda handler
"""
import os
import sys
import json
import base64
import pytest
from lambda_function import lambda_handler
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_dir)
sys.path.insert(0, os.path.dirname(parent_dir))
@pytest.fixture
def event():
"""Example json event"""
# Get the directory of the current test file
test_dir = os.path.dirname(os.path.abspath(__file__))
# Construct the image path relative to the test directory
image_path = os.path.join(test_dir, "data", "savanna.jpg")
# Read image data
with open(image_path, "rb") as image_file:
image_data = image_file.read()
# Encode the image data in base64
encoded_image = base64.b64encode(image_data).decode("utf-8")
# Prepare the payload
json_event = {
"body": encoded_image,
"isBase64Encoded": True,
}
return json_event
@pytest.fixture
def context():
"""Example context"""
return None
def test_lambda_handler(event, context):
"""Tests lambda handler"""
lambda_response = lambda_handler(event, context)
response_data = json.loads(lambda_response["body"])
print("lambda_response - type", type(lambda_response))
print("lambda_response", lambda_response)
print("response_data - type", type(response_data))
print("response_data", response_data)
response_keys = list(response_data.keys())
gt_keys = ["scores", "labels", "boxes"]
assert lambda_response["statusCode"] == 200
assert set(response_keys) == set(gt_keys), "Response keys do not match ground truth"
assert len(response_data["scores"]) == 5
assert len(response_data["labels"]) == 5
|