Skip to content

Commit d2bd4f4

Browse files
committed
* Add unit tests for video transcoding and AWS S3 interactions; include mock environment setup.
1 parent 95ecb42 commit d2bd4f4

File tree

6 files changed

+192
-0
lines changed

6 files changed

+192
-0
lines changed

events/apigw.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"requestContext": {
3+
"http": {
4+
"method": "GET",
5+
"path": "/",
6+
"protocol": "HTTP/1.1",
7+
"sourceIp": "127.0.0.1",
8+
"userAgent": "Mozilla/5.0"
9+
}
10+
},
11+
"isBase64Encoded": false
12+
}

events/basic.json

Whitespace-only changes.

events/transcribe.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"Records": [
3+
{
4+
"eventVersion": "2.0",
5+
"eventSource": "aws:s3",
6+
"awsRegion": "us-east-1",
7+
"eventTime": "1970-01-01T00:00:00.000Z",
8+
"eventName": "ObjectCreated:Put",
9+
"userIdentity": {
10+
"principalId": "EXAMPLE"
11+
},
12+
"requestParameters": {
13+
"sourceIPAddress": "127.0.0.1"
14+
},
15+
"responseElements": {
16+
"x-amz-request-id": "EXAMPLE123456789",
17+
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH"
18+
},
19+
"s3": {
20+
"s3SchemaVersion": "1.0",
21+
"configurationId": "testConfigRule",
22+
"bucket": {
23+
"name": "lambda-video-transcoder",
24+
"ownerIdentity": {
25+
"principalId": "EXAMPLE"
26+
},
27+
"arn": "arn:aws:s3:::lambda-video-transcoder"
28+
},
29+
"object": {
30+
"key": "processed/d41d8cd98f00b204e9800998ecf8427e/transcripts/test-1672531200.json",
31+
"size": 1024,
32+
"eTag": "0123456789abcdef0123456789abcdef",
33+
"sequencer": "0A1B2C3D4E5F678901"
34+
}
35+
}
36+
}
37+
]
38+
}

events/video_upload.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"Records": [
3+
{
4+
"eventVersion": "2.0",
5+
"eventSource": "aws:s3",
6+
"awsRegion": "us-east-1",
7+
"eventTime": "1970-01-01T00:00:00.000Z",
8+
"eventName": "ObjectCreated:Put",
9+
"userIdentity": {
10+
"principalId": "EXAMPLE"
11+
},
12+
"requestParameters": {
13+
"sourceIPAddress": "127.0.0.1"
14+
},
15+
"responseElements": {
16+
"x-amz-request-id": "EXAMPLE123456789",
17+
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH"
18+
},
19+
"s3": {
20+
"s3SchemaVersion": "1.0",
21+
"configurationId": "testConfigRule",
22+
"bucket": {
23+
"name": "lambda-video-transcoder",
24+
"ownerIdentity": {
25+
"principalId": "EXAMPLE"
26+
},
27+
"arn": "arn:aws:s3:::lambda-video-transcoder"
28+
},
29+
"object": {
30+
"key": "uploads/test.mp4",
31+
"size": 1024,
32+
"eTag": "0123456789abcdef0123456789abcdef",
33+
"sequencer": "0A1B2C3D4E5F678901"
34+
}
35+
}
36+
}
37+
]
38+
}

tests/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
moto==5.1.6
2+
Flask==3.1.1
3+
boto3==1.38.37

tests/test_handler.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import json
2+
import os
3+
import unittest
4+
from unittest.mock import patch, MagicMock
5+
6+
import boto3
7+
from moto import mock_aws
8+
9+
# Add the src directory to the Python path
10+
import sys
11+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../src')))
12+
13+
from transcoder.app import app, lambda_handler
14+
15+
16+
class TranscoderTestCase(unittest.TestCase):
17+
def setUp(self):
18+
"""Set up test client and mock AWS services."""
19+
app.config["TESTING"] = True
20+
self.client = app.test_client()
21+
22+
# Mock environment variables
23+
self.mock_env = patch.dict(
24+
os.environ,
25+
{
26+
"BUCKET_NAME": "test-bucket",
27+
"LAMBDA_FUNCTION_URL": "http://localhost:8000",
28+
"GENERATE_DASH": "true",
29+
"GENERATE_SUBTITLES": "true",
30+
},
31+
)
32+
self.mock_env.start()
33+
34+
# Start moto AWS mock
35+
self.mock_aws = mock_aws()
36+
self.mock_aws.start()
37+
38+
# Create a mock S3 bucket
39+
self.s3 = boto3.client("s3", region_name="us-east-1")
40+
self.s3.create_bucket(Bucket="test-bucket")
41+
42+
# Mock subprocess calls for ffmpeg/ffprobe
43+
self.mock_subprocess = patch("transcoder.app.subprocess")
44+
self.mock_subprocess_instance = self.mock_subprocess.start()
45+
46+
def tearDown(self):
47+
"""Stop mocking."""
48+
self.mock_aws.stop()
49+
self.mock_env.stop()
50+
self.mock_subprocess.stop()
51+
52+
def test_index_route(self):
53+
"""Test the index route."""
54+
response = self.client.get("/")
55+
self.assertEqual(response.status_code, 200)
56+
self.assertIn(b"<h2 class=\"text-2xl font-bold text-[var(--text-primary)] mb-6\">Upload Video</h2>", response.data)
57+
58+
@patch("transcoder.app.process_video")
59+
def test_lambda_handler_s3_upload(self, mock_process_video):
60+
"""Test lambda_handler for S3 video upload event."""
61+
event = {
62+
"Records": [
63+
{
64+
"s3": {
65+
"bucket": {"name": "test-bucket"},
66+
"object": {"key": "uploads/test.mp4"},
67+
}
68+
}
69+
]
70+
}
71+
lambda_handler(event, {})
72+
mock_process_video.assert_called_once_with(event)
73+
74+
@patch("transcoder.app.process_transcription_result")
75+
def test_lambda_handler_s3_transcription(self, mock_process_transcription):
76+
"""Test lambda_handler for S3 transcription result event."""
77+
event = {
78+
"Records": [
79+
{
80+
"s3": {
81+
"bucket": {"name": "test-bucket"},
82+
"object": {
83+
"key": "processed/some_id/transcripts/job.json"
84+
},
85+
}
86+
}
87+
]
88+
}
89+
lambda_handler(event, {})
90+
mock_process_transcription.assert_called_once_with(event)
91+
92+
@patch("serverless_wsgi.handle_request")
93+
def test_lambda_handler_api_gateway(self, mock_handle_request):
94+
"""Test lambda_handler for API Gateway event."""
95+
event = {"requestContext": {"http": {"method": "GET", "path": "/"}}}
96+
lambda_handler(event, {})
97+
mock_handle_request.assert_called_once()
98+
99+
100+
if __name__ == "__main__":
101+
unittest.main()

0 commit comments

Comments
 (0)