"""Test AI audio detection logic.""" from contextlib import nullcontext from typing import Any from unittest.mock import MagicMock, call import pytest import pytest_mock from requests.exceptions import HTTPError from assets.logic import ai_detection as ai_detection_logic from assets.models import hive_segment as hive_segment_model, ows_track @pytest.mark.parametrize( ( "test_description", "get_tracks_by_product_id_response", "expected_raise", "expected_get_ai_generated_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.", {"items": []}, nullcontext(), [], [], ), ( "Test success with get_tracks_by_product_id success.", { "items": [ {"tuid": 111}, {"tuid": 222}, {"tuid": 333}, ] }, nullcontext(), [call(123, [111, 222, 333])], ["some", "results"], ), ], ) def test_get_ai_generated_audio_results_by_product( mocker: pytest_mock.MockerFixture, test_description: str, get_tracks_by_product_id_response: Any, expected_raise: Any, expected_get_ai_generated_audio_results_calls: list[Any], expected_result: Any, ) -> None: """Test get_ai_generated_audio_results_by_product.""" mock_get_tracks_by_product_id = mocker.patch.object( ows_track, "get_tracks_by_product_id", side_effect=[get_tracks_by_product_id_response], ) mock_get_ai_generated_audio_results = mocker.patch.object( hive_segment_model, "get_ai_generated_audio_results_by_product", return_value=["some", "results"], ) with expected_raise: result = ai_detection_logic.get_ai_generated_audio_results_by_product( product_id=123 ) assert result == expected_result assert mock_get_tracks_by_product_id.call_args_list == [call(123)] assert ( mock_get_ai_generated_audio_results.call_args_list == expected_get_ai_generated_audio_results_calls ) def test_get_ai_generated_audio_results_by_product_http_error( mocker: pytest_mock.MockerFixture, ) -> None: """Test get_ai_generated_audio_results_by_product when get_tracks raises HTTPError.""" mock_error = HTTPError("Some error message.") mock_error.response = mocker.MagicMock() mock_error.response.status_code = 500 mock_get_tracks_by_product_id = mocker.patch.object( ows_track, "get_tracks_by_product_id", side_effect=mock_error, ) mock_get_ai_generated_audio_results = mocker.patch.object( hive_segment_model, "get_ai_generated_audio_results_by_product", return_value=["some", "results"], ) with pytest.raises(HTTPError) as exc: ai_detection_logic.get_ai_generated_audio_results_by_product(product_id=123) assert str(exc.value) == "Some error message." assert mock_get_tracks_by_product_id.call_args_list == [call(123)] assert mock_get_ai_generated_audio_results.call_args_list == [] def test_get_ai_generated_audio_results_exception( mocker: pytest_mock.MockerFixture, ) -> None: """Test get_ai_generated_audio_results_by_product when hive_segment raises an exception.""" mocker.patch.object( ows_track, "get_tracks_by_product_id", return_value={"items": [{"tuid": "track_111"}]}, ) # Mock get_ai_generated_audio_results_by_product to raise an exception mocker.patch.object( hive_segment_model, "get_ai_generated_audio_results_by_product", side_effect=Exception("Database connection error"), ) with pytest.raises(Exception) as exc_info: ai_detection_logic.get_ai_generated_audio_results_by_product(product_id=123) assert str(exc_info.value) == "Database connection error"