|
| 1 | +from unittest import TestCase |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +import os |
| 4 | +from src.converter import lambda_handler, ensure_dat_extension |
| 5 | + |
| 6 | + |
| 7 | +class TestLambdaHandler(TestCase): |
| 8 | + |
| 9 | + @patch('boto3.client') |
| 10 | + @patch('os.getenv') |
| 11 | + def test_lambda_handler_success(self, mock_getenv, mock_boto_client): |
| 12 | + # Mock environment variable |
| 13 | + mock_getenv.return_value = "destination-bucket" |
| 14 | + |
| 15 | + # Mock boto3 S3 client |
| 16 | + mock_s3 = MagicMock() |
| 17 | + mock_boto_client.return_value = mock_s3 |
| 18 | + mock_s3.get_object.return_value = { |
| 19 | + 'Metadata': {'mex-filename': '20250320121710483244_2DB240.txt'} |
| 20 | + } |
| 21 | + |
| 22 | + # Define the event |
| 23 | + event = { |
| 24 | + "Records": [ |
| 25 | + { |
| 26 | + "s3": { |
| 27 | + "bucket": {"name": "source-bucket"}, |
| 28 | + "object": {"key": "20250320121710483244_2DB240.dat"} |
| 29 | + } |
| 30 | + } |
| 31 | + ] |
| 32 | + } |
| 33 | + context = {} |
| 34 | + |
| 35 | + # Call the lambda_handler function |
| 36 | + response = lambda_handler(event, context) |
| 37 | + |
| 38 | + # Assertions |
| 39 | + mock_s3.get_object.assert_called_with(Bucket="source-bucket", Key="20250320121710483244_2DB240.dat") |
| 40 | + mock_s3.copy_object.assert_called_with( |
| 41 | + CopySource={'Bucket': "source-bucket", 'Key': "20250320121710483244_2DB240.dat"}, |
| 42 | + Bucket="destination-bucket", |
| 43 | + Key="20250320121710483244_2DB240.dat" |
| 44 | + ) |
| 45 | + self.assertEqual(response['statusCode'], 200) |
| 46 | + self.assertEqual(response['body'], 'Files converted and uploaded successfully!') |
| 47 | + |
| 48 | + def test_ensure_dat_extension_with_other_extension(self): |
| 49 | + # Test case where file has an extension other than 'dat' |
| 50 | + result = ensure_dat_extension("COVID19_Vaccinations_v5_YGM41_20240927T13005921.txt") |
| 51 | + self.assertEqual(result, "COVID19_Vaccinations_v5_YGM41_20240927T13005921.dat") |
| 52 | + |
| 53 | + def test_ensure_dat_extension_with_dat_extension(self): |
| 54 | + # Test case where file already has a 'dat' extension |
| 55 | + result = ensure_dat_extension("COVID19_Vaccinations_v5_YGM41_20240927T13005921.dat") |
| 56 | + self.assertEqual(result, "COVID19_Vaccinations_v5_YGM41_20240927T13005921.dat") |
| 57 | + |
| 58 | + def test_ensure_dat_extension_without_extension(self): |
| 59 | + # Test case where file has no extension |
| 60 | + result = ensure_dat_extension("COVID19_Vaccinations_v5_YGM41_20240927T13005921") |
| 61 | + self.assertEqual(result, "COVID19_Vaccinations_v5_YGM41_20240927T13005921.dat") |
0 commit comments