"""Functional tests for Account Statement Period.""" from contextlib import contextmanager from moneyhub.connectors.snowflake import db from moneyhub.models import AccountStatementPeriods from moneyhub.models import CombinedPayments from moneyhub.models import LedgerSummary from moneyhub.models import RevenueByActivityMonth from moneyhub.models import RevenueByArtist from moneyhub.models import RevenueByImprint from moneyhub.models import RevenueByProductDistro from moneyhub.models import RevenueByProject from moneyhub.models import RevenueByStore from moneyhub.models import RevenueByTransactionType from moneyhub.models import RevenueDistro from moneyhub.models import RevenueNr from moneyhub.models import Vendor from moneyhub.models import WorkstationSummary from tests.functional.conftest import _using_mock_account_statements_data from tests.functional.conftest import insert_mock_data MOCK_DATA = { 'account': [ {'account_id': 1}, {'account_id': 2}, ], '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': 1, 'reference_signing_entity_id': 1, }, 'account_contract': {'account_id': 1, 'contract_id': 1}, 'statement_period': { 'statement_period_id': 1, 'statement_period_name': 'Statement Period 1', 'statement_period_status': 'closed', }, 'statement_period_payment_entity': { 'statement_period_payment_entity_id': 1, 'statement_period_id': 1, 'reference_payment_entity_id': 1, 'is_visible_to_customer': 1, }, 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': 1}, {'abacus_event_id': 2, 'statement_period_id': 1}, {'abacus_event_id': 3, 'statement_period_id': 1}, ], 'ledger_account_contract': [ { 'account_id': 1, 'contract_id': 1, 'abacus_event_id': 1, 'currency_code': 'NOK', 'currency_amount': 3000, 'previous_balance': 0, 'current_balance': 2000, }, { 'account_id': 1, 'contract_id': 1, 'abacus_event_id': 2, 'currency_code': 'NOK', 'currency_amount': 2000, 'previous_balance': 123, 'current_balance': 5000, }, { 'account_id': 1, 'contract_id': 1, 'abacus_event_id': 3, 'currency_code': 'NOK', 'currency_amount': 3000, 'previous_balance': 133, 'current_balance': 1000, } ], 'accounting_period': {'accounting_period_id': 1, 'statement_period_id': 1}, 'run_controller': {'run_controller_id': 1}, 'accounting_run': { 'accounting_run_id': 1, 'accounting_period_id': 1, 'run_controller_id': 1, 'run_status': 'Committed' }, 'ledger_accounting_run_balance': { 'contract_id': 1, 'currency_code': 'GBP', 'accounting_run_id': 1, 'abacus_event_id': 1, 'total_gross_revenue_amount': 126, 'total_net_revenue_amount': 156, 'distribution_fee': 18.90, 'mechanical_deduction_total': 36.00, 'mechanical_deduction_admin_fee_total': 21.00, 'adjusted_net_revenue': 150, }, 'ledger_accounting_run_vat': { 'contract_id': 1, 'accounting_run_id': 1, 'abacus_event_id': 1, 'currency_code': 'GBP', 'gross_vat_rate': 12, 'gross_revenue': 156, 'net_revenue': 126, 'distribution_fee': 18.90, 'distribution_vat_rate': 7, 'gross_vat': 18.9, 'distribution_vat': 3, 'adjusted_net_revenue': 6, }, 'ledger_vat_summary': { 'statement_period_id': 1, 'activity_statement_period_id': 1, 'abacus_event_id': 3, 'account_id': 2, 'contract_id': 1, 'vat_category': 'gross_revenue', 'payee_currency_code': 'NOK', 'vat_currency_code': 'NOK', 'base_amount_payee_currency': 100, 'vat_rate': 20, 'vat_amount_payee_currency': 20, 'vat_amount_vat_currency': 20, 'net_amount_payee_currency': 80, 'abacus_exempt_reason': None, 'is_reporting_only': False, }, } MOCK_ACTIVITY_DATA = { 'revenue_distro_dbt': { 'account_id': 1, 'subaccount_id': 1 }, 'revenue_nr_dbt': { 'account_id': 1 }, 'combined_payments_dbt': { 'unique_key': 'AB_5', 'account_id': 1, 'contract_id': 1, 'currency_code': 'USD', 'currency_amount': -2000.00, 'created_at': '2022-01-02 12:11:15', 'statement_period_id': 20, 'event_name': 'send_payments', 'action_status': 'complete', 'withholding_tax_ledger_account_id': 123, 'withholding_tax_currency_code': 'USD', 'withholding_tax_currency_amount': 100.00, 'withholding_tax_created_at': '2022-01-02 12:11:15', }, 'ledger_summary_dbt': { 'account_id': 1, 'statement_period_id': 20, 'contract_id': 1, 'currency_code': 'USD', 'total_gross_revenue_amount': 123.45, 'total_net_revenue_amount': 103.45, 'distribution_fee': -10.0, 'mechanical_deduction_total': -10.0, 'mechanical_deduction_admin_fee_total': 0.0, } } @contextmanager def _using_mock_vendor_data(mock_data: dict): """Create the necessary mock vendor table and fills it with data. Args: mock_data (dict): Data to enter. """ Vendor.__table__.drop(db.engine, checkfirst=True) db.session.commit() Vendor.__table__.create(db.engine) insert_mock_data(mock_data, database='snowflake') yield db.session.commit() Vendor.__table__.drop(db.engine) db.session.close() @contextmanager def _using_mock_account_activity_data(mock_data: dict): """Create the necessary mock vendor table and fills it with data. Args: mock_data (dict): Data to enter. """ RevenueDistro.__table__.drop(db.engine, checkfirst=True) RevenueNr.__table__.drop(db.engine, checkfirst=True) CombinedPayments.__table__.drop(db.engine, checkfirst=True) LedgerSummary.__table__.drop(db.engine, checkfirst=True) db.session.commit() RevenueDistro.__table__.create(db.engine) RevenueNr.__table__.create(db.engine) CombinedPayments.__table__.create(db.engine) LedgerSummary.__table__.create(db.engine) insert_mock_data(mock_data, database='snowflake') yield db.session.commit() RevenueDistro.__table__.drop(db.engine) RevenueNr.__table__.drop(db.engine) CombinedPayments.__table__.drop(db.engine) LedgerSummary.__table__.drop(db.engine) db.session.close() def _insert_mock_account_statement_periods_data(): insert_mock_data(MOCK_DATA) @contextmanager def _using_mock_ledger_summary_data(mock_data: dict): """Create the necessary mock vendor tables and fills them with data. Args: mock_data (list): Data to enter. """ LedgerSummary.__table__.drop(db.engine, checkfirst=True) WorkstationSummary.__table__.drop(db.engine, checkfirst=True) AccountStatementPeriods.__table__.drop(db.engine, checkfirst=True) db.session.commit() LedgerSummary.__table__.create(db.engine) WorkstationSummary.__table__.create(db.engine) AccountStatementPeriods.__table__.create(db.engine) insert_mock_data(mock_data, database='snowflake') yield db.session.commit() LedgerSummary.__table__.drop(db.engine) WorkstationSummary.__table__.drop(db.engine) AccountStatementPeriods.__table__.drop(db.engine) db.session.close() @contextmanager def _using_mock_revenue_data(mock_data: dict): """Create the necessary mock revenue tables and fills them with data. Args: mock_data (list): Data to enter. """ RevenueByActivityMonth.__table__.drop(db.engine, checkfirst=True) RevenueByArtist.__table__.drop(db.engine, checkfirst=True) RevenueByProductDistro.__table__.drop(db.engine, checkfirst=True) RevenueByStore.__table__.drop(db.engine, checkfirst=True) RevenueByTransactionType.__table__.drop(db.engine, checkfirst=True) RevenueByImprint.__table__.drop(db.engine, checkfirst=True) RevenueByProject.__table__.drop(db.engine, checkfirst=True) db.session.commit() RevenueByActivityMonth.__table__.create(db.engine) RevenueByArtist.__table__.create(db.engine) RevenueByProductDistro.__table__.create(db.engine) RevenueByStore.__table__.create(db.engine) RevenueByTransactionType.__table__.create(db.engine) RevenueByImprint.__table__.create(db.engine) RevenueByProject.__table__.create(db.engine) insert_mock_data(mock_data, database='snowflake') yield db.session.commit() RevenueByActivityMonth.__table__.drop(db.engine) RevenueByArtist.__table__.drop(db.engine) RevenueByProductDistro.__table__.drop(db.engine) RevenueByStore.__table__.drop(db.engine) RevenueByTransactionType.__table__.drop(db.engine) RevenueByImprint.__table__.drop(db.engine) RevenueByProject.__table__.drop(db.engine) db.session.close() def test_get_activity_periods_by_account(fixture_client): """Test getting activity periods by account.""" account_id = 24601 mock_data = { 'revenue_by_activity_month_dbt': [ { 'account_id': account_id, 'account_payee_currency': 'NOK', 'activity_period_id': 123, 'activity_month_name': 'March 2009', 'artist_id': 9001, 'contract_id': 10001, 'subaccount_id': 12345, 'country_code': 'US', 'transaction_type_id': 'ST', 'statement_period_id': 124, 'net_share_payee_currency': 123.45, 'gross_revenue_payee_currency': 234.56, }, { 'account_id': account_id, 'account_payee_currency': 'NOK', 'activity_period_id': 234, 'activity_month_name': 'June 2018', 'artist_id': 9001, 'contract_id': 10001, 'subaccount_id': 99999, 'country_code': 'US', 'transaction_type_id': 'ST', 'statement_period_id': 124, 'net_share_payee_currency': 123.45, 'gross_revenue_payee_currency': 234.56, }, ], } with _using_mock_revenue_data(mock_data): response = fixture_client.get(f'/account/{account_id}/activity-periods') assert response.status_code == 200 assert response.json() == [ {'activity_period_id': 123, 'activity_month_name': 'March 2009'}, {'activity_period_id': 234, 'activity_month_name': 'June 2018'}, ] def test_get_activity_periods_by_account_and_subaccount(fixture_client): """Test getting activity periods by account and subaccount.""" account_id = 24601 subaccount_id = 12345 mock_data = { 'revenue_by_activity_month_dbt': [ { 'account_id': account_id, 'account_payee_currency': 'NOK', 'activity_period_id': 123, 'activity_month_name': 'March 2009', 'artist_id': 9001, 'contract_id': 10001, 'subaccount_id': subaccount_id, 'country_code': 'US', 'transaction_type_id': 'ST', 'statement_period_id': 124, 'net_share_payee_currency': 123.45, 'gross_revenue_payee_currency': 234.56, }, { 'account_id': account_id, 'account_payee_currency': 'NOK', 'activity_period_id': 234, 'activity_month_name': 'June 2018', 'artist_id': 9001, 'contract_id': 10001, 'subaccount_id': 99999, 'country_code': 'US', 'transaction_type_id': 'ST', 'statement_period_id': 124, 'net_share_payee_currency': 123.45, 'gross_revenue_payee_currency': 234.56, }, ], } with _using_mock_revenue_data(mock_data): response = fixture_client.get( f'/account/{account_id}/activity-periods?subaccount_id={subaccount_id}') assert response.status_code == 200 assert response.json() == [ {'activity_period_id': 123, 'activity_month_name': 'March 2009'}, ] def test_get_account_statements_by_account_id(fixture_client): """Test to get ledger info per statement period for a specified account.""" account_id = 24601 mock_data = { 'ledger_summary_dbt': [ { 'account_id': account_id, 'statement_period_id': 123, 'contract_id': 10001, 'currency_code': 'GBP', 'total_gross_revenue_amount': 100.0, 'total_net_revenue_amount': 80.0, 'distribution_fee': -20.0, 'mechanical_deduction_total': 0.0, 'mechanical_deduction_admin_fee_total': 0.0, }, { 'account_id': account_id, 'statement_period_id': 123, 'contract_id': 10002, 'currency_code': 'GBP', 'total_gross_revenue_amount': 100.0, 'total_net_revenue_amount': 80.0, 'distribution_fee': -20.0, 'mechanical_deduction_total': -10.0, 'mechanical_deduction_admin_fee_total': -10.0, }, { 'account_id': account_id, 'statement_period_id': 124, 'contract_id': 10001, 'currency_code': 'GBP', 'total_gross_revenue_amount': 100.0, 'total_net_revenue_amount': 80.0, 'distribution_fee': -20.0, 'mechanical_deduction_total': 0.0, 'mechanical_deduction_admin_fee_total': 0.0, }, ], 'workstation_summary_dbt': [ { 'account_id': account_id, 'account_name': 'Some Account', 'contract_id': 10001, 'statement_period_id': 122, 'currency': 'GBP', 'opening_balance': 100.0, 'payments': 100.0, 'net_revenue': 80.0, 'gross_revenue': 100.0, 'fee': -20.0, 'expenses': 0.0, 'adjustments': 0.0, 'closing_balance': 180.0, 'mechanicals': 0.0, 'mechanical_fees': 0.0, }, ], 'account_statement_periods_dbt': [ { 'statement_period_id': 122, 'statement_period_name': 'Statement Period 122', 'statement_period_status': 'closed', 'account_id': account_id, 'contract_id': 10001 }, { 'statement_period_id': 123, 'statement_period_name': 'Statement Period 123', 'statement_period_status': 'closed', 'account_id': account_id, 'contract_id': 10001 }, { 'statement_period_id': 124, 'statement_period_name': 'Statement Period 124', 'statement_period_status': 'closed', 'account_id': account_id, 'contract_id': 10001 }, ], } with _using_mock_ledger_summary_data(mock_data): res = fixture_client.get(f'/account/{account_id}/statements') assert res.status_code == 200 assert res.json() == { 'items': [ { 'account_id': 24601, 'contract_id': None, 'statement_period_id': 122, 'currency_code': 'GBP', 'total_gross_revenue_amount': 100.0, 'total_net_revenue_amount': 80.0, 'distribution_fee': -20.0, 'mechanical_deduction_total': 0.0, 'mechanical_deduction_admin_fee_total': 0.0 }, { 'account_id': 24601, 'contract_id': None, 'statement_period_id': 123, 'currency_code': 'GBP', 'total_gross_revenue_amount': 200.0, 'total_net_revenue_amount': 160.0, 'distribution_fee': -40.0, 'mechanical_deduction_total': -10.0, 'mechanical_deduction_admin_fee_total': -10.0 }, { 'account_id': 24601, 'contract_id': None, 'statement_period_id': 124, 'currency_code': 'GBP', 'total_gross_revenue_amount': 100.0, 'total_net_revenue_amount': 80.0, 'distribution_fee': -20.0, 'mechanical_deduction_total': 0.0, 'mechanical_deduction_admin_fee_total': 0.0 } ], 'pagination': { 'pagination_type': 'standard', 'total_records': 3 } } MOCK_PAYMENTS_DATA = { 'combined_payments_dbt': [ { 'unique_key': 'AB_1', 'account_id': 10, 'contract_id': 1, 'currency_code': 'USD', 'currency_amount': -5000.00, 'created_at': '2022-01-01 11:55:55', 'statement_period_id': 20, 'event_name': 'send_payments', 'action_status': 'complete', 'withholding_tax_ledger_account_id': 3, 'withholding_tax_currency_code': 'USD', 'withholding_tax_currency_amount': -1000.00, 'withholding_tax_created_at': '2022-01-01' }, { 'unique_key': 'AB_2', 'account_id': 10, 'contract_id': 2, 'currency_code': 'USD', 'currency_amount': -2000.00, 'created_at': '2022-01-02 12:11:15', 'statement_period_id': 20, 'event_name': 'send_payments', 'action_status': 'complete', 'withholding_tax_ledger_account_id': None, 'withholding_tax_currency_code': None, 'withholding_tax_currency_amount': None, 'withholding_tax_created_at': None }, { 'unique_key': 'AB_3', 'account_id': 10, 'contract_id': 2, 'currency_code': 'USD', 'currency_amount': -1000.00, 'created_at': '2022-01-01 11:55:55', 'statement_period_id': 20, 'event_name': 'send_payments', 'action_status': 'complete', 'withholding_tax_ledger_account_id': None, 'withholding_tax_currency_code': None, 'withholding_tax_currency_amount': None, 'withholding_tax_created_at': None }, { 'unique_key': 'AB_4', 'account_id': 10, 'contract_id': 1, 'currency_code': 'USD', 'currency_amount': -2000.00, 'created_at': '2022-01-02 12:11:15', 'statement_period_id': 20, 'event_name': 'send_payments', 'action_status': 'complete', 'withholding_tax_ledger_account_id': None, 'withholding_tax_currency_code': None, 'withholding_tax_currency_amount': None, 'withholding_tax_created_at': None }, ], } def test_get_payments_for_account_and_statement_period(fixture_client): """Test to get payments for specified account and statement period.""" account_id = 10 statement_period_id = 20 with _using_mock_account_statements_data(MOCK_PAYMENTS_DATA): res = fixture_client.get( f'/account/{account_id}/statement-period/{statement_period_id}/payments' ) assert res.status_code == 200 assert res.json() == [ { 'ledger_account_id': 'AB_1', 'account_id': 10, 'contract_id': 1, 'currency_code': 'USD', 'currency_amount': -5000.00, 'created_at': '2022-01-01', 'statement_period_id': 20, 'event_name': 'send_payments', 'action_status': 'complete', 'withholding_tax_ledger_account_id': 3, 'withholding_tax_currency_code': 'USD', 'withholding_tax_currency_amount': -1000.00, 'withholding_tax_created_at': '2022-01-01' }, { 'ledger_account_id': 'AB_4', 'account_id': 10, 'contract_id': 1, 'currency_code': 'USD', 'currency_amount': -2000.00, 'created_at': '2022-01-02', 'statement_period_id': 20, 'event_name': 'send_payments', 'action_status': 'complete', 'withholding_tax_ledger_account_id': None, 'withholding_tax_currency_code': None, 'withholding_tax_currency_amount': None, 'withholding_tax_created_at': None }, { 'ledger_account_id': 'AB_2', 'account_id': 10, 'contract_id': 2, 'currency_code': 'USD', 'currency_amount': -2000.00, 'created_at': '2022-01-02', 'statement_period_id': 20, 'event_name': 'send_payments', 'action_status': 'complete', 'withholding_tax_ledger_account_id': None, 'withholding_tax_currency_code': None, 'withholding_tax_currency_amount': None, 'withholding_tax_created_at': None }, { 'ledger_account_id': 'AB_3', 'account_id': 10, 'contract_id': 2, 'currency_code': 'USD', 'currency_amount': -1000.00, 'created_at': '2022-01-01', 'statement_period_id': 20, 'event_name': 'send_payments', 'action_status': 'complete', 'withholding_tax_ledger_account_id': None, 'withholding_tax_currency_code': None, 'withholding_tax_currency_amount': None, 'withholding_tax_created_at': None }, ] def test_get_payments_for_account_contract_and_statement_period(fixture_client): """Test to get payments for specified account, contract, and statement period.""" account_id = 10 contract_id = 1 statement_period_id = 20 with _using_mock_account_statements_data(MOCK_PAYMENTS_DATA): res = fixture_client.get( f'/account/{account_id}/statement-period/{statement_period_id}/payments?contract_id={contract_id}' # noqa: E501 ) assert res.status_code == 200 assert res.json() == [ { 'ledger_account_id': 'AB_1', 'account_id': 10, 'contract_id': 1, 'currency_code': 'USD', 'currency_amount': -5000.00, 'created_at': '2022-01-01', 'statement_period_id': 20, 'event_name': 'send_payments', 'action_status': 'complete', 'withholding_tax_ledger_account_id': 3, 'withholding_tax_currency_code': 'USD', 'withholding_tax_currency_amount': -1000.00, 'withholding_tax_created_at': '2022-01-01' }, { 'ledger_account_id': 'AB_4', 'account_id': 10, 'contract_id': 1, 'currency_code': 'USD', 'currency_amount': -2000.00, 'created_at': '2022-01-02', 'statement_period_id': 20, 'event_name': 'send_payments', 'action_status': 'complete', 'withholding_tax_ledger_account_id': None, 'withholding_tax_currency_code': None, 'withholding_tax_currency_amount': None, 'withholding_tax_created_at': None }, ] def test_get_vat_for_account_statement_period_accounting_run(fixture_client): """Test getting VAT for an account and statement period with accounting run VAT data.""" _insert_mock_account_statement_periods_data() account_id = 1 statement_period_id = 1 result = fixture_client.get(f'/account/{account_id}/statement-period/{statement_period_id}/vat') assert result.status_code == 200 assert result.json() == { 'statement_period_id': 1, 'account_id': 1, 'payee_currency_code': 'GBP', 'base_amount_payee_currency': 156.00, 'net_amount_payee_currency': 6.00, 'vat_amount_payee_currency': 18.90, 'currency_code': 'GBP', 'gross_revenue': 156.00, 'net_revenue': 126.00, 'distribution_fee': 18.90, 'gross_vat_rate': 12.00, 'distribution_vat_rate': 7.00, 'gross_vat': 18.90, 'distribution_vat': 3.00, 'adjusted_net_revenue': 6.00 } def test_get_vat_for_account_statement_period_vat_summary(fixture_client): """Test getting VAT for an account and statement period with VAT summary data.""" _insert_mock_account_statement_periods_data() account_id = 2 statement_period_id = 1 result = fixture_client.get(f'/account/{account_id}/statement-period/{statement_period_id}/vat') assert result.status_code == 200 assert result.json() == { 'statement_period_id': statement_period_id, 'account_id': account_id, 'payee_currency_code': 'NOK', 'base_amount_payee_currency': 100, 'net_amount_payee_currency': 80, 'vat_amount_payee_currency': 20, 'currency_code': 'NOK', 'gross_revenue': None, 'net_revenue': None, 'distribution_fee': None, 'gross_vat_rate': None, 'distribution_vat_rate': None, 'gross_vat': None, 'distribution_vat': None, 'adjusted_net_revenue': None, } def test_get_vat_for_account_statement_period_no_data(fixture_client): """Test getting VAT for an account that has no data.""" _insert_mock_account_statement_periods_data() account_id = 999 statement_period_id = 1 result = fixture_client.get(f'/account/{account_id}/statement-period/{statement_period_id}/vat') assert result.status_code == 200 assert result.json() is None def test_get_balance_for_account_statement_period_no_data(fixture_client): """Test getting balance for an account that has no data.""" _insert_mock_account_statement_periods_data() account_id = 999 statement_period_id = 1 with _using_mock_ledger_summary_data({}): result = fixture_client.get( f'/account/{account_id}/statement-period/{statement_period_id}/balance') assert result.status_code == 200 assert result.json() is None def test_get_statement_periods_by_account_id(fixture_client): """Test getting statement periods by account ID.""" account_id = 1 mock_data = { 'account_statement_periods_dbt': [ { 'statement_period_id': 122, 'statement_period_name': 'Statement Period 122', 'statement_period_status': 'closed', 'account_id': account_id, 'contract_id': 10001, 'currency_code': 'USD' }, { 'statement_period_id': 123, 'statement_period_name': 'Statement Period 123', 'statement_period_status': 'closed', 'account_id': account_id, 'contract_id': 10001, 'currency_code': 'USD' }, { 'statement_period_id': 124, 'statement_period_name': 'Statement Period 124', 'statement_period_status': 'closed', 'account_id': account_id, 'contract_id': 10001, 'currency_code': 'USD' }, ], } with _using_mock_ledger_summary_data(mock_data): result = fixture_client.get(f'/account/{account_id}/account-statement-periods') assert result.status_code == 200 assert result.json() == [ { 'statement_period_id': 122, 'statement_period_name': 'Statement Period 122', 'statement_period_status': 'closed', 'currency_code': 'USD', 'statement_year': None, 'statement_month': None, }, { 'statement_period_id': 123, 'statement_period_name': 'Statement Period 123', 'statement_period_status': 'closed', 'currency_code': 'USD', 'statement_year': None, 'statement_month': None, }, { 'statement_period_id': 124, 'statement_period_name': 'Statement Period 124', 'statement_period_status': 'closed', 'currency_code': 'USD', 'statement_year': None, 'statement_month': None, }, ] def test_get_account_activity(fixture_client): """Test getting account activity.""" account_id = 1 insert_mock_data(MOCK_DATA) with _using_mock_account_activity_data(MOCK_ACTIVITY_DATA): result = fixture_client.get(f'/account/{account_id}/activity') assert result.status_code == 200 assert result.json() == { 'adjustments': False, 'advances': False, 'expenses': False, 'mechanicals': True, 'physical_reserves': False, 'publishing_revenue': False, 'revenue': True, 'taxes': True } def test_get_account_activity_with_subaccount(fixture_client): """Test getting account activity.""" account_id = 1 subaccount_id = 1 is_subaccount = True insert_mock_data(MOCK_DATA) with _using_mock_account_activity_data(MOCK_ACTIVITY_DATA): result = fixture_client.get( f'/account/{account_id}/activity?subaccount_id={subaccount_id}&is_subaccount={is_subaccount}') # noqa: E501 assert result.status_code == 200 assert result.json() == { 'adjustments': False, 'advances': False, 'expenses': False, 'mechanicals': True, 'physical_reserves': False, 'publishing_revenue': False, 'revenue': True, 'taxes': True } MOCK_CONTRACTS_DATA = { 'contract_dbt': [ { 'account_id': 57608, 'contract_id': 531211, 'contract_name': 'Contract 1' }, { 'account_id': 57608, 'contract_id': 531219, 'contract_name': 'Contract 2' }, { 'account_id': 57608, 'contract_id': 531458, 'contract_name': 'Contract 3' }, ] } def test_get_transaction_types_by_account(fixture_client): """Test getting transaction type by account.""" account_id = 24601 artist_id = 1 mock_data = { 'revenue_by_transaction_type_dbt': { 'account_id': account_id, 'artist_id': artist_id, 'product_id': 1, 'track_unique_id': 1, 'account_payee_currency': 'USD', 'contract_id': 10001, 'statement_period_id': 123, 'subaccount_id': 24604, 'transaction_type_id': 1, 'transaction_type_desc': 'Money', 'transaction_type_group_name': 'Money Group', 'quantity': 10, 'net_revenue_payee_currency': 10.0, 'gross_revenue_payee_currency': 20.0, }, } with _using_mock_revenue_data(mock_data): result = fixture_client.get( f'/account/{account_id}/transaction-types') assert result.status_code == 200 assert result.json() == [ { 'transaction_type_id': 1, 'transaction_type_desc': 'Money', 'transaction_type_group_name': 'Money Group' } ] def test_load_account_contracts(fixture_client): """Test loading contracts by account.""" account_id = 57608 contract_ids = [531211, 531219, 531458] payload = { 'contract_ids': contract_ids } with _using_mock_account_statements_data(MOCK_CONTRACTS_DATA): result = fixture_client.post( f'account/{account_id}/dataloader/contracts', json=payload ) assert result.status_code == 200 assert result.json() == [ { 'account_id': 57608, 'contract_id': 531211, 'contract_name': 'Contract 1' }, { 'account_id': 57608, 'contract_id': 531219, 'contract_name': 'Contract 2' }, { 'account_id': 57608, 'contract_id': 531458, 'contract_name': 'Contract 3' }, ] def test_get_stores_by_account_id(fixture_client): """Test stores by account id.""" account_id = 24601 mock_data = { 'revenue_by_store_dbt': { 'store_id': 1, 'store_name': 'Spotify', 'account_id': account_id, 'artist_id': 2, 'subaccount_id': 2, 'product_id': 2, 'track_unique_id': 1234554, 'contract_id': 10001, 'statement_period_id': 123, 'account_payee_currency': 'USD', 'net_revenue_payee_currency': 10.0, 'gross_revenue_payee_currency': 20.0 }, } with _using_mock_revenue_data(mock_data): result = fixture_client.get(f'/account/{account_id}/stores') assert result.status_code == 200 assert result.json() == [ { 'store_id': 1, 'store_name': 'Spotify', } ] def test_get_imprints_by_account(fixture_client): """Test getting imprints by account.""" account_id = 24601 mock_data = { 'revenue_by_imprint_dbt': [ { 'account_id': account_id, 'imprintid': 123456, 'imprint': 'Universal Music Group', 'subaccount_id': 24604, 'artist_id': 1, 'statement_period_id': 123, 'account_payee_currency': 'USD', 'mechanical_deduction_amount_payee_currency': 10.0, 'net_share_payee_currency': 10.0, 'gross_revenue_payee_currency': 20.0, }, { 'account_id': account_id, 'imprintid': 123457, 'imprint': 'Sony Music Entertainment', 'subaccount_id': 24605, 'artist_id': 2, 'statement_period_id': 123, 'account_payee_currency': 'USD', 'mechanical_deduction_amount_payee_currency': 15.0, 'net_share_payee_currency': 15.0, 'gross_revenue_payee_currency': 30.0, } ] } with _using_mock_revenue_data(mock_data): result = fixture_client.get( f'/account/{account_id}/imprints') assert result.status_code == 200 response_data = result.json() assert response_data == [ { 'imprintid': 123457, 'imprint': 'Sony Music Entertainment', 'account_id': 24601, }, { 'imprintid': 123456, 'imprint': 'Universal Music Group', 'account_id': 24601, }, ] def test_get_artists_by_account(fixture_client): """Test getting artists by account.""" account_id = 24601 mock_data = { 'revenue_by_artist_dbt': [ { 'account_id': account_id, 'artist_id': 123456, 'artist_name': 'Taylor Swift', 'subaccount_id': 24604, 'statement_period_id': 123, 'contract_id': 10001, 'account_payee_currency': 'USD', 'mechanical_deduction_amount_payee_currency': 10.0, 'publisher_admin_fee_payee_currency': 1.0, 'net_revenue_payee_currency': 10.0, 'gross_revenue_payee_currency': 20.0, 'activity_period_id': 202301, 'store_id': 1, 'country_code': 'US', 'imprint_id': 101, 'transaction_type_id': 201, }, { 'account_id': account_id, 'artist_id': 123457, 'artist_name': 'The Beatles', 'subaccount_id': 24605, 'statement_period_id': 123, 'contract_id': 10002, 'account_payee_currency': 'USD', 'mechanical_deduction_amount_payee_currency': 15.0, 'publisher_admin_fee_payee_currency': 1.5, 'net_revenue_payee_currency': 15.0, 'gross_revenue_payee_currency': 30.0, 'activity_period_id': 202302, 'store_id': 2, 'country_code': 'GB', 'imprint_id': 102, 'transaction_type_id': 202, } ] } with _using_mock_revenue_data(mock_data): result = fixture_client.get( f'/account/{account_id}/artists') assert result.status_code == 200 response_data = result.json() assert response_data == [ { 'artist_id': 123456, 'artist_name': 'Taylor Swift' }, { 'artist_id': 123457, 'artist_name': 'The Beatles' } ] def test_get_projects_by_account(fixture_client): """Test getting projects by account.""" account_id = 24601 mock_data = { 'revenue_by_project_dbt': [ { 'project_id': 501, 'project_name': 'Test Project', 'project_code': 'PROJ001', 'project_artist': 'Test Artist', 'account_id': account_id, 'contract_id': 10001, 'subaccount_id': 24604, 'statement_period_id': 123, 'activity_period_id': 202301, 'store_id': 1, 'country_code': 'US', 'imprint_id': 101, 'transaction_type_id': 201, 'account_payee_currency': 'USD', 'product_count': 14, 'net_share_payee_currency': 10.0, 'gross_revenue_payee_currency': 20.0, }, { 'project_id': 502, 'project_name': 'Another Project', 'project_code': 'PROJ002', 'project_artist': 'Best Artist', 'account_id': account_id, 'contract_id': 10002, 'subaccount_id': 24605, 'statement_period_id': 124, 'activity_period_id': 202302, 'store_id': 2, 'country_code': 'NO', 'imprint_id': 102, 'transaction_type_id': 202, 'account_payee_currency': 'NOK', 'product_count': 15, 'net_share_payee_currency': 20.0, 'gross_revenue_payee_currency': 40.0, }, ] } with _using_mock_revenue_data(mock_data): result = fixture_client.get(f'/account/{account_id}/projects') response_data = result.json() assert result.status_code == 200 assert response_data == [ { 'project_id': 502, }, { 'project_id': 501, } ]