"""Functional tests for ledger adjustment endpoints.""" from contextlib import contextmanager from moneyhub.connectors.snowflake import db from moneyhub.constants.constants import FLOWTHROUGH_ADJUSTMENT_TYPE_ID from moneyhub.models import AdjustmentsByType from moneyhub.models import CombinedAdjustments from moneyhub.models.snowflake_base import BaseModel from tests.functional.conftest import insert_mock_data # TODO remove reference_sap_profit_center table reference when FF is removed. @contextmanager def _using_mock_data(model: BaseModel, mock_data: dict): """Create the necessary mock tables and fills them with the data. Args: mock_data (dict): Data to enter. """ model.__table__.drop(db.engine, checkfirst=True) db.session.commit() model.__table__.create(db.engine) insert_mock_data(mock_data, database='snowflake') yield db.session.commit() model.__table__.drop(db.engine) db.session.close() def _insert_mock_data(account_id): insert_mock_data({ 'account': [ {'account_id': account_id}, {'account_id': 999}, ], 'reference_payment_entity': {'reference_payment_entity_id': 1}, 'reference_sap_profit_center': { 'reference_sap_profit_center_id': 1, 'profit_center': 'UK1234', 'company_code': '1234', 'business_group': 'ORC' }, 'reference_signing_entity': { 'reference_signing_entity_id': 1, 'reference_payment_entity_id': 1, 'reference_sap_profit_center_id': 1, 'company_code': '1234', 'tax_entity_company_code': '1234', 'legal_name': 'Company', 'vat_number': '12341234', 'company_registration_number': '12341234', 'address': None, }, 'contract': [ { 'contract_id': 1001, 'reference_signing_entity_id': 1, }, { 'contract_id': 999, 'reference_signing_entity_id': 1, }, ], 'reference_adjustment_type': [ { 'reference_adjustment_type_id': 1, 'type_name': 'Label Earnings' } ], 'statement_period': [ {'statement_period_id': 123, 'statement_period_status': 'closed'}, {'statement_period_id': 124, 'statement_period_status': 'current'}, ], 'abacus_event': [ {'abacus_event_id': 1}, {'abacus_event_id': 2}, {'abacus_event_id': 3}, {'abacus_event_id': 4}, {'abacus_event_id': 5}, ], 'statement_period_adjustment_file': { 'statement_period_adjustment_file_id': 1, 'statement_period_id': 123, }, 'worksheet_adjustment': [ { 'worksheet_adjustment_id': 1, 'abacus_event_id': 1, 'statement_period_adjustment_file_id': 1, 'account_id': account_id, 'contract_id': 1001, 'activity_statement_period_id': 123, 'apply_to_statement_period_id': 123, 'reference_adjustment_type_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'note': 'mock adjustment' }, { 'worksheet_adjustment_id': 2, 'abacus_event_id': 2, 'statement_period_adjustment_file_id': 1, 'account_id': account_id, 'contract_id': 1001, 'activity_statement_period_id': 124, 'apply_to_statement_period_id': 124, 'reference_adjustment_type_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'note': 'mock adjustment' }, { 'worksheet_adjustment_id': 3, 'abacus_event_id': 3, 'statement_period_adjustment_file_id': 1, 'account_id': account_id, 'contract_id': 1001, 'activity_statement_period_id': 123, 'apply_to_statement_period_id': 123, 'reference_adjustment_type_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'note': 'mock adjustment' }, { 'worksheet_adjustment_id': 4, 'abacus_event_id': 4, 'statement_period_adjustment_file_id': 1, 'account_id': 999, 'contract_id': 1001, 'activity_statement_period_id': 123, 'apply_to_statement_period_id': 123, 'reference_adjustment_type_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'note': 'mock adjustment' }, { 'worksheet_adjustment_id': 5, 'abacus_event_id': 5, 'statement_period_adjustment_file_id': 1, 'account_id': account_id, 'contract_id': 1001, 'activity_statement_period_id': 124, 'apply_to_statement_period_id': 124, 'reference_adjustment_type_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'note': 'mock adjustment' }, ], 'ledger_adjustment_applied': [ { 'ledger_adjustment_applied_id': 1, 'abacus_event_id': 1, 'account_id': account_id, 'contract_id': 1001, 'statement_period_id': 123, 'worksheet_adjustment_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, { 'ledger_adjustment_applied_id': 2, 'abacus_event_id': 2, 'account_id': account_id, 'contract_id': 1001, 'statement_period_id': 124, 'worksheet_adjustment_id': 2, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, { 'ledger_adjustment_applied_id': 3, 'abacus_event_id': 3, 'account_id': account_id, 'contract_id': 1001, 'statement_period_id': 123, 'worksheet_adjustment_id': 3, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, { 'ledger_adjustment_applied_id': 4, 'abacus_event_id': 4, 'account_id': 999, 'contract_id': 1001, 'statement_period_id': 123, 'worksheet_adjustment_id': 4, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, { 'ledger_adjustment_applied_id': 5, 'abacus_event_id': 5, 'account_id': account_id, 'contract_id': 1001, 'statement_period_id': 124, 'worksheet_adjustment_id': 5, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, ] }) def test_get_ledger_adjustments_by_account_id(fixture_client): """Test getting ledger adjustments.""" account_id = 24601 _insert_mock_data(account_id) mock_dbt = { 'combined_adjustments_dbt': [ { 'worksheet_adjustment_id': 1, 'account_id': account_id, 'contract_id': 1001, 'activity_statement_period_id': 123, 'apply_to_statement_period_id': 123, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', 'ledger_adjustment_applied_id': 1, 'note': 'mock adjustment' }, { 'worksheet_adjustment_id': 2, 'account_id': account_id, 'contract_id': 1001, 'activity_statement_period_id': 124, 'apply_to_statement_period_id': 124, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', 'ledger_adjustment_applied_id': 2, 'note': 'mock adjustment' }, { 'worksheet_adjustment_id': 3, 'account_id': account_id, 'contract_id': 1001, 'activity_statement_period_id': 123, 'apply_to_statement_period_id': 123, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', 'ledger_adjustment_applied_id': 3, 'note': 'mock adjustment' }, { 'worksheet_adjustment_id': 4, 'account_id': 999, 'contract_id': 1001, 'activity_statement_period_id': 123, 'apply_to_statement_period_id': 123, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', 'ledger_adjustment_applied_id': 4, 'note': 'mock adjustment' }, { 'worksheet_adjustment_id': 5, 'account_id': account_id, 'contract_id': 1001, 'activity_statement_period_id': 124, 'apply_to_statement_period_id': 124, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', 'ledger_adjustment_applied_id': 5, 'note': 'mock adjustment' }, ] } with _using_mock_data(CombinedAdjustments, mock_dbt): result = fixture_client.get(f'/ledger-adjustments/account/{account_id}') assert result.status_code == 200 assert result.json() == [ { 'account_id': 24601, 'activity_statement_period_id': 123, 'adjustment_amount_payee_currency': 800.00, 'adjustment_payee_currency_code': 'GBP', 'apply_to_statement_period_id': 123, 'contract_id': 1001, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'note': 'mock adjustment', 'worksheet_adjustment_id': 1 }, { 'account_id': 24601, 'activity_statement_period_id': 123, 'adjustment_amount_payee_currency': 800.00, 'adjustment_payee_currency_code': 'GBP', 'apply_to_statement_period_id': 123, 'contract_id': 1001, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'note': 'mock adjustment', 'worksheet_adjustment_id': 3, } ] def test_get_ledger_adjustments_types_by_account_id(fixture_client): """Test getting ledger adjustment types.""" account_id = 24601 mock_dbt = { 'combined_adjustments_dbt': [ { 'worksheet_adjustment_id': 1, 'account_id': account_id, 'contract_id': 1001, 'activity_statement_period_id': 123, 'apply_to_statement_period_id': 123, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', 'ledger_adjustment_applied_id': 1, 'note': 'mock adjustment' }, { 'worksheet_adjustment_id': 2, 'account_id': account_id, 'contract_id': 1001, 'activity_statement_period_id': 124, 'apply_to_statement_period_id': 124, 'reference_adjustment_type_id': 2, 'reference_adjustment_type_name': 'Physical Sales', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', 'ledger_adjustment_applied_id': 2, 'note': 'mock adjustment' } ] } with _using_mock_data(CombinedAdjustments, mock_dbt): result = fixture_client.get(f'/ledger-adjustments/account/{account_id}/types') assert result.status_code == 200 assert result.json() == [ { 'reference_adjustment_type_id': 1, 'type_name': 'Label Earnings', 'oa_category_name': None }, { 'reference_adjustment_type_id': 2, 'type_name': 'Physical Sales', 'oa_category_name': None }, ] def test_get_adjustments_by_account_and_statement_period(fixture_client): """Test getting adjustments for a given statement period.""" account_id = 24601 statement_period_id = 123 mock_statement_period = { 'statement_period': { 'statement_period_id': statement_period_id, 'statement_period_status': 'closed' }, } mock_data = { 'combined_adjustments_by_type_dbt': [ { 'account_id': account_id, 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', 'contract_id': 1001, 'statement_period_id': statement_period_id, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'ledger_adjustment_applied_id': 1, }, { 'account_id': account_id, 'adjustment_amount_payee_currency': 400, 'adjustment_payee_currency_code': 'GBP', 'contract_id': 1002, 'statement_period_id': statement_period_id, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'ledger_adjustment_applied_id': 2, }, { 'account_id': account_id, 'adjustment_amount_payee_currency': 400, 'adjustment_payee_currency_code': 'GBP', 'contract_id': 1002, 'statement_period_id': statement_period_id, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'ledger_adjustment_applied_id': 3, }, ], } insert_mock_data(mock_statement_period) with _using_mock_data(AdjustmentsByType, mock_data): result = fixture_client.get( f'/ledger-adjustments/account/{account_id}/statement-period/{statement_period_id}') assert result.status_code == 200 assert result.json() == { 'adjustment_payee_currency_code': 'GBP', 'adjustment_total_payee_currency': 1600.00, 'breakdown_items': [ { 'adjustment_payee_currency_code': 'GBP', 'adjustment_total_payee_currency': 1600.00, 'contract_id': None, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', }, ], 'statement_period_id': 123 } def test_get_adjustments_by_account_and_statement_period_flowthrough(fixture_client): """Test getting flowthrough adjustments for a given statement period.""" account_id = 24601 statement_period_id = 123 mock_statement_period = { 'statement_period': { 'statement_period_id': statement_period_id, 'statement_period_status': 'closed' }, } mock_data = { 'combined_adjustments_by_type_dbt': [ { 'account_id': account_id, 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', 'contract_id': 1001, 'statement_period_id': statement_period_id, 'reference_adjustment_type_id': FLOWTHROUGH_ADJUSTMENT_TYPE_ID, 'reference_adjustment_type_name': 'Flowthrough', 'ledger_adjustment_applied_id': 1, }, { 'account_id': account_id, 'adjustment_amount_payee_currency': -800, 'adjustment_payee_currency_code': 'GBP', 'contract_id': 1002, 'statement_period_id': statement_period_id, 'reference_adjustment_type_id': FLOWTHROUGH_ADJUSTMENT_TYPE_ID, 'reference_adjustment_type_name': 'Flowthrough', 'ledger_adjustment_applied_id': 2, } ], } insert_mock_data(mock_statement_period) with _using_mock_data(AdjustmentsByType, mock_data): result = fixture_client.get( f'/ledger-adjustments/account/{account_id}/statement-period/{statement_period_id}') assert result.status_code == 200 assert result.json() == { 'adjustment_payee_currency_code': 'GBP', 'adjustment_total_payee_currency': 0.00, 'breakdown_items': [ { 'adjustment_payee_currency_code': 'GBP', 'adjustment_total_payee_currency': 800.00, 'contract_id': 1001, 'reference_adjustment_type_id': FLOWTHROUGH_ADJUSTMENT_TYPE_ID, 'reference_adjustment_type_name': 'Flowthrough', }, { 'adjustment_payee_currency_code': 'GBP', 'adjustment_total_payee_currency': -800.00, 'contract_id': 1002, 'reference_adjustment_type_id': FLOWTHROUGH_ADJUSTMENT_TYPE_ID, 'reference_adjustment_type_name': 'Flowthrough', }, ], 'statement_period_id': 123 } def test_get_adjustments_by_account_and_statement_period_not_visible(fixture_client): """Test getting adjustments for a given statement period that's not visible.""" account_id = 24601 statement_period_id = 124 mock_statement_period = { 'statement_period': { 'statement_period_id': statement_period_id, 'statement_period_status': 'current' }, } mock_data = { 'combined_adjustments_by_type_dbt': [ { 'account_id': account_id, 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', 'contract_id': 1001, 'statement_period_id': statement_period_id, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'ledger_adjustment_applied_id': 1, }, ], } insert_mock_data(mock_statement_period) with _using_mock_data(AdjustmentsByType, mock_data): result = fixture_client.get( f'/ledger-adjustments/account/{account_id}/statement-period/{statement_period_id}') assert result.status_code == 200 assert result.json() is None