"""Test match_audio logic.""" from contextlib import nullcontext from typing import Any, ContextManager from unittest.mock import MagicMock, _Call, call import pytest from pytest_mock import MockerFixture from requests.exceptions import HTTPError from assets.constants import asset_types from assets.logic import match_audio as match_audio_logic, product as product_logic from assets.models import match_audio as match_audio_model @pytest.mark.parametrize( ( "test_description", "get_asset_final_with_asset_upload_by_product_id_response", "expected_raise", "expected_get_match_audio_results_calls", "expected_result", ), [ ( "Test error with get_tracks_by_product_id error.", HTTPError(response=MagicMock(status_code=500)), pytest.raises(HTTPError, check=lambda e: e.response.status_code == 500), [], None, ), ( "Test success with get_tracks_by_product_id success and no tracks on product.", [], nullcontext(), [], [], ), ( "Test success with get_tracks_by_product_id success.", [ { "id": 25, "asset_upload_id": 14, "asset_type": "FLAC", "asset_upload": {"id": 14, "track_unique_id": 4}, }, { "id": 26, "asset_upload_id": 15, "asset_type": "FLAC", "asset_upload": {"id": 15, "track_unique_id": 5}, }, { "id": 29, "asset_upload_id": 16, "asset_type": "FLAC", "asset_upload": {"id": 16, "track_unique_id": 6}, }, ], nullcontext(), [call({25: 4, 26: 5, 29: 6})], ["some", "results"], ), ], ) def test_get_match_audio_results( mocker: MockerFixture, test_description: str, get_asset_final_with_asset_upload_by_product_id_response: list[dict[str, Any]], expected_raise: ContextManager[Exception], expected_get_match_audio_results_calls: list[_Call], expected_result: list[dict[str, Any]], ) -> None: """Test get_match_audio_results.""" get_asset_final_items_with_asset_upload_by_product_id_v2_mock = mocker.patch.object( product_logic, "get_asset_final_items_with_asset_upload_by_product_id_v2", side_effect=[get_asset_final_with_asset_upload_by_product_id_response], ) get_match_audio_results_mock = mocker.patch.object( match_audio_model, "get_match_audio_results", return_value=["some", "results"] ) with expected_raise: result = match_audio_logic.get_match_audio_results(12321) assert result == expected_result assert get_asset_final_items_with_asset_upload_by_product_id_v2_mock.mock_calls == [ call(12321, asset_final_asset_types_filter={asset_types.TYPE_FILE_FLAC}) ] assert get_match_audio_results_mock.mock_calls == ( expected_get_match_audio_results_calls )