"""Integration tests for lambda function.""" from utils.helpers import ( dockerized_lambda_api_client, get_invalid_file_location, get_s3_file_contents, ) def test_invoke_lambda(basic_headers): """Assert lambda action.""" dockerized_lambda = dockerized_lambda_api_client(basic_headers) body = { 'abacus_event_id': 1, 'target_type': 'statement_period_adjustment_file', 'target_id': '1', 'event_name': 'generate_flowthrough_adjustments', 'event_date': '2026-02-13T11:20:35.45+01:00', 'statement_period_id': 282, } res = dockerized_lambda.post_lambda('2015-03-31', body) assert res.status_code == 200 assert 'status' in res.json(), ( f'error type was: {res.json()["errorType"]}, ' f'error message was: {res.json()["errorMessage"]}' ) assert res.json()['status'] == 'FILE_NOT_VALID' invalid_file_location = get_invalid_file_location(1) records = get_s3_file_contents(invalid_file_location) # statement period error is expected since validation happens against snowflake and not the local docker db. invalid_records = [r for r in records if r.get('Validation Errors')] assert len(invalid_records) == 1, ( f'Expected exactly 1 invalid record in the error file, got {len(invalid_records)}' ) assert invalid_records[0]['Validation Errors'] == [ 'Statement Period is missing', 'closed', 'or in the future', ], f'Unexpected Validation Errors value: {invalid_records[0]["Validation Errors"]}' def test_invoke_lambda_invalid_file(basic_headers): """Assert lambda action for an invalid file.""" dockerized_lambda = dockerized_lambda_api_client(basic_headers) body = { 'abacus_event_id': 1, 'target_type': 'statement_period_adjustment_file', 'target_id': '2', 'event_name': 'generate_flowthrough_adjustments', 'event_date': '2026-02-13T11:20:35.45+01:00', 'statement_period_id': 282, } res = dockerized_lambda.post_lambda('2015-03-31', body) assert res.status_code == 200 assert 'status' in res.json(), ( f'error type was: {res.json()["errorType"]}, ' f'error message was: {res.json()["errorMessage"]}' ) assert res.json()['status'] == 'FILE_NOT_VALID' invalid_file_location = get_invalid_file_location(2) records = get_s3_file_contents(invalid_file_location) invalid_records = [r for r in records if r.get('Validation Errors')] assert len(invalid_records) == 1, ( f'Expected exactly 1 invalid record in the error file, got {len(invalid_records)}' ) assert invalid_records[0]['Validation Errors'] == [ 'Account ID is not valid', 'No payment term is associated with account.', 'Contract ID is not valid', 'Currency is required', 'Statement Period is missing', 'closed', 'or in the future', ], f'Unexpected Validation Errors value: {invalid_records[0]["Validation Errors"]}'