"""Test match_audio logic.""" from unittest.mock import call import pytest from src.logic import match_audio as match_audio_logic from src.model import acrcloud as match_audio_model from src.model import songwhip as songwhip_model mock_acrcloud_match_result = { 'metadata': { 'music': [ { 'acrid': '3gH2kL1pQ9zX7vB5nM4cR8', 'artists': [ { 'name': 'Focusss Soundzzz' } ], 'score': 52, 'external_metadata': { 'youtube': { 'test_youtube_metadata_without_vid': 'test youtube metadata without vid' } }, 'label': 'A Garage 2', 'title': 'Pink Noise 3', 'rights_claim': [ { 'distributor': {'id': 'abcd3', 'name': 'Distributor Test 3'} } ] }, { 'album': { 'name': 'Pink Noises', }, 'acrid': '6fF1jR9xQ2WmN4yB8tKdP3', 'artists': [ { 'name': 'Focus Soundz' } ], 'score': 100, 'external_ids': { 'isrc': 'GBBKS1500214', 'upc': '886445581959' }, 'external_metadata': { 'youtube': { 'vid': 'asdfPmySO8Sr' } }, 'label': 'A Garage', 'title': 'Pink Noise 2', 'rights_claim': [ { 'distributor': {'id': 'abcd222', 'name': 'Distributor Test 2'} } ] }, { 'acrid': '9nV4cX2mP7kD6rF1tS8yQ5', 'artists': [ { 'name': 'Focus Sounds' } ], 'score': 100, 'external_metadata': { 'deezer': { 'album': { 'id': '2077004897' } }, 'youtube': { 'vid': 'rlqPmySO9Sg' } }, 'label': 'Reseda Garage', 'title': 'Pink Noise 1' }, { 'acrid': '5mR8tK3pN1xV6cQ9zD2fH7', 'external_metadata': { 'deezer': { 'album': { 'id': '2086392997' } }, 'spotify': { 'album': { 'id': '66HZAIWWjo2o7nkmEwafVa' } } }, 'artists': [ { 'name': 'Vibrant Noise' }, { 'name': 'Another Artist' } ], 'score': 70, 'title': 'Pink Noise For Insomnia' } ] }, 'status': { 'code': 0 } } @pytest.mark.parametrize(( 'test_description', 'fingerprint', 'expected_match_audio_model_identify_calls', 'match_audio_model_identify_response', 'expected_songwhip_lookup_calls', 'expected_result' ), [ ( 'Success with empty fingerprint.', b'', [], None, [], {'matches': None} ), ( 'Success with no matches.', b'SomeAudioFingerprint', [call(b'SomeAudioFingerprint')], {'status': {'code': 1001}}, [], {'_acrcloud_response': {'status': {'code': 1001}}, 'matches': None} ), ( 'Success with matches.', b'SomeAudioFingerprint', [call(b'SomeAudioFingerprint')], mock_acrcloud_match_result, [ call(spotify_id=None, deezer_id=None, youtube_id='asdfPmySO8Sr'), call(spotify_id=None, deezer_id='2077004897', youtube_id='rlqPmySO9Sg'), call(spotify_id='66HZAIWWjo2o7nkmEwafVa', deezer_id='2086392997', youtube_id=None), call(spotify_id=None, deezer_id=None, youtube_id=None), ], { '_acrcloud_response': mock_acrcloud_match_result, 'matches': [ { 'acrid': '6fF1jR9xQ2WmN4yB8tKdP3', 'album': 'Pink Noises', 'artists': [ 'Focus Soundz' ], 'isrc': 'GBBKS1500214', 'title': 'Pink Noise 2', 'label': 'A Garage', 'songwhip_external_links': {'spotify': [{'link': 'test url'}]}, 'songwhip_url': None, 'rights_claim': [ { 'distributor': {'id': 'abcd222', 'name': 'Distributor Test 2'} } ], 'score': 100, 'upc': '886445581959' }, { 'acrid': '9nV4cX2mP7kD6rF1tS8yQ5', 'album': None, 'artists': [ 'Focus Sounds' ], 'isrc': None, 'title': 'Pink Noise 1', 'label': 'Reseda Garage', 'songwhip_external_links': {'spotify': [{'link': 'test url'}]}, 'songwhip_url': None, 'rights_claim': None, 'score': 100, 'upc': None }, { 'acrid': '5mR8tK3pN1xV6cQ9zD2fH7', 'album': None, 'artists': [ 'Vibrant Noise', 'Another Artist' ], 'isrc': None, 'title': 'Pink Noise For Insomnia', 'label': None, 'songwhip_external_links': {'spotify': [{'link': 'test url'}]}, 'songwhip_url': None, 'rights_claim': None, 'score': 70, 'upc': None }, { 'acrid': '3gH2kL1pQ9zX7vB5nM4cR8', 'album': None, 'artists': [ 'Focusss Soundzzz' ], 'isrc': None, 'title': 'Pink Noise 3', 'label': 'A Garage 2', 'songwhip_external_links': {'spotify': [{'link': 'test url'}]}, 'songwhip_url': None, 'rights_claim': [ { 'distributor': {'id': 'abcd3', 'name': 'Distributor Test 3'} } ], 'score': 52, 'upc': None }, ] } ) ]) def test_get_audio_matches_success( mocker, test_description, fingerprint, expected_match_audio_model_identify_calls, match_audio_model_identify_response, expected_songwhip_lookup_calls, expected_result ): """Test get_audio_matches success with no matches.""" mocker.patch.object( match_audio_model, 'identify', return_value=match_audio_model_identify_response) mocker.patch.object( songwhip_model, 'lookup_songwhip_links', return_value={'spotify': [{'link': 'test url'}]} ) result = match_audio_logic.get_audio_matches(fingerprint) assert match_audio_model.identify.mock_calls == expected_match_audio_model_identify_calls assert songwhip_model.lookup_songwhip_links.mock_calls == expected_songwhip_lookup_calls assert result == expected_result