"""Tests for activity task logger decorator.""" from unittest.mock import call import pytest import sentry_sdk from video.constants import job_io_fields from video.constants import job_statuses from video.logic.activity_task_logger import activity_task_logger from video.models import ows_video job_id_field = ( 'super_cool_activity_task_handler_long_name_applesauce_bananas_job_id') super_cool_job_id = 12 alternate_job_id = 12321 test_a_input_fields = [ job_io_fields.INPUT_VIDEO_S3_BUCKET, job_io_fields.INPUT_VIDEO_S3_KEY, ] test_a_inputs = { job_id_field: super_cool_job_id, job_io_fields.INPUT_VIDEO_S3_BUCKET: 'foo', job_io_fields.INPUT_VIDEO_S3_KEY: 'bar', 'foo': 'bar', } test_a_filtered_inputs = { job_io_fields.INPUT_VIDEO_S3_BUCKET: 'foo', job_io_fields.INPUT_VIDEO_S3_KEY: 'bar', } test_a_expected_outputs = { job_io_fields.INPUT_VIDEO_S3_BUCKET: 'foo', job_io_fields.INPUT_VIDEO_S3_KEY: 'bar', } @pytest.mark.parametrize( ( 'test_description', 'input_fields', 'is_first_handler', 'is_last_handler', 'job_type', 'inputs', 'filtered_inputs', 'expected_outputs', 'expected_set_job_fields_calls', 'job_id', 'output_fields_to_omit_from_pipeline_payload', ), [ ( 'test inputs filtered before passed to handler.', test_a_input_fields, True, True, None, test_a_inputs, test_a_filtered_inputs, test_a_expected_outputs, [ call({ 'id': super_cool_job_id, 'status': job_statuses.PROGRESSING, 'inputs': test_a_filtered_inputs, }), call({ 'id': super_cool_job_id, 'status': job_statuses.COMPLETE, 'outputs': test_a_expected_outputs, }), ], super_cool_job_id, [], ), ( 'test passing job_type.', test_a_input_fields, True, True, 'abc123', {**test_a_inputs, 'abc123_job_id': 12321}, test_a_filtered_inputs, test_a_expected_outputs, [ call({ 'id': 12321, 'status': job_statuses.PROGRESSING, 'inputs': test_a_filtered_inputs, }), call({ 'id': 12321, 'status': job_statuses.COMPLETE, 'outputs': test_a_expected_outputs, }), ], 12321, [], ), ( 'test is_last_handler == False.', test_a_input_fields, True, False, None, test_a_inputs, test_a_filtered_inputs, test_a_expected_outputs, [ call({ 'id': super_cool_job_id, 'status': job_statuses.PROGRESSING, 'inputs': test_a_filtered_inputs, }), call({ 'id': super_cool_job_id, 'outputs': test_a_expected_outputs, }), ], super_cool_job_id, [], ), ( 'test is_first_handler == False.', test_a_input_fields, False, True, None, test_a_inputs, test_a_filtered_inputs, test_a_expected_outputs, [ call({ 'id': super_cool_job_id, 'status': job_statuses.COMPLETE, 'outputs': test_a_expected_outputs, }), ], super_cool_job_id, [], ), ( 'test is_first_handler == False and is_last_handler == False.', test_a_input_fields, False, False, None, test_a_inputs, test_a_filtered_inputs, test_a_expected_outputs, [], super_cool_job_id, [], ), ( 'eetest is_first_handler == False and is_last_handler == False with error.', # noqa test_a_input_fields + ['error_test'], False, False, None, {**test_a_inputs, 'error_test': 'error test'}, {**test_a_filtered_inputs, 'error_test': 'error test'}, {**test_a_expected_outputs, 'error_test': 'error test'}, [ call({ 'id': super_cool_job_id, 'status': job_statuses.ERROR, 'outputs': { **test_a_expected_outputs, 'error_test': 'error test'}, }), ], super_cool_job_id, [], ), ( 'test activity task handler returned errors.', test_a_input_fields + ['error_test'], True, True, None, {**test_a_inputs, 'error_test': 'error test'}, {**test_a_filtered_inputs, 'error_test': 'error test'}, {**test_a_expected_outputs, 'error_test': 'error test'}, [ call({ 'id': super_cool_job_id, 'status': job_statuses.PROGRESSING, 'inputs': { **test_a_expected_outputs, 'error_test': 'error test'}, }), call({ 'id': super_cool_job_id, 'status': job_statuses.ERROR, 'outputs': { **test_a_expected_outputs, 'error_test': 'error test'}, }), ], super_cool_job_id, [], ), ( 'test output_fields_to_omit_from_pipeline_payload', test_a_input_fields, True, True, None, test_a_inputs, test_a_filtered_inputs, { job_io_fields.INPUT_VIDEO_S3_KEY: test_a_expected_outputs[job_io_fields.INPUT_VIDEO_S3_KEY] }, [ call({ 'id': super_cool_job_id, 'status': job_statuses.PROGRESSING, 'inputs': test_a_filtered_inputs, }), call({ 'id': super_cool_job_id, 'status': job_statuses.COMPLETE, 'outputs': test_a_expected_outputs, }), ], super_cool_job_id, [job_io_fields.INPUT_VIDEO_S3_BUCKET], ), ]) def test_activity_task_logger( mocker, test_description, input_fields, is_first_handler, is_last_handler, job_type, inputs, filtered_inputs, expected_outputs, expected_set_job_fields_calls, job_id, output_fields_to_omit_from_pipeline_payload): """Test activity task logger.""" mocker.patch.object(ows_video, 'set_job_fields', autospec=True) @activity_task_logger( input_fields, is_first_handler=is_first_handler, is_last_handler=is_last_handler, job_type=job_type, output_fields_to_omit_from_pipeline_payload=( output_fields_to_omit_from_pipeline_payload) ) def super_cool_activity_task_handler_long_name_applesauce_bananas(event): return event result = super_cool_activity_task_handler_long_name_applesauce_bananas( inputs) ows_video.set_job_fields.assert_has_calls(expected_set_job_fields_calls) assert result == expected_outputs def test_activity_task_logger_with_handler_exception(mocker): """Test activity task logger with handler exception.""" mocker.patch.object(ows_video, 'set_job_fields', autospec=True) mocker.patch.object(sentry_sdk, 'capture_exception', autospec=True) @activity_task_logger([]) def super_cool_activity_task_handler_long_name_applesauce_bananas(event): raise Exception('oh noes!') expected_outputs = { job_io_fields.ERROR_UNKNOWN: 'oh noes!', } result = super_cool_activity_task_handler_long_name_applesauce_bananas({ 'super_cool_activity_task_handler_long_name_applesauce_bananas_job_id': 221100 }) ows_video.set_job_fields.assert_has_calls([ call({ 'id': 221100, 'status': job_statuses.PROGRESSING, }), call({ 'id': 221100, 'status': job_statuses.ERROR, 'outputs': expected_outputs, }), ]) sentry_sdk.capture_exception.assert_called_once() assert result == expected_outputs