"""Test Class for transforms.py.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from feed_sender.flows.proper_new_releases_tracks import exceptions from feed_sender.flows.proper_new_releases_tracks.util import transforms def test_transform_title(): """Sample test cases for title.""" assert 'BLUES' == transforms.transform_title('Blues') assert 'BLUES,THE' == transforms.transform_title('The Blues ') assert 'NIGHT BLUES,THE', transforms.transform_title('The Night Blues') def test_transform_genre_subgenre(): """Test if correct value is returned with given key.""" # mock the genre mapping DB lookup. genre_subgenre_mapping = { '1_4': {'subgenre': 'Sub Rock', 'genre': 'Rock'}} assert 'Rock' == transforms.transform_genre_subgenre( 1, **{'subgenre': 4, 'genre_subgenre_mapping': genre_subgenre_mapping}) def test_transform_subgenre_genre(): """Test if correct value is returned with given key.""" # mock the genre mapping DB lookup. genre_subgenre_mapping = { '1_4': {'subgenre': 'Sub Rock', 'genre': 'Rock'}} assert 'Sub Rock' == transforms.transform_subgenre_genre( 4, **{'genre': 1, 'genre_subgenre_mapping': genre_subgenre_mapping}) def test_transform_artist(): """Sample test cases for artists.""" assert 'ANNA' == transforms.transform_artist('Anna') assert 'KNIGHT,THE' == transforms.transform_artist( 'The Knight', **{'individual': 'Y'}) assert 'SMITH,ANNE' == transforms.transform_artist( 'Anne Smith', **{'individual': 'Y'}) assert 'SMITH,AN MARY' == transforms.transform_artist( 'An Mary Smith', **{'individual': 'Y'}) assert 'SMITH,AN-MARY' == transforms.transform_artist( 'An-Mary Smith', **{'individual': 'Y'}) @patch('feed_sender.flows.proper_new_releases_tracks.util.' 'distribution_format_mapping.get_distribution_format_mapping') def test_transform_formid(get_distribution_format_mapping): """Sample test cases for distribution formid.""" get_distribution_format_mapping.return_value = { 67: {'format': '7', 'formid': 'VNL7'}, 90: {'format': 'SACD', 'formid': 'SACD'} } assert 'VNL7' == transforms.transform_formid(67) assert 'VHS' != transforms.transform_formid(90) @patch('feed_sender.flows.proper_new_releases_tracks.util.' 'distribution_format_mapping.get_distribution_format_mapping') def test_transform_format_id(get_distribution_format_mapping): """Sample test cases for distribution format id.""" get_distribution_format_mapping.return_value = { 67: {'format': '7', 'formid': 'VNL7'}, 88: {'format': 'SACD', 'formid': 'SACD'} } assert '7' == transforms.transform_format_id(67) assert 'DVD' != transforms.transform_format_id(88) def test_transform_packageid(): """Sample test cases for distribution format id.""" assert 'DC' == transforms.transform_packageid(8) assert 'V' != transforms.transform_packageid(21) assert '' == transforms.transform_packageid(-1) def test_transform_price(): """Sample test cases for distribution format id.""" assert '1.00' == transforms.transform_price(1.0) assert '999.90' == transforms.transform_price(999.9) @patch( 'feed_sender.flows.proper_new_releases_tracks.util.transforms.request.' 'process') def test_transform_carveout_no_UK(mock_request_process): """Sample test cases for getting export only flag.""" mock_response = MagicMock() mock_response.status_code = 200 mock_response.content = b'{"240":"AT","241":"US","242":"DE"}' mock_request_process.return_value = mock_response upc = 988266277734 carveout_service_name = 'test_service_name' carveout_service_resource_path = 'test_carveout_service_resource_path' application_name = 'test_application_name' application_env = 'test_application_env' correlation_id = 'test_correlation_id' actual = transforms.transform_carveout( upc, carveout_service_name=carveout_service_name, carveout_service_resource_path=carveout_service_resource_path, application_name=application_name, application_env=application_env, correlation_id=correlation_id) assert actual == '' @patch( 'feed_sender.flows.proper_new_releases_tracks.util.transforms.request.' 'process') def test_transform_carveout_has_UK(mock_request_process): """Sample test cases for getting export only flag.""" mock_response = MagicMock() mock_response.status_code = 200 mock_response.content = b'{"240":"AT","244":"UK","242":"DE"}' mock_request_process.return_value = mock_response upc = 988266277734 carveout_service_name = 'test_service_name' carveout_service_resource_path = 'test_carveout_service_resource_path' application_name = 'test_application_name' application_env = 'test_application_env' correlation_id = 'test_correlation_id' actual = transforms.transform_carveout( upc, carveout_service_name=carveout_service_name, carveout_service_resource_path=carveout_service_resource_path, application_name=application_name, application_env=application_env, correlation_id=correlation_id) assert actual == 'Y' @patch( 'feed_sender.flows.proper_new_releases_tracks.util.transforms.request.' 'process') def test_transform_carveout_404(mock_request_process): """Sample test cases for getting export only flag.""" mock_response = MagicMock() mock_response.status_code = 404 mock_response.content = ( b'{"statusCode":400,"message":"UPC (988266277734) ' b'not found","timestamp":1476210033}') mock_request_process.return_value = mock_response upc = 988266277734 carveout_service_name = 'test_service_name' carveout_service_resource_path = 'test_carveout_service_resource_path' application_name = 'test_application_name' application_env = 'test_application_env' correlation_id = 'test_correlation_id' actual = transforms.transform_carveout( upc, carveout_service_name=carveout_service_name, carveout_service_resource_path=carveout_service_resource_path, application_name=application_name, application_env=application_env, correlation_id=correlation_id) assert actual == '' @patch( 'feed_sender.flows.proper_new_releases_tracks.util.transforms.request.' 'process') def test_transform_carveout_response_error(mock_request_process): """Sample test cases for getting export only flag.""" mock_response = MagicMock() mock_response.status_code = 400 mock_response.content = ( b'{"statusCode":400,"message":"UPC (988266277734) not found",' b'"timestamp":1476210033}') mock_request_process.return_value = mock_response upc = 988266277734 carveout_service_name = 'test_service_name' carveout_service_resource_path = 'test_carveout_service_resource_path' application_name = 'test_application_name' application_env = 'test_application_env' correlation_id = 'test_correlation_id' with pytest.raises(Exception) as err: actual = transforms.transform_carveout( upc, carveout_service_name=carveout_service_name, carveout_service_resource_path=carveout_service_resource_path, application_name=application_name, application_env=application_env, correlation_id=correlation_id) assert actual == '' assert str(err) == 'UPC (988266277734) not found' def test_transform_duration(): """Sample test cases for track duration.""" assert '14:20' == transforms.transform_duration( 14, **{'length_seconds': 20}) assert '04:20' == transforms.transform_duration( 4, **{'length_seconds': 20}) assert '14:02' == transforms.transform_duration( 14, **{'length_seconds': 2}) assert '04:02' == transforms.transform_duration( 4, **{'length_seconds': 2}) assert '00:02' == transforms.transform_duration( 0, **{'length_seconds': 2}) assert '' == transforms.transform_duration( 0, **{'length_seconds': 0}) assert '00:02' == transforms.transform_duration( None, **{'length_seconds': 2}) def test_transform_skip_release_if_product_code_null(): """Sample test cases for transforming product code.""" release_id = 1 err_message = "Product code doesn't exist for release " \ '{release_id}'.format(release_id=release_id) # check exception thrown if product code doesn't exist for product_code in ['', None]: with pytest.raises(exceptions.TransformException) as err: transforms.transform_product_code_skip_if_null( product_code, **{'release_id': release_id}) assert err_message in str(err) # check correct catalogue number is returned if product code exists product_code = '1' catalogue_number = transforms.transform_product_code_skip_if_null( product_code, **{'release_id': release_id}) assert catalogue_number == product_code def test_transform_upc(): """Sample test cases for upc.""" display_upc = '100000000000' # check display upc is used if manufacturer upc doesn't exist assert display_upc == transforms.transform_upc( display_upc, **{'manufacturer_upc': None}) # check display upc is used if manufacturer upc is invalid for manufacturer_upc in ['0', '', '{:<16}'.format(0), 16 * ' ']: assert display_upc == transforms.transform_upc( display_upc, **{'manufacturer_upc': manufacturer_upc}) # check manufacturer upc is used if it is valid manufacturer_upc = '200000000000' assert manufacturer_upc == transforms.transform_upc( display_upc, **{'manufacturer_upc': manufacturer_upc}) def test_transform_description(): """Sample test cases for description.""" description = '12 harmonica players live compilation' display_configuration = '10" Vinyl' # Check nonempty display_configuration narrative = '12 harmonica players live compilation. The full display ' \ 'configuration for this product, including value adds, is: ' \ '10" Vinyl' assert narrative == transforms.transform_description( description, **{'display_configuration': display_configuration}) # Check empty display_configuration's narrative = description assert narrative == transforms.transform_description( description, **{'display_configuration': None}) assert narrative == transforms.transform_description( description, **{'display_configuration': ''}) assert narrative == transforms.transform_description( description, **{}) # Check empty description narrative = 'The full display configuration for this product, including ' \ 'value adds, is: 10" Vinyl' assert None is transforms.transform_description( None, **{'display_configuration': None}) assert narrative == transforms.transform_description( None, **{'display_configuration': display_configuration})