"""Tests for Track Utility functions.""" from oto import response from ows_product_physical.utils import track_utils def test_map_track_length(): """Test minutes and seconds mapped into dict with formatted length data.""" mapped_length = track_utils._map_track_length(*(73, 42)) assert mapped_length == { 'formatted': '01:13:42', 'hours': 1, 'minutes': 13, 'seconds': 42 } def test_map_track_length_with_zero(): """Test minutes and seconds mapped into dict with formatted length data.""" mapped_length = track_utils._map_track_length(1, 0) assert mapped_length == { 'formatted': '00:01:00', 'hours': 0, 'minutes': 1, 'seconds': 0 } def test_map_track_length_with_none(): """Assert length set to None if no track length data present.""" mapped_length = track_utils._map_track_length(*(None, None)) assert mapped_length is None def test_fake_pagination(): """Assert dict returned with items and pagination information.""" tracks = ['a track', 'another track'] pagination = track_utils.fake_pagination(tracks) assert pagination == { 'items': tracks, 'pagination': { 'type': 'standard', 'offset': 0, 'limit': 2, 'total_records': 2 } } def test_format_length_data_no_data(): """Assert dict returned with minutes and seconds set to None.""" length = track_utils.format_length_data() assert length == { 'length_minute': None, 'length_seconds': None } def test_format_length_data(): """Assert dict returned with minutes and seconds set to None.""" length = track_utils.format_length_data('5:10:15') assert length == { 'length_minute': 310, 'length_seconds': 15 } def test_map_track(): """Assert dict returned with track metadata mapped from track row.""" track_item = ( 1, 1, 'Track Name', 'SideB', 1234, 1, 73, 42, 'Composition', 'Y', 'Billy,Joe', 'Joe,Mills', 'Smith,Doe') mapped_track = track_utils.map_track(track_item) assert mapped_track == { 'track_id': 1, 'track_number': 1, 'track_name': 'Track Name', 'side': 'SideB', 'isrc': 1234, 'disc': 1, 'song_writers': ['Billy', 'Joe'], 'length': { 'formatted': '01:13:42', 'hours': 1, 'minutes': 13, 'seconds': 42 }, 'performer': ['Joe,Mills'], 'us_publishing_obligation': 'Composition', 'third_party_publisher': 'Y', 'publisher_names': ['Smith', 'Doe'] } def test_map_track_no_performers(): """Assert dict returned with track metadata mapped from track row.""" track_item = ( 1, 1, 'Track Name', 'SideA', 1234, 1, 73, 42, None, 'N', 'Billy,Joe', '', 'Smith,Doe') mapped_track = track_utils.map_track(track_item) assert mapped_track == { 'track_id': 1, 'track_number': 1, 'track_name': 'Track Name', 'side': 'SideA', 'isrc': 1234, 'disc': 1, 'song_writers': ['Billy', 'Joe'], 'length': { 'formatted': '01:13:42', 'hours': 1, 'minutes': 13, 'seconds': 42 }, 'performer': [], 'us_publishing_obligation': None, 'third_party_publisher': 'N', 'publisher_names': ['Smith', 'Doe'] } def test_map_track_no_publishers(): """Assert dict returned with track metadata mapped from track row.""" track_item = ( 1, 1, 'Track Name', None, 1234, 1, 73, 42, None, 'N', '', 'Joe,Mills', '') mapped_track = track_utils.map_track(track_item) assert mapped_track == { 'track_id': 1, 'track_number': 1, 'track_name': 'Track Name', 'isrc': 1234, 'side': None, 'disc': 1, 'song_writers': [], 'length': { 'formatted': '01:13:42', 'hours': 1, 'minutes': 13, 'seconds': 42 }, 'performer': ['Joe,Mills'], 'us_publishing_obligation': None, 'third_party_publisher': 'N', 'publisher_names': [] } def test_map_tracks_message_to_fields_for_copy(): """Test that items are correctly converted.""" tracks_response = response.Response(message={ 'items': [ { 'track_id': 42, 'track_number': 1, 'track_name': 'First track', 'isrc': None, 'side': 'A', 'disc': 1, 'length': None, 'performer': ['Sonny'], 'us_publishing_obligation': 'Composition', 'third_party_publisher': 'Y', 'publisher_names': ['Brown', 'Grey'], 'song_writers': ['Bob Dylan', 'Jackson Browne'] }, { 'track_id': 43, 'track_number': 2, 'track_name': 'Second track', 'isrc': 'abc123', 'side': 'A', 'disc': 1, 'length': { 'formatted': '01:13:42', 'hours': 1, 'minutes': 13, 'seconds': 42 }, 'performer': ['Sonny, Cher'], 'us_publishing_obligation': 'Composition', 'third_party_publisher': 'Y', 'publisher_names': ['Richard', 'Don'], 'song_writers': ['Lita Ford', 'James Hetfield'] }, ] }) fields = track_utils.map_tracks_response_to_fields_for_copy( tracks_response) assert fields == [ {'track_number': 1, 'track_name': 'First track', 'isrc': None, 'side': 'A', 'disc': 1, 'length': None, 'performer': ['Sonny'], 'us_publishing_obligation': 'Composition', 'third_party_publisher': 'Y', 'publisher_names': ['Brown', 'Grey'], 'song_writers': ['Bob Dylan', 'Jackson Browne']}, {'track_number': 2, 'track_name': 'Second track', 'isrc': 'abc123', 'side': 'A', 'disc': 1, 'length': '01:13:42', 'performer': ['Sonny, Cher'], 'us_publishing_obligation': 'Composition', 'third_party_publisher': 'Y', 'publisher_names': ['Richard', 'Don'], 'song_writers': ['Lita Ford', 'James Hetfield'] }] def test_map_track_publishing_obligation(): """Assert dict returned with publishing_obligation metadata mapped.""" publishing_obligation_item = ( 1, 3, 'Awesome', 'Composition', 'Y', 'First,Second') mapped_publishing_obligation = track_utils.map_track_publishing_obligation( publishing_obligation_item) assert mapped_publishing_obligation == { 'track_id': 1, 'track_number': 3, 'track_name': 'Awesome', 'us_publishing_obligation': 'Composition', 'third_party_publisher': 'Y', 'publisher_names': ['First', 'Second'] } def test_map_track_publishing_obligation_no_publisher_names(): """Assert dict returned with publishing_obligation metadata mapped.""" publishing_obligation_item = (1, 4, 'Mega Hit', 'Composition', 'Y', '') mapped_publishing_obligation = track_utils.map_track_publishing_obligation( publishing_obligation_item) assert mapped_publishing_obligation == { 'track_id': 1, 'track_number': 4, 'track_name': 'Mega Hit', 'us_publishing_obligation': 'Composition', 'third_party_publisher': 'Y', 'publisher_names': [] }