"""Revenue By Statement Period model tests.""" from decimal import Decimal from unittest.mock import patch from moneyhub.constants.constants import StatementPeriodStatus from moneyhub.constants.features import FEATURE_PUBLISHING_PHASE_ONE from moneyhub.models import RevenueByStatementPeriod from tests.unit.conftest import using_mock_snowflake_table from tests.utils.factories import DimSubaccountFactory ACCOUNT_ID = 24601 _MOCK_DATA = { 'revenue_by_statement_period_dbt': [ { 'account_id': 24601, 'artist_id': 1, 'contract_id': 10001, 'statement_period_id': 10, 'statement_period_name': 'January 2024', 'statement_period_status': StatementPeriodStatus.CLOSED, 'subaccount_id': 1, 'product_id': 1, 'track_unique_id': 1234556, 'account_payee_currency': 'GBP', 'net_revenue_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'net_publishing_revenue_payee_currency': Decimal('1.00'), 'gross_publishing_revenue_payee_currency': Decimal('2.00'), 'project_id': 1, }, { 'account_id': 24601, 'artist_id': 2, 'contract_id': 10001, 'statement_period_id': 11, 'statement_period_name': 'February 2024', 'statement_period_status': StatementPeriodStatus.CLOSED, 'subaccount_id': 2, 'product_id': 2, 'track_unique_id': 1234554, 'account_payee_currency': 'GBP', 'net_revenue_payee_currency': Decimal('15.00'), 'gross_revenue_payee_currency': Decimal('13.00'), 'net_publishing_revenue_payee_currency': Decimal('1.50'), 'gross_publishing_revenue_payee_currency': Decimal('2.50'), 'project_id': 2, }, { 'account_id': 24601, 'account_payee_currency': 'GBP', 'artist_id': None, 'contract_id': 10001, 'statement_period_id': 11, 'statement_period_name': 'May 2024', 'subaccount_id': None, 'product_id': None, 'track_unique_id': None, 'statement_period_status': StatementPeriodStatus.CLOSED, 'net_revenue_payee_currency': None, 'gross_revenue_payee_currency': None, 'net_publishing_revenue_payee_currency': Decimal('5.00'), 'gross_publishing_revenue_payee_currency': Decimal('10.00'), 'project_id': None, }, { 'account_id': 24601, 'account_payee_currency': 'GBP', 'artist_id': 2, 'contract_id': 10001, 'statement_period_id': 12, 'statement_period_name': 'March 2024', 'subaccount_id': 2, 'product_id': 2, 'track_unique_id': 1234554, 'statement_period_status': StatementPeriodStatus.CLOSED, 'net_revenue_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'net_publishing_revenue_payee_currency': Decimal('1.00'), 'gross_publishing_revenue_payee_currency': Decimal('2.00'), 'project_id': 2, }, { 'account_id': 24601, 'account_payee_currency': 'GBP', 'artist_id': 1, 'contract_id': 10002, 'statement_period_id': 13, 'statement_period_name': 'April 2024', 'subaccount_id': 1, 'product_id': 1, 'track_unique_id': 1234556, 'statement_period_status': StatementPeriodStatus.CLOSED, 'net_revenue_payee_currency': Decimal('12.00'), 'gross_revenue_payee_currency': Decimal('9.00'), 'net_publishing_revenue_payee_currency': Decimal('1.20'), 'gross_publishing_revenue_payee_currency': Decimal('1.80'), 'project_id': 2, }, { 'account_id': 24601, 'account_payee_currency': 'GBP', 'artist_id': 3, 'contract_id': 10001, 'statement_period_id': 20, 'statement_period_name': 'May 2024', 'subaccount_id': 3, 'product_id': 3, 'track_unique_id': 1234478, 'statement_period_status': StatementPeriodStatus.CLOSED, 'net_revenue_payee_currency': Decimal('15.00'), 'gross_revenue_payee_currency': Decimal('13.00'), 'net_publishing_revenue_payee_currency': None, 'gross_publishing_revenue_payee_currency': None, 'project_id': 2, }, { 'account_id': 24601, 'account_payee_currency': 'GBP', 'artist_id': None, 'contract_id': None, 'statement_period_id': 30, 'statement_period_name': 'June 2024', 'subaccount_id': None, 'product_id': None, 'track_unique_id': None, 'statement_period_status': StatementPeriodStatus.CLOSED, 'net_revenue_payee_currency': None, 'gross_revenue_payee_currency': None, 'net_publishing_revenue_payee_currency': Decimal('7.50'), 'gross_publishing_revenue_payee_currency': Decimal('0.00'), 'project_id': None, }, ], } @using_mock_snowflake_table(RevenueByStatementPeriod, _MOCK_DATA) def test_get_by_account_id(): """Test getting revenue by account.""" account_id = 24601 items = RevenueByStatementPeriod.get_by_account_id( account_id, None, None, None, None, None, 10, 10, None) actual = items[0]._asdict() assert actual == { 'account_payee_currency': 'GBP', 'statement_period_id': 10, 'net_revenue_payee_currency': Decimal('10.000000000000'), 'gross_revenue_payee_currency': Decimal('8.000000000000'), 'net_publishing_revenue_payee_currency': Decimal('1.000000000000'), 'gross_publishing_revenue_payee_currency': Decimal('2.000000000000'), } @using_mock_snowflake_table(RevenueByStatementPeriod, _MOCK_DATA) def test_get_by_account_id_publishing_only_period(): """Test getting revenue for a period that only has publishing revenue.""" account_id = 24601 items = RevenueByStatementPeriod.get_by_account_id( account_id, None, None, None, None, None, 30, 30, None) actual = items[0]._asdict() assert actual == { 'account_payee_currency': 'GBP', 'statement_period_id': 30, 'net_revenue_payee_currency': None, 'gross_revenue_payee_currency': None, 'net_publishing_revenue_payee_currency': Decimal('7.500000000000'), 'gross_publishing_revenue_payee_currency': Decimal('0.000000000000'), } @using_mock_snowflake_table(RevenueByStatementPeriod, _MOCK_DATA) def test_get_by_account_id_by_subaccount(): """Test getting revenue by account for a subaccount.""" account_id = 24601 subaccount_id = 1 subaccount_info = DimSubaccountFactory.build( subaccount_id=subaccount_id) items = RevenueByStatementPeriod.get_by_account_id( account_id, None, subaccount_id, None, None, None, 10, 10, subaccount_info) actual = items[0]._asdict() assert actual == { 'account_payee_currency': 'GBP', 'statement_period_id': 10, 'net_revenue_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'net_publishing_revenue_payee_currency': Decimal('1.00'), 'gross_publishing_revenue_payee_currency': Decimal('2.00'), 'subaccount_revenue': Decimal('9.0') } @using_mock_snowflake_table(RevenueByStatementPeriod, _MOCK_DATA) def test_get_by_account_id_with_filters(): """Test getting by account with filters.""" account_id = 24601 artist_id = 2 statement_period_start = 11 statement_period_end = 11 subaccount_id = 2 product_id = 2 track_unique_id = 1234554 items = RevenueByStatementPeriod.get_by_account_id( account_id, artist_id, subaccount_id, None, product_id, track_unique_id, statement_period_start, statement_period_end, None ) assert [item._asdict() for item in items] == [ { 'account_payee_currency': 'GBP', 'statement_period_id': 11, 'net_revenue_payee_currency': Decimal('15.000000000000'), 'gross_revenue_payee_currency': Decimal('13.000000000000'), 'net_publishing_revenue_payee_currency': Decimal('1.500000000000'), 'gross_publishing_revenue_payee_currency': Decimal('2.500000000000'), }, ] @using_mock_snowflake_table(RevenueByStatementPeriod, _MOCK_DATA) @patch('moneyhub.models.revenue_by_statement_period.is_feature_enabled') def test_get_revenue_by_statement_period_by_account_id(mock_is_feature_enabled): """Test getting revenue by statement period for an account.""" expected = { 'currency_code': 'GBP', 'gross_revenue_payee_currency': Decimal('38.000000000000'), 'net_revenue_payee_currency': Decimal('47.000000000000') } mock_is_feature_enabled.return_value = False result = RevenueByStatementPeriod.get_revenue_total_for_account_statement_periods( 24601, 10, 13, None, None, None) assert result._asdict() == expected @using_mock_snowflake_table(RevenueByStatementPeriod, _MOCK_DATA) @patch('moneyhub.models.revenue_by_statement_period.is_feature_enabled') def test_get_total_revenue_with_publishing(mock_is_feature_enabled): """Test getting total revenue including the publishing revenue.""" expected = { 'currency_code': 'GBP', 'gross_revenue_payee_currency': Decimal('56.3'), 'net_revenue_payee_currency': Decimal('56.7') } mock_is_feature_enabled.return_value = lambda ff: ff == FEATURE_PUBLISHING_PHASE_ONE result = RevenueByStatementPeriod.get_revenue_total_for_account_statement_periods( 24601, 10, 13, None, None, None) assert result._asdict() == expected @using_mock_snowflake_table(RevenueByStatementPeriod, _MOCK_DATA) @patch('moneyhub.models.revenue_by_statement_period.is_feature_enabled') def test_get_total_revenue_with_null_publishing(mock_is_feature_enabled): """Test getting total revenue including null publishing revenue.""" expected = { 'currency_code': 'GBP', 'gross_revenue_payee_currency': Decimal('13.0'), 'net_revenue_payee_currency': Decimal('15.0') } mock_is_feature_enabled.return_value = lambda ff: ff == FEATURE_PUBLISHING_PHASE_ONE result = RevenueByStatementPeriod.get_revenue_total_for_account_statement_periods( 24601, 20, 20, None, None, None) assert result._asdict() == expected @using_mock_snowflake_table(RevenueByStatementPeriod, _MOCK_DATA) def test_get_currencies_periods(): """Test getting currencies by statement period for an account.""" expected = [ { 'account_payee_currency': 'GBP', 'statement_period_id': Decimal(13), }, { 'account_payee_currency': 'GBP', 'statement_period_id': Decimal(12), }, { 'account_payee_currency': 'GBP', 'statement_period_id': Decimal(11), }, { 'account_payee_currency': 'GBP', 'statement_period_id': Decimal(10), } ] result = RevenueByStatementPeriod.get_currencies_periods( 24601, [10, 11, 12, 13], None, None) for i in range(len(result)): item = result[i]._asdict() assert item == expected[i] @using_mock_snowflake_table(RevenueByStatementPeriod, _MOCK_DATA) def test_get_currencies_periods_subaccount(): """Test getting currencies by statement period for a subaccount.""" expected = [ { 'account_payee_currency': 'GBP', 'statement_period_id': 20, } ] result = RevenueByStatementPeriod.get_currencies_periods( 24601, [10, 11, 12, 13, 20], 3, None) for i in range(len(result)): item = result[i]._asdict() assert item == expected[i] @using_mock_snowflake_table(RevenueByStatementPeriod, _MOCK_DATA) def test_get_currencies_periods_artist(): """Test getting currencies by statement period for an artist.""" expected = [ { 'account_payee_currency': 'GBP', 'statement_period_id': 13, }, { 'account_payee_currency': 'GBP', 'statement_period_id': 10, }, ] result = RevenueByStatementPeriod.get_currencies_periods( 24601, [10, 11, 12, 13, 20], None, 1) assert [item._asdict() for item in result] == expected @using_mock_snowflake_table(RevenueByStatementPeriod, _MOCK_DATA) @patch('moneyhub.models.revenue_by_statement_period.is_feature_enabled') def test_get_revenue_total_by_subaccount(mock_is_feature_enabled): """Test getting revenue total for a subaccount.""" subaccount_id = 1 expected = { 'currency_code': 'GBP', 'gross_revenue_payee_currency': Decimal('17.00000000000'), 'net_revenue_payee_currency': Decimal('22.000000000000'), 'subaccount_revenue': Decimal('19.800000000000') } subaccount_info = DimSubaccountFactory.build( subaccount_id=subaccount_id) mock_is_feature_enabled.return_value = False result = RevenueByStatementPeriod.get_revenue_total_for_account_statement_periods( 24601, 10, 13, None, subaccount_id, subaccount_info) assert result._asdict() == expected @using_mock_snowflake_table(RevenueByStatementPeriod, _MOCK_DATA) def test_get_by_account_id_with_project_id_filter(): """Test filtering revenue by statement period by project ID.""" items = RevenueByStatementPeriod.get_by_account_id( 24601, None, None, None, None, None, None, None, None, project_id=1) assert len(items) == 1 assert items[0].statement_period_id == 10