"""Revenue By Track model tests.""" from decimal import Decimal from moneyhub.constants.constants import ORDER_BY_TRACK_UNIQUE_ID from moneyhub.constants.constants import OrderDirection from moneyhub.models import RevenueByTrack from tests.unit.conftest import using_mock_snowflake_table from tests.utils.factories import DimSubaccountFactory _MOCK_DATA = { 'revenue_by_track_dbt': [ { 'track_unique_id': 12341231, 'track_name': 'Hey', 'isrc': 'IIIII1234186', 'subaccount_id': 1, 'account_id': 57608, 'product_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_revenue_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'store_id': 101, 'country_code': 'US', 'imprint_id': 201, 'transaction_type_id': 301, 'project_id': 1, 'project_name': 'Project Alpha', }, { 'track_unique_id': 12341232, 'track_name': 'Push Sky', 'isrc': 'IIIII1234187', 'subaccount_id': 1, 'account_id': 57608, 'product_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_revenue_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'store_id': 102, 'country_code': 'GB', 'imprint_id': 202, 'transaction_type_id': 302, 'project_id': 1, 'project_name': 'Project Beta', }, { 'track_unique_id': 12341234, 'track_name': 'Best Name', 'isrc': 'IIIII1234188', 'subaccount_id': 2, 'statement_period_id': 10, 'account_id': 57608, 'product_id': 1, 'account_payee_currency': 'JPY', 'mechanical_deduction_amount_payee_currency': Decimal('10.00'), 'publisher_admin_fee_payee_currency': Decimal('1.00'), 'net_revenue_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'store_id': 103, 'country_code': 'CA', 'imprint_id': 203, 'transaction_type_id': 303, 'project_id': 2, 'project_name': 'Project Gamma', }, { 'track_unique_id': 12341234, 'track_name': 'Best Name', 'isrc': 'IIIII1234188', 'subaccount_id': 2, 'statement_period_id': 11, 'account_id': 57608, 'product_id': 1, 'account_payee_currency': 'JPY', 'mechanical_deduction_amount_payee_currency': Decimal('10.00'), 'publisher_admin_fee_payee_currency': Decimal('1.00'), 'net_revenue_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'store_id': 104, 'country_code': 'DE', 'imprint_id': 204, 'transaction_type_id': 304, 'project_id': 2, 'project_name': 'Project Gamma', }, ], } @using_mock_snowflake_table(RevenueByTrack, _MOCK_DATA) def test_get_by_account_id(): """Test getting revenue by account.""" account_id = 57608 limit = 50 offset = 0 (items, total_record) = RevenueByTrack.get_by_account_id( account_id, limit, offset, None, None, None, None, None, None, None, None, ORDER_BY_TRACK_UNIQUE_ID, OrderDirection.ASC, None, None, None, None, None, None) actual = items[0]._asdict() assert actual == { 'track_unique_id': Decimal('12341231'), 'track_name': 'Hey', 'isrc': 'IIIII1234186', 'account_id': Decimal('57608'), 'account_payee_currency': 'JPY', 'project_name': 'Project Alpha', 'subaccount_id': Decimal('1'), 'subaccount_name': None, 'imprint': None, 'mechanical_deduction_amount_payee_currency': Decimal('10.000000000000'), 'publisher_admin_fee_payee_currency': Decimal('1.000000000000'), 'net_revenue_payee_currency': Decimal('10.000000000000'), 'gross_revenue_payee_currency': Decimal('8.000000000000'), 'total_records': 3, } @using_mock_snowflake_table(RevenueByTrack, _MOCK_DATA) def test_get_by_account_id_with_filters(): """Test getting by account with filters.""" account_id = 57608 limit = 50 offset = 0 subaccount_id = 2 search_query = 'Best' statement_period_start = 10 statement_period_end = 11 order_by = 'track_name' order_dir = OrderDirection.ASC (items, total_record) = RevenueByTrack.get_by_account_id( account_id, limit, offset, None, statement_period_start, statement_period_end, None, None, None, subaccount_id, None, order_by, order_dir, search_query, None, None, None, None, None) assert [item._asdict() for item in items] == [ { 'track_unique_id': Decimal('12341234'), 'track_name': 'Best Name', 'isrc': 'IIIII1234188', 'account_id': Decimal('57608'), 'account_payee_currency': 'JPY', 'project_name': 'Project Gamma', 'subaccount_id': Decimal('2'), 'subaccount_name': None, 'imprint': None, 'mechanical_deduction_amount_payee_currency': Decimal('20.000000000000'), 'publisher_admin_fee_payee_currency': Decimal('2.000000000000'), 'net_revenue_payee_currency': Decimal('20.000000000000'), 'gross_revenue_payee_currency': Decimal('16.000000000000'), 'total_records': 1, }, ] @using_mock_snowflake_table(RevenueByTrack, _MOCK_DATA) def test_get_by_account_id_with_list_filters(): """Test getting by account with list filters.""" account_id = 57608 limit = 50 offset = 0 store_ids = [101, 102] country_codes = ['US', 'GB'] imprint_ids = [201] transaction_type_ids = [301, 302] (items, total_record) = RevenueByTrack.get_by_account_id( account_id, limit, offset, None, None, None, None, None, None, None, None, ORDER_BY_TRACK_UNIQUE_ID, OrderDirection.ASC, None, store_ids, country_codes, transaction_type_ids, imprint_ids, None) assert [item._asdict() for item in items] == [ { 'track_unique_id': Decimal('12341231'), 'track_name': 'Hey', 'isrc': 'IIIII1234186', 'account_id': Decimal('57608'), 'account_payee_currency': 'JPY', 'project_name': 'Project Alpha', 'subaccount_id': Decimal('1'), 'subaccount_name': None, 'imprint': None, 'mechanical_deduction_amount_payee_currency': Decimal('10.000000000000'), 'publisher_admin_fee_payee_currency': Decimal('1.000000000000'), 'net_revenue_payee_currency': Decimal('10.000000000000'), 'gross_revenue_payee_currency': Decimal('8.000000000000'), 'total_records': 1, }, ] assert total_record == 1 @using_mock_snowflake_table(RevenueByTrack, _MOCK_DATA) def test_get_by_account_id_with_subaccount(): """Test getting revenue by track with subaccount.""" account_id = 57608 limit = 50 offset = 0 subaccount_id = 1 subaccount_info = DimSubaccountFactory.build( subaccount_id=subaccount_id) (items, total_record) = RevenueByTrack.get_by_account_id( account_id, limit, offset, None, 10, 12, None, None, None, subaccount_id, None, ORDER_BY_TRACK_UNIQUE_ID, OrderDirection.ASC, None, None, None, None, None, subaccount_info) actual = items[0]._asdict() assert actual == { 'track_unique_id': Decimal('12341231'), 'track_name': 'Hey', 'isrc': 'IIIII1234186', 'account_id': Decimal('57608'), 'account_payee_currency': 'JPY', 'project_name': 'Project Alpha', 'subaccount_id': Decimal('1'), 'subaccount_name': None, 'imprint': None, 'mechanical_deduction_amount_payee_currency': Decimal('10.000000000000'), 'publisher_admin_fee_payee_currency': Decimal('1.000000000000'), 'net_revenue_payee_currency': Decimal('10.000000000000'), 'gross_revenue_payee_currency': Decimal('8.000000000000'), 'total_records': 2, 'subaccount_revenue': Decimal('9.000000000000'), } assert total_record == 2 @using_mock_snowflake_table(RevenueByTrack, _MOCK_DATA) def test_get_by_account_id_with_isrc_search_term(): """Test getting revenue by account with an ISRC as search term.""" account_id = 57608 search_term = 'IIIII1234186' (items, total_record) = RevenueByTrack.get_by_account_id( account_id, 10, 0, None, None, None, None, None, None, None, None, ORDER_BY_TRACK_UNIQUE_ID, OrderDirection.ASC, search_term, None, None, None, None, None) assert len(items) == 1 assert items[0]['isrc'] == search_term @using_mock_snowflake_table(RevenueByTrack, _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 search_term = '12341234' (items, total_record) = RevenueByTrack.get_by_account_id( account_id, 10, 0, None, None, None, None, None, None, None, None, ORDER_BY_TRACK_UNIQUE_ID, OrderDirection.ASC, search_term, None, None, None, None, None) assert len(items) == 1 assert items[0]['track_unique_id'] == int(search_term) @using_mock_snowflake_table(RevenueByTrack, _MOCK_DATA) def test_get_tracks_by_account_id(): """Test getting distinct tracks by account id.""" account_id = 57608 limit = 50 tracks = RevenueByTrack.get_tracks_by_account_id(account_id, limit, None, None) first_track = tracks[0] assert len(tracks) == 3 assert first_track.track_name == 'Best Name' assert first_track.track_unique_id == 12341234 assert first_track.isrc == 'IIIII1234188' @using_mock_snowflake_table(RevenueByTrack, _MOCK_DATA) def test_get_tracks_by_account_id_with_name_search(): """Test getting distinct tracks by account id with name search term.""" account_id = 57608 limit = 50 search_term = 'Hey' tracks = RevenueByTrack.get_tracks_by_account_id(account_id, limit, search_term, None) assert len(tracks) == 1 assert tracks[0].track_name == 'Hey' assert tracks[0].track_unique_id == 12341231 @using_mock_snowflake_table(RevenueByTrack, _MOCK_DATA) def test_get_tracks_by_account_id_with_isrc_search(): """Test getting distinct tracks by account id with ISRC search term.""" account_id = 57608 limit = 50 search_term = 'IIIII1234186' tracks = RevenueByTrack.get_tracks_by_account_id(account_id, limit, search_term, None) assert len(tracks) == 1 assert tracks[0].isrc == search_term @using_mock_snowflake_table(RevenueByTrack, _MOCK_DATA) def test_get_tracks_by_account_id_with_numeric_search(): """Test getting distinct tracks by account id with numeric search term.""" account_id = 57608 limit = 50 search_term = '12341231' tracks = RevenueByTrack.get_tracks_by_account_id(account_id, limit, search_term, None) assert len(tracks) == 1 assert tracks[0].track_unique_id == int(search_term) @using_mock_snowflake_table(RevenueByTrack, _MOCK_DATA) def test_get_tracks_by_account_id_with_subaccount(): """Test getting distinct tracks by account id with subaccount filter.""" account_id = 57608 limit = 50 subaccount_id = 1 tracks = RevenueByTrack.get_tracks_by_account_id(account_id, limit, None, subaccount_id) assert len(tracks) == 2 track_names = {t.track_name for t in tracks} assert track_names == {'Hey', 'Push Sky'} @using_mock_snowflake_table(RevenueByTrack, { 'revenue_by_track_dbt': [ { 'track_unique_id': 1001, 'track_name': 'Valid Track', 'isrc': 'IIIII1234186', 'subaccount_id': None, 'statement_period_id': 10, 'account_id': 57608, 'product_id': 1, 'account_payee_currency': 'JPY', 'mechanical_deduction_amount_payee_currency': Decimal('10.00'), 'publisher_admin_fee_payee_currency': Decimal('1.00'), 'net_revenue_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'store_id': 101, 'country_code': 'US', 'imprint_id': 201, 'transaction_type_id': 301, 'project_name': 'Project A', }, { 'track_unique_id': 0, 'track_name': 'Unknown Track', 'isrc': None, 'subaccount_id': None, 'statement_period_id': 10, 'account_id': 57608, 'product_id': 1, 'account_payee_currency': 'JPY', 'mechanical_deduction_amount_payee_currency': Decimal('10.00'), 'publisher_admin_fee_payee_currency': Decimal('1.00'), 'net_revenue_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'store_id': 102, 'country_code': 'US', 'imprint_id': 202, 'transaction_type_id': 302, 'project_name': None, }, ], }) def test_get_tracks_by_account_id_excludes_zero_id(): """Test that tracks with track_unique_id=0 are excluded.""" account_id = 57608 limit = 50 tracks = RevenueByTrack.get_tracks_by_account_id(account_id, limit, None, None) track_ids = [t.track_unique_id for t in tracks] assert 0 not in track_ids assert len(tracks) == 1 @using_mock_snowflake_table(RevenueByTrack, _MOCK_DATA) def test_get_by_account_id_with_project_id_filter(): """Test filtering revenue by track by project ID.""" (items, total_record) = RevenueByTrack.get_by_account_id( 57608, 50, 0, None, None, None, None, None, None, None, None, ORDER_BY_TRACK_UNIQUE_ID, OrderDirection.ASC, None, None, None, None, None, None, project_id=1) assert total_record == 2 track_names = {item.track_name for item in items} assert track_names == {'Hey', 'Push Sky'}