"""Tests for product utils.""" from collections import defaultdict from backend.constants import artist_role from backend.utils import product_utils def test_get_product_featuring_artist_corrections( track_release_correction_details): """Verify featuring artist corrections for release are fetched.""" product = {'corrections': track_release_correction_details} feat_corrections = product_utils.get_product_featuring_artist_corrections(product) assert feat_corrections == \ defaultdict(dict, {2664741: {'featuring': [{'artist_name': 'test feat', 'role': 'featuring'}]}}) def test_get_product_artist_correction_names(): """Verify artist correction names for release are fetched.""" corrs = defaultdict(dict, {2664741: {'featuring': [{'artist_name': 'test feat', 'role': 'featuring'}]}}) names = product_utils.get_product_artist_correction_names(corrs, 2664741) assert names == ['test feat'] def test_get_featuring_artists(): """Verify featuring artist names for release are fetched.""" artists = [{'role': 'featuring', 'name': 'test', 'id': 1322313, 'artist_info_id': 1334566}] feat_artists = product_utils.get_featuring_artists(artists, 'role') assert feat_artists == ['test'] def test_get_artist_by_key(): """Verify artists from release with the given key are fetched.""" artists = [{'role': 'composer', 'name': 'test', 'id': 1322313, 'artist_info_id': 1334566}] fetched_artists = product_utils.get_artists_by_key('composer', artists, 'role') assert fetched_artists == ['test'] def test_get_product_artist_corrections( track_release_correction_details): """Verify artist corrections for release are fetched.""" product = {'corrections': track_release_correction_details} artist_corrections = product_utils.get_product_artist_corrections_for_key(artist_role.COMPOSER, product) assert artist_corrections == \ defaultdict(dict, {2664741: {'composer': [{'artist_name': 'test composer', 'role': 'composer'}]}}) def test_get_track_corrections(): """Verify track_artist corrections are properly separated between primary_artist and composer.""" digital_product = { "corrections": { "release_correction_id": 9990000, "release_id": 9990000, "status": "active", "items": [ { "release_correction_detail_id": 9990000, "table_name": "track", "field_name": "track_artist", "key_id": 22244770, "key_value": [ {"name": "performer1", "type": "performer"}, {"name": "performer2", "type": "performer"}, {"name": "composer1", "type": "composer"}, {"name": "composer2", "type": "composer"}, {"name": "orchestra1", "type": "orchestra"}, {"name": "orchestra2", "type": "orchestra"}, {"name": "conductor1", "type": "conductor"}, {"name": "conductor2", "type": "conductor"}, {"name": "ensemble1", "type": "ensemble"}, {"name": "ensemble2", "type": "ensemble"}, ], } ], }, "release_approval_status": "approved", } track_corrections = product_utils.get_track_corrections(digital_product) assert track_corrections == { 22244770: { 'primary_artist': [ {'name': 'performer1', 'type': 'performer'}, {'name': 'performer2', 'type': 'performer'} ], 'composer': [ {'name': 'composer1', 'type': 'composer'}, {'name': 'composer2', 'type': 'composer'} ], 'orchestra': [ {'name': 'orchestra1', 'type': 'orchestra'}, {'name': 'orchestra2', 'type': 'orchestra'} ], 'conductor': [ {'name': 'conductor1', 'type': 'conductor'}, {'name': 'conductor2', 'type': 'conductor'} ], 'ensemble': [ {'name': 'ensemble1', 'type': 'ensemble'}, {'name': 'ensemble2', 'type': 'ensemble'} ] } }