"""Revenue By Product Distro model tests.""" from datetime import datetime from decimal import Decimal from moneyhub.constants.constants import ORDER_BY_PRODUCT_ID from moneyhub.constants.constants import OrderDirection from moneyhub.models import RevenueByProductDistro from tests.unit.conftest import using_mock_snowflake_table from tests.utils.factories import DimSubaccountFactory _MOCK_DATA = { 'revenue_by_product_distro_dbt': [ { 'account_id': 24601, 'product_name': 'Hey', 'display_upc': '1111111111111', 'artist_name': 'Artist', 'subaccount_id': 1, 'release_date': datetime(2000, 1, 1, 0, 0, 0), 'contract_id': 10001, 'product_id': 1234, 'statement_period_id': 10, 'account_payee_currency': 'NOK', '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'), 'store_id': 101, 'country_code': 'US', 'imprint_id': 201, 'transaction_type_id': 301, 'product_code': 'PROD001', 'project_id': 501, 'project_code': 'PROJ001', 'project_name': 'Project Alpha', }, { 'account_id': 24601, 'product_name': 'Push Sky', 'display_upc': '1111111111112', 'artist_name': 'Artist', 'subaccount_id': 1, 'release_date': datetime(2000, 1, 1, 0, 0, 0), 'contract_id': 10001, 'product_id': 1235, 'statement_period_id': 10, 'account_payee_currency': 'NOK', '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'), 'store_id': 102, 'country_code': 'GB', 'imprint_id': 202, 'transaction_type_id': 302, 'product_code': 'PROD002', 'project_id': 502, 'project_code': 'PROJ002', 'project_name': 'Project Beta', }, { 'account_id': 24601, 'product_name': 'Push Me', 'display_upc': '1111111111113', 'artist_name': 'Artist', 'artist_id': 1, 'subaccount_id': 1, 'release_date': datetime(2000, 1, 1, 0, 0, 0), 'contract_id': 10001, 'product_id': 1236, 'statement_period_id': 10, 'account_payee_currency': 'NOK', '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'), 'store_id': 103, 'country_code': 'CA', 'imprint_id': 203, 'transaction_type_id': 303, 'project_id': 502, 'project_name': 'Project Beta', }, { 'account_id': 24601, 'product_name': 'Push Me', 'display_upc': '1111111111113', 'artist_name': 'Artist', 'subaccount_id': 1, 'release_date': datetime(2000, 1, 1, 0, 0, 0), 'contract_id': 10001, 'product_id': 1236, 'statement_period_id': 11, 'account_payee_currency': 'NOK', 'mechanical_deduction_amount_payee_currency': Decimal('15.00'), 'publisher_admin_fee_payee_currency': Decimal('1.00'), 'net_share_payee_currency': Decimal('15.00'), 'gross_revenue_payee_currency': Decimal('13.00'), 'store_id': 104, 'country_code': 'DE', 'imprint_id': 204, 'transaction_type_id': 304, 'project_id': 502, 'project_name': 'Project Beta', }, ], } @using_mock_snowflake_table(RevenueByProductDistro, _MOCK_DATA) def test_get_by_account_id(): """Test getting revenue by account.""" account_id = 24601 limit = 50 offset = 0 (items, total_record) = RevenueByProductDistro.get_by_account_id( account_id, limit, offset, None, None, None, None, None, None, None, ORDER_BY_PRODUCT_ID, OrderDirection.ASC, None, None, None, None, None, None) actual = items[0]._asdict() assert actual == { 'product_id': Decimal('1234'), 'account_payee_currency': 'NOK', 'project_name': 'Project Alpha', '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(RevenueByProductDistro, _MOCK_DATA) def test_get_by_account_id_with_filters(): """Test getting by account with filters.""" account_id = 24601 limit = 50 offset = 0 subaccount_id = 1 search_query = 'Push' statement_period_start = 10 statement_period_end = 11 order_by = 'product_name' order_dir = OrderDirection.ASC (items, total_record) = RevenueByProductDistro.get_by_account_id( account_id, limit, offset, None, statement_period_start, statement_period_end, None, None, None, subaccount_id, order_by, order_dir, search_query, None, None, None, None, None) assert [item._asdict() for item in items] == [ { 'product_id': Decimal('1236'), 'account_payee_currency': 'NOK', 'project_name': 'Project Beta', 'mechanical_deduction_amount_payee_currency': Decimal('25.000000000000'), 'publisher_admin_fee_payee_currency': Decimal('2.000000000000'), 'net_share_payee_currency': Decimal('25.000000000000'), 'gross_revenue_payee_currency': Decimal('21.000000000000'), 'total_records': 2, }, { 'product_id': Decimal('1235'), 'account_payee_currency': 'NOK', 'project_name': 'Project Beta', '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': 2, }, ] @using_mock_snowflake_table(RevenueByProductDistro, _MOCK_DATA) def test_get_by_account_id_with_subaccount(): """Test getting revenue by account with subaccount.""" account_id = 24601 limit = 50 offset = 0 subaccount_id = 1 subaccount_info = DimSubaccountFactory.build( subaccount_id=subaccount_id) (items, total_record) = RevenueByProductDistro.get_by_account_id( account_id, limit, offset, None, 10, 12, None, None, None, subaccount_id, ORDER_BY_PRODUCT_ID, OrderDirection.ASC, None, None, None, None, None, subaccount_info) actual = items[0]._asdict() assert actual == { 'product_id': Decimal('1234'), 'account_payee_currency': 'NOK', 'project_name': 'Project Alpha', '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(RevenueByProductDistro, _MOCK_DATA) def test_get_by_account_id_with_list_filters(): """Test getting by account with list filters.""" account_id = 24601 limit = 50 offset = 0 store_ids = [101, 102] country_codes = ['US', 'GB'] imprint_ids = [201] transaction_type_ids = [301, 302] (items, total_record) = RevenueByProductDistro.get_by_account_id( account_id, limit, offset, None, None, None, None, None, None, None, ORDER_BY_PRODUCT_ID, OrderDirection.ASC, None, store_ids, country_codes, transaction_type_ids, imprint_ids, None) assert [item._asdict() for item in items] == [ { 'product_id': Decimal('1234'), 'account_payee_currency': 'NOK', 'project_name': 'Project Alpha', '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': 1, }, ] assert total_record == 1 @using_mock_snowflake_table(RevenueByProductDistro, _MOCK_DATA) def test_get_by_account_id_with_upc_search_term(): """Test getting products by searching for a UPC.""" account_id = 24601 search_term = '1111111111112' (items, total_record) = RevenueByProductDistro.get_by_account_id( account_id, 10, 0, None, None, None, None, None, None, None, ORDER_BY_PRODUCT_ID, OrderDirection.ASC, search_term, None, None, None, None, None) assert len(items) == 1 assert items[0].product_id == 1235 assert total_record == 1 @using_mock_snowflake_table(RevenueByProductDistro, _MOCK_DATA) def test_get_by_account_id_with_id_search_term(): """Test getting products by searching for a specific ID.""" account_id = 24601 search_term = '1234' (items, total_record) = RevenueByProductDistro.get_by_account_id( account_id, 10, 0, None, None, None, None, None, None, None, ORDER_BY_PRODUCT_ID, OrderDirection.ASC, search_term, None, None, None, None, None) assert len(items) == 1 assert items[0].product_id == int(search_term) assert total_record == 1 @using_mock_snowflake_table(RevenueByProductDistro, _MOCK_DATA) def test_get_products_by_account_id(): """Test getting distinct products by account id.""" account_id = 24601 limit = 50 products = RevenueByProductDistro.get_products_by_account_id( account_id, limit, None, None) first_product = products[0] assert len(products) == 3 assert first_product.product_id == 1234 assert first_product.product_name == 'Hey' assert first_product.display_upc == '1111111111111' @using_mock_snowflake_table(RevenueByProductDistro, _MOCK_DATA) def test_get_products_by_account_id_with_name_search(): """Test getting distinct products by account id with name search term.""" account_id = 24601 limit = 50 search_term = 'Push' products = RevenueByProductDistro.get_products_by_account_id( account_id, limit, search_term, None) assert len(products) == 2 assert all(p.product_name.startswith('Push') for p in products) @using_mock_snowflake_table(RevenueByProductDistro, _MOCK_DATA) def test_get_products_by_account_id_with_upc_search(): """Test getting distinct products by account id with UPC search term.""" account_id = 24601 limit = 50 search_term = '1111111111112' products = RevenueByProductDistro.get_products_by_account_id( account_id, limit, search_term, None) assert len(products) == 1 assert products[0].product_id == 1235 assert products[0].display_upc == '1111111111112' @using_mock_snowflake_table(RevenueByProductDistro, _MOCK_DATA) def test_get_products_by_account_id_with_exact_id_search(): """Test getting distinct products by account id with exact numeric product_id search.""" account_id = 24601 limit = 50 search_term = '1234' products = RevenueByProductDistro.get_products_by_account_id( account_id, limit, search_term, None) assert len(products) == 1 assert products[0].product_id == 1234 @using_mock_snowflake_table(RevenueByProductDistro, _MOCK_DATA) def test_get_products_by_account_id_with_subaccount(): """Test getting distinct products filtered by subaccount id.""" account_id = 24601 limit = 50 subaccount_id = 1 products = RevenueByProductDistro.get_products_by_account_id( account_id, limit, None, subaccount_id) assert len(products) == 3