"""Test for ows-product.""" from contextlib import nullcontext from typing import Any from unittest.mock import MagicMock, call import pytest from flask.ctx import AppContext from owsrequest.utils import mock_request from requests import HTTPError from assets.constants import service from assets.exceptions import ProductNotFound from assets.models import ows_product @pytest.mark.parametrize( "raised_exception, expected_raises, expected_result", [ pytest.param(None, nullcontext(), "ok", id="success"), pytest.param( HTTPError(response=MagicMock(status_code=404)), pytest.raises(ProductNotFound), None, id="not found", ), pytest.param( HTTPError(response=MagicMock(status_code=500)), pytest.raises(HTTPError), None, id="internal error", ), ], ) def test_get_product_details_by_id( raised_exception: Exception | None, expected_raises: Any, expected_result: Any, fixture_app: AppContext, mock_raise_for_status: MagicMock, ) -> None: """Test for get_product_by_id.""" product_id = 2011621 path = "/product/2011621" mock_raise_for_status.side_effect = raised_exception mock_request.get(service.OWS_PRODUCT, path, expected_result) with expected_raises: product_response = ows_product.get_product_by_id(product_id) assert product_response == expected_result assert mock_raise_for_status.mock_calls == [call()] @pytest.mark.parametrize( ( "account_type", "account_id", "product_id", "raised_exception", "expected_raises", ), [ ("vendor", 100, 111, None, nullcontext()), ( "subaccount", 100, 111, HTTPError(response=MagicMock(status_code=404)), pytest.raises(ProductNotFound), ), ( "vendor", 100, 111, HTTPError(response=MagicMock(status_code=403)), pytest.raises(HTTPError, check=lambda e: e.response.status_code == 403), ), ( "vendor", 100, 111, HTTPError(response=MagicMock(status_code=400)), pytest.raises(HTTPError, check=lambda e: e.response.status_code == 400), ), ( "vendor", 100, 111, HTTPError(response=MagicMock(status_code=500)), pytest.raises(HTTPError, check=lambda e: e.response.status_code == 500), ), ], ) def test_check_ownership( account_type: str, account_id: int, product_id: int, raised_exception: Exception | None, expected_raises: Any, fixture_app: AppContext, mock_raise_for_status: MagicMock, ) -> None: """Test product ownership check.""" path = service.OWS_PRODUCT_OWNERSHIP_RESOURCE.format( account_type=account_type, account_id=account_id, product_id=product_id ) mock_raise_for_status.side_effect = raised_exception mock_request.head(service.OWS_PRODUCT, path) with expected_raises: ows_product.check_ownership(product_id, account_type, account_id) assert mock_raise_for_status.mock_calls == [call()] @pytest.mark.parametrize( "description, product_ids, raised_exception, expected_raises, expected_result", [ ( "success result", [2080166, 2080168], None, nullcontext(), { "items": [ {"upc": 191773467098, "product_id": 2080166}, {"upc": 191773467104, "product_id": 2080168}, ] }, ), ( "product ids not found", [2080166, 208016820801682080168], HTTPError(response=MagicMock(status_code=404)), pytest.raises(HTTPError, check=lambda e: e.response.status_code == 404), {}, ), ( "bad request", [], HTTPError(response=MagicMock(status_code=400)), pytest.raises(HTTPError, check=lambda e: e.response.status_code == 400), {}, ), ( "internal error", [2080166, 2080168], HTTPError(response=MagicMock(status_code=500)), pytest.raises(HTTPError, check=lambda e: e.response.status_code == 500), {}, ), ], ) def test_get_upcs_by_product_ids( description: str, product_ids: list[int], raised_exception: Exception | None, expected_raises: Any, expected_result: dict[str, Any], fixture_app: AppContext, mock_raise_for_status: MagicMock, ) -> None: """Test for upcs by product ids.""" mock_raise_for_status.side_effect = raised_exception mock_request.get(service.OWS_PRODUCT, "/products", expected_result) with expected_raises: assert ows_product.get_upcs_by_product_ids(product_ids) == expected_result assert mock_raise_for_status.mock_calls == [call()]