"""Lambda test module.""" import typing from unittest.mock import call, MagicMock, patch import urllib.parse from pydantic import ValidationError import pytest from src import constants, main from src.main import PROCESSORS from src.models import S3Event from tests.unit.factories import S3EventFactory @patch.dict(PROCESSORS, {'folder': MagicMock()}, clear=True) def test_handler_success(sample_event: typing.Dict[str, typing.Any]) -> None: """Test handler function.""" result = main.handler(sample_event, None) bucket_name = sample_event['Records'][0]['s3']['bucket']['name'] object_key = urllib.parse.unquote_plus( sample_event['Records'][0]['s3']['object']['key'] ) assert result == {'status': constants.SUCCESS_STATUS} assert PROCESSORS['folder'].execute.call_args_list == [ # type: ignore call(bucket_name, object_key) ] def test_handler_failure_bad_event() -> None: """Test handler function failure on event validation.""" event = {'some_wrong_key': 'some_wrong_value'} with pytest.raises(ValidationError): main.handler(event, None) @patch.dict(PROCESSORS, {}, clear=True) def test_handler_failure_bad_processor() -> None: """Test handler function on file download.""" event: S3Event = S3EventFactory.build() with pytest.raises(Exception, match='The logic is not assigned for this directory'): main.handler(event, None) # type: ignore