"""Tests for validation utils.""" from oto import response import pytest from backend.constants import error from backend.utils import api as api_utils from backend.utils import validation as validation_utils from tests.testutils.constants import TEST_INVALID_P_INFOS_LIST @pytest.mark.parametrize('p_info', TEST_INVALID_P_INFOS_LIST) def test_invalid_pinfos(p_info): """Test is_pinfo returns False with invalid p_info values.""" assert not validation_utils.is_pinfo(p_info) @pytest.mark.parametrize('p_info', ['2017 Yeah', '2016 ¿Aú?', '2010 X Y Z']) def test_valid_pinfos(p_info): """Test is_pinfo returns True with valid p_info values.""" assert validation_utils.is_pinfo(p_info) @pytest.mark.parametrize('meta_language_code, expected_result', [ ('ENG', True), ('cmn-Hanz', True), (None, False), ('', False), ('eng', False), ('XY', False), ('ABCD', False), ('X12', False), ('Eng', False), ('cmn-', False), ('N/A', True), ('cmn', True), ('zh', True) ]) def test_is_meta_language_code_format( meta_language_code, expected_result): """Test is_valid_artist_name returns a correct response.""" assert validation_utils.is_meta_language_code_format( meta_language_code) == expected_result @pytest.mark.parametrize('name, expected_result', [ ('Limp Bizkit', True), ('Papa Roach', True), ('Cypress Hill', True), ('Various Artists', False), ('Verschillende artiesten', False), ('Vários intérpretes', False), ('V.A.', False), (' V.A. ', False), ('various artists', False), ('v.a.', False), ('Various artists', False) ]) def test_is_valid_artist_name(name, expected_result): """Test is_valid_artist_name returns a correct response.""" assert validation_utils.is_valid_artist_name(name) == expected_result @pytest.mark.parametrize('name, expected_result', [ ('Randy Butternubs', False), ('', True), (' ', True) ]) def test_is_empty_artist_name(name, expected_result): """Test is_artist_empty returns a correct response.""" assert validation_utils.is_artist_name_empty(name) == expected_result def invalid_artists_response(artist_name): """Helper function to create an invalid artist name response.""" return api_utils.create_validation_error_response( message=error.INVALID_ARTIST_NAME_ERROR_MSG.format(artist_name)) @pytest.mark.parametrize('artist_names, expected_response', [ (['Limp Bizkit', 'Papa Roach'], response.Response()), (['Various Artists'], invalid_artists_response('Various Artists')), (['Vários intérpretes', 'Lady Gaga'], invalid_artists_response('Vários intérpretes')), (['various artists'], invalid_artists_response('various artists')), ]) def test_validate_artist_names(artist_names, expected_response): """Test valida_artist_names returns a correct response.""" response = validation_utils.validate_artist_names(artist_names) assert response.message == expected_response.message assert response.errors == expected_response.errors assert response.status == expected_response.status def test_transform_to_performers_tuple(): """Test that a list of artists is transformed to a tuple.""" track_artists = [ {'type': 'performer', 'track_artist_id': 123, 'name': 'Some Artist'}, {'type': 'performer', 'track_artist_id': 456, 'name': 'An Artist'}, {'type': 'featuring', 'track_artist_id': 999, 'name': 'The Artist'}] expected_result = ('An Artist', 'Some Artist',) result = validation_utils.transform_to_performers_tuple(track_artists) assert result == expected_result @pytest.mark.parametrize('artist_type, expected_result', [ ('orchestra', False), ('ensemble', False), ('conductor', False), ('composer', True), ('performer', True), ('remixer', False) ]) def test_is_artist_required(artist_type, expected_result): """Test is_artist_required returns a correct response.""" assert validation_utils.is_artist_required(artist_type) == expected_result