"""Test product eligibility model.""" from unittest.mock import AsyncMock, MagicMock, patch import pytest from src.models import ows_vectororder @pytest.mark.asyncio @patch('src.models.ows_vectororder.async_owsclient') async def test_check_product_eligibility_success(mock_owsclient): """Test check_product_eligibility returns status and body on success.""" mock_response = MagicMock() mock_response.status_code = 200 mock_response.text = 'OK' mock_owsclient.get = AsyncMock(return_value=mock_response) upc, status, text = await ows_vectororder.check_product_eligibility('11111', retries=1) assert upc == '11111' assert status == '200' assert text == 'OK' @pytest.mark.asyncio @patch('src.models.ows_vectororder.async_owsclient') async def test_check_product_eligibility_exception(mock_owsclient): """Test check_product_eligibility returns ERROR when an exception is raised.""" mock_owsclient.get = AsyncMock(side_effect=Exception('Boom!')) upc, status, text = await ows_vectororder.check_product_eligibility('22222', retries=0) assert upc == '22222' assert status == 'ERROR' assert 'Boom!' in text @pytest.mark.asyncio @patch('asyncio.sleep') @patch('src.models.ows_vectororder.async_owsclient') async def test_check_product_eligibility_server_error(mock_owsclient, mock_sleep): """Test check_product_eligibility returns ERROR on 5xx response after retries.""" mock_response = MagicMock() mock_response.status_code = 500 mock_owsclient.get = AsyncMock(return_value=mock_response) upc, status, text = await ows_vectororder.check_product_eligibility('33333', retries=1) assert upc == '33333' assert status == 'ERROR' assert 'Internal Server Error' in text @pytest.mark.asyncio @patch('asyncio.sleep') @patch('src.models.ows_vectororder.async_owsclient') async def test_check_product_eligibility_retries_then_succeeds(mock_owsclient, mock_sleep): """Test check_product_eligibility retries on failure and succeeds on a later attempt.""" mock_response = MagicMock() mock_response.status_code = 200 mock_response.text = 'OK' mock_owsclient.get = AsyncMock(side_effect=[Exception('transient'), mock_response]) upc, status, text = await ows_vectororder.check_product_eligibility('44444', retries=1) assert upc == '44444' assert status == '200' assert text == 'OK' assert mock_owsclient.get.call_count == 2