"""Test acrcloud model.""" from unittest.mock import call from freezegun import freeze_time import pytest import responses from src import config from src.config import app_logger from src.model import acrcloud as acrcloud_model @pytest.mark.parametrize(( 'test_description', 'api_response_code', ), [ ('ACRCloud API returned match_found_code: 0', 0), ('ACRCloud API returned no_match_found_code: 1001', 1001), ]) def test_identify_success(mocker, test_description, api_response_code): """Test identify success.""" mocker.patch.object(config, 'ACRCLOUD_IDENTIFICATION_API_ACCESS_KEY', 'SomeAccessKey') mocker.patch.object(config, 'ACRCLOUD_IDENTIFICATION_API_ACCESS_SECRET', 'SomeAccessSecret') test_fingerprint = b'SomeAudioFingerprint' expected_result = {'status': {'code': api_response_code}} with responses.RequestsMock() as requests_mock, freeze_time('2023-01-25T18:08:51.123456Z'): request_files = [('sample', (None, test_fingerprint))] request_data = { 'access_key': 'SomeAccessKey', 'sample_bytes': 53, 'timestamp': '1674670131.123456', 'signature': '3+aiP09WRpPjC6YSn2klVBJMSHk=', 'data_type': 'fingerprint', 'signature_version': '1' } requests_mock.post( url='https://identify-us-west-2.acrcloud.com/v1/identify', match=[responses.matchers.multipart_matcher(request_files, data=request_data)], json=expected_result, ) result = acrcloud_model.identify(test_fingerprint) assert result == expected_result def test_identify_http_error(mocker): """Test identify http error.""" mocker.patch.object(app_logger, 'error') mocker.patch.object(config, 'ACRCLOUD_IDENTIFICATION_API_ACCESS_KEY', 'SomeAccessKey') mocker.patch.object(config, 'ACRCLOUD_IDENTIFICATION_API_ACCESS_SECRET', 'SomeAccessSecret') test_fingerprint = b'SomeAudioFingerprint' expected_result = 'ACRCloud API returned http status code: 500, and text: Some error' with responses.RequestsMock() as requests_mock, freeze_time('2023-01-25T18:08:51.123456Z'), pytest.raises(Exception) as exc_info: # noqa: E501 request_files = [('sample', (None, test_fingerprint))] request_data = { 'access_key': 'SomeAccessKey', 'sample_bytes': 53, 'timestamp': '1674670131.123456', 'signature': '3+aiP09WRpPjC6YSn2klVBJMSHk=', 'data_type': 'fingerprint', 'signature_version': '1' } requests_mock.post( url='https://identify-us-west-2.acrcloud.com/v1/identify', match=[responses.matchers.multipart_matcher(request_files, data=request_data)], status=500, body='Some error', ) acrcloud_model.identify(test_fingerprint) assert app_logger.error.mock_calls == [ call('ACRCloud API returned http status code: 500, and text: Some error')] assert str(exc_info.value) == expected_result def test_identify_api_error(mocker): """Test identify api error.""" mocker.patch.object(app_logger, 'error') mocker.patch.object(config, 'ACRCLOUD_IDENTIFICATION_API_ACCESS_KEY', 'SomeAccessKey') mocker.patch.object(config, 'ACRCLOUD_IDENTIFICATION_API_ACCESS_SECRET', 'SomeAccessSecret') test_fingerprint = b'SomeAudioFingerprint' expected_result = ( 'ACRCloud API returned http status code: 200, and json: ' '{"status": {"code": 12321}}' ) with responses.RequestsMock() as requests_mock, freeze_time('2023-01-25T18:08:51.123456Z'), pytest.raises(Exception) as exc_info: # noqa: E501 request_files = [('sample', (None, test_fingerprint))] request_data = { 'access_key': 'SomeAccessKey', 'sample_bytes': 53, 'timestamp': '1674670131.123456', 'signature': '3+aiP09WRpPjC6YSn2klVBJMSHk=', 'data_type': 'fingerprint', 'signature_version': '1' } requests_mock.post( url='https://identify-us-west-2.acrcloud.com/v1/identify', match=[responses.matchers.multipart_matcher(request_files, data=request_data)], status=200, json={'status': {'code': 12321}}, ) acrcloud_model.identify(test_fingerprint) assert app_logger.error.mock_calls == [ call('ACRCloud API returned http status code: 200, and json: {"status": {"code": 12321}}')] assert str(exc_info.value) == expected_result