"""Test coverart.""" from unittest import mock from unittest.mock import patch import pytest from artwork import config from artwork.logic.coverart import CoverArtClient, get_cover_art from artwork.models.ows_assets import OwsAssetsError @pytest.fixture def get_path_mock(): """Get path from cover art client mock.""" return [ { 'productId': '2843243131231231313123123123', 'url': 'https://qa-images.theorchard.io/product/cover' '/5ad916209a74ff8dd884da38bb4fb139.jpg', 'imageFormat': 'coverart', 'apiVersion': 'v1', }, { 'productId': '284324', 'url': 'https://qa-images.theorchard.io/product/cover' '/44a0f9e741c25771481b186fcfd538b6.jpg', 'imageFormat': 'coverart', 'apiVersion': 'v1', }, ] @pytest.fixture def get_v2_path_mock(): """Get v2 path from cover art client mock.""" return [ { 'productId': '123', 'url': 'https://qa-images.theorchard.io/product/cover/123.jpg', 'imageFormat': 'coverart', 'apiVersion': 'v2', }, ] @pytest.fixture def get_v1_location_mock(): """Mock of v1 ows-assets.""" class Response: message = { '2843243131231231313123123123': 'https://qa-images.theorchard.io/product' '/cover/5ad916209a74ff8dd884da38bb4fb139.jpg', '284324': 'https://qa-images.theorchard.io/product/cover' '/44a0f9e741c25771481b186fcfd538b6.jpg', } return Response @pytest.fixture def get_v2_location_mock(): """Mock of v2 ows-assets.""" class Response: message = {'123': 'https://qa-images.theorchard.io/product/cover/123.jpg'} return Response def test_add_default_images(get_path_mock): """Test add default images.""" product_ids = [x['productId'] for x in get_path_mock] r = CoverArtClient().add_default_images( product_ids + ['non_existent_id'], get_path_mock, 'coverart', 'v1' ) assert r == get_path_mock + [ { 'productId': 'non_existent_id', 'url': '{}/placeholder.jpg'.format(config.CDN_URL), 'imageFormat': 'coverart', 'apiVersion': 'v1', } ] @patch('artwork.logic.coverart.CoverArtClient.get_path') def test_coverart_get_covert_art(mock_coverart_get_path, get_path_mock): """Test the coverart main logic function.""" mock_coverart_get_path.return_value = get_path_mock r = get_cover_art(['2857710', '285771111'], 'coverart') assert r == get_path_mock @patch('artwork.logic.coverart.ows_assets.get') def test_coverart_class(mock_ows_assets_get, get_v1_location_mock, get_path_mock): """Test the coverart class that pulls from ows-assets.""" mock_ows_assets_get.return_value = get_v1_location_mock r = CoverArtClient().get_v1_path(['2857710', '285771111'], 'coverart') assert r == get_path_mock @patch('artwork.logic.coverart.ows_assets.get') def test_coverart_get_v2_class( mock_ows_assets_get, get_v2_location_mock, get_v2_path_mock ): """Test the coverart class that pulls from ows-assets v2 only.""" mock_ows_assets_get.return_value = get_v2_location_mock r = CoverArtClient().get_v2_path(['123'], 'coverart') assert r == get_v2_path_mock @patch('artwork.logic.coverart.CoverArtClient.get_v1_path') @patch('artwork.logic.coverart.CoverArtClient.get_v2_path') @patch('artwork.logic.coverart.is_enabled') def test_coverart_get_path_with_v2( mock_is_enabled, mock_get_v2_path, mock_get_v1_path, get_path_mock, get_v2_path_mock ): """Test the coverart class that gets all paths.""" mock_get_v1_path.return_value = get_path_mock mock_get_v2_path.return_value = get_v2_path_mock r = CoverArtClient().get_path( ['2843243131231231313123123123', '284324', '123', '456'], 'coverart' ) assert r == get_path_mock + get_v2_path_mock + [ { 'productId': '456', 'url': '{}/placeholder.jpg'.format(config.CDN_URL), 'imageFormat': 'coverart', 'apiVersion': 'v2', } ] @mock.patch('artwork.models.ows_assets.request.get', side_effect=OwsAssetsError) def test_coverart_exception_class(mock_ows_assets_get): """Test the coverart exception class that pulls from ows-assets.""" with pytest.raises(OwsAssetsError) as exc_info: CoverArtClient().get_v1_path(['1'], 'coverart') assert exc_info.typename == 'OwsAssetsError' assert str(exc_info.value) == ''