"""Tests for ows_track model.""" import pytest from src.constants import urls from src.models import ows_track def test_get_track_artist_info( mocker, test_track_id, track_info_success_response, get_track_info_fixture ): """Test get track info from ows-track successfully.""" mocker.patch("requests.get", return_value=track_info_success_response) result = ows_track.get_track_artist_info(test_track_id) assert result == get_track_info_fixture def test_get_track_info_not_found( mocker, ows_not_found_response, test_track_id, get_track_info_not_found_fixture ): """Test get track info from ows-track not found.""" mocker.patch("requests.get", return_value=ows_not_found_response) result = ows_track.get_track_artist_info(test_track_id) assert result == get_track_info_not_found_fixture def test_get_track_info_error(mocker, ows_error_response, test_track_id): """Test get track info from ows-track error.""" mocker.patch("requests.get", return_value=ows_error_response) with pytest.raises(Exception, match=urls.OWS_TRACK_ERROR): ows_track.get_track_artist_info(test_track_id) def test_get_track_info_retry_on_error(mocker, ows_error_response, test_track_id): """Test get track info with 2 retries.""" request = mocker.patch("requests.get", return_value=ows_error_response) try: ows_track.get_track_artist_info(test_track_id) except Exception as error: assert len(request.mock_calls) == 2 assert str(error) == urls.OWS_TRACK_ERROR def test_get_all_tracks_by_product_id( mocker, test_release_id, tracks_by_product_id_success_response, get_tracks_by_product_id_fixture ): """Test get tracks from product id from ows-track successfully.""" mocker.patch("requests.get", return_value=tracks_by_product_id_success_response) result = ows_track.get_all_tracks_by_product_id(test_release_id) assert len(result) == 2 assert result == get_tracks_by_product_id_fixture def test_get_all_tracks_not_found(mocker, ows_not_found_response, test_release_id): """Test get tracks from product id from ows-track not found.""" mocker.patch("requests.get", return_value=ows_not_found_response) result = ows_track.get_all_tracks_by_product_id(test_release_id) assert result == [] def test_get_all_tracks_error(mocker, ows_error_response, test_release_id): """Test get tracks from product id from ows-track error.""" mocker.patch("requests.get", return_value=ows_error_response) with pytest.raises(Exception, match=urls.OWS_TRACK_ERROR): ows_track.get_all_tracks_by_product_id(test_release_id) def test_get_all_tracks_retry_on_error(mocker, ows_error_response, test_release_id): """Test get tracks from product id with 2 retries.""" request = mocker.patch("requests.get", return_value=ows_error_response) try: ows_track.get_all_tracks_by_product_id(test_release_id) except Exception as error: assert len(request.mock_calls) == 2 assert str(error) == urls.OWS_TRACK_ERROR def test_get_track_by_tuid( mocker, test_track_id, track_info_success_response, get_track_by_tuid_fixture ): """Test get_track_by_tuid successfully with performer artist.""" mocker.patch("requests.get", return_value=track_info_success_response) result = ows_track.get_track_by_tuid(test_track_id) assert result == get_track_by_tuid_fixture def test_get_track_by_tuid_no_performer(mocker, test_track_id, track_info_fixture): """Test get_track_by_tuid when no performer artist exists.""" track_info_fixture["artists"] = [ {"type": "featuring", "track_artist_id": 81151700, "name": "Some Artist"} ] response = mocker.MagicMock() response.status_code = 200 response.json.return_value = track_info_fixture mocker.patch("requests.get", return_value=response) result = ows_track.get_track_by_tuid(test_track_id) assert result["artist_id"] == 0 assert result["artist_name"] == "" def test_get_track_by_tuid_not_found(mocker, ows_not_found_response, test_track_id): """Test get_track_by_tuid when track is not found.""" mocker.patch("requests.get", return_value=ows_not_found_response) result = ows_track.get_track_by_tuid(test_track_id) assert result == {} def test_get_track_by_tuid_error(mocker, ows_error_response, test_track_id): """Test get_track_by_tuid raises on error response.""" mocker.patch("requests.get", return_value=ows_error_response) with pytest.raises(Exception, match=urls.OWS_TRACK_ERROR): ows_track.get_track_by_tuid(test_track_id) def test_get_track_by_tuid_retry_on_error(mocker, ows_error_response, test_track_id): """Test get_track_by_tuid retries on error response.""" request = mocker.patch("requests.get", return_value=ows_error_response) try: ows_track.get_track_by_tuid(test_track_id) except Exception as error: assert len(request.mock_calls) == 2 assert str(error) == urls.OWS_TRACK_ERROR