"""Test format artists logic.""" from content_utils.exceptions import IneligibleEventError import pytest from src.logic import format_artists def test_get_current_product_data_success(request_engine): """Test get current product data success.""" request_engine['ows-product-digital'].add_spec( 'GET', '/product/audio/123', response={'foo': 'bar'} ) actual = format_artists.get_current_product_data(123) assert actual == {'foo': 'bar'} def test_get_current_product_data_not_found(request_engine): """Test get current product data not found.""" request_engine['ows-product-digital'].add_spec( 'GET', '/product/audio/123', status=404, response={'foo': 'bar'} ) with pytest.raises(IneligibleEventError) as e: format_artists.get_current_product_data(123) assert e.value.args[0] == 'Release not found.' def test_get_current_product_data_error(request_engine): """Test get current product data error.""" request_engine['ows-product-digital'].add_spec( 'GET', '/product/audio/123', status=500, response='service error' ) with pytest.raises(Exception) as e: format_artists.get_current_product_data(123) assert e.value.args[0] == 'ows-product-digital service error.' assert e.value.args[1] == '"service error"' def test_format_artists_no_changes(): """Test format artists returns with no artist changes.""" assert format_artists.format_artists(1, {}, {}) is None def test_format_artists(request_engine, mock_product_response): """Test format artists.""" request_engine['ows-product-digital'].add_spec( 'GET', '/product/audio/123', response=mock_product_response ) mock_artist_changes = { 'performer': [{'role': 'performer', 'name': 'foo'}] } actual = format_artists.format_artists(123, {}, mock_artist_changes) assert actual == [ { 'role': 'performer', 'name': 'foo' }, { 'role': 'featuring', 'name': 'test3', 'id': 9874434, 'artist_info_id': 3144046 }, { 'role': 'featuring', 'name': 'test4', 'id': 9874435, 'artist_info_id': 3144047 }, { 'role': 'remixer', 'name': 'remixer1', 'id': 9874436, 'artist_info_id': None }, { 'role': 'remixer', 'name': 'remixer2', 'id': 9874437, 'artist_info_id': None }, { 'role': 'producer', 'name': 'producer1', 'id': 9874438, 'artist_info_id': None }, { 'role': 'producer', 'name': 'producer 2', 'id': 9874439, 'artist_info_id': None } ] def test_format_artist_uses_revised_genre_id(request_engine, mock_product_response): """Test format artist uses revised genre id.""" request_engine['ows-product-digital'].add_spec( 'GET', '/product/audio/123', response=mock_product_response ) mock_artist_changes = { 'performer': [{'role': 'performer', 'name': 'foo'}], 'composer': [{'role': 'composer', 'name': 'bar'}], 'conductor': [{'role': 'conductor', 'name': 'baz'}] } actual = format_artists.format_artists(123, {'genre_id': 12}, mock_artist_changes) expected = [ { 'role': 'conductor', 'name': 'baz' }, { 'role': 'composer', 'name': 'bar' }, { 'role': 'performer', 'name': 'foo' }, { 'role': 'remixer', 'name': 'remixer1', 'id': 9874436, 'artist_info_id': None }, { 'role': 'remixer', 'name': 'remixer2', 'id': 9874437, 'artist_info_id': None } ] _compare_lists(actual, expected) def test_format_artist_uses_revised_subgenre_id(request_engine, mock_product_response): """Test format artist uses revised subgenre id.""" request_engine['ows-product-digital'].add_spec( 'GET', '/product/audio/123', response=mock_product_response ) mock_artist_changes = { 'performer': [{'role': 'performer', 'name': 'foo'}], 'composer': [{'role': 'composer', 'name': 'bar'}] } actual = format_artists.format_artists(123, {'subgenre_id': 546}, mock_artist_changes) expected = [ { 'role': 'performer', 'name': 'foo' }, { 'role': 'composer', 'name': 'bar' }, { 'role': 'featuring', 'name': 'test3', 'id': 9874434, 'artist_info_id': 3144046 }, { 'role': 'featuring', 'name': 'test4', 'id': 9874435, 'artist_info_id': 3144047 }, { 'role': 'remixer', 'name': 'remixer1', 'id': 9874436, 'artist_info_id': None }, { 'role': 'remixer', 'name': 'remixer2', 'id': 9874437, 'artist_info_id': None }, { 'role': 'producer', 'name': 'producer1', 'id': 9874438, 'artist_info_id': None }, { 'role': 'producer', 'name': 'producer 2', 'id': 9874439, 'artist_info_id': None } ] _compare_lists(actual, expected) def _compare_lists(actual, expected): assert len(actual) == len(expected) for item in actual: assert item in expected