-
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.
- Loading branch information
1 parent
2c32a6c
commit 438eeef
Showing
6 changed files
with
178 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,49 @@ | ||
import json | ||
import boto3 | ||
|
||
|
||
def lambda_handler(event, context): | ||
bucket_name = 's3-trigger-lambda-function-test12345' | ||
file_name = 'employees.json' | ||
|
||
s3 = boto3.client('s3') | ||
|
||
try: | ||
# Extract bucket name and object key from the S3 event | ||
bucket_name = event["Records"][0]["s3"]["bucket"]["name"] | ||
file_name = event["Records"][0]["s3"]["object"]["key"] | ||
|
||
s3 = boto3.client("s3") | ||
|
||
response = s3.get_object(Bucket=bucket_name, Key=file_name) | ||
content = response['Body'].read().decode('utf-8') | ||
content = response["Body"].read().decode("utf-8") | ||
employees_data = json.loads(content) | ||
|
||
department_totals = {} | ||
|
||
for employee in employees_data: | ||
department = employee.get('department') | ||
salary = employee.get('salary') | ||
|
||
|
||
if salary is None or not isinstance(salary, (int, float)) or department is None: | ||
department = employee.get("department") | ||
salary = employee.get("salary") | ||
|
||
if ( | ||
salary is None | ||
or not isinstance(salary, (int, float)) | ||
or department is None | ||
or not isinstance(department,(str)) | ||
): | ||
raise ValueError("Invalid/missing data for department or salary..") | ||
|
||
if department not in department_totals: | ||
department_totals[department] = salary | ||
else: | ||
department_totals[department] += salary | ||
|
||
sorted_departments = sorted(department_totals.items(), key=lambda x: x[1], reverse=True) | ||
|
||
for department, total_salary in sorted_departments: | ||
print(f"Department: {department}, Total Salary: {total_salary}") | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': json.dumps(department_totals) | ||
} | ||
|
||
|
||
# Sort the department totals by total salary | ||
sorted_departments = dict(sorted(department_totals.items(), key=lambda x: x[1], reverse=True)) | ||
|
||
return {"statusCode": 200, "body": json.dumps(sorted_departments)} | ||
|
||
except ValueError as ve: | ||
error_message = f"Value error occurred: {str(ve)}" | ||
print(error_message) | ||
return { | ||
'statusCode': 400, | ||
'body': json.dumps({'error': error_message}) | ||
} | ||
|
||
return {"statusCode": 400, "body": json.dumps({"error": error_message})} | ||
|
||
except Exception as e: | ||
error_message = f"An unexpected error occurred: {str(e)}" | ||
print(error_message) | ||
return { | ||
'statusCode': 500, | ||
'body': json.dumps({'error': error_message}) | ||
} | ||
return {"statusCode": 500, "body": json.dumps({"error": error_message})} |
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
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
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
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
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,142 @@ | ||
import json | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
import sys | ||
sys.path.append('../src') # To add 'src' directory to the module search path | ||
# sys.path.insert(0, '../src') # Adjust the path based on your project structure | ||
|
||
|
||
from lambda_function import lambda_handler | ||
|
||
|
||
class TestLambdaFunction(unittest.TestCase): | ||
|
||
@patch('lambda_function.boto3') | ||
def test_lambda_handler_valid_data(self, mock_boto3): | ||
# Define a sample event with valid data | ||
event = { | ||
"Records": [ | ||
{ | ||
"s3": { | ||
"bucket": { | ||
"name": "test-bucket" | ||
}, | ||
"object": { | ||
"key": "test-file.json" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
|
||
# Mock the S3 client and its get_object method to return sample employee data | ||
mock_s3 = MagicMock() | ||
mock_boto3.client.return_value = mock_s3 | ||
mock_s3.get_object.return_value = { | ||
"Body": MagicMock(read=MagicMock(return_value=json.dumps([ | ||
{"department": "HR", "salary": 50000}, | ||
{"department": "Finance", "salary": 60000}, | ||
{"department": "HR", "salary": 55000} | ||
]).encode('utf-8'))) | ||
} | ||
|
||
# Execute the lambda handler function with the mocked S3 client | ||
result = lambda_handler(event, None) | ||
|
||
# Define the expected result based on the mocked employee data | ||
expected_result = { | ||
"Finance": 60000, | ||
"HR": 105000 | ||
} | ||
|
||
# Assert that the result matches the expected department totals | ||
self.assertEqual(result['statusCode'], 200) | ||
self.assertEqual(json.loads(result['body']), expected_result) | ||
|
||
|
||
@patch('lambda_function.boto3') | ||
def test_lambda_handler_salary_not_numeric_or_none(self, mock_boto3): | ||
# Define a sample event with missing or invalid salary values | ||
event = { | ||
"Records": [ | ||
{ | ||
"s3": { | ||
"bucket": { | ||
"name": "test-bucket" | ||
}, | ||
"object": { | ||
"key": "test-file.json" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
|
||
# Mock the S3 client and its get_object method to return sample employee data | ||
mock_s3 = MagicMock() | ||
mock_boto3.client.return_value = mock_s3 | ||
mock_s3.get_object.return_value = { | ||
"Body": MagicMock(read=MagicMock(return_value=json.dumps([ | ||
{"department": "HR", "salary": "invalid"}, | ||
{"department": "Finance", "salary": None}, | ||
{"department": "HR", "salary": "60000"} | ||
]).encode('utf-8'))) | ||
} | ||
|
||
# Execute the lambda handler function with the mocked S3 client | ||
result = lambda_handler(event, None) | ||
|
||
# Define the expected result when salary is not numeric or None | ||
expected_result = { | ||
"error": "Value error occurred: Invalid/missing data for department or salary.." | ||
} | ||
|
||
# Assert that the result contains the expected error message | ||
self.assertEqual(result['statusCode'], 400) | ||
self.assertEqual(json.loads(result['body']), expected_result) | ||
|
||
@patch('lambda_function.boto3') | ||
def test_lambda_handler_department_invalid_missing_values(self, mock_boto3): | ||
# Define a sample event with missing or invalid department values | ||
event = { | ||
"Records": [ | ||
{ | ||
"s3": { | ||
"bucket": { | ||
"name": "test-bucket" | ||
}, | ||
"object": { | ||
"key": "test-file.json" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
|
||
# Mock the S3 client and its get_object method to return sample employee data | ||
mock_s3 = MagicMock() | ||
mock_boto3.client.return_value = mock_s3 | ||
mock_s3.get_object.return_value = { | ||
"Body": MagicMock(read=MagicMock(return_value=json.dumps([ | ||
{"department": None, "salary": 50000}, | ||
{"department": "HR", "salary": None}, | ||
{"department": 32873, "salary": 55000} | ||
]).encode('utf-8'))) | ||
} | ||
|
||
# Execute the lambda handler function with the mocked S3 client | ||
result = lambda_handler(event, None) | ||
|
||
# Define the expected result when department values are None or non string | ||
expected_result = { | ||
"error": "Value error occurred: Invalid/missing data for department or salary.." | ||
} | ||
|
||
# Assert that the result contains the expected error message | ||
self.assertEqual(result['statusCode'], 400) | ||
self.assertEqual(json.loads(result['body']), expected_result) | ||
|
||
# ... (Previous test case remains unchanged) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |