"""Test product video.""" from datetime import datetime from unittest.mock import MagicMock import pytest from pytest_mock import MockerFixture from sqlalchemy import func, select from video.connectors import mysql from video.exceptions import ProductVideoNotFound from video.models.sql.classes import product_video, release as release_class from tests.unit import ar_db_utils @ar_db_utils.test_schema def test_upsert(mocker: MockerFixture) -> None: """Test upsert creates.""" ar_db_utils.seed_models( [release_class.Release(release_id=12, upc=12, project_id=1)] ) mocker.patch.object( product_video, "get_associated_track", return_value={"name": "value"}, autospec=True, ) video = product_video.upsert( { "latest_pipeline_run_id": 14, "release_id": 12, "type_of_video": "Live Performance", "language_of_video_title": "ENG", "video_title": "Yum", "version": "version", "product_code": "product_code", "upc": 666677899, "isrc": "isrc", "imprint": "imprint", "description": "description", "lyrics": "lyrics", "c_line_year": 2014, "p_line_year": 2016, "c_line_copyright_holder": "C line", "p_line_copyright_holder": "P line", "primary_artist_id": 1, "new_release": True, "deliver_to_all": True, "original_release_date": datetime(2015, 1, 1, 12), "release_date": datetime(2015, 1, 1, 12), "parental_advisory": "Yes", "genre_id": 1, "subgenre_id": 2, "keywords": ["a", "b", "c"], "contributors": [ {"role": "Composer", "name": "Jane Smith"}, {"role": "Producer", "name": "George"}, ], "preview_start_time": 45000, "thumbnail_path": "123/12.0000001.jpg", "thumbnail_at_milliseconds": 1000, "custom_thumbnail_path": "123/custom.jpg", "prores_path": "123/0001.mov", "h264_path": "123/custom.h264", "channel_selection": "cuthburt", "not_for_distribution": "AccountingDummy", "ingest_filename": "video_to_ingest.mp4", "associated_track_id": 12345, }, ) assert video["latest_pipeline_run_id"] == 14 assert video["release_id"] == 12 assert video["type_of_video"] == "Live Performance" assert video["language_of_video_title"] == "ENG" assert video["video_title"] == "Yum" assert video["version"] == "version" assert video["product_code"] == "product_code" assert video["upc"] == 666677899 assert video["isrc"] == "isrc" assert video["imprint"] == "imprint" assert video["description"] == "description" assert video["language_of_video_title"] == "ENG" assert video["lyrics"] == "lyrics" assert video["c_line_year"] == 2014 assert video["p_line_year"] == 2016 assert video["c_line_copyright_holder"] == "C line" assert video["p_line_copyright_holder"] == "P line" assert video["new_release"] is True assert video["deliver_to_all"] is True assert video["original_release_date"] == datetime(2015, 1, 1, 12) assert video["release_date"] == datetime(2015, 1, 1, 12) assert video["parental_advisory"] == "Yes" assert video["genre_id"] == 1 assert video["subgenre_id"] == 2 assert video["primary_artist_id"] == 1 assert video["keywords"] == ["a", "b", "c"] assert video["contributors"] == [ {"role": "Composer", "name": "Jane Smith"}, {"role": "Producer", "name": "George"}, ] assert video["preview_start_time"] == 45000 assert video["thumbnail_path"] == "123/12.0000001.jpg" assert video["thumbnail_at_milliseconds"] == 1000 assert video["custom_thumbnail_path"] == "123/custom.jpg" assert video["prores_path"] == "123/0001.mov" assert video["h264_path"] == "123/custom.h264" assert video["channel_selection"] == "cuthburt" assert video["not_for_distribution"] == "AccountingDummy" assert video["ingest_filename"] == "video_to_ingest.mp4" assert video["associated_track_id"] == 12345 assert "associated_track" in video @ar_db_utils.test_schema def test_upsert_defaults() -> None: """Test upsert creates.""" ar_db_utils.seed_models( [release_class.Release(release_id=14, upc=14, project_id=1)] ) video = product_video.upsert( { "release_id": 14, }, ) assert video["release_id"] == 14 assert video["deliver_to_all"] is True assert video["new_release"] is True assert video["associated_track_id"] is None assert video["associated_track"] is None @ar_db_utils.test_schema def test_upsert_updates() -> None: """Test upsert updates.""" ar_db_utils.seed_models( [release_class.Release(release_id=12, upc=12, project_id=1)] ) product_video.upsert( {"release_id": 12, "type_of_video": "Live Performance"}, ) video = product_video.upsert( {"release_id": 12, "type_of_video": "Other"}, ) assert video["type_of_video"] == "Other" with mysql.ar_db_session() as session: count = session.execute( select(func.count()).select_from(product_video.ProductVideo) ).scalar() assert count == 1 @ar_db_utils.test_schema def test_delete() -> None: """Test upsert updates.""" ar_db_utils.seed_models( [release_class.Release(release_id=12, upc=12, project_id=1)] ) product_video.upsert( {"release_id": 12, "type_of_video": "Live Performance"}, ) with mysql.ar_db_session() as session: product_video.delete_product(12, session=session) video = product_video.get(12) assert video == {} @ar_db_utils.test_schema def test_upsert_does_not_allow_certain_keys() -> None: """Test upsert updates.""" ar_db_utils.seed_models( [release_class.Release(release_id=12, upc=12, project_id=1)] ) video = product_video.upsert( { "release_id": 12, "primary_key": 10000, "completely_made_up": 12, }, ) assert video["id"] != 10000 def test_valid_product() -> None: """Test valid product.""" required_fields = { "release_id": 12, "type_of_video": "Live Performance", "video_title": "yum", "imprint": "yum", "product_code": "product_code", "primary_artist_id": 1, "release_date": datetime(2015, 1, 1, 12), "new_release": True, "language_of_video_title": "ENG", "language_of_video_content": "ENG", "genre_id": 1, "subgenre_id": 1, "parental_advisory": "Clean Version", "c_line_year": 2018, "c_line_copyright_holder": "Bob", "p_line_year": 2018, "p_line_copyright_holder": "Bob", "channel_selection": "cuthburt", "thumbnail_path": "3460774/12.0000000.jpg", "thumbnail_at_milliseconds": 0, "contributors": [{"role": "Composer", "name": "Test Composer"}], } assert product_video.ProductVideo().is_valid() is False assert product_video.ProductVideo(**required_fields).is_valid() is True required_fields["new_release"] = False assert product_video.ProductVideo(**required_fields).is_valid() is False required_fields["original_release_date"] = datetime(2015, 1, 1, 12) assert product_video.ProductVideo(**required_fields).is_valid() is True required_fields["isrc"] = "" required_fields["upc"] = "" assert product_video.ProductVideo(**required_fields).is_valid() is False @ar_db_utils.test_schema def test_get_by_ingest_filename() -> None: """Test get by ingest_filename.""" ar_db_utils.seed_models([release_class.Release(release_id=1, upc=1, project_id=1)]) ingest_filename = "video_to_ingest.mp4" product_video.upsert( {"release_id": 1, "ingest_filename": ingest_filename}, ) result = product_video.get_by_ingest_filename(ingest_filename) assert result is not None @ar_db_utils.test_schema def test_get_by_ingest_filename_not_found() -> None: """Test get by ingest_filename.""" ingest_filename = "video_to_ingest.mp4" result = product_video.get_by_ingest_filename(ingest_filename) assert result == {} @ar_db_utils.test_schema def test_update_primary_artist(mocker: MockerFixture) -> None: """Test primary artist migration.""" mocker.patch.object(product_video, "_get", return_value=MagicMock()) mock_update: MagicMock = mocker.patch.object( product_video, "update", return_value={} ) video = product_video.update_primary_artist(14, 1) assert video is not None mock_update.assert_called_once() @ar_db_utils.test_schema def test_update_primary_artist_not_found(mocker: MockerFixture) -> None: """Test update_primary_artist raises ProductVideoNotFound when product does not exist.""" mocker.patch.object(product_video, "_get", return_value=None) with pytest.raises(ProductVideoNotFound): product_video.update_primary_artist(14, 1) @ar_db_utils.test_schema def test_clear_associated_track() -> None: """Nulls associated_track_id only for the given track ids.""" ar_db_utils.seed_models( [ product_video.ProductVideo(release_id=1, associated_track_id=100), product_video.ProductVideo(release_id=2, associated_track_id=100), product_video.ProductVideo(release_id=3, associated_track_id=200), product_video.ProductVideo(release_id=4, associated_track_id=300), ] ) product_video.clear_associated_track([100, 200]) with mysql.ar_db_session() as session: remaining = ( session.execute( select(product_video.ProductVideo.associated_track_id).order_by( product_video.ProductVideo.release_id ) ) .scalars() .all() ) assert remaining == [None, None, None, 300] @ar_db_utils.test_schema def test_clear_associated_track_empty_list() -> None: """An empty track id list updates nothing.""" ar_db_utils.seed_models( [product_video.ProductVideo(release_id=1, associated_track_id=100)] ) product_video.clear_associated_track([]) with mysql.ar_db_session() as session: value = session.execute( select(product_video.ProductVideo.associated_track_id) ).scalar_one() assert value == 100