"""Test product video.""" from datetime import datetime, timezone from unittest import TestCase from freezegun import freeze_time from video.models.sql.classes import video_asset from tests.unit import db_utils class VideoAssetTestCase(TestCase): """Test case for VideoAsset model.""" @db_utils.test_schema def test_upsert(self) -> None: """Test upsert creates.""" video = video_asset.upsert( { "product_id": 1234, "asset_type": "video_master", "asset_path": "not_a_path", "tuid": 123, "duration": 12.23, }, ) self.assertEqual(video["product_id"], 1234) self.assertEqual(video["asset_type"], "video_master") self.assertEqual(video["asset_path"], "not_a_path") self.assertEqual(video["tuid"], 123) self.assertEqual(video["duration"], 12.23) @db_utils.test_schema def test_upsert_updates(self) -> None: """Test upsert updates.""" video = video_asset.upsert( { "product_id": 12, "asset_type": "video_master", "asset_path": "asset_path 1", "duration": 12.23, }, ) self.assertEqual(video["asset_path"], "asset_path 1") self.assertEqual(video["duration"], 12.23) video = video_asset.upsert( { "product_id": 12, "asset_type": "video_master", "asset_path": "asset_path 2", "duration": 12.23, }, ) self.assertEqual(video["asset_path"], "asset_path 2") self.assertEqual(video["duration"], 12.23) @db_utils.test_schema @freeze_time("2024-01-01 12:00:00") def test_get_by_product_id(self) -> None: """Test get by product id.""" current_time = ( datetime.now(timezone.utc).replace(microsecond=0).replace(tzinfo=None) ) video_asset.upsert( { "product_id": 12, "asset_type": "video_image_S2", "asset_path": "asset_path 1", "last_updated": current_time, "duration": 13.44, }, ) video = video_asset.get_by_product_id(12) expected_result = [ { "id": 1, "product_id": 12, "asset_type": "video_image_S2", "asset_path": "asset_path 1", "tuid": 0, "last_updated": ( datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S") ), "duration": 13.44, } ] self.assertTrue(video) self.assertEqual(video, expected_result) @db_utils.test_schema def get_by_product_id_and_asset_type(self) -> None: """Test get by product id and asset_type.""" video_asset.upsert( { "product_id": 12, "asset_type": "video_master", "asset_path": "asset_path 1", }, ) video_asset.upsert( { "product_id": 12, "asset_type": "H264_HD", "asset_path": "asset_path 2", }, ) video_asset.upsert( { "product_id": 12, "asset_type": "H264_SD", "asset_path": "asset_path 3", }, ) video_master = video_asset.get_by_product_id_and_asset_type(12, "video_master") h264_hd = video_asset.get_by_product_id_and_asset_type(12, "H264_HD") h264_sd = video_asset.get_by_product_id_and_asset_type(12, "H264_SD") self.assertTrue(video_master["asset_path"] == "asset_path 1") self.assertTrue(h264_hd["asset_path"] == "asset_path 2") self.assertTrue(h264_sd["asset_path"] == "asset_path 3") @db_utils.test_schema def test_delete(self) -> None: """Test delete deletes.""" video_asset.upsert( {"product_id": 12, "asset_type": "video_master", "asset_path": "path 1"}, ) video_asset.upsert( {"product_id": 12, "asset_type": "H264_HD", "asset_path": "path 2"}, ) video_asset.delete_asset(12) asset_data = video_asset.get_by_product_id(12) self.assertEqual(asset_data, []) @db_utils.test_schema def test_upsert_raises_value_error_on_missing_column_data(self) -> None: """Test validation raises ValueError on missing fields.""" with self.assertRaises(ValueError): video_asset.upsert( { "product_id": 1254, "asset_path": "", "asset_type": "master", }, ) @db_utils.test_schema def test_bulk_upsert_video_assets(self) -> None: """Test bulk upsert creates.""" new_video_assets = [ { "product_id": 1, "asset_type": "video_master", "asset_path": "/path/to/video1.mp4", "tuid": 212, }, { "product_id": 2, "asset_type": "H264_HD", "asset_path": "/path/to/video2.mp4", "tuid": 123, }, { "product_id": 1, "asset_type": "H264_SD", "asset_path": "/path/to/video1.mp4", "tuid": 456, }, { "product_id": 3, "asset_type": "video_image_S5", "asset_path": "/path/to/image1.png", }, ] expected_response = [ { "id": 1, "product_id": 1, "asset_type": "video_master", "asset_path": "/path/to/video1.mp4", "tuid": 212, "duration": 0.0, }, { "id": 2, "product_id": 2, "asset_type": "H264_HD", "asset_path": "/path/to/video2.mp4", "tuid": 123, "duration": 0.0, }, { "id": 3, "product_id": 1, "asset_type": "H264_SD", "asset_path": "/path/to/video1.mp4", "tuid": 456, "duration": 0.0, }, { "id": 4, "product_id": 3, "asset_type": "video_image_S5", "asset_path": "/path/to/image1.png", "tuid": 0, "duration": 0.0, }, ] result = video_asset.bulk_upsert_video_assets(new_video_assets) for record in result: record.pop("last_updated") assert result == expected_response @db_utils.test_schema def test_bulk_upsert_multiple_video_assets_with_same_asset_type(self) -> None: """Test bulk upsert creates.""" new_video_assets = [ { "product_id": 1, "asset_type": "video_master", "asset_path": "/path/to/video1.mov", "tuid": 123, }, { "product_id": 1, "asset_type": "video_master", "asset_path": "/path/to/video1.mov", "tuid": 456, }, { "product_id": 2, "asset_type": "H264_HD", "asset_path": "/path/to/video2.mp4", "tuid": 123, }, { "product_id": 2, "asset_type": "H264_HD", "asset_path": "/path/to/video1.mp4", "tuid": 456, }, { "product_id": 2, "asset_type": "video_image_S5", "asset_path": "/path/to/image1.png", }, ] expected_response = [ { "id": 1, "product_id": 1, "asset_type": "video_master", "asset_path": "/path/to/video1.mov", "tuid": 123, "duration": 0.0, }, { "id": 2, "product_id": 1, "asset_type": "video_master", "asset_path": "/path/to/video1.mov", "tuid": 456, "duration": 0.0, }, { "id": 3, "product_id": 2, "asset_type": "H264_HD", "asset_path": "/path/to/video2.mp4", "tuid": 123, "duration": 0.0, }, { "id": 4, "product_id": 2, "asset_type": "H264_HD", "asset_path": "/path/to/video1.mp4", "tuid": 456, "duration": 0.0, }, { "id": 5, "product_id": 2, "asset_type": "video_image_S5", "asset_path": "/path/to/image1.png", "tuid": 0, "duration": 0.0, }, ] result = video_asset.bulk_upsert_video_assets(new_video_assets) for record in result: record.pop("last_updated") assert result == expected_response