"""Revenue By Country model tests.""" from decimal import Decimal from moneyhub.models import RevenueByCountry from tests.unit.conftest import using_mock_snowflake_table from tests.utils.factories import DimSubaccountFactory _MOCK_DATA = { 'revenue_by_country_dbt': [ { 'account_id': 24601, 'account_payee_currency': 'GBP', 'artist_id': 1, 'subaccount_id': 1, 'product_id': 1, 'track_unique_id': 1234556, 'contract_id': 10001, 'statement_period_id': 10, 'country_code': 'GB', 'imprint_id': 101, 'store_id': 201, 'transaction_type_id': 301, 'net_revenue_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'activity_period_id': 100, 'project_id': 1, }, { 'account_id': 24601, 'account_payee_currency': 'BBD', 'artist_id': 1, 'subaccount_id': 1, 'product_id': 1, 'track_unique_id': 1234556, 'contract_id': 10001, 'statement_period_id': 10, 'country_code': 'BB', 'imprint_id': 102, 'store_id': 202, 'transaction_type_id': 302, 'net_revenue_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'activity_period_id': 101, 'project_id': 2, }, { 'account_id': 24601, 'account_payee_currency': 'GBP', 'artist_id': 2, 'subaccount_id': 2, 'product_id': 2, 'track_unique_id': 1234554, 'contract_id': 10001, 'statement_period_id': 11, 'country_code': 'GB', 'imprint_id': 103, 'store_id': 203, 'transaction_type_id': 303, 'net_revenue_payee_currency': Decimal('15.00'), 'gross_revenue_payee_currency': Decimal('13.00'), 'activity_period_id': 102, 'project_id': 2, }, ], } @using_mock_snowflake_table(RevenueByCountry, _MOCK_DATA) def test_get_by_account_id(): """Test getting revenue by account.""" account_id = 24601 items = RevenueByCountry.get_by_account_id( account_id, None, None, None, None, None, None, None, None, None, None, None, None, None, None ) actual = items[0]._asdict() assert actual == { 'account_id': account_id, 'account_payee_currency': 'BBD', 'country_code': 'BB', 'net_revenue_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), } @using_mock_snowflake_table(RevenueByCountry, _MOCK_DATA) def test_get_by_account_id_with_filters(): """Test getting by account with filters.""" account_id = 24601 artist_id = 2 subaccount_id = 2 product_id = 2 track_unique_id = 1234554 statement_period_start = 10 statement_period_end = 11 items = RevenueByCountry.get_by_account_id( account_id, artist_id, subaccount_id, product_id, track_unique_id, None, statement_period_start, statement_period_end, 102, 102, None, None, None, None, None) assert [item._asdict() for item in items] == [ { 'account_id': account_id, 'account_payee_currency': 'GBP', 'country_code': 'GB', 'net_revenue_payee_currency': Decimal('15.00'), 'gross_revenue_payee_currency': Decimal('13.00'), }, ] @using_mock_snowflake_table(RevenueByCountry, _MOCK_DATA) def test_get_by_account_id_with_subaccount(): """Test getting by account with subaccount.""" account_id = 24601 subaccount_id = 1 subaccount_info = DimSubaccountFactory.build( subaccount_id=subaccount_id) items = RevenueByCountry.get_by_account_id( account_id, None, subaccount_id, None, None, None, None, None, 100, 101, None, None, None, None, subaccount_info) assert [item._asdict() for item in items] == [ { 'account_id': account_id, 'account_payee_currency': 'BBD', 'country_code': 'BB', 'net_revenue_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'subaccount_revenue': Decimal('9.00'), }, { 'account_id': account_id, 'account_payee_currency': 'GBP', 'country_code': 'GB', 'net_revenue_payee_currency': Decimal('10.00'), 'gross_revenue_payee_currency': Decimal('8.00'), 'subaccount_revenue': Decimal('9.00'), }, ] @using_mock_snowflake_table(RevenueByCountry, _MOCK_DATA) def test_get_by_account_id_with_multiple_list_filters(): """Test getting by account with multiple list filters.""" account_id = 24601 store_ids = [201] country_codes = ['GB'] imprint_ids = [101] transaction_type_ids = [301] items = RevenueByCountry.get_by_account_id( account_id, None, None, None, None, None, None, None, None, None, store_ids, country_codes, imprint_ids, transaction_type_ids, None) assert len(items) == 1 assert items[0].country_code == 'GB' assert items[0].account_payee_currency == 'GBP' @using_mock_snowflake_table(RevenueByCountry, _MOCK_DATA) def test_get_by_account_id_with_activity_period_filter(): """Test getting by account with activity period filters.""" account_id = 24601 items = RevenueByCountry.get_by_account_id( account_id, None, None, None, None, None, None, None, 100, 101, None, None, None, None, None) assert len(items) == 2 expected_countries = {'GB', 'BB'} actual_countries = {item.country_code for item in items} assert actual_countries == expected_countries @using_mock_snowflake_table(RevenueByCountry, _MOCK_DATA) def test_get_by_account_id_with_project_id_filter(): """Test filtering revenue by country by project ID.""" items = RevenueByCountry.get_by_account_id( 24601, None, None, None, None, None, None, None, None, None, None, None, None, None, None, project_id=1) assert len(items) == 1 assert items[0].country_code == 'GB'