"""Test ows-artist.""" import json from contextlib import ExitStack as nullcontext from http.client import INTERNAL_SERVER_ERROR, NOT_FOUND, OK from typing import Any import pytest import requests from owsrequest import request from pytest_mock import MockerFixture from requests.exceptions import HTTPError from video.exceptions import ArtistNotFound from video.models.ows import artist as artist_model from tests.unit.factories.ows.artist import ArtistFactory artist_a = ArtistFactory.build() @pytest.mark.parametrize( ("expected_status", "expected_raises", "expected_return"), [ (OK, nullcontext(), artist_a.to_dict()), (NOT_FOUND, pytest.raises(ArtistNotFound), None), (INTERNAL_SERVER_ERROR, pytest.raises(HTTPError), None), ], ) def test_get_artist( mocker: MockerFixture, expected_status: Any, expected_raises: Any, expected_return: Any, ) -> None: """Test get_artist.""" ows_artist_response = requests.Response() ows_artist_response.status_code = expected_status ows_artist_response._content = json.dumps( { "id": artist_a.id, "name": artist_a.name, } ).encode() mocker.patch.object( request, "process", return_value=ows_artist_response, autospec=True ) artist = None with expected_raises: artist = artist_model.get_artist(artist_a.id) assert expected_return == artist def test_check_artist_ownership_success(mocker: MockerFixture) -> None: """Test check artist ownership success.""" ows_artist_response = requests.Response() ows_artist_response.status_code = OK mocker.patch.object( request, "process", return_value=ows_artist_response, autospec=True, ) artist_model.check_artist_ownership(1, "1", "1") def test_check_artist_ownership_no_creds(mocker: MockerFixture) -> None: """Test check artist ownership no credentials.""" artist_model.check_artist_ownership(1)