import utils.formatters as formatters import pandas as pd from pandas import testing as tm import math class TestFormatters: def test_format_duration(self): assert formatters.format_duration(30) == '00:00:30', 'Formats duration in seconds correctly' assert formatters.format_duration(305) == '00:05:05', 'Formats duration in minutes correctly' assert formatters.format_duration(5405) == '01:30:05', 'Formats duration in hours correctly' assert formatters.format_duration(305.9) == '00:05:06', 'Formats duration in hours correctly' assert formatters.format_duration(5405.2) == '01:30:05', 'Formats duration in hours correctly' assert formatters.format_duration('00:03:20') == '00:03:20', 'Returns already-formatted duration' def test_format_date(self): mock_date_df = pd.DataFrame(data={'Dates': ['01-02-23', '01-January-21', '05-Nov-20', '07-Mar-2023']}) expected_output = pd.DataFrame(data={'Dates': ['01-Feb-2023', '01-Jan-2021', '05-Nov-2020', '07-Mar-2023']}) tm.assert_series_equal(formatters.format_date(mock_date_df.loc[:,'Dates']), expected_output.loc[:,'Dates']), 'Formats date column values correctly' def test_format_dataframe(self): mock_df = pd.DataFrame(data={ 'Product Type': [math.nan], 'Sales Start Date': ['01-01-2020'], 'Collection Ownership Start Date': ['02-Feb-1999'], 'Collection Ownership End Date': ['02-02-2003'], 'Duration': [300], 'Disc Number': [math.nan], 'Track Number': [math.nan], 'Contributor Legal Name': ['David Randolph | WILLOW'], 'Contributor Type': ['Featured'], 'Contributor Role(s)': ['Piano | Guitar'], 'Collection Ownership Percentage of Rights': [math.nan], 'Collection Ownership Country': ['US'], 'Collection Ownership Country - Exclusion': ['#NO-TERRITORY-REQUIRED'], 'Country of Label/Individual/Party Which Funded Recording': ['US'], 'Country of Recording': ['DE'], 'Explicit': ['EXPLICIT'], 'Product Primary Artists': ['Elton John | WILLOW'], 'Product Featuring Artists': [''], 'Primary Artist': ['Elton John, WILLOW'], 'Featuring Artist': [''] }) expected_output = pd.DataFrame(data={ 'Product Type': ['Audio'], 'Sales Start Date': ['01-Jan-2020'], 'Collection Ownership Start Date': ['02-Feb-1999'], 'Collection Ownership End Date': ['02-Feb-2003'], 'Duration': ['00:05:00'], 'Disc Number': '1', 'Track Number': '1', 'Contributor Legal Name': ['Randolph, David|WILLOW'], 'Contributor Type': ['Main Performer'], 'Contributor Role(s)': ['Piano|Guitar'], 'Collection Ownership Percentage of Rights': [100], 'Collection Ownership Country': ['US'], 'Collection Ownership Country - Exclusion': [''], 'Country of Label/Individual/Party Which Funded Recording': ['US'], 'Country of Recording': ['DE'], 'Explicit': ['Yes'], 'Product Primary Artists': ['Elton John|WILLOW'], 'Product Featuring Artists': [''], 'Primary Artist': ['Elton John|WILLOW'], 'Featuring Artist': [''] }) tm.assert_frame_equal(formatters.format_dataframe(mock_df), expected_output, check_dtype=False), 'Formats column values correctly' def test_format_contributor_names(self): valid_row = 'David Randolph | Elton Michael John' single_name = 'WILLOW' multi_middle_names = 'David Randolph | WILLOW | Elton Michael Andrew John' row_with_pipe = 'David Randolph | Elton Michael John |' pipe_row_only = '|' multi_pipes = 'David Randolph | | Elton Michael John | |' assert formatters.format_contributor_names(valid_row) == 'Randolph, David|John, Elton Michael' assert formatters.format_contributor_names(math.nan) == '' assert formatters.format_contributor_names('') == '' assert formatters.format_contributor_names(single_name) == 'WILLOW' assert formatters.format_contributor_names(multi_middle_names) == 'Randolph, David|WILLOW|John, Elton Michael Andrew' assert formatters.format_contributor_names(row_with_pipe) == 'Randolph, David|John, Elton Michael' assert formatters.format_contributor_names(multi_pipes) == 'Randolph, David|John, Elton Michael' assert formatters.format_contributor_names(pipe_row_only) == '' def test_format_participant_names(self): valid_row = 'David Randolph | Elton Michael John' single_name = 'WILLOW' comma_row = 'David Randolph, WILLOW,Elton Michael Andrew John' row_with_pipe = 'David Randolph | Elton Michael John |' pipe_row_only = '|' multi_pipes = 'David Randolph | | Elton Michael John | |' ampersand_names = 'David Randolph, WILLOW, & Elton Michael John' assert formatters.format_participant_names(valid_row) == 'David Randolph|Elton Michael John' assert formatters.format_participant_names(math.nan) == '' assert formatters.format_participant_names('') == '' assert formatters.format_participant_names(single_name) == 'WILLOW' assert formatters.format_participant_names(comma_row) == 'David Randolph|WILLOW|Elton Michael Andrew John' assert formatters.format_participant_names(row_with_pipe) == 'David Randolph|Elton Michael John' assert formatters.format_participant_names(multi_pipes) == 'David Randolph|Elton Michael John' assert formatters.format_participant_names(pipe_row_only) == '' assert formatters.format_participant_names(ampersand_names) == 'David Randolph|WILLOW|Elton Michael John' def test_format_contributor_types(self): valid_types = 'Featured | Main Performers | Artist | Other Featured | Non-Featured Artist' types_with_pipes = 'Featured | | Featured |' assert formatters.format_contributor_types(valid_types) == 'Main Performer|Main Performer|Main Performer|Featuring Performer|Session Musician' assert formatters.format_contributor_types(types_with_pipes) == 'Main Performer|Main Performer' assert formatters.format_contributor_types(math.nan) == '' assert formatters.format_contributor_types('') == '' def test_format_piped_values(self): assert formatters.format_piped_values('US |') == 'US' assert formatters.format_piped_values('Piano | Bass Guitar') == 'Piano|Bass Guitar' assert formatters.format_piped_values('Piano | Guitar | ') == 'Piano|Guitar' assert formatters.format_piped_values('Piano | | Vocals') == 'Piano|Vocals' assert formatters.format_piped_values(math.nan) == '' assert formatters.format_piped_values('') == '' def test_strip_all_strings(self): mock_df = pd.DataFrame(data={ 'Song Name': ['Some Song Name '], 'Primary Artist': [' A Primary Artist '], 'Duration': [300], 'Explicit': [math.nan] }) mock_expected_result = pd.DataFrame(data={ 'Song Name': ['Some Song Name'], 'Primary Artist': ['A Primary Artist'], 'Duration': [300], 'Explicit': [math.nan] }) tm.assert_frame_equal(formatters.strip_all_strings(mock_df), mock_expected_result), 'All string values are trimmed' def test_format_video_dataframe(self): mock_df = pd.DataFrame(data={ 'Product Type': [math.nan], 'Sales Start Date': ['01-01-2020'], 'Collection Ownership Start Date': ['02-Feb-1999'], 'Collection Ownership End Date': ['02-02-2003'], 'Duration': [300], 'Contributor Legal Name': ['David Randolph | WILLOW'], 'Contributor Type': ['Featured'], 'Contributor Role(s)': ['Piano | Guitar'], 'Collection Ownership Percentage of Rights': [math.nan], 'Collection Ownership Country': ['US'], 'Collection Ownership Country - Exclusion': ['#NO-TERRITORY-REQUIRED'], 'Country of Label/Individual/Party Which Funded Recording': ['US'], 'Country of Recording': ['DE'], 'Explicit': ['EXPLICIT'], 'Product Primary Artists': ['Elton John | WILLOW'], 'Product Featuring Artists': [''], 'Primary Artist': ['Elton John, WILLOW'], 'Featuring Artist': [''] }) expected_output = pd.DataFrame(data={ 'Product Type': ['Video'], 'Sales Start Date': ['01-Jan-2020'], 'Collection Ownership Start Date': ['02-Feb-1999'], 'Collection Ownership End Date': ['02-Feb-2003'], 'Duration': ['00:05:00'], 'Contributor Legal Name': ['Randolph, David|WILLOW'], 'Contributor Type': ['Main Performer'], 'Contributor Role(s)': ['Piano|Guitar'], 'Collection Ownership Percentage of Rights': [100], 'Collection Ownership Country': ['US'], 'Collection Ownership Country - Exclusion': [''], 'Country of Label/Individual/Party Which Funded Recording': ['US'], 'Country of Recording': ['DE'], 'Explicit': ['Yes'], 'Product Primary Artists': ['Elton John|WILLOW'], 'Product Featuring Artists': [''], 'Primary Artist': ['Elton John|WILLOW'], 'Featuring Artist': [''] }) tm.assert_frame_equal(formatters.format_video_dataframe(mock_df), expected_output, check_dtype=False), 'Formats column values correctly' def test_format_video_no_product_dataframe(self): mock_df = pd.DataFrame(data={ 'Product Type': [math.nan], 'Sales Start Date': [math.nan], 'Collection Ownership Start Date': ['02-Feb-1999'], 'Collection Ownership End Date': ['02-02-2003'], 'Duration': [300], 'Contributor Legal Name': ['David Randolph | WILLOW'], 'Contributor Type': ['Featured'], 'Contributor Role(s)': ['Piano | Guitar'], 'Collection Ownership Percentage of Rights': [math.nan], 'Collection Ownership Country': ['US'], 'Collection Ownership Country - Exclusion': ['#NO-TERRITORY-REQUIRED'], 'Country of Label/Individual/Party Which Funded Recording': ['US'], 'Country of Recording': ['DE'], 'Explicit': ['EXPLICIT'], 'Product Primary Artists': [''], 'Product Featuring Artists': [''], 'Primary Artist': ['Elton John, WILLOW'], 'Featuring Artist': [''] }) expected_output = pd.DataFrame(data={ 'Product Type': ['Video'], 'Sales Start Date': [math.nan], 'Collection Ownership Start Date': ['02-Feb-1999'], 'Collection Ownership End Date': ['02-Feb-2003'], 'Duration': ['00:05:00'], 'Contributor Legal Name': ['Randolph, David|WILLOW'], 'Contributor Type': ['Main Performer'], 'Contributor Role(s)': ['Piano|Guitar'], 'Collection Ownership Percentage of Rights': [100], 'Collection Ownership Country': ['US'], 'Collection Ownership Country - Exclusion': [''], 'Country of Label/Individual/Party Which Funded Recording': ['US'], 'Country of Recording': ['DE'], 'Explicit': ['Yes'], 'Product Primary Artists': [''], 'Product Featuring Artists': [''], 'Primary Artist': ['Elton John|WILLOW'], 'Featuring Artist': [''] }) tm.assert_frame_equal(formatters.format_video_dataframe(mock_df), expected_output, check_dtype=False), 'Formats column values correctly'