from unittest.mock import patch import pytest from marshmallow import ValidationError from analytics.logic.account_products import get_products def test_get_account_products_requires_account_id(): with pytest.raises(ValidationError): get_products({"account_type": "vendor", "order_by": "streams_all_time"}, {}) MOCK_ROWS = [ { "product_id": 1, "growth_percentage_1_day": 1.45, "growth_percentage_7_days": 1.45, "growth_percentage_28_days": 1.45, "streams_1_day": 100, "streams_7_days": 100, "streams_28_days": 100, "streams_all_time": 100, }, { "product_id": 2, "growth_percentage_1_day": 1.45, "growth_percentage_7_days": 1.45, "growth_percentage_28_days": 1.45, "streams_1_day": 100, "streams_7_days": 100, "streams_28_days": 100, "streams_all_time": 100, }, { "product_id": 3, "growth_percentage_1_day": 1.45, "growth_percentage_7_days": 1.45, "growth_percentage_28_days": 1.45, "streams_1_day": 100, "streams_7_days": 100, "streams_28_days": 100, "streams_all_time": 100, }, ] def test_get_account_products_streams_fixed_period(): with patch( "analytics.logic.account_products." "AccountProductsByStreamsFixedPeriod.execute", return_value=MOCK_ROWS, ): recent_releases = get_products( { "account_id": 123, "account_type": "vendor", "order_by": "streams_all_time", }, {}, ) assert len(recent_releases) == 3 def test_get_account_products_streams_custom_period(): with patch( "analytics.logic.account_products." "AccountProductsByStreamsCustomPeriod.execute", return_value=MOCK_ROWS, ): recent_releases = get_products( { "account_id": 123, "account_type": "vendor", "order_by": "streams", "start_date": "2023-01-01", "end_date": "2023-01-02", }, {}, ) assert len(recent_releases) == 3 def test_get_account_products_streams_custom_period_transfer_ownership(): """With the transfer FF on, custom-period streams resolves co-owner pairs (for the BY_ACCOUNT account_scope_filter) and threads them into the query.""" with ( patch( "analytics.logic.account_products." "AccountProductsByStreamsCustomPeriod.execute", return_value=MOCK_ROWS, ), patch( "analytics.logic.account_products.get_cross_attribution_pairs", return_value=[(35602, None)], ) as mock_pairs, ): recent_releases = get_products( { "account_id": 123, "account_type": "vendor", "order_by": "streams", "start_date": "2023-01-01", "end_date": "2023-01-02", "transfer_product_ownership_enabled": True, }, {}, ) assert len(recent_releases) == 3 mock_pairs.assert_called_once_with(123, "vendor") def test_get_account_products_downloads_fixed_period(): with patch( "analytics.logic.account_products." "AccountProductsByDownloadsFixedPeriod.execute", return_value=MOCK_ROWS, ): recent_releases = get_products( { "account_id": 123, "account_type": "vendor", "order_by": "downloads_all_time", }, {}, ) assert len(recent_releases) == 3 def test_get_account_products_downloads_custom_period(): with patch( "analytics.logic.account_products." "AccountProductsByDownloadsCustomPeriod.execute", return_value=MOCK_ROWS, ): recent_releases = get_products( { "account_id": 123, "account_type": "vendor", "order_by": "downloads", "start_date": "2023-01-01", "end_date": "2023-01-02", }, {}, ) assert len(recent_releases) == 3