Clement Vachet commited on
Commit
b41850c
·
1 Parent(s): c2555b8

Add unit tests via pytest

Browse files
tests/__init__.py ADDED
File without changes
tests/data/boats.jpg ADDED
tests/data/savanna.jpg ADDED
tests/test_lambda.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import pytest
4
+ import json
5
+ import base64
6
+
7
+
8
+ current_dir = os.path.dirname(os.path.abspath(__file__))
9
+ parent_dir = os.path.dirname(current_dir)
10
+ sys.path.insert(0, os.path.dirname(parent_dir))
11
+
12
+ from lambda_function import lambda_handler
13
+
14
+
15
+ @pytest.fixture
16
+ def event():
17
+ # Get the directory of the current test file
18
+ test_dir = os.path.dirname(os.path.abspath(__file__))
19
+ # Construct the image path relative to the test directory
20
+ image_path = os.path.join(test_dir, 'data', 'savanna.jpg')
21
+
22
+ # Read image data
23
+ with open(image_path, 'rb') as image_file:
24
+ image_data = image_file.read()
25
+
26
+ # Encode the image data in base64
27
+ encoded_image = base64.b64encode(image_data).decode('utf-8')
28
+
29
+ # Prepare the payload
30
+ json_event = {
31
+ 'body': encoded_image
32
+ }
33
+
34
+ return json_event
35
+
36
+
37
+ @pytest.fixture
38
+ def context():
39
+ return None
40
+
41
+
42
+ def test_lambda_handler(event, context):
43
+ lambda_response = lambda_handler(event, context)
44
+ response_data = json.loads(lambda_response["body"])
45
+
46
+ print("lambda_response - type",type(lambda_response))
47
+ print("lambda_response", lambda_response)
48
+ print("response_data - type", type(response_data))
49
+ print("response_data", response_data)
50
+
51
+ response_keys = list(response_data.keys())
52
+ gt_keys = ['scores', 'labels', 'boxes']
53
+
54
+ assert lambda_response["statusCode"] == 200
55
+ assert set(response_keys) == set(gt_keys), "Response keys do not match ground truth"
56
+ assert len(response_data['scores']) == 5
57
+ assert len(response_data['labels']) == 5
tests/test_ml_detection.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from detection import ml_detection
2
+ import os
3
+ import pytest
4
+
5
+
6
+ # Test model loading
7
+ @pytest.mark.parametrize("test_model_uri",[
8
+ ("facebook/detr-resnet-50"),
9
+ ("facebook/detr-resnet-101"),
10
+ ])
11
+ def test_load_model(test_model_uri):
12
+ processor, model = ml_detection.load_model(test_model_uri)
13
+ assert processor is not None
14
+ assert model is not None
15
+
16
+
17
+ # Test image detection
18
+ @pytest.mark.parametrize("test_model_uri", [
19
+ ("facebook/detr-resnet-50"),
20
+ ("facebook/detr-resnet-101"),
21
+ ])
22
+ def test_object_detection(test_model_uri):
23
+ processor, model = ml_detection.load_model(test_model_uri)
24
+
25
+ # Get the directory of the current test file
26
+ test_dir = os.path.dirname(os.path.abspath(__file__))
27
+ # Construct the image path relative to the test directory
28
+ image_path = os.path.join(test_dir, 'data', 'savanna.jpg')
29
+
30
+ with open(image_path, "rb") as f:
31
+ image_bytes = f.read()
32
+
33
+ results = ml_detection.object_detection(processor, model, image_bytes)
34
+ assert results is not None
35
+ assert isinstance(results, dict)
tests/test_ml_utils.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from detection import ml_utils
2
+ import torch
3
+ import json
4
+
5
+
6
+ # Test dictionary conversion
7
+ def test_convert_tensor_dict_to_json():
8
+ my_dict = {'scores': torch.tensor([1, 2, 3])}
9
+ my_list_gt = {'scores': [1, 2, 3]}
10
+ my_list = ml_utils.convert_tensor_dict_to_json(my_dict)
11
+ assert my_list == my_list_gt