"""Revenue By Imprint model tests.""" from decimal import Decimal import pytest from moneyhub.constants.constants import ORDER_BY_IMPRINT from moneyhub.constants.constants import OrderDirection from moneyhub.models import RevenueByImprint from tests.unit.conftest import using_mock_snowflake_table from tests.utils.factories import DimSubaccountFactory _MOCK_DATA = { 'revenue_by_imprint_dbt': [ { 'imprintid': 123456, 'imprint': 'TEST IMPRINT', 'account_id': 57608, 'subaccount_id': 57609, 'artist_id': 1, 'statement_period_id': 10, 'account_payee_currency': 'JPY', 'mechanical_deduction_amount_payee_currency': Decimal('10.00'), 'publisher_admin_fee_payee_currency': Decimal('1.00'), 'net_share_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'activity_period_id': 202301, 'store_id': 1, 'country_code': 'US', 'transaction_type_id': 201 }, { 'imprintid': 123455, 'imprint': 'TEST IMPRINT 1', 'account_id': 57608, 'subaccount_id': 57609, 'artist_id': 1, 'statement_period_id': 10, 'account_payee_currency': 'JPY', 'mechanical_deduction_amount_payee_currency': Decimal('20.00'), 'publisher_admin_fee_payee_currency': Decimal('2.00'), 'net_share_payee_currency': Decimal('20.00'), 'gross_revenue_payee_currency': Decimal('18.00'), 'activity_period_id': 202302, 'store_id': 2, 'country_code': 'GB', 'transaction_type_id': 202 }, { 'imprintid': 123457, 'imprint': 'TEST IMPRINT 2', 'statement_period_id': 10, 'account_id': 57608, 'subaccount_id': 57609, 'artist_id': 1, 'account_payee_currency': 'JPY', 'mechanical_deduction_amount_payee_currency': Decimal('10.00'), 'publisher_admin_fee_payee_currency': Decimal('1.00'), 'net_share_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'activity_period_id': 202301, 'store_id': 1, 'country_code': 'US', 'transaction_type_id': 201 }, { 'imprintid': 123456, 'imprint': 'TEST IMPRINT', 'statement_period_id': 11, 'account_id': 57608, 'subaccount_id': 57609, 'artist_id': 2, 'account_payee_currency': 'GBP', 'mechanical_deduction_amount_payee_currency': Decimal('11.00'), 'publisher_admin_fee_payee_currency': Decimal('1.00'), 'net_share_payee_currency': Decimal('11.00'), 'gross_revenue_payee_currency': Decimal('9.00'), 'activity_period_id': 202303, 'store_id': 3, 'country_code': 'CA', 'transaction_type_id': 203 }, ], } @using_mock_snowflake_table(RevenueByImprint, _MOCK_DATA) def test_get_by_account_id(): """Test getting revenue by account.""" account_id = 57608 limit = 50 offset = 0 (items, total_record) = RevenueByImprint.get_by_account_id( account_id, None, limit, offset, None, 10, 10, None, None, None, ORDER_BY_IMPRINT, OrderDirection.ASC, None, None, None, None, None, None) actual = items[0]._asdict() assert actual == { 'account_id': Decimal('57608'), 'imprintid': Decimal('123456'), 'imprint': 'TEST IMPRINT', 'account_payee_currency': 'JPY', 'subaccount_id': Decimal('57609'), 'mechanical_deduction_amount_payee_currency': Decimal('10.000000000000'), 'publisher_admin_fee_payee_currency': Decimal('1.000000000000'), 'net_share_payee_currency': Decimal('10.000000000000'), 'gross_revenue_payee_currency': Decimal('8.000000000000'), 'total_records': 3, } @using_mock_snowflake_table(RevenueByImprint, _MOCK_DATA) def test_get_by_account_id_with_filters(): """Test getting by account with filters.""" account_id = 57608 subaccount_id = 57609 artist_id = 1 limit = 50 offset = 0 search_query = 'IMPRINT 1' statement_period_start = 10 statement_period_end = 11 order_by = 'imprint' order_dir = OrderDirection.ASC (items, total_record) = RevenueByImprint.get_by_account_id( account_id, artist_id, limit, offset, None, statement_period_start, statement_period_end, None, None, subaccount_id, order_by, order_dir, search_query, None, None, None, None, None) assert [item._asdict() for item in items] == [ { 'account_id': Decimal('57608'), 'imprintid': Decimal('123455'), 'imprint': 'TEST IMPRINT 1', 'account_payee_currency': 'JPY', 'subaccount_id': Decimal('57609'), 'mechanical_deduction_amount_payee_currency': Decimal('20.000000000000'), 'publisher_admin_fee_payee_currency': Decimal('2.000000000000'), 'net_share_payee_currency': Decimal('20.000000000000'), 'gross_revenue_payee_currency': Decimal('18.000000000000'), 'total_records': 1, }, ] @using_mock_snowflake_table(RevenueByImprint, _MOCK_DATA) def test_get_by_account_id_with_subaccount(): """Test getting revenue by account with subaccount.""" account_id = 57608 limit = 50 offset = 0 subaccount_id = 57609 subaccount_info = DimSubaccountFactory.build( subaccount_id=subaccount_id) (items, total_record) = RevenueByImprint.get_by_account_id( account_id, None, limit, offset, None, 10, 10, None, None, None, ORDER_BY_IMPRINT, OrderDirection.ASC, None, None, None, None, None, subaccount_info) actual = items[0]._asdict() assert actual == { 'account_id': Decimal('57608'), 'imprintid': Decimal('123456'), 'imprint': 'TEST IMPRINT', 'account_payee_currency': 'JPY', 'subaccount_id': Decimal('57609'), 'mechanical_deduction_amount_payee_currency': Decimal('10.000000000000'), 'publisher_admin_fee_payee_currency': Decimal('1.000000000000'), 'net_share_payee_currency': Decimal('10.000000000000'), 'gross_revenue_payee_currency': Decimal('8.000000000000'), 'total_records': 3, 'subaccount_revenue': Decimal('9.000000000000'), } @using_mock_snowflake_table(RevenueByImprint, _MOCK_DATA) def test_get_imprints_by_account_id(): """Test getting imprints by account ID.""" account_id = 57608 expected = [ { 'imprintid': 123455, 'imprint': 'TEST IMPRINT 1', 'account_id': 57608, }, { 'imprintid': 123456, 'imprint': 'TEST IMPRINT', 'account_id': 57608, }, { 'imprintid': 123457, 'imprint': 'TEST IMPRINT 2', 'account_id': 57608, } ] result = RevenueByImprint.get_imprints_by_account_id(account_id, None, None, None, None) result_dicts = [dict(row) for row in result] result_dicts.sort(key=lambda x: x['imprintid']) expected.sort(key=lambda x: x['imprintid']) assert result_dicts == expected @pytest.mark.parametrize('search_term, expected', [ ('123457', { 'imprintid': 123457, 'imprint': 'TEST IMPRINT 2', 'account_id': 57608, }), ('TEST IMPRINT 2', { 'imprintid': 123457, 'imprint': 'TEST IMPRINT 2', 'account_id': 57608, }), ]) @using_mock_snowflake_table(RevenueByImprint, _MOCK_DATA) def test_get_imprints_by_account_id_with_search_term(search_term, expected): """Test getting imprints by account ID and search term.""" account_id = 57608 result = RevenueByImprint.get_imprints_by_account_id(account_id, None, search_term, None, None) result[0] = result[0]._asdict() assert result == [expected] @using_mock_snowflake_table(RevenueByImprint, _MOCK_DATA) def test_get_imprints_by_account_id_with_imprint_ids(): """Test getting imprints by account ID and imprint IDs.""" account_id = 57608 imprint_ids = [123456, 123455] expected = [ { 'imprintid': 123456, 'imprint': 'TEST IMPRINT', 'account_id': 57608, }, { 'imprintid': 123455, 'imprint': 'TEST IMPRINT 1', 'account_id': 57608, }, ] result = RevenueByImprint.get_imprints_by_account_id(account_id, None, None, None, imprint_ids) assert [item._asdict() for item in result] == expected _MOCK_DATA_WITH_DUPLICATE_IMPRINTS = { 'revenue_by_imprint_dbt': [ { 'imprintid': 100001, 'imprint': 'DUPE IMPRINT', 'account_id': 57608, 'subaccount_id': 57609, 'artist_id': 1, 'statement_period_id': 10, 'account_payee_currency': 'GBP', 'mechanical_deduction_amount_payee_currency': Decimal('5.00'), 'publisher_admin_fee_payee_currency': Decimal('1.00'), 'net_share_payee_currency': Decimal('5.00'), 'gross_revenue_payee_currency': Decimal('4.00'), 'activity_period_id': 202301, 'store_id': 1, 'country_code': 'US', 'transaction_type_id': 201, }, { 'imprintid': 100001, 'imprint': 'DUPE IMPRINT', 'account_id': 57608, 'subaccount_id': 57610, 'artist_id': 2, 'statement_period_id': 11, 'account_payee_currency': 'GBP', 'mechanical_deduction_amount_payee_currency': Decimal('6.00'), 'publisher_admin_fee_payee_currency': Decimal('1.00'), 'net_share_payee_currency': Decimal('6.00'), 'gross_revenue_payee_currency': Decimal('5.00'), 'activity_period_id': 202302, 'store_id': 2, 'country_code': 'GB', 'transaction_type_id': 202, }, { 'imprintid': 100002, 'imprint': 'UNIQUE IMPRINT', 'account_id': 57608, 'subaccount_id': 57609, 'artist_id': 1, 'statement_period_id': 10, 'account_payee_currency': 'GBP', 'mechanical_deduction_amount_payee_currency': Decimal('3.00'), 'publisher_admin_fee_payee_currency': Decimal('1.00'), 'net_share_payee_currency': Decimal('3.00'), 'gross_revenue_payee_currency': Decimal('2.00'), 'activity_period_id': 202301, 'store_id': 1, 'country_code': 'US', 'transaction_type_id': 201, }, ], } @using_mock_snowflake_table(RevenueByImprint, _MOCK_DATA_WITH_DUPLICATE_IMPRINTS) def test_get_imprints_by_account_id_deduplicates_by_imprintid(): """Test that imprints with the same imprintid are returned only once.""" account_id = 57608 result = RevenueByImprint.get_imprints_by_account_id(account_id, None, None, None, None) result_dicts = [row._asdict() for row in result] assert len(result_dicts) == 2 @using_mock_snowflake_table(RevenueByImprint, _MOCK_DATA) def test_get_by_account_id_with_multiple_filters(): """Test getting revenue by account with multiple filters combined.""" account_id = 57608 limit = 50 offset = 0 store_ids = [1, 3] country_codes = ['US', 'CA'] transaction_type_ids = [201, 203] imprint_ids = [123457] activity_period_id_start = 202301 activity_period_id_end = 202303 (items, total_record) = RevenueByImprint.get_by_account_id( account_id, None, limit, offset, None, 10, 11, activity_period_id_start, activity_period_id_end, None, ORDER_BY_IMPRINT, OrderDirection.ASC, None, store_ids, country_codes, imprint_ids, transaction_type_ids, None) assert sorted([item.imprintid for item in items]) == imprint_ids @using_mock_snowflake_table(RevenueByImprint, _MOCK_DATA) def test_get_by_account_id_with_activity_period_range(): """Test getting revenue by account with specific activity period start and end range.""" account_id = 57608 limit = 50 offset = 0 activity_period_id_start = 202302 activity_period_id_end = 202303 (items, total_record) = RevenueByImprint.get_by_account_id( account_id, None, limit, offset, None, 10, 11, activity_period_id_start, activity_period_id_end, None, ORDER_BY_IMPRINT, OrderDirection.ASC, None, None, None, None, None, None) assert len(items) >= 1 @using_mock_snowflake_table(RevenueByImprint, _MOCK_DATA) def test_get_by_account_id_with_numeric_search_term(): """Test getting revenue by account with a numeric search term.""" account_id = 57608 limit = 50 offset = 0 search_term = '123457' (items, total_record) = RevenueByImprint.get_by_account_id( account_id, None, limit, offset, None, None, None, None, None, None, ORDER_BY_IMPRINT, OrderDirection.ASC, search_term, None, None, None, None, None) assert len(items) == 1 assert items[0]['imprint'] == 'TEST IMPRINT 2'