"""Revenue By Artist model tests.""" from decimal import Decimal from moneyhub.constants.constants import ORDER_BY_ARTIST_NAME from moneyhub.constants.constants import OrderDirection from moneyhub.models import RevenueByArtist from tests.unit.conftest import using_mock_snowflake_table from tests.utils.factories import DimSubaccountFactory _MOCK_DATA = { 'revenue_by_artist_dbt': [ { 'artist_id': 12341231, 'artist_name': 'Pharoah Sanders', 'subaccount_id': 1, 'account_id': 57608, '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'), 'activity_period_id': 202301, 'store_id': 1, 'country_code': 'US', 'imprint_id': 101, 'transaction_type_id': 201 }, { 'artist_id': 12341232, 'artist_name': 'Jorge Ben Jor', 'subaccount_id': 1, 'account_id': 57608, '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_revenue_payee_currency': Decimal('20.00'), 'gross_revenue_payee_currency': Decimal('18.00'), 'activity_period_id': 202302, 'store_id': 2, 'country_code': 'GB', 'imprint_id': 102, 'transaction_type_id': 202 }, { 'artist_id': 12341234, 'artist_name': 'Emeralds', 'subaccount_id': 1, 'statement_period_id': 10, 'account_id': 57608, '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'), 'activity_period_id': 202301, 'store_id': 1, 'country_code': 'US', 'imprint_id': 101, 'transaction_type_id': 201 }, { 'artist_id': 12341234, 'artist_name': 'Necrot', 'subaccount_id': 1, 'statement_period_id': 11, 'account_id': 57608, 'account_payee_currency': 'GBP', 'mechanical_deduction_amount_payee_currency': Decimal('11.00'), 'publisher_admin_fee_payee_currency': Decimal('1.00'), 'net_revenue_payee_currency': Decimal('11.00'), 'gross_revenue_payee_currency': Decimal('9.00'), 'activity_period_id': 202303, 'store_id': 3, 'country_code': 'CA', 'imprint_id': 103, 'transaction_type_id': 203 }, ], } @using_mock_snowflake_table(RevenueByArtist, _MOCK_DATA) def test_get_by_account_id(): """Test getting revenue by account.""" account_id = 57608 limit = 50 offset = 0 (items, total_record) = RevenueByArtist.get_by_account_id( account_id, limit, offset, None, None, None, None, None, None, ORDER_BY_ARTIST_NAME, OrderDirection.ASC, None, None, None, None, None) actual = items[0]._asdict() assert actual == { 'artist_id': Decimal('12341234'), 'artist_name': 'Emeralds', 'account_id': Decimal('57608'), 'account_payee_currency': 'JPY', 'subaccount_id': Decimal('1'), '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': 4, } @using_mock_snowflake_table(RevenueByArtist, _MOCK_DATA) def test_get_by_account_id_with_filters(): """Test getting by account with filters.""" account_id = 57608 limit = 50 offset = 0 search_query = 'Pharoah' statement_period_start = 10 statement_period_end = 11 subaccount_id = 1 order_by = 'artist_name' order_dir = OrderDirection.ASC (items, total_record) = RevenueByArtist.get_by_account_id( account_id, limit, offset, None, subaccount_id, statement_period_start, statement_period_end, None, None, order_by, order_dir, search_query, None, None, None, None) assert [item._asdict() for item in items] == [ { 'artist_id': Decimal('12341231'), 'artist_name': 'Pharoah Sanders', 'account_id': Decimal('57608'), 'account_payee_currency': 'JPY', 'subaccount_id': Decimal('1'), '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, }, ] @using_mock_snowflake_table(RevenueByArtist, _MOCK_DATA) def test_get_by_account_id_with_subaccount_revenue(): """Test getting by account with filters.""" account_id = 57608 limit = 50 offset = 0 search_query = 'Pharoah' statement_period_start = 10 statement_period_end = 11 subaccount_id = 1 order_by = 'artist_name' order_dir = OrderDirection.ASC subaccount_info = DimSubaccountFactory.build( subaccount_id=subaccount_id) (items, total_record) = RevenueByArtist.get_by_account_id( account_id, limit, offset, None, subaccount_id, statement_period_start, statement_period_end, None, None, order_by, order_dir, search_query, None, None, None, None, subaccount_info) assert [item._asdict() for item in items] == [ { 'artist_id': Decimal('12341231'), 'artist_name': 'Pharoah Sanders', 'account_id': Decimal('57608'), 'account_payee_currency': 'JPY', 'subaccount_id': Decimal('1'), '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, 'subaccount_revenue': Decimal('9.000000000000'), }, ] @using_mock_snowflake_table(RevenueByArtist, _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 (items, total_record) = RevenueByArtist.get_by_account_id( account_id, limit, offset, None, None, None, None, None, None, ORDER_BY_ARTIST_NAME, OrderDirection.ASC, None, [1], None, None, None) assert len(items) == 2 (items, total_record) = RevenueByArtist.get_by_account_id( account_id, limit, offset, None, None, None, None, None, None, ORDER_BY_ARTIST_NAME, OrderDirection.ASC, None, None, ['US'], None, None) assert len(items) == 2 (items, total_record) = RevenueByArtist.get_by_account_id( account_id, limit, offset, None, None, None, None, None, None, ORDER_BY_ARTIST_NAME, OrderDirection.ASC, None, None, None, [201], None) assert len(items) == 2 (items, total_record) = RevenueByArtist.get_by_account_id( account_id, limit, offset, None, None, None, None, None, None, ORDER_BY_ARTIST_NAME, OrderDirection.ASC, None, None, None, None, [102]) assert len(items) == 1 @using_mock_snowflake_table(RevenueByArtist, _MOCK_DATA) def test_get_by_account_id_with_activity_period_filters(): """Test getting by account with activity period filters.""" account_id = 57608 limit = 50 offset = 0 (items, total_record) = RevenueByArtist.get_by_account_id( account_id, limit, offset, None, None, None, None, 202301, 202302, ORDER_BY_ARTIST_NAME, OrderDirection.ASC, None, None, None, None, None) assert len(items) == 3 @using_mock_snowflake_table(RevenueByArtist, _MOCK_DATA) def test_get_artists_by_account_id(): """Test getting distinct artists by account id.""" account_id = 57608 limit = 50 artists = RevenueByArtist.get_artists_by_account_id( account_id, limit, None, None) first_artist = artists[0] assert len(artists) == 4 assert first_artist.artist_id == 12341234 assert first_artist.artist_name == 'Emeralds' @using_mock_snowflake_table(RevenueByArtist, _MOCK_DATA) def test_get_artists_by_account_id_with_search(): """Test getting distinct artists by account id with search term.""" account_id = 57608 limit = 50 search_term = 'Jorge' artists = RevenueByArtist.get_artists_by_account_id( account_id, limit, search_term, None) assert len(artists) == 1 assert artists[0].artist_name == 'Jorge Ben Jor' assert artists[0].artist_id == 12341232 @using_mock_snowflake_table(RevenueByArtist, _MOCK_DATA) def test_get_artist_names_by_ids(): """Test getting distinct artists by account id with search term.""" artist_ids = [12341231, 12341232] expected = ['Pharoah Sanders', 'Jorge Ben Jor'] artists = RevenueByArtist.get_artist_names_by_ids(artist_ids) artists_mapped = [artist_name for artist_id, artist_name in artists] assert len(artists) == 2 assert artists_mapped == expected