"""Unit tests for sound recording streams_by_sos logic layer.""" import datetime from unittest.mock import patch import pytest from sound_recordings.logic import streams_by_sos as streams_by_sos @pytest.fixture def mock_get_streams_by_source_of_streams_model_payload(): """Mock payload for sound recording streams_by_sos model.""" return [ {"source": "active", "date": datetime.date(2018, 6, 28), "streams": 500}, {"source": "passive", "date": datetime.date(2018, 6, 28), "streams": 3000}, {"source": "collection", "date": datetime.date(2018, 6, 28), "streams": 3000}, {"source": "active", "date": datetime.date(2018, 6, 29), "streams": 2000}, {"source": "unknown", "date": datetime.date(2018, 6, 28), "streams": 1000}, {"source": "passive", "date": datetime.date(2018, 6, 29), "streams": 1000}, ] @pytest.fixture def expected_payload(): """Return expected payload from streams_by_sos logic.""" return { "isrc": "isrc", "source_of_streams": [ { "source": "active", "items": [ {"streams": 500, "date": "2018-06-28T00:00:00Z"}, {"streams": 2000, "date": "2018-06-29T00:00:00Z"}, ], }, { "source": "passive", "items": [ {"streams": 3000, "date": "2018-06-28T00:00:00Z"}, {"streams": 1000, "date": "2018-06-29T00:00:00Z"}, ], }, { "source": "collection", "items": [{"streams": 3000, "date": "2018-06-28T00:00:00Z"}], }, { "source": "unknown", "items": [{"streams": 1000, "date": "2018-06-28T00:00:00Z"}], }, ], } @pytest.fixture def mock_get_streams_by_sos_model_success( mock_get_streams_by_source_of_streams_model_payload, ): """Mock successful get_streams_by_source_of_streams model response.""" with patch( "sound_recordings.models.streams_by_sos." "get_streams_by_source_of_streams" ) as get_streams_by_sos: get_streams_by_sos.return_value = ( mock_get_streams_by_source_of_streams_model_payload ) yield get_streams_by_sos @pytest.fixture def mock_get_streams_by_sos_model_failure(): """Mock failure get_streams_by_source_of_streams model response.""" with patch( "sound_recordings.models.streams_by_sos." "get_streams_by_source_of_streams" ) as get_streams_by_sos: get_streams_by_sos.return_value = [] yield get_streams_by_sos class TestGetStreamsBySosWithLabel: """Test get_streams_by_source_of_streams with label.""" isrc = "isrc" start_date = datetime.date(2018, 6, 28) end_date = datetime.date(2018, 6, 29) @pytest.fixture def response( self, mock_get_streams_by_sos_model_success, request_context_for_label, mock_permissions_for_label, ): """Run get_streams_by_source_of_streams.""" return streams_by_sos.get_streams_by_source_of_streams( request_context_for_label, self.isrc, ["theorchard"], [], [], self.start_date, self.end_date, ) def test_succeeds(self, response, expected_payload): """Test successful response.""" assert response.status == 200 # Sorted the response and expected_payload as set() changes the order # of the `all_sources` list every time assert sorted(response.message) == sorted(expected_payload) def test_get_streams_by_sos_model_is_called( self, response, mock_get_streams_by_sos_model_success, permissions_for_label ): """Test get_streams_by_source_of_streams model is called.""" mock_get_streams_by_sos_model_success.assert_called_once_with( permissions_for_label, self.isrc, ["theorchard"], [], [], self.start_date, self.end_date, ) def test_fails_when_streams_by_sos_model_fails( self, mock_get_streams_by_sos_model_failure, mock_permissions_for_label, request_context_for_label, ): """Test failure response when streams_by_sos model fails.""" response = streams_by_sos.get_streams_by_source_of_streams( request_context_for_label, self.isrc, ["theorchard"], [], [], self.start_date, self.end_date, ) assert response.status == 200 assert response.message == {"isrc": self.isrc, "source_of_streams": []} class TestGetStreamsBySosWithSubaccount: """Test get_streams_by_source_of_streams with subaccount.""" isrc = "isrc" start_date = datetime.date(2018, 6, 28) end_date = datetime.date(2018, 6, 29) @pytest.fixture def response( self, mock_get_streams_by_sos_model_success, request_context_for_subaccount, mock_permissions_for_subaccount, ): """Run get_streams_by_source_of_streams.""" return streams_by_sos.get_streams_by_source_of_streams( request_context_for_subaccount, self.isrc, ["theorchard"], [], [], self.start_date, self.end_date, ) def test_succeeds(self, response, expected_payload): """Test successful response.""" assert response.status == 200 # Sorted the response and expected_payload as set() changes the order # of the `all_sources` list every time assert sorted(response.message) == sorted(expected_payload) def test_get_streams_by_sos_model_is_called( self, response, mock_get_streams_by_sos_model_success, permissions_for_subaccount, ): """Test get_streams_by_source_of_streams model is called.""" mock_get_streams_by_sos_model_success.assert_called_once_with( permissions_for_subaccount, self.isrc, ["theorchard"], [], [], self.start_date, self.end_date, ) def test_fails_when_streams_by_sos_model_fails( self, mock_get_streams_by_sos_model_failure, mock_permissions_for_subaccount, request_context_for_subaccount, ): """Test failure response when streams_by_sos model fails.""" response = streams_by_sos.get_streams_by_source_of_streams( request_context_for_subaccount, self.isrc, ["theorchard"], [], [], self.start_date, self.end_date, ) assert response.status == 200 assert response.message == {"isrc": self.isrc, "source_of_streams": []}