"""Integration tests for lambda function.""" import pytest from conftest import ( create_abacus_event_fixture, create_account_contract_fixture, create_account_fixture, create_contract_advance_fixture, create_contract_fixture, create_ledger_account_contract_fixture, create_ledger_contract_advance_applied_fixture, create_payment_group_fixture, create_payment_group_payment_account_detail_fixture, create_payment_group_payment_account_fixture, create_payment_group_payment_batch_account_fixture, create_payment_group_payment_batch_fixture, create_payment_group_payment_fixture, create_worksheet_account_contract_closing_balance_fixture, create_worksheet_account_contract_payable_after_tax_fixture, create_worksheet_payment_contract_advance_fixture, dockerized_lambda_api_client, get_db_field_value, ) @pytest.mark.jira('ACC-6485') def test_invoke_lambda_advance_payment(basic_headers, clean_db): """Test invoking the lambda with a "commit_advance_payment" event.""" account_id = 1 create_account_fixture(account_id) contract_id = create_contract_fixture() create_account_contract_fixture(account_id, contract_id) contract_advance_id = create_contract_advance_fixture(contract_id) worksheet_advance_id = create_worksheet_payment_contract_advance_fixture( contract_advance_id ) commit_to_subledger_event_id = create_abacus_event_fixture( 'commit_to_subledger', 'worksheet_payment_contract_advance', worksheet_advance_id, ) create_ledger_contract_advance_applied_fixture( contract_advance_id, worksheet_advance_id, commit_to_subledger_event_id, account_id, contract_id, ) commit_advance_payment_event_id = create_abacus_event_fixture( 'commit_advance_payment', 'worksheet_payment_contract_advance', worksheet_advance_id, ) row_count = get_db_field_value( 'ledger_account_contract', 'COUNT(*)', f'abacus_event_id = {commit_advance_payment_event_id}', ) assert row_count == 0 dockerized_lambda = dockerized_lambda_api_client(basic_headers) body = { 'event_name': 'commit_advance_payment', 'event_date': '2021-07-11T11:20:35.45+01:00', 'abacus_event_id': commit_advance_payment_event_id, 'target_type': 'worksheet_payment_contract_advance', 'target_id': worksheet_advance_id, } res = dockerized_lambda.post_lambda('2015-03-31', body) row_count = get_db_field_value( 'ledger_account_contract', 'COUNT(*)', f'abacus_event_id = {commit_advance_payment_event_id}', ) assert res.status_code == 200 assert res.json() == {'status': 'OK'} assert row_count == 1 @pytest.mark.jira('ACC-7720') def test_invoke_lambda_payment_group(basic_headers, clean_db): """Test invoking the lambda with a "payment group" event.""" account_id = 1 create_account_fixture(account_id) contract_id = create_contract_fixture() create_account_contract_fixture(account_id, contract_id) payment_group_id = create_payment_group_fixture() payment_group_payment_id = create_payment_group_payment_fixture(payment_group_id) payment_group_payment_batch_id = create_payment_group_payment_batch_fixture( payment_group_payment_id ) payment_group_payment_account_id = create_payment_group_payment_account_fixture( payment_group_payment_id, account_id ) create_payment_group_payment_batch_account_fixture( payment_group_payment_batch_id, payment_group_payment_account_id ) payments_generate_event_id = create_abacus_event_fixture( 'payments_generate', 'payment_group_payment', payment_group_payment_id ) ledger_account_contract_id = create_ledger_account_contract_fixture( payments_generate_event_id, account_id, contract_id ) close_balance_event_id = create_abacus_event_fixture( 'close_balance', 'statement_period_payment_entity', 73 ) closing_balance_id = create_worksheet_account_contract_closing_balance_fixture( account_id, contract_id, close_balance_event_id, ledger_account_contract_id ) after_tax_id = create_worksheet_account_contract_payable_after_tax_fixture( closing_balance_id, account_id, contract_id, payments_generate_event_id ) create_payment_group_payment_account_detail_fixture( payment_group_payment_account_id, after_tax_id, account_id, contract_id ) commit_batch_payment_event_id = create_abacus_event_fixture( 'commit_batch_payment', 'payment_group_payment_batch', payment_group_payment_batch_id, ) row_count = get_db_field_value( 'ledger_account_contract', 'COUNT(*)', f'abacus_event_id = {commit_batch_payment_event_id}', ) assert row_count == 0 dockerized_lambda = dockerized_lambda_api_client(basic_headers) body = { 'event_name': 'commit_batch_payment', 'event_date': '2021-07-11T11:20:35.45+01:00', 'abacus_event_id': commit_batch_payment_event_id, 'target_type': 'payment_group_payment_batch', 'target_id': payment_group_payment_batch_id, } res = dockerized_lambda.post_lambda('2015-03-31', body) row_count = get_db_field_value( 'ledger_account_contract', 'COUNT(*)', f'abacus_event_id = {commit_batch_payment_event_id}', ) assert res.status_code == 200 assert res.json() == {'status': 'OK'} assert row_count == 1 @pytest.mark.jira('ACC-7768') def test_invoke_lambda_commit_vat_summary_payment_group(basic_headers, clean_db): """Test a "commit_vat_summary" event with a "payment_group" target type.""" account_id = 1 create_account_fixture(account_id) contract_id = create_contract_fixture() create_account_contract_fixture(account_id, contract_id) payment_group_id = create_payment_group_fixture() payment_group_payment_id = create_payment_group_payment_fixture(payment_group_id) payment_group_payment_batch_id = create_payment_group_payment_batch_fixture( payment_group_payment_id ) payment_group_payment_account_id = create_payment_group_payment_account_fixture( payment_group_payment_id, account_id ) create_payment_group_payment_batch_account_fixture( payment_group_payment_batch_id, payment_group_payment_account_id ) payments_generate_event_id = create_abacus_event_fixture( 'payments_generate', 'payment_group_payment', payment_group_payment_id ) ledger_account_contract_id = create_ledger_account_contract_fixture( payments_generate_event_id, account_id, contract_id ) close_balance_event_id = create_abacus_event_fixture( 'close_balance', 'statement_period_payment_entity', 73 ) closing_balance_id = create_worksheet_account_contract_closing_balance_fixture( account_id, contract_id, close_balance_event_id, ledger_account_contract_id ) after_tax_id = create_worksheet_account_contract_payable_after_tax_fixture( closing_balance_id, account_id, contract_id, payments_generate_event_id ) create_payment_group_payment_account_detail_fixture( payment_group_payment_account_id, after_tax_id, account_id, contract_id ) commit_vat_summary_event_id = create_abacus_event_fixture( 'commit_vat_summary', 'payment_group_payment_batch', payment_group_payment_batch_id, ) row_count = get_db_field_value( 'ledger_account_contract', 'COUNT(*)', f'abacus_event_id = {commit_vat_summary_event_id}', ) assert row_count == 0 dockerized_lambda = dockerized_lambda_api_client(basic_headers) body = { 'event_name': 'commit_vat_summary', 'event_date': '2021-07-11T11:20:35.45+01:00', 'abacus_event_id': commit_vat_summary_event_id, 'target_type': 'payment_group_payment_batch', 'target_id': payment_group_payment_batch_id, } res = dockerized_lambda.post_lambda('2015-03-31', body) row_count = get_db_field_value( 'ledger_account_contract', 'COUNT(*)', f'abacus_event_id = {commit_vat_summary_event_id}', ) assert res.status_code == 200 assert res.json() == {'status': 'OK'} assert row_count == 1