"""Test ows-video model.""" from http.client import OK import json from owsrequest.utils import mock_request import pytest from video.constants import job_io_fields from video.constants import services from video.models import ows_video from video.utils import exception as exception_utils def test_get_job(): """Test get_job.""" ows_request_mock = mock_request.get( service=services.OWS_VIDEO, path='/job/123', ) ows_video.get_job(123) assert ows_request_mock.calls == [ { 'correlation_id': None, 'method': 'GET', 'path': '/job/123' } ] def test_change_job_status(): """Test change_job_status.""" ows_request_mock = mock_request.post( service=services.OWS_VIDEO, path='/job', ) ows_video.change_job_status(123, 'cool_status') assert ows_request_mock.calls == [ { 'correlation_id': None, 'json': {'id': 123, 'status': 'cool_status'}, 'method': 'POST', 'path': '/job' } ] def test_add_job_inputs(): """Test add_job_inputs.""" ows_request_mock = mock_request.post( service=services.OWS_VIDEO, path='/job', ) ows_video.add_job_inputs(123, 'cool_inputs') assert ows_request_mock.calls == [ { 'correlation_id': None, 'json': {'id': 123, 'inputs': 'cool_inputs'}, 'method': 'POST', 'path': '/job' } ] def test_add_job_outputs(): """Test add_job_outputs.""" ows_request_mock = mock_request.post( service=services.OWS_VIDEO, path='/job', ) ows_video.add_job_outputs(123, 'cool_outputs') assert ows_request_mock.calls == [ { 'correlation_id': None, 'json': {'id': 123, 'outputs': 'cool_outputs'}, 'method': 'POST', 'path': '/job' } ] def test_get_product_metadata(): """Test get_product_metadata.""" ows_request_mock = mock_request.get( service=services.OWS_VIDEO, path='/metadata/123', response={ 'test': 'er', } ) resp = ows_video.get_product_metadata(123) assert ows_request_mock.calls == [ { 'correlation_id': None, 'method': 'GET', 'path': '/metadata/123', }, ] assert {'test': 'er'} == resp def test_is_mezzanine_ready_for_delivery(): """Test get_product_delivery_status.""" upc = 871621431207 asset_type = 'asset_type' ows_request_mock = mock_request.get( service=services.OWS_VIDEO, path='/delivery/{}/status'.format(upc), status=OK ) is_ready = ows_video.is_asset_ready_for_delivery(upc, asset_type) import time # start failing after a few days since this is a temporary fix if time.time() > 1554490795: assert ows_request_mock.calls == [ { 'correlation_id': None, 'method': 'GET', 'path': '/delivery/{}/status'.format(upc), 'params': {'asset_type': asset_type}, }, ] assert is_ready def test_set_track_metadata(): """Test set_track_metadata.""" ows_request_mock = mock_request.post( service=services.OWS_VIDEO, path='/metadata/123/track', response={'cool': 'cool'} ) ows_video.set_track_metadata({ job_io_fields.PRODUCT_ID: 123, job_io_fields.VIDEO_STREAM_FRAME_RATE_FRAMES_PER_SECOND: 21, job_io_fields.VIDEO_STREAM_WIDTH_PIXELS: 1920, job_io_fields.VIDEO_STREAM_HEIGHT_PIXELS: 768, job_io_fields.VIDEO_STREAM_DURATION_SECONDS: 123.45, job_io_fields.AUDIO_STREAM_DURATION_SECONDS: 124.58, }) assert [ { 'correlation_id': None, 'json': { 'fps': '24', 'resolution': '1080HD', 'aspect_ratio': '5:2', 'color': 'color', 'channel': '2', 'duration_minutes': 2, 'duration_seconds': 5, 'closed_caption_exists': 0, 'closed_caption_reason_id': 1, }, 'method': 'POST', 'path': '/metadata/123/track', }, ] == ows_request_mock.calls assert ows_request_mock.status == OK assert ows_request_mock.response == {'cool': 'cool'} def test_get_video_resizing_rules_request(mocker): """Test get_video_resizing_rules.""" ows_request_mock = mock_request.get( service=services.OWS_VIDEO, path='/sizing_rules', response=[ { "id": 268, "input_inner_width": 1382, "input_inner_height": 1080, "output_inner_width": 1382, "output_inner_height": 1080, "output_outer_width": 1920, "output_outer_height": 1080, "video_resolution_id": 26, }, { "id": 269, "input_inner_width": 1388, "input_inner_height": 1080, "output_inner_width": 1388, "output_inner_height": 1080, "output_outer_width": 1920, "output_outer_height": 1080, "video_resolution_id": 26, }, ] ) result = ows_video.get_video_resizing_rules() assert ows_request_mock.calls == [ { 'correlation_id': None, 'method': 'GET', 'path': '/sizing_rules' } ] assert result == ows_request_mock.response def test_get_supported_outer_resolutions(mocker): """Test get_supported_outer_resolutions.""" ows_request_mock = mock_request.get( service=services.OWS_VIDEO, path='/output_video_outer_resolutions', response=[ [ 1, 720, 404, ], [ 2, 720, 480, ], ] ) ows_video.get_supported_outer_resolutions() assert ows_request_mock.calls == [ { 'correlation_id': None, 'method': 'GET', 'path': '/output_video_outer_resolutions' } ] def test_upsert_video_asset(): """Test upsert_video_asset.""" ows_request_mock = mock_request.post( service=services.OWS_VIDEO, path='/video-asset', response={'cool': 'cool'} ) ows_video.upsert_video_asset({ 'product_id': 123, 'asset_type': 'video_image_s2', 'asset_path': '/123/abc/findme.jpg' }) assert [ { 'correlation_id': None, 'json': { 'product_id': 123, 'asset_type': 'video_image_s2', 'asset_path': '/123/abc/findme.jpg', }, 'method': 'POST', 'path': '/video-asset', }, ] == ows_request_mock.calls assert ows_request_mock.status == OK assert ows_request_mock.response == {'cool': 'cool'} @pytest.mark.parametrize( ( 'description', 'expected_status', 'expected_response', 'expected_raise_exception_count' ), [ ( '200 response from ows-video', OK, {'cool': 'cool'}, 0 ), ( '400 response from ows-video', 400, { 'code': 'bad_request', 'message': 'Bad request' }, 1 ), ( '500 response from ows-video in case of any other error', 500, { 'code': 'internal_error', 'message': 'Internal error' }, 1 ), ] ) def test_set_product_status( mocker, description, expected_status, expected_response, expected_raise_exception_count ): """Test set_product_status.""" ows_request_mock = mock_request.post( service=services.OWS_VIDEO, path='/product/123/approval', status=expected_status, response=expected_response, ) mocker.patch.object(exception_utils, 'raise_exception', autospec=True) product_id = 123 upc = 123456789 isrc = 'test_isrc' user_id = 'oa:2204' ows_video.set_product_status(product_id, upc, isrc, user_id) assert [ { 'correlation_id': None, 'json': { 'approval_type': 'final', 'value': True, 'approval_in_progress': False, 'upc': upc, 'isrc': isrc, }, 'method': 'POST', 'path': '/product/123/approval', 'headers': { 'Orchard-User-Id': user_id } }, ] == ows_request_mock.calls assert ows_request_mock.status == expected_status assert ows_request_mock.response == expected_response assert exception_utils.raise_exception.call_count == expected_raise_exception_count # noqa def test_get_track_video_duration(): """Test Get track video length seconds.""" mezzanine_metadata = json.loads( '{"video_stream_duration_seconds": 71.36, ' '"audio_stream_duration_seconds": 71.365}' ) audio_seconds = float( mezzanine_metadata[job_io_fields.AUDIO_STREAM_DURATION_SECONDS] ) video_seconds = float( mezzanine_metadata[job_io_fields.VIDEO_STREAM_DURATION_SECONDS] ) max_duration_seconds = max(audio_seconds, video_seconds) assert max_duration_seconds == 71.365