"""Unit tests for source of streams model layer.""" import datetime from unittest.mock import patch import pytest from sound_recordings.models import source_of_streams from tests.unit.models.conftest import snowflake_fetch_assert MOCK_SQL = "{filter_clause}" @pytest.fixture def mock_db_result(): """Mock database result.""" return "TEST", 0, 0.333, 1, -0.5, 2, None, 3 @pytest.fixture def mock_dynamic_db_result(): """Mock database result.""" return "TEST", 0, 1, 2, 3, datetime.date(2018, 7, 6) @pytest.fixture def mock_snowflake_fetchone(mock_db_result): """Mock Snowflake fetchone.""" with patch( "sound_recordings.models.source_of_streams.snowflake.fetchone" ) as fetchone: fetchone.return_value = mock_db_result yield fetchone @pytest.fixture def mock_dynamic_snowflake_fetchone(mock_dynamic_db_result): """Mock Snowflake fetchone.""" with patch( "sound_recordings.models.source_of_streams.snowflake.fetchone" ) as fetchone: fetchone.return_value = mock_dynamic_db_result yield fetchone @pytest.fixture def mock_snowflake_fetchone_empty(): """Mock Snowflake fetchone empty response.""" with patch( "sound_recordings.models.source_of_streams.snowflake.fetchone" ) as fetchone: fetchone.return_value = () yield fetchone @pytest.fixture(autouse=True) def no_cache(): """Mock out caching of response.""" with patch("sound_recordings.connectors.redis.client.get") as get: get.return_value = None yield get @pytest.fixture def mock_load_query(): """Mock load query.""" with patch("sound_recordings.models.source_of_streams.SQLLoader") as sql_loader: load_query = sql_loader.load_query load_query.return_value = MOCK_SQL yield load_query @pytest.fixture def mock_store_availability(): """Mock store_availability.""" with patch( "sound_recordings.models.source_of_streams.store_availability" ) as store_availability: store_availability.get_store_ids.return_value = [1, 286] yield store_availability def test_get_sos_breakdown(mock_load_query, mock_snowflake_fetchone): """Test successful response.""" expected_response = { "isrc": "TEST", "streams_passive_7_days": 0, "streams_passive_7_days_growth": 0.333, "streams_active_7_days": 1, "streams_active_7_days_growth": -0.5, "streams_collection_7_days": 2, "streams_collection_7_days_growth": None, "streams_7_days": 3, } permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, } response = source_of_streams.get_sos_breakdown( permissions_filter, "test", ["theorchard"] ) assert response == expected_response mock_load_query.assert_called_with("source_of_streams") def test_get_dynamic_sos_breakdown( mock_load_query, mock_dynamic_snowflake_fetchone, mock_store_availability ): """Test successful response.""" expected_response = { "isrc": "TEST", "streams_passive": 0, "streams_active": 1, "streams_collection": 2, "streams": 3, } permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, } response = source_of_streams.get_dynamic_sos_breakdown( permissions_filter, "test", ["theorchard"] ) assert response == expected_response mock_load_query.assert_called_with("source_of_streams_dynamic") def test_get_sos_breakdown_fail(mock_snowflake_fetchone_empty): """Test empty response.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, } response = source_of_streams.get_sos_breakdown( permissions_filter, "test", ["theorchard"] ) assert response is None def test_get_dynamic_sos_breakdown_fail( mock_snowflake_fetchone_empty, mock_store_availability ): """Test empty response.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, "feed_ids": [1, 2], } response = source_of_streams.get_dynamic_sos_breakdown( permissions_filter, "test", ["theorchard"] ) assert response is None def test_get_sos_breakdown_with_subaccount(mock_load_query, mock_snowflake_fetchone): """Test get_sos_with_breakdown with subaccount.""" permissions_filter = { "label_ids": None, "artist_ids": None, "subaccount_ids": [345], "feed_ids": [1, 2], } expected_args = { "label_ids": None, "artist_ids": None, "subaccount_ids": [345], "isrc": "isrc", "feed_ids": [1, 2], "distributors": ["theorchard"], } source_of_streams.get_sos_breakdown(permissions_filter, "isrc", ["theorchard"]) expected_sql = ( "product_id IN (SELECT product_id FROM dim_release " "WHERE subaccountid IN (:subaccount_ids)) " "AND feed_id IN (:feed_ids) " ) snowflake_fetch_assert(mock_snowflake_fetchone, expected_sql, expected_args) def test_get_dynamic_sos_breakdown_with_subaccount( mock_load_query, mock_dynamic_snowflake_fetchone, mock_store_availability ): """Test get_sos_with_breakdown with subaccount.""" permissions_filter = { "label_ids": None, "artist_ids": None, "subaccount_ids": [345], "feed_ids": [1, 2], } expected_args = { "label_ids": None, "artist_ids": None, "subaccount_ids": [345], "isrc": "isrc", "store_ids": [286], "start_date": "2019-11-01", "end_date": "2019-12-01", "country_codes": ["DE", "NO"], "feed_ids": [1, 2], "distributors": ["theorchard"], } source_of_streams.get_dynamic_sos_breakdown( permissions_filter, "isrc", ["theorchard"], ["DE", "NO"], [286, 712], "2019-11-01", "2019-12-01", ) mock_load_query.assert_called_with("source_of_streams_dynamic_by_country") expected_sql = ( "product_id IN (SELECT product_id FROM dim_release " "WHERE subaccountid IN (:subaccount_ids)) AND " "(download_activity_date BETWEEN :start_date AND :end_date) " "AND feed_id IN (:feed_ids) " ) snowflake_fetch_assert(mock_dynamic_snowflake_fetchone, expected_sql, expected_args) def test_get_sos_breakdown_with_empty_subaccount( mock_load_query, mock_snowflake_fetchone ): """Test get_sos_with_breakdown with empty subaccount.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, "feed_ids": [1, 2], } expected_args = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, "isrc": "isrc", "feed_ids": [1, 2], "distributors": ["theorchard"], } source_of_streams.get_sos_breakdown(permissions_filter, "isrc", ["theorchard"]) assert mock_snowflake_fetchone.call_count == 1 call, *_ = mock_snowflake_fetchone.call_args_list (_, args), *_ = call assert args == expected_args def test_get_dynamic_sos_breakdown_with_empty_subaccount( mock_load_query, mock_dynamic_snowflake_fetchone, mock_store_availability ): """Test get_sos_with_breakdown with empty subaccount.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, "feed_ids": [1, 2], } expected_args = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, "isrc": "isrc", "store_ids": [1], "start_date": None, "end_date": None, "country_codes": ["DE", "NO"], "feed_ids": [1, 2], "distributors": ["theorchard"], } source_of_streams.get_dynamic_sos_breakdown( permissions_filter, "isrc", ["theorchard"], ["DE", "NO"], [1, 721], None, None ) assert mock_dynamic_snowflake_fetchone.call_count == 1 call, *_ = mock_dynamic_snowflake_fetchone.call_args_list (_, args), *_ = call assert args == expected_args mock_load_query.assert_called_with("source_of_streams_dynamic_by_country") def test_get_sos_breakdown_with_artist_profile( mock_load_query, mock_snowflake_fetchone ): """Test get_sos_with_breakdown with empty subaccount.""" permissions_filter = { "label_ids": None, "subaccount_ids": None, "artist_ids": [917636, 1552280], "feed_ids": [1, 2], } expected_args = { "label_ids": None, "subaccount_ids": None, "artist_ids": [917636, 1552280], "isrc": "isrc", "feed_ids": [1, 2], "distributors": ["theorchard"], } source_of_streams.get_sos_breakdown(permissions_filter, "isrc", ["theorchard"]) assert mock_snowflake_fetchone.call_count == 1 call, *_ = mock_snowflake_fetchone.call_args_list (_, args), *_ = call assert args == expected_args def test_get_dynamic_sos_breakdown_with_artist_profile( mock_load_query, mock_dynamic_snowflake_fetchone, mock_store_availability ): """Test get_sos_with_breakdown with empty subaccount.""" permissions_filter = { "label_ids": None, "subaccount_ids": None, "artist_ids": [917636, 1552280], } expected_args = { "label_ids": None, "subaccount_ids": None, "artist_ids": [917636, 1552280], "isrc": "isrc", "store_ids": [1, 286], "start_date": "2019-11-01", "end_date": "2019-12-01", "distributors": ["theorchard"], } source_of_streams.get_dynamic_sos_breakdown( permissions_filter, "isrc", ["theorchard"], [], [], "2019-11-01", "2019-12-01" ) assert mock_dynamic_snowflake_fetchone.call_count == 1 call, *_ = mock_dynamic_snowflake_fetchone.call_args_list (_, args), *_ = call assert args == expected_args mock_load_query.assert_called_with("source_of_streams_dynamic")