Initial commit
contains: * Actual template * Example function * Tests * Integration test (make file) * Integration tests (deployed as lambda) * drone CI and CI scripts
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
46
tests/test_lambda_function.py
Normal file
46
tests/test_lambda_function.py
Normal file
@ -0,0 +1,46 @@
|
||||
""" Tests for cloud_interest_rate_calculator """
|
||||
# pylint: disable=no-self-use,missing-function-docstring,missing-class-docstring
|
||||
|
||||
import pytest
|
||||
import mock
|
||||
from src import lambda_function
|
||||
|
||||
|
||||
class TestExampleFunction:
|
||||
@pytest.mark.parametrize("event", [{"value": "One Thousand"}, {"not_value": 1000}])
|
||||
def test_handler_bad_input(self, event):
|
||||
with pytest.raises(Exception):
|
||||
assert lambda_function.handler(event, {})
|
||||
|
||||
@mock.patch("src.lambda_function.get_factors")
|
||||
def test_handler_unexpected_error(self, mock_calculate_interest):
|
||||
event = {"value": "10"}
|
||||
mock_calculate_interest.side_effect = Exception()
|
||||
with pytest.raises(Exception):
|
||||
assert lambda_function.handler(event, {})
|
||||
|
||||
def test_handler(self):
|
||||
event = {"value": "10"}
|
||||
response = lambda_function.handler(event, {})
|
||||
assert response == "1,2,5,10"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"initial,expected",
|
||||
[
|
||||
(1, {1}),
|
||||
(10, {1, 2, 5, 10}),
|
||||
(60, {1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60}),
|
||||
(64, {1, 2, 4, 8, 16, 32, 64}),
|
||||
],
|
||||
)
|
||||
def test_get_factors(self, initial, expected):
|
||||
response = lambda_function.get_factors(initial)
|
||||
assert response == expected
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"initial,expected",
|
||||
[([], ""), ([1], "1"), ([2], "2"), ([1, 2, 3], "1,2,3"), ([1, 3, 2], "1,2,3"),],
|
||||
)
|
||||
def test_stringify(self, initial, expected):
|
||||
response = lambda_function.stringify(initial)
|
||||
assert response == expected
|
66
tests/test_lambda_verification.py
Normal file
66
tests/test_lambda_verification.py
Normal file
@ -0,0 +1,66 @@
|
||||
""" Tests for lambda_verification function """
|
||||
# pylint: disable=no-self-use,missing-function-docstring,missing-class-docstring
|
||||
|
||||
import os
|
||||
import pytest
|
||||
import mock
|
||||
from src import lambda_verification
|
||||
|
||||
PAYLOAD = {
|
||||
"DeploymentId": "TestDeployment001",
|
||||
"LifecycleEventHookExecutionId": "LifeCycleTest001",
|
||||
}
|
||||
|
||||
os.environ["CurrentVersion"] = "CurVer"
|
||||
os.environ["TestData"] = '{"value":"10"}'
|
||||
os.environ["TestExpected"] = "2,5"
|
||||
|
||||
|
||||
class TestLambdaVerfication:
|
||||
def test_handler_bad_payload(self):
|
||||
with pytest.raises(Exception):
|
||||
assert lambda_verification.handler({}, {})
|
||||
|
||||
@mock.patch("src.lambda_verification.CODEDEPLOY")
|
||||
@mock.patch("src.lambda_verification.AWSLAMBDA")
|
||||
@pytest.mark.parametrize(
|
||||
"response,expected", [("2,4", "Failed"), ("2,5", "Succeeded")],
|
||||
)
|
||||
def test_handler(self, mock_lambda, mock_codedeploy, response, expected):
|
||||
response_mock = mock.MagicMock()
|
||||
response_mock.read.return_value = response
|
||||
mock_lambda.invoke.return_value = {"Payload": response_mock}
|
||||
|
||||
lambda_verification.handler(PAYLOAD, {})
|
||||
|
||||
mock_codedeploy.put_lifecycle_event_hook_execution_status.assert_called_with(
|
||||
status=expected,
|
||||
deploymentId="TestDeployment001",
|
||||
lifecycleEventHookExecutionId="LifeCycleTest001",
|
||||
)
|
||||
|
||||
@mock.patch("src.lambda_verification.CODEDEPLOY")
|
||||
@mock.patch("src.lambda_verification.AWSLAMBDA")
|
||||
def test_handler_lambda_failure(self, mock_lambda, mock_codedeploy):
|
||||
mock_lambda.invoke.side_effect = Exception()
|
||||
with pytest.raises(Exception):
|
||||
assert lambda_verification.handler(PAYLOAD, {})
|
||||
|
||||
mock_codedeploy.put_lifecycle_event_hook_execution_status.assert_called_with(
|
||||
status="Failed",
|
||||
deploymentId="TestDeployment001",
|
||||
lifecycleEventHookExecutionId="LifeCycleTest001",
|
||||
)
|
||||
|
||||
@mock.patch("src.lambda_verification.CODEDEPLOY")
|
||||
@mock.patch("src.lambda_verification.AWSLAMBDA")
|
||||
def test_handler_codedeploy_failure(self, mock_lambda, mock_codedeploy):
|
||||
response_mock = mock.MagicMock()
|
||||
response_mock.read.return_value = "2,5"
|
||||
mock_lambda.invoke.return_value = {"Payload": response_mock}
|
||||
mock_codedeploy.put_lifecycle_event_hook_execution_status.side_effect = (
|
||||
Exception()
|
||||
)
|
||||
|
||||
with pytest.raises(Exception):
|
||||
assert lambda_verification.handler(PAYLOAD, {})
|
Reference in New Issue
Block a user