"""Asset logic tests.""" from unittest.mock import MagicMock, call import pytest from pytest_mock import MockerFixture from video.config import VIDEO_BUCKET_NAME from video.constants import job_io_fields from video.constants.thumbnails import CUSTOM_THUMBNAIL_PATH_PREFIX from video.exceptions import VideoAssetNotFound from video.logic import asset as asset_logic from video.models.s3 import presigned_url from video.models.sql import queries from video.models.sql.classes import product_video def test_get_product_assets(mocker: MockerFixture) -> None: """Test get product assets.""" product_ids = [123, 234] assets = [ { "product_id": 123, "asset_type": "video_master", "asset_path": "video_master_s3_path", }, { "product_id": 234, "asset_type": "video_image_S10", "asset_path": "video_image_s3_path", }, ] expected = [] for asset in assets: asset.update({"presigned_url": "URL"}) expected.append(asset) mock_get_assets: MagicMock = mocker.patch.object( queries, "get_product_assets", return_value=assets ) mocker.patch.object(presigned_url, "create_presigned_url", return_value="URL") result = asset_logic.get_product_assets(product_ids) mock_get_assets.assert_called_once_with(product_ids, asset_types=[]) assert result == expected def test_get_product_assets_not_found(mocker: MockerFixture) -> None: """Test get product assets not found.""" product_ids = [123, 3242] mocker.patch.object(queries, "get_product_assets", return_value=None) with pytest.raises(VideoAssetNotFound): asset_logic.get_product_assets(product_ids) def test_get_custom_thumbnail_s3_key(mocker: MockerFixture) -> None: """Test get custom thumbnail s3 key.""" product_id = 123 path = "123/custom.jpg" mock_get: MagicMock = mocker.patch.object( product_video, "get", return_value={"custom_thumbnail_path": path} ) result = asset_logic.get_custom_thumbnail_s3_key(product_id) mock_get.assert_called_once_with(123) assert result == "{}{}".format(CUSTOM_THUMBNAIL_PATH_PREFIX, path) def test_get_custom_thumbnail_s3_key_not_found(mocker: MockerFixture) -> None: """Test get custom thumbnail s3 key not found.""" product_id = 123 mocker.patch.object(product_video, "get", return_value=None) result = asset_logic.get_custom_thumbnail_s3_key(product_id) assert result is None def test_get_custom_thumbnail_s3_key_no_path(mocker: MockerFixture) -> None: """Test get custom thumbnail s3 key no path.""" product_id = 123 mocker.patch.object(product_video, "get", return_value={}) result = asset_logic.get_custom_thumbnail_s3_key(product_id) assert result is None def test_generate_presigned_urls(mocker: MockerFixture) -> None: """Test generate presigned urls.""" keys = { job_io_fields.PRORES_MEZZANINE_OUTPUT_S3_KEY: "123.mov", job_io_fields.H264_MEZZANINE_OUTPUT_S3_KEY: "123.mp4", job_io_fields.THUMBNAIL_S3_KEY: "123.jpg", "custom_thumbnail_s3_key": "custom.jpg", } mock_presigned: MagicMock = mocker.patch.object( presigned_url, "create_presigned_url", return_value="URL" ) result = asset_logic.generate_presigned_urls(keys) mock_presigned.assert_has_calls( [ call(VIDEO_BUCKET_NAME, "123.mov"), call(VIDEO_BUCKET_NAME, "123.mp4"), call(VIDEO_BUCKET_NAME, "123.jpg"), call(VIDEO_BUCKET_NAME, "custom.jpg"), ] ) assert result == { "prores_mezzanine_url": "URL", "h264_mezzanine_url": "URL", "thumbnail_url": "URL", "custom_thumbnail_url": "URL", } def test_generate_presigned_urls_no_custom(mocker: MockerFixture) -> None: """Test generate presigned urls no custom.""" keys = { job_io_fields.PRORES_MEZZANINE_OUTPUT_S3_KEY: "123.mov", job_io_fields.H264_MEZZANINE_OUTPUT_S3_KEY: "123.mp4", job_io_fields.THUMBNAIL_S3_KEY: "123.jpg", "custom_thumbnail_s3_key": None, } mock_presigned2: MagicMock = mocker.patch.object( presigned_url, "create_presigned_url", return_value="URL" ) result = asset_logic.generate_presigned_urls(keys) mock_presigned2.assert_has_calls( [ call(VIDEO_BUCKET_NAME, "123.mov"), call(VIDEO_BUCKET_NAME, "123.mp4"), call(VIDEO_BUCKET_NAME, "123.jpg"), ] ) assert result == { "prores_mezzanine_url": "URL", "h264_mezzanine_url": "URL", "thumbnail_url": "URL", "custom_thumbnail_url": None, }