"""Revenue By Store model tests.""" from decimal import Decimal from moneyhub.constants.constants import OrderDirection from moneyhub.models import RevenueByStore from tests.unit.conftest import using_mock_snowflake_table from tests.utils.factories import DimSubaccountFactory ACCOUNT_ID = 24601 _MOCK_DATA = { 'revenue_by_store_dbt': [ { 'account_id': 24601, 'artist_id': 1, 'subaccount_id': 1, 'product_id': 1, 'track_unique_id': 1234556, 'contract_id': 10001, 'statement_period_id': 10, 'store_id': 123, 'store_name': 'iTunes/Apple', 'account_payee_currency': 'USD', 'net_revenue_payee_currency': Decimal('8.00'), 'gross_revenue_payee_currency': Decimal('10.00'), 'activity_period_id': 1, 'country_code': 'US', 'imprint_id': 1, 'transaction_type_id': 1, 'project_id': 1, }, { 'account_id': 24601, 'artist_id': 2, 'subaccount_id': 2, 'product_id': 2, 'track_unique_id': 1234554, 'contract_id': 10001, 'statement_period_id': 10, 'store_id': 456, 'store_name': 'Spotify', 'account_payee_currency': 'USD', 'net_revenue_payee_currency': Decimal('8.00'), 'gross_revenue_payee_currency': Decimal('10.00'), 'activity_period_id': 1, 'country_code': 'GB', 'imprint_id': 1, 'transaction_type_id': 1, 'project_id': 2, }, { 'account_id': 24601, 'artist_id': 1, 'subaccount_id': 1, 'product_id': 1, 'track_unique_id': 1234556, 'contract_id': 10001, 'statement_period_id': 11, 'store_id': 123, 'store_name': 'iTunes/Apple', 'account_payee_currency': 'USD', 'net_revenue_payee_currency': Decimal('13.00'), 'gross_revenue_payee_currency': Decimal('15.00'), 'activity_period_id': 1, 'country_code': 'US', 'imprint_id': 2, 'transaction_type_id': 1, 'project_id': 1, }, { 'account_id': 24601, 'artist_id': 2, 'subaccount_id': 2, 'product_id': 2, 'track_unique_id': 1234554, 'contract_id': 10001, 'statement_period_id': 11, 'store_id': 789, 'store_name': 'Amazon Unlimited', 'account_payee_currency': 'USD', 'net_revenue_payee_currency': Decimal('23.00'), 'gross_revenue_payee_currency': Decimal('25.00'), 'activity_period_id': 1, 'country_code': 'US', 'imprint_id': 1, 'transaction_type_id': 2, 'project_id': 2, }, ], } @using_mock_snowflake_table(RevenueByStore, _MOCK_DATA) def test_get_by_account_id(): """Test getting revenue by account.""" items, total_records = RevenueByStore.get_by_account_id( ACCOUNT_ID, None, None, 2, 0, None, None, None, None, None, 'store_id', OrderDirection.ASC, None, None, None, None, None, None, None, None) actual = items[0]._asdict() assert actual == { 'store_id': Decimal('123'), 'store_name': 'iTunes/Apple', 'account_payee_currency': 'USD', 'net_revenue_payee_currency': Decimal('21.000000000000'), 'gross_revenue_payee_currency': Decimal('25.000000000000'), 'total_records': 3, } assert total_records == 3 @using_mock_snowflake_table(RevenueByStore, _MOCK_DATA) def test_get_by_account_id_with_filters(): """Test getting by account with filters.""" statement_period_start = 10 statement_period_end = 10 order_by = 'store_id' artist_id = 2 subaccount_id = 2 product_id = 2 track_unique_id = 1234554 order_dir = OrderDirection.DESC search_term = 'Spotif' activity_period_id_start = 1 activity_period_id_end = 1 country_codes = ['GB'] imprint_ids = [1] transaction_type_ids = [1] items, total_records = RevenueByStore.get_by_account_id( ACCOUNT_ID, artist_id, subaccount_id, 0, 0, None, product_id, track_unique_id, statement_period_start, statement_period_end, order_by, order_dir, search_term, None, activity_period_id_start, activity_period_id_end, country_codes, imprint_ids, transaction_type_ids, None, ) assert [item._asdict() for item in items] == [ { 'store_id': Decimal('456'), 'store_name': 'Spotify', 'account_payee_currency': 'USD', 'net_revenue_payee_currency': Decimal('8.000000000000'), 'gross_revenue_payee_currency': Decimal('10.000000000000'), 'total_records': 1, }, ] @using_mock_snowflake_table(RevenueByStore, _MOCK_DATA) def test_get_by_account_id_with_subaccount(): """Test getting by account with subaccount.""" statement_period_start = 10 statement_period_end = 10 order_by = 'store_id' artist_id = 2 subaccount_id = 2 product_id = 2 track_unique_id = 1234554 order_dir = OrderDirection.DESC search_term = 'Spotif' subaccount_info = DimSubaccountFactory.build( subaccount_id=subaccount_id) items, total_records = RevenueByStore.get_by_account_id( ACCOUNT_ID, artist_id, subaccount_id, 0, 0, None, product_id, track_unique_id, statement_period_start, statement_period_end, order_by, order_dir, search_term, subaccount_info, None, None, None, None, None, None) assert [item._asdict() for item in items] == [ { 'store_id': Decimal('456'), 'store_name': 'Spotify', 'account_payee_currency': 'USD', 'net_revenue_payee_currency': Decimal('8.000000000000'), 'gross_revenue_payee_currency': Decimal('10.000000000000'), 'total_records': 1, 'subaccount_revenue': Decimal('7.200000000000'), }, ] @using_mock_snowflake_table(RevenueByStore, _MOCK_DATA) def test_get_stores_totals_by_account_id(): """Test getting stores totals by account.""" actual = RevenueByStore.get_stores_totals_by_account_id( ACCOUNT_ID, None, None, None, None, None, None, None, None, None, None, None, None, None, None) assert actual == (Decimal('52.00'), Decimal('60.00')) @using_mock_snowflake_table(RevenueByStore, _MOCK_DATA) def test_get_stores_totals_by_account_id_with_filters(): """Test getting stores totals by account with filters.""" statement_period_id_start = 11 statement_period_id_end = 11 artist_id = 2 subaccount_id = 2 product_id = 2 track_unique_id = 1234554 activity_period_id_start = 1 activity_period_id_end = 1 country_codes = ['US'] imprint_ids = [1] transaction_type_ids = [2] actual = RevenueByStore.get_stores_totals_by_account_id( ACCOUNT_ID, artist_id, subaccount_id, None, product_id, track_unique_id, statement_period_id_start, statement_period_id_end, None, activity_period_id_start, activity_period_id_end, country_codes, imprint_ids, transaction_type_ids, None, ) assert actual == (Decimal('23.00'), Decimal('25.00')) @using_mock_snowflake_table(RevenueByStore, _MOCK_DATA) def test_get_stores_totals_by_account_id_with_subaccount(): """Test getting stores totals by account with subaccount.""" statement_period_id_start = 11 statement_period_id_end = 11 artist_id = 2 subaccount_id = 2 product_id = 2 track_unique_id = 1234554 subaccount_info = DimSubaccountFactory.build( subaccount_id=subaccount_id) actual = RevenueByStore.get_stores_totals_by_account_id( ACCOUNT_ID, artist_id, subaccount_id, None, product_id, track_unique_id, statement_period_id_start, statement_period_id_end, subaccount_info, None, None, None, None, None, None) assert actual == (Decimal('23.00'), Decimal('25.00'), Decimal('20.7')) @using_mock_snowflake_table(RevenueByStore, _MOCK_DATA) def test_get_stores_by_account_id(): """Test getting stores by account.""" expected = [ { 'store_id': 789, 'store_name': 'Amazon Unlimited', }, { 'store_id': 123, 'store_name': 'iTunes/Apple' }, { 'store_id': 456, 'store_name': 'Spotify' } ] actual = RevenueByStore.get_stores_by_account_id(ACCOUNT_ID) for idx, item in enumerate(actual): assert item._asdict() == expected[idx] @using_mock_snowflake_table(RevenueByStore, _MOCK_DATA) def test_get_stores_by_account_id_subaccount_id(): """Test getting stores by account and subaccount.""" expected = [ { 'store_id': 789, 'store_name': 'Amazon Unlimited', }, { 'store_id': 456, 'store_name': 'Spotify' } ] actual = RevenueByStore.get_stores_by_account_id(ACCOUNT_ID, 2) for idx, item in enumerate(actual): assert item._asdict() == expected[idx] @using_mock_snowflake_table(RevenueByStore, _MOCK_DATA) def test_get_by_account_id_with_store_ids(): """Test getting by account with store_ids filter.""" store_ids = [123, 456] items, total_records = RevenueByStore.get_by_account_id( ACCOUNT_ID, None, None, 10, 0, None, None, None, None, None, 'store_id', OrderDirection.ASC, None, None, None, None, None, None, None, store_ids) store_ids_returned = [item.store_id for item in items] assert all(store_id in [123, 456] for store_id in store_ids_returned) assert total_records == 2 @using_mock_snowflake_table(RevenueByStore, _MOCK_DATA) def test_get_by_account_id_with_id_search_term(): """Test getting by account with an ID as search term.""" search_term = '123' items, total_records = RevenueByStore.get_by_account_id( ACCOUNT_ID, None, None, 10, 0, None, None, None, None, None, 'store_id', OrderDirection.ASC, search_term, None, None, None, None, None, None, None) assert len(items) == 1 assert items[0].store_id == int(search_term) assert total_records == 1 @using_mock_snowflake_table(RevenueByStore, _MOCK_DATA) def test_get_by_account_id_with_project_id_filter(): """Test filtering revenue by store by project ID.""" items, total_records = RevenueByStore.get_by_account_id( ACCOUNT_ID, None, None, 50, 0, None, None, None, None, None, 'store_id', OrderDirection.ASC, None, None, project_id=1) assert total_records == 1 assert items[0].store_id == 123 @using_mock_snowflake_table(RevenueByStore, _MOCK_DATA) def test_get_stores_totals_by_account_id_with_project_id_filter(): """Test filtering stores totals by project ID.""" actual = RevenueByStore.get_stores_totals_by_account_id( ACCOUNT_ID, None, None, None, None, None, None, None, None, project_id=1) assert actual == (Decimal('21.00'), Decimal('25.00'))