-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from san99tiago/develop
Include UnitTests for the Lambda Function
- Loading branch information
Showing
10 changed files
with
176 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pytest==6.2.5 | ||
moto==4.1.7 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"Records": [ | ||
{ | ||
"messageId": "4332b17c-b7cd-4aef-8dba-7cb7240e3d0e", | ||
"receiptHandle": "AWEBZGy/Ajve1xtBoqIJhPmO0ANH9FWhcpjt7xevdsSw5+BK0OIljV8Jq/6SNgQ9boZKcTdGkK2aaiSo9iCAgsnSyUJ3u3FenB5rOnlOTf3ctMHHEJ4tGrwSR6lx8be8/CggdbtuCxjzZsPU2IUEIo4URuA4Kum54DELIWYaBNTBbfKlvlyKeRilpTh3a3I3rmy2ctn5HkKjGVmIv1WyIPNga3POZ28pxAEcQsVEFuU0fU/Bf9H5sDuj0i47JxzCBuimyFQEN1cMYwE5W7jz54hCEN3wC1blt9/M0U+jWqsC3psmlPb+qkj50ZriJb8bNoIHMutp+ERFy5CGrBJIiVIt8xvsJmkz9LuUhKDOTmwSN9yg9p+jrTD5L6B8yH8/WSu+4XMgK0QDv/BE/5eeLZcv6Q==", | ||
"body": "{\n \"Message\": \"Test Message OK: good example message\",\n \"Details\": \"Test details\" \n}", | ||
"attributes": { | ||
"ApproximateReceiveCount": "1", | ||
"AWSTraceHeader": "Root=1-64495159-17ceb00068aada396a66bd23", | ||
"SentTimestamp": "1682526553465", | ||
"SenderId": "AROASECKEBOYZPRE774YO:BackplaneAssumeRoleSession", | ||
"ApproximateFirstReceiveTimestamp": "1682526553466" | ||
}, | ||
"messageAttributes": {}, | ||
"md5OfBody": "d46c3294a7ebf36b9a9a99f46b768a9d", | ||
"eventSource": "aws:sqs", | ||
"eventSourceARN": "arn:aws:sqs:us-east-1:12345678901:apigw-sqs-lambda-queue", | ||
"awsRegion": "us-east-1" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"Records": [ | ||
{ | ||
"messageId": "4332b17c-b7cd-4aef-8dba-7cb7240e3d0e", | ||
"receiptHandle": "AWEBZGy/Ajve1xtBoqIJhPmO0ANH9FWhcpjt7xevdsSw5+BK0OIljV8Jq/6SNgQ9boZKcTdGkK2aaiSo9iCAgsnSyUJ3u3FenB5rOnlOTf3ctMHHEJ4tGrwSR6lx8be8/CggdbtuCxjzZsPU2IUEIo4URuA4Kum54DELIWYaBNTBbfKlvlyKeRilpTh3a3I3rmy2ctn5HkKjGVmIv1WyIPNga3POZ28pxAEcQsVEFuU0fU/Bf9H5sDuj0i47JxzCBuimyFQEN1cMYwE5W7jz54hCEN3wC1blt9/M0U+jWqsC3psmlPb+qkj50ZriJb8bNoIHMutp+ERFy5CGrBJIiVIt8xvsJmkz9LuUhKDOTmwSN9yg9p+jrTD5L6B8yH8/WSu+4XMgK0QDv/BE/5eeLZcv6Q==", | ||
"body": "{\n \"Message\": \"Test Message OK: good example message 1\",\n \"Details\": \"Test details\" \n}", | ||
"attributes": { | ||
"ApproximateReceiveCount": "1", | ||
"AWSTraceHeader": "Root=1-64495159-17ceb00068aada396a66bd23", | ||
"SentTimestamp": "1682526553465", | ||
"SenderId": "AROASECKEBOYZPRE774YO:BackplaneAssumeRoleSession", | ||
"ApproximateFirstReceiveTimestamp": "1682526553466" | ||
}, | ||
"messageAttributes": {}, | ||
"md5OfBody": "d46c3294a7ebf36b9a9a99f46b768a9d", | ||
"eventSource": "aws:sqs", | ||
"eventSourceARN": "arn:aws:sqs:us-east-1:12345678901:apigw-sqs-lambda-queue", | ||
"awsRegion": "us-east-1" | ||
}, | ||
{ | ||
"messageId": "4332b17c-b7cd-4aef-8dba-7cb7240e3d0e", | ||
"receiptHandle": "AWEBZGy/Ajve1xtBoqIJhPmO0ANH9FWhcpjt7xevdsSw5+BK0OIljV8Jq/6SNgQ9boZKcTdGkK2aaiSo9iCAgsnSyUJ3u3FenB5rOnlOTf3ctMHHEJ4tGrwSR6lx8be8/CggdbtuCxjzZsPU2IUEIo4URuA4Kum54DELIWYaBNTBbfKlvlyKeRilpTh3a3I3rmy2ctn5HkKjGVmIv1WyIPNga3POZ28pxAEcQsVEFuU0fU/Bf9H5sDuj0i47JxzCBuimyFQEN1cMYwE5W7jz54hCEN3wC1blt9/M0U+jWqsC3psmlPb+qkj50ZriJb8bNoIHMutp+ERFy5CGrBJIiVIt8xvsJmkz9LuUhKDOTmwSN9yg9p+jrTD5L6B8yH8/WSu+4XMgK0QDv/BE/5eeLZcv6Q==", | ||
"body": "{\n \"Message\": \"Test Message OK: good example message 2\",\n \"Details\": \"Test details\" \n}", | ||
"attributes": { | ||
"ApproximateReceiveCount": "1", | ||
"AWSTraceHeader": "Root=1-64495159-17ceb00068aada396a66bd23", | ||
"SentTimestamp": "1682526553465", | ||
"SenderId": "AROASECKEBOYZPRE774YO:BackplaneAssumeRoleSession", | ||
"ApproximateFirstReceiveTimestamp": "1682526553466" | ||
}, | ||
"messageAttributes": {}, | ||
"md5OfBody": "d46c3294a7ebf36b9a9a99f46b768a9d", | ||
"eventSource": "aws:sqs", | ||
"eventSourceARN": "arn:aws:sqs:us-east-1:12345678901:apigw-sqs-lambda-queue", | ||
"awsRegion": "us-east-1" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"Records": [ | ||
{ | ||
"messageId": "4332b17c-b7cd-4aef-8dba-7cb7240e3d0e", | ||
"receiptHandle": "AWCBZGy/Ajve1xtBoqIJhPmO0ANH9FWhcpjt7xevdsSw5+BK0OIljV8Jq/6SNgQ9boZKcTdGkK2aaiSo9iCAgsnSyUJ3u3FenB5rOnlOTf3ctMHHEJ4tGrwSR6lx8be8/CggdbtuCxjzZsPU2IUEIo4URuA4Kum54DELIWYaBNTBbfKlvlyKeRilpTh3a3I3rmy2ctn5HkKjGVmIv1WyIPNga3POZ28pxAEcQsVEFuU0fU/Bf9H5sDuj0i47JxzCBuimyFQEN1cMYwE5W7jz54hCEN3wC1blt9/M0U+jWqsC3psmlPb+qkj50ZriJb8bNoIHMutp+ERFy5CGrBJIiVIt8xvsJmkz9LuUhKDOTmwSN9yg9p+jrTD5L6B8yH8/WSu+4XMgK0QDv/BE/5eeLZcv6Q==", | ||
"body": "{\n \"MessageIntentionalWrongKey\": \"Test Message NOT OK: good example message with wrong keys\"\n}", | ||
"attributes": { | ||
"ApproximateReceiveCount": "1", | ||
"AWSTraceHeader": "Root=1-64495159-17ceb00068aada396a66bd23", | ||
"SentTimestamp": "1682526553465", | ||
"SenderId": "AROASECKEBOYZPRE774YO:BackplaneAssumeRoleSession", | ||
"ApproximateFirstReceiveTimestamp": "1682526553466" | ||
}, | ||
"messageAttributes": {}, | ||
"md5OfBody": "d46c3294a7ebf36b9a9a99f46b768a9d", | ||
"eventSource": "aws:sqs", | ||
"eventSourceARN": "arn:aws:sqs:us-east-1:12345678901:apigw-sqs-lambda-queue", | ||
"awsRegion": "us-east-1" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Built-in imports | ||
import os, sys | ||
import json | ||
import unittest | ||
|
||
# External imports | ||
from aws_lambda_powertools.utilities.data_classes import event_source, SQSEvent | ||
from moto import mock_sts | ||
|
||
# Add path to find lambda directory for own imports | ||
sys.path.append(os.path.abspath(os.path.dirname(os.path.dirname(__file__)))) | ||
|
||
# Own imports | ||
import src.lambda_function as _lambda # noqa: E402 | ||
|
||
|
||
class TestLambdaFunction(unittest.TestCase): | ||
""" | ||
TestCase for unit testing the inner Lambda Function functionalities. | ||
""" | ||
|
||
def load_test_event(self, test_event_name: str) -> dict: | ||
""" | ||
Load test event from given file. | ||
""" | ||
path_to_event = os.path.join( | ||
os.path.dirname(__file__), | ||
test_event_name, | ||
) | ||
with open(path_to_event) as file: | ||
test_event = json.load(file) | ||
return test_event | ||
|
||
# The "mock_sts" decorator allows to "mock/fake" the sts API calls for tests | ||
@mock_sts() | ||
def test_process_messages_success_single(self): | ||
""" | ||
Test successful process_messages call for a single message. | ||
""" | ||
# Load pre-configured event for current test case | ||
event = self.load_test_event("test_event_01_good_single.json") | ||
|
||
# Middleware to load event with correct SQSEvent data class | ||
event_sqs = SQSEvent(event) | ||
result = _lambda.process_messages(event_sqs) | ||
|
||
self.assertEqual(result, True) | ||
|
||
@mock_sts() | ||
def test_process_messages_success_multiple(self): | ||
""" | ||
Test successful process_messages call for multiple messages. | ||
""" | ||
# Load pre-configured event for current test case | ||
event = self.load_test_event("test_event_02_good_multiple.json") | ||
|
||
# Middleware to load event with correct SQSEvent data class | ||
event_sqs = SQSEvent(event) | ||
result = _lambda.process_messages(event_sqs) | ||
|
||
self.assertEqual(result, True) | ||
|
||
@mock_sts() | ||
def test_process_messages_error(self): | ||
""" | ||
Test errors on process_messages call due to wrong message format. | ||
""" | ||
# Load pre-configured event for current test case | ||
event = self.load_test_event("test_event_03_bad.json") | ||
|
||
# Middleware to load event with correct SQSEvent data class | ||
event_sqs = SQSEvent(event) | ||
|
||
# Expected an exception intentionally, otherwise fails | ||
with self.assertRaises(Exception): | ||
_lambda.process_messages(event_sqs) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |