"""Unit tests for the top sound_recordings logic layer.""" from decimal import Decimal from unittest.mock import patch import pytest from sound_recordings.logic import top_sound_recordings @pytest.fixture def mock_top_sound_recordings_model_payload(): """Mock payload for top sound_recordings model.""" return [ { "isrc": "USA561066580", "streams": 500000, "growth_percentage": Decimal("16"), }, { "isrc": "USJMZ1800003", "streams": 400000, "growth_percentage": Decimal("16.25"), }, ] @pytest.fixture def expected_payload(): """Return expected payload from logic.""" return { "items": [ { "isrc": "USA561066580", "streams": 500000, "growth_percentage": 16, }, { "isrc": "USJMZ1800003", "streams": 400000, "growth_percentage": 16.25, }, ] } @pytest.fixture def mock_get_top_sound_recordings_model_success( mock_top_sound_recordings_model_payload, ): """Mock successful get_top_sound_recordings model response.""" with patch( "sound_recordings.models.top_sound_recordings." "get_top_sound_recordings" ) as get_top_sound_recordings: get_top_sound_recordings.return_value = mock_top_sound_recordings_model_payload yield get_top_sound_recordings @pytest.fixture def mock_get_top_sound_recordings_model_failure(): """Mock failure get_top_sound_recordings model response.""" with patch( "sound_recordings.models.top_sound_recordings" ".get_top_sound_recordings" ) as get_top_sound_recordings: get_top_sound_recordings.return_value = [] yield get_top_sound_recordings class TestGetTopSoundRecordingsWithLabel: """Test get_top_sound_recordings with label.""" limit = 10 offset = 0 countries_empty = [] countries = ["DE", "NO", "US"] @pytest.fixture def top_sound_recordings_response( self, mock_get_top_sound_recordings_model_success, mock_permissions_for_label, request_context_for_label, ): """Run get_top_sound_recordings.""" return top_sound_recordings.get_top_sound_recordings( request_context_for_label, ["theorchard"], self.limit, self.offset, self.countries_empty, ) @pytest.fixture def top_sound_recordings_with_countries_response( self, mock_get_top_sound_recordings_model_success, mock_permissions_for_label, request_context_for_label, ): """Run get_top_sound_recordings.""" return top_sound_recordings.get_top_sound_recordings( request_context_for_label, ["theorchard"], self.limit, self.offset, self.countries, ) def test_succeeds( self, top_sound_recordings_response, top_sound_recordings_with_countries_response, expected_payload, ): """Test successful response.""" assert top_sound_recordings_response.status == 200 assert top_sound_recordings_response.message == expected_payload assert top_sound_recordings_with_countries_response.status == 200 assert top_sound_recordings_with_countries_response.message == expected_payload def test_get_top_sound_recordings_is_called( self, top_sound_recordings_response, mock_get_top_sound_recordings_model_success, mock_permissions_for_label, permissions_for_label, ): """Test top sound_recordings model is called with correct arguments.""" mock_get_top_sound_recordings_model_success.assert_called_once_with( permissions_for_label, ["theorchard"], self.limit, self.offset, self.countries_empty, "streams_7_days", ) def test_get_top_sound_recordings_with_countries_is_called( self, top_sound_recordings_with_countries_response, mock_get_top_sound_recordings_model_success, mock_permissions_for_label, permissions_for_label, ): """Test top sound_recordings model is called with correct arguments.""" mock_get_top_sound_recordings_model_success.assert_called_once_with( permissions_for_label, ["theorchard"], self.limit, self.offset, self.countries, "streams_7_days", ) def test_fails_when_top_sound_recordings_model_fails( self, mock_get_top_sound_recordings_model_failure, mock_permissions_for_label, request_context_for_label, expected_payload, ): """Test failure response when top sound_recording model fails.""" response = top_sound_recordings.get_top_sound_recordings( request_context_for_label, ["theorchard"], self.limit, self.offset, self.countries_empty, ) response_with_countries = top_sound_recordings.get_top_sound_recordings( request_context_for_label, ["theorchard"], self.limit, self.offset, self.countries, ) assert response.status == 200 assert response_with_countries.status == 200 expected_payload["items"] = [] assert response.message == expected_payload assert response_with_countries.message == expected_payload class TestGetTopSoundRecordingsWithSubaccount: """Test get_top_sound_recordings with subaccount.""" limit = 10 offset = 0 countries_empty = [] countries = ["DE", "NO", "US"] order_by_28_days = "streams_28_days" order_by_1_day = "streams_1_day" order_by_all_time = "streams_all_time" @pytest.fixture def top_sound_recordings_response( self, mock_get_top_sound_recordings_model_success, mock_permissions_for_subaccount, request_context_for_subaccount, ): """Run get_top_sound_recordings.""" return top_sound_recordings.get_top_sound_recordings( request_context_for_subaccount, ["theorchard"], self.limit, self.offset, self.countries_empty, self.order_by_28_days, ) @pytest.fixture def top_sound_recordings_all_time_response( self, mock_get_top_sound_recordings_model_success, mock_permissions_for_subaccount, request_context_for_subaccount, ): """Run get_top_sound_recordings.""" return top_sound_recordings.get_top_sound_recordings( request_context_for_subaccount, ["theorchard"], self.limit, self.offset, self.countries_empty, self.order_by_all_time, ) @pytest.fixture def top_sound_recordings_with_countries_response( self, mock_get_top_sound_recordings_model_success, mock_permissions_for_subaccount, request_context_for_subaccount, ): """Run get_top_sound_recordings.""" return top_sound_recordings.get_top_sound_recordings( request_context_for_subaccount, ["theorchard"], self.limit, self.offset, self.countries, self.order_by_all_time, ) @pytest.fixture def top_sound_recordings_with_countries_all_time_response( self, mock_get_top_sound_recordings_model_success, mock_permissions_for_subaccount, request_context_for_subaccount, ): """Run get_top_sound_recordings.""" return top_sound_recordings.get_top_sound_recordings( request_context_for_subaccount, ["theorchard"], self.limit, self.offset, self.countries, self.order_by_all_time, ) def test_succeeds( self, top_sound_recordings_response, top_sound_recordings_with_countries_response, expected_payload, ): """Test successful response.""" assert top_sound_recordings_response.status == 200 assert top_sound_recordings_response.message == expected_payload assert top_sound_recordings_with_countries_response.status == 200 assert top_sound_recordings_with_countries_response.message == expected_payload def test_get_top_sound_recordings_is_called( self, top_sound_recordings_response, mock_get_top_sound_recordings_model_success, mock_permissions_for_subaccount, permissions_for_subaccount, ): """Test top sound_recordings model is called with correct arguments.""" mock_get_top_sound_recordings_model_success.assert_called_once_with( permissions_for_subaccount, ["theorchard"], self.limit, self.offset, self.countries_empty, self.order_by_28_days, ) def test_get_top_sound_recordings_all_time_is_called( self, top_sound_recordings_all_time_response, mock_get_top_sound_recordings_model_success, mock_permissions_for_subaccount, permissions_for_subaccount, ): """Test top sound_recordings model is called with correct arguments.""" mock_get_top_sound_recordings_model_success.assert_called_once_with( permissions_for_subaccount, ["theorchard"], self.limit, self.offset, self.countries_empty, self.order_by_all_time, ) def test_get_top_sound_recordings_with_countries_is_called( self, top_sound_recordings_with_countries_response, mock_get_top_sound_recordings_model_success, mock_permissions_for_subaccount, permissions_for_subaccount, ): """Test top sound_recordings model is called with correct arguments.""" mock_get_top_sound_recordings_model_success.assert_called_once_with( permissions_for_subaccount, ["theorchard"], self.limit, self.offset, self.countries, self.order_by_all_time, ) def test_get_top_sound_recordings_with_countries_all_time_is_called( self, top_sound_recordings_with_countries_all_time_response, mock_get_top_sound_recordings_model_success, mock_permissions_for_subaccount, permissions_for_subaccount, ): """Test top sound_recordings model is called with correct arguments.""" mock_get_top_sound_recordings_model_success.assert_called_once_with( permissions_for_subaccount, ["theorchard"], self.limit, self.offset, self.countries, self.order_by_all_time, ) def test_fails_when_top_sound_recordings_model_fails( self, mock_get_top_sound_recordings_model_failure, mock_permissions_for_subaccount, request_context_for_subaccount, expected_payload, ): """Test failure response when top sound_recording model fails.""" response = top_sound_recordings.get_top_sound_recordings( request_context_for_subaccount, ["theorchard"], self.limit, self.offset, self.countries_empty, ) response_with_countries = top_sound_recordings.get_top_sound_recordings( request_context_for_subaccount, ["theorchard"], self.limit, self.offset, self.countries, ) assert response.status == 200 assert response_with_countries.status == 200 expected_payload["items"] = [] assert response.message == expected_payload assert response_with_countries.message == expected_payload