"""Test poll_video_workflow_status.""" from unittest.mock import patch import pytest from ddex_ingester_common.lambda_exceptions import PollVideoFatalException from ddex_ingester_common.schemas.state_machine_schema import \ StateMachineSchema from index import (SAVE_VIDEO_SINGLE, VideoException, get_workflow_status, handler, save_thumbnail_path) @patch('index.graphql_gateway') def test_get_workflow_status(mock_graphql_gateway): """Test get video workflow status calls.""" expected_output = { 'items': [ { 'status': 'COMPLETE', 'type': 'create_streamable_preview_and_thumbnails', } ], } mock_response = { 'data': { 'getVideoWorkflowJobDetails': expected_output } } mock_graphql_gateway.execute.return_value = mock_response workflow_id = 1234 output = get_workflow_status(workflow_id, 'SME') assert output == expected_output @patch('index.graphql_gateway') def test_get_workflow_status_on_error(mock_graphql_gateway): """Test get video workflow status on error.""" mock_graphql_gateway.execute.return_value = { 'data': { 'getVideoWorkflowJobDetails': { 'items': [ { 'type': 'test', 'status': 'ERROR', 'outputs': [ { 'code': 'error_cannot_resize_to_standard_resolution', # noqa 'message': 'Cannot convert video to a standard resolution.' # noqa } ] } ] } } } workflow_id = 1234 with pytest.raises(PollVideoFatalException): get_workflow_status(workflow_id, 'SME') @patch('index.graphql_gateway') def test_get_workflow_status_on_processing(mock_graphql_gateway): """Test get video workflow status on processing.""" mock_graphql_gateway.execute.return_value = { 'data': { 'getVideoWorkflowJobDetails': { 'items': [ { 'type': 'test', 'status': 'PROCESSING', 'outputs': [] } ] } } } workflow_id = 1234 with pytest.raises(VideoException): get_workflow_status(workflow_id, 'SME') @patch('index.StateMachineSchema.load', wrap=StateMachineSchema.load) @patch('index.StateMachineSchema.dump', wrap=StateMachineSchema.dump) @patch('index.logger') @patch('index.get_workflow_status', return_value={'status': 'COMPLETE', 'error': None}) @patch('index.save_thumbnail_path') def test_handler(mock_save_thumbnail_path, mock_get_workflow_status, mock_logger, mock_context_schema_dump, mock_context_schema_load, lambda_input): """Test handler.""" handler(lambda_input, None) mock_get_workflow_status.assert_called_once() mock_save_thumbnail_path.assert_called_once() mock_context_schema_load.assert_called_once() mock_context_schema_dump.assert_called_once() @patch('index.logger') @patch('index.get_workflow_status') @patch('index.save_thumbnail_path') def test_handler_unsupported_in_content( mock_save_thumbnail_path, mock_get_workflow_status, mock_logger, lambda_input): """Test handler.""" lambda_input['context']['product']['status'] = 'in_content' handler(lambda_input, None) mock_get_workflow_status.assert_not_called() mock_save_thumbnail_path.assert_not_called() @patch('index.graphql_gateway') def test_save_thumbnail_path(mock_graphql_gateway): """Test save thumbnail path.""" product_id = 1234 workflow_result = { 'thumbnails': [ 'https://video-thumbnails.qaorch.com/small/121305/123.0000000.jpg' ] * 100 } expected_payload = { 'data': { 'update': { 'productId': str(product_id), 'thumbnailPath': '121305/123.0000000.jpg' } } } save_thumbnail_path(product_id, workflow_result) mock_graphql_gateway.execute.assert_called_once_with( SAVE_VIDEO_SINGLE, expected_payload ) def test_save_thumbnail_path_no_thumbnails(): """Test save thumbnail path no thumbnails.""" product_id = 1234 workflow_result = { 'thumbnails': [] } with pytest.raises(VideoException): save_thumbnail_path(product_id, workflow_result) @patch('index.graphql_gateway') def test_save_thumbnail_path_no_14_second_thumbnail(mock_graphql_gateway): """Test save thumbnail path no thumbnails for 14 seconds.""" product_id = 1234 workflow_result = { 'thumbnails': [ 'https://video-thumbnails.qaorch.com/small/121305/123.0000000.jpg' ] } # Should take whatever is the last thumbnail provided expected_payload = { 'data': { 'update': { 'productId': str(product_id), 'thumbnailPath': '121305/123.0000000.jpg' } } } save_thumbnail_path(product_id, workflow_result) mock_graphql_gateway.execute.assert_called_once_with( SAVE_VIDEO_SINGLE, expected_payload )