"""Test message utils.""" import json from unittest.mock import call from unittest.mock import patch import pytest from src.utils import message_utils from ...conftest import trending_track_message @pytest.mark.parametrize( ('input_data', 'expected_output'), [ # 1 event ( [trending_track_message('Canada', 'spotify', 123, 25)], [ ( 'Track Number 123 had a 25% increase in streams on Spotify in Canada on June 15', # noqa:E501 {'isrc': 'ISRC-123'} ) ] ), # country aggregation and platform plurality ( [ trending_track_message('Mexico', 'itunes/apple', 123, 20), trending_track_message('Canada', 'spotify', 123, 10), trending_track_message('Canada', 'itunes/apple', 123, 15), trending_track_message('Iceland', 'spotify', 123, 90), trending_track_message('Iceland', 'itunes/apple', 123, 75), trending_track_message('Iceland', 'amazon', 123, 50), ], [ ( 'Track Number 123 had a 20% increase in streams on Apple Music in Mexico on June 15', # noqa:E501 {'isrc': 'ISRC-123'} ), ( 'Track Number 123 had an increase in streams on Apple Music and 1 other platform in Canada on June 15', # noqa:E501 {'isrc': 'ISRC-123'} ), ( 'Track Number 123 had an increase in streams on Spotify and 2 other platforms in Iceland on June 15', # noqa:E501 {'isrc': 'ISRC-123'} ) ] ), # track aggregation ( [ trending_track_message('Canada', 'spotify', 123, 25), trending_track_message('Canada', 'spotify', 456, 35) ], [ ( 'Track Number 123 had a 25% increase in streams on Spotify in Canada on June 15', # noqa:E501 {'isrc': 'ISRC-123'} ), ( 'Track Number 456 had a 35% increase in streams on Spotify in Canada on June 15', # noqa:E501 {'isrc': 'ISRC-456'} ) ] ) ] ) @patch('src.common.messages.create_message_string') @patch('uuid.uuid4') def test_create_trending_track_message( mock_uuid, mock_create_message, input_data, expected_output): """Test event list input and message copy output.""" mock_uuid.return_value = 'uuid-mock' mock_create_message.return_value = 'message-mock' messages = message_utils.create_trending_track_message( input_data, message_utils.messages.get_translations('en'), **{ 'profile_id': 123, 'profile_type': 'InsightsProfile', 'identity_id': 'xyz', 'brand': 'awal' } ) assert len(messages) == len(expected_output) for idx in range(0, len(expected_output)): expected_str = expected_output[idx][0] expected_payload = { 'data': { 'profileId': 123, 'profileType': 'InsightsProfile', 'identityId': 'xyz', 'brand': 'awal', 'notificationId': 'uuid-mock', 'type': 'trending_tracks', 'payload': expected_output[idx][1] } } expected_call = call(expected_str, expected_payload) assert mock_create_message.call_args_list[idx] == expected_call (message_json, message_metadata) = messages[idx] assert message_json == 'message-mock' assert message_metadata == expected_payload @pytest.mark.parametrize( ('events_index', 'expected_strings', 'expected_length', 'expected_payload'), [ ( 1, ['great song has been added to Top 100 on Apple Music!'], 1, [ { 'isrc': 'US23A1500056', 'playlistIds': ['pl.42b3fe9c75a947ab84a80019e7bcd704'], 'playlists': [{ 'id': 'pl.42b3fe9c75a947ab84a80019e7bcd704', 'store_id': '1' }] } ] ), ( 2, [ 'great song has been added to Top 100 on Apple Music!', 'I Feel the Transition has been added to Volume on Spotify!' ], 2, [ { 'isrc': 'US23A1500056', 'playlistIds': ['pl.42b3fe9c75a947ab84a80019e7bcd704'], 'playlists': [{ 'id': 'pl.42b3fe9c75a947ab84a80019e7bcd704', 'store_id': '1' }] }, { 'isrc': 'CAB391802328', 'playlistIds': ['37i9dQZF1DWUv0cTKdT8jJ'], 'playlists': [{ 'id': '37i9dQZF1DWUv0cTKdT8jJ', 'store_id': '286' }] } ] ), ( 3, [ 'great song has been added to Top 100 on Apple Music!', 'I Feel the Transition has been added to Other Playlist on Spotify and 1 other playlist!' # noqa:E501 ], 2, [ { 'isrc': 'US23A1500056', 'playlistIds': ['pl.42b3fe9c75a947ab84a80019e7bcd704'], 'playlists': [{ 'id': 'pl.42b3fe9c75a947ab84a80019e7bcd704', 'store_id': '1' }] }, { 'isrc': 'CAB391802328', 'playlistIds': ['37i9dQZF1DWUv0cTKdT8jJ'], 'playlists': [{ 'id': '37i9dQZF1DWUv0cTKdT8jJ', 'store_id': '286' }] } ] ), ( None, [ 'great song has been added to Top 100 on Apple Music!', 'I Feel the Transition has been added to Other Playlist on Spotify and 2 other playlists!' # noqa:E501 ], 2, [ { 'isrc': 'US23A1500056', 'playlistIds': ['pl.42b3fe9c75a947ab84a80019e7bcd704'], 'playlists': [{ 'id': 'pl.42b3fe9c75a947ab84a80019e7bcd704', 'store_id': '1' }] }, { 'isrc': 'CAB391802328', 'playlistIds': ['29'], 'playlists': [{ 'id': '29', 'store_id': '286' }] } ] ), ] ) @patch('uuid.uuid4') def test_create_playlist_placement_message( mock_uuid, test_buffered_events, events_index, expected_strings, expected_length, expected_payload, ): """Test create_playlist_placement_message.""" mock_uuid.return_value = 'uuid-mock' notification_settings = [{ 'feed_type': 'playlist_placements', 'notification_type': 'push_notifications', 'followed_entity': 'participant' }] user_data = { 'notification_settings': notification_settings, 'profile_id': 123, 'profile_type': 'InsightsProfile', 'identity_id': 'xyz', 'brand': 'orchard' } result = message_utils.create_playlist_placement_message( test_buffered_events[:events_index], message_utils.messages.get_translations('en'), **user_data) assert len(result) == expected_length for i in range(len(result)): assert expected_strings[i] in result[i][0] expected_metadata = { 'data': { 'profileId': 123, 'profileType': 'InsightsProfile', 'identityId': 'xyz', 'brand': 'orchard', 'notificationId': 'uuid-mock', 'type': 'playlist_placements', 'payload': expected_payload[i] } } assert result[i][1] == expected_metadata @pytest.mark.parametrize( 'notification_settings, feed_type, notification_type, entity_type, enabled', [ ([{ 'feed_type': 'social_spike', 'notification_type': 'push_notifications', 'followed_entity': 'participant' }], 'social_spike', 'push_notifications', 'participant', True), ([{ 'feed_type': 'social_spike', 'notification_type': 'push_notifications', 'followed_entity': 'participant' }], 'social_spike', 'push_notifications', 'sound_recording', False), ([{ 'feed_type': 'social_spike', 'notification_type': 'email', 'followed_entity': 'participant' }], 'social_spike', 'push_notifications', 'participant', False), ([{ 'feed_type': 'trending_tracks', 'notification_type': 'push_notifications', 'followed_entity': 'participant' }], 'social_spike', 'push_notifications', 'participant', False), ([], 'social_spike', 'push_notifications', 'participant', False), ] ) def test_notification_enabled( notification_settings, feed_type, notification_type, entity_type, enabled): """Test notification settings check for a feed type.""" assert message_utils._notification_enabled( notification_settings, feed_type, notification_type, entity_type) == enabled @patch('src.utils.message_utils.messages.get_translations') @patch('src.common.messages.create_message_string') @patch('uuid.uuid4') def test_trending_track_check_get_translations( mock_uuid, mock_create_message, mock_get_translations): """Test what gets sent to translations.""" mock_uuid.return_value = 'uuid-mock' mock_create_message.return_value = 'message-mock' input_data = [{ 'actor': '35225411', 'foreign_id': 'trending_tracks:35225411:spotify:Brazil:InsightsProfile_986', 'id': 'd3fb8000-36b8-11ec-8080-80002c48dd08', 'time': '2021-10-27T00:00:00.000000', 'object': { 'activity_sources': ['participant', 'sound_recording'], 'day_streams': 9938, 'dsp': 'spotify', 'percent_diff': 403, 'region': 'Brazil', 'track': { 'id': 35225411, 'isrc': 'QM6MZ2156864', 'name': 'Butter', 'subaccount_id': None, 'vendor_id': 26536}}, 'origin': None, 'target': '', 'verb': 'trending_tracks', }] messages = message_utils.create_trending_track_message( input_data, mock_get_translations, **{ 'profile_id': 123, 'profile_type': 'InsightsProfile', 'identity_id': 'xyz' } ) assert len(messages) == 1 mock_get_translations.assert_called_with('%(songname)s had a %(number)s%% increase in streams on %(service)s in %(country)s on %(month)s %(day)s') # noqa @patch('src.utils.message_utils.messages.get_translations') @patch('src.common.messages.create_message_string') @patch('uuid.uuid4') def test_social_spike_check_get_translations( mock_uuid, mock_create_message, mock_get_translations): """Test what gets sent to translations.""" mock_uuid.return_value = 'uuid-mock' mock_create_message.return_value = 'message-mock' input_data = [{ 'actor': '0aef424f-ff8d-4dd7-b0ac-4bd3191639a9', 'foreign_id': 'social_spike_twitter:0aef424f-ff8d-4dd7-b0ac:InsightsProfile_1470', 'id': 'b4384000-14ee-11ec-8080-80012bcd9891', 'object': { 'activity_sources': ['participant'], 'id': '0aef424f-ff8d-4dd7-b0ac-4bd3191639a9', 'name': 'Coldplay', 'network': 'twitter', 'new_followers': 47664}, 'origin': None, 'target': '', 'verb': 'social_spike_twitter', }] messages = message_utils.create_social_spike_message( input_data, mock_get_translations, **{ 'profile_id': 123, 'profile_type': 'InsightsProfile', 'identity_id': 'xyz' } ) assert len(messages) == 1 mock_get_translations.assert_called_once_with('%(artist)s gained %(number)s %(service)s %(followers)s since yesterday!') # noqa @patch('src.utils.message_utils.messages.get_translations') @patch('src.common.messages.create_message_string') @patch('uuid.uuid4') def test_playlist_placement_check_get_translations( mock_uuid, mock_create_message, mock_get_translations): """Test what gets sent to translations.""" mock_uuid.return_value = 'uuid-mock' mock_create_message.return_value = 'message-mock' input_data = [{ 'actor': '351a3752-e77d-4566-8ac4-9f40a5fc0aaf', 'foreign_id': 'playlist_placements:37i9dQ:spotify:8ac4-9f40a5fc0aaf:InsightsProfile_998', 'id': '2bf31d00-1133-11ec-8080-80016ad96f89', 'time': '2021-09-09T06:00:02.000000', 'object': { 'activity_sources': ['participant'], 'playlist': { 'dsp': 'itunes/apple', 'id': 'pl.1fa57a04cd794a8aa482a3492f26fbcd', 'name': 'New in Hip-Hop', 'rank': 22700}, 'sound_recording': { 'id': 'e7047698-3153-4dfa-94c2-b9d561cebb31', 'isrc': 'QMDA62176303', 'name': 'Duck Duck Goose'}}, 'origin': None, 'target': '', 'verb': 'playlist_placements', }] messages = message_utils.create_playlist_placement_message( input_data, mock_get_translations, **{ 'profile_id': 123, 'profile_type': 'InsightsProfile', 'identity_id': 'xyz' } ) assert len(messages) == 1 mock_get_translations.assert_called_once_with('%(songname)s has been added to %(playlistname)s on %(service)s!') # noqa @pytest.mark.parametrize( ('input_data', 'expected_output'), [ # 1 event ( [ trending_track_message('Canada', 'spotify', 111, 10), [ 'カナダ', '6月', '23日', '%(songname)s のストリーミング回数が %(month)s %(day)s' + ' に %(country)s の %(service)s で %(number)s 増加しました' # noqa:W503 ] ], [ 'Track Number 111 のストリーミング回数が 6月 23日 に カナダ の Spotify で 10 増加しました' ] # noqa ) ] ) @patch('src.utils.message_utils.messages.get_translations') def test_create_translation(mock_get_translations, input_data, expected_output): """Test event list input and message copy output.""" def substitute_return_values(v): if v == 'Canada': return input_data[1][0] elif v == 'June': return input_data[1][1] elif v == '15': return input_data[1][2] elif v == '%(songname)s had a %(number)s%% increase in streams on' \ ' %(service)s in %(country)s on %(month)s %(day)s': return input_data[1][3] return v mock_get_translations.side_effect = substitute_return_values messages = message_utils.create_trending_track_message( [input_data[0]], mock_get_translations, **{ 'profile_id': 123, 'profile_type': 'InsightsProfile', 'identity_id': 'xyz', 'brand': 'awal' } ) calls = [ call( '%(songname)s had a %(number)s%% increase in streams' + ' on %(service)s in %(country)s on %(month)s %(day)s' # noqa:W503 ) ] mock_get_translations.assert_has_calls(calls) assert mock_get_translations.call_count == 240 resp = json.loads(messages[0][0]) assert resp['default'] == expected_output[0]