"""Lambda test module.""" from collections import namedtuple import copy import io from owsrequest import request import pytest from unittest.mock import MagicMock import s3 import video_dashboard_item_finder s3_write_success_response = {'ResponseMetadata': {'HTTPStatusCode': 200}} s3_write_500_response = {'ResponseMetadata': {'HTTPStatusCode': 500}} s3_read_success_response = { 'ResponseMetadata': { 'HTTPStatusCode': 200 }, 'Body': io.BytesIO(b'123') } s3_read_non_int_file_data_response = { 'ResponseMetadata': { 'HTTPStatusCode': 200 }, 'Body': io.BytesIO(b'testing') } s3_read_500_response = {'ResponseMetadata': {'HTTPStatusCode': 500}} get_new_assets_success_response = { 'items': [ { 'asset_id': 12345, 'dashboard_item_id': 124, 'product_id': 54321 } ], 'pagination': { 'limit': 1000, 'offset': 0, 'total_records': 1, 'type': 'standard' } } post_setup_workflow_success_response = [ { 'id': 123, 'type': 'transfer_from_windows_to_s3' } ] get_max_dashboard_item_id_success_response = { 'max_dashboard_item_id': 123 } def mock_process(expected_response, mocker): """Mock request call manually.""" Response = namedtuple('response', ['json', 'raise_for_status']) json_mock = MagicMock(return_value=expected_response) response = Response(json_mock, MagicMock()) mocker.patch.object(request, 'process', return_value=response) def test_check_dashboard_item_id_file_exists_on_s3_success(mocker): """Test reading the max video dashboard item id from s3.""" mocker.patch.object( s3, 'object_exists', return_value=True, autospec=True ) result = video_dashboard_item_finder.\ check_dashboard_item_id_file_exists_on_s3() assert result is True @pytest.mark.parametrize(( 'test_description', 's3_response', 'expected_response' ), [ ( 'test successful write to s3', copy.deepcopy(s3_read_success_response), 123, ), ]) def test_read_max_video_dashboard_id_from_s3_success( mocker, test_description, s3_response, expected_response, ): """Test reading the max video dashboard item id from s3.""" mocker.patch.object( s3, 'get_object', return_value=s3_response, autospec=True ) result = video_dashboard_item_finder.\ read_max_video_dashboard_item_id_from_s3() assert result == expected_response @pytest.mark.parametrize(( 'test_description', 's3_response', ), [ ( 'test 500 exception while reading from s3', copy.deepcopy(s3_read_500_response), ), ( 'test invalid file data while reading from s3', copy.deepcopy(s3_read_non_int_file_data_response), ), ]) def test_read_max_video_dashboard_id_from_s3_exception( mocker, test_description, s3_response, ): """Test exceptions when reading the max video dashboard item id.""" mocker.patch.object( s3, 'get_object', return_value=s3_response, autospec=True ) with pytest.raises(Exception): video_dashboard_item_finder.\ read_max_video_dashboard_item_id_from_s3() @pytest.mark.parametrize(( 'test_description', 'max_video_dashboard_item_id', 's3_response', 'expected_response' ), [ ( 'test successful write to s3', 123, copy.deepcopy(s3_write_success_response), True, ), ]) def test_write_max_video_dashboard_id_to_s3_success( mocker, test_description, max_video_dashboard_item_id, s3_response, expected_response, ): """Test writing the max video dashboard item id to s3.""" mocker.patch.object( s3, 'put_object', return_value=s3_response, autospec=True ) result = video_dashboard_item_finder.\ write_max_video_dashboard_item_id_to_s3( max_video_dashboard_item_id) assert result == expected_response @pytest.mark.parametrize(( 'test_description', 'max_video_dashboard_item_id', 's3_response' ), [ ( 'test exception while writing to s3', 123, copy.deepcopy(s3_write_500_response), ), ( 'test invalid parameter for write to s3', 'test string input', copy.deepcopy(s3_write_success_response), ), ]) def test_write_max_video_dashboard_id_to_s3_exception( mocker, test_description, max_video_dashboard_item_id, s3_response, ): """Test exceptions when writing the max video dashboard item id to s3.""" mocker.patch.object( s3, 'put_object', return_value=s3_response, autospec=True ) with pytest.raises(Exception): video_dashboard_item_finder.\ write_max_video_dashboard_item_id_to_s3( max_video_dashboard_item_id) @pytest.mark.parametrize(( 'test_description', 'video_dashboard_item_id', 'expected_response', ), [ ( 'test successful get new assets', 123, copy.deepcopy(get_new_assets_success_response), ), ]) def test_get_new_assets_success( test_description, video_dashboard_item_id, expected_response, mocker ): """Test getting the assets for new videos.""" mock_process(expected_response, mocker) result = video_dashboard_item_finder.get_new_assets( video_dashboard_item_id) assert result == expected_response @pytest.mark.parametrize(( 'test_description', 'video_dashboard_item_id', ), [ ( 'test 404 exception when getting new assets', 'invalid_input', ), ]) def test_get_new_assets_exception( test_description, video_dashboard_item_id, ): """Test exceptions when getting the assets for new videos.""" with pytest.raises(Exception): video_dashboard_item_finder.get_new_assets(video_dashboard_item_id) def test_handler_first_run_success(mocker): """Test handler when getting the max dashboard item id on first run.""" mocker.patch.object( video_dashboard_item_finder, 'check_dashboard_item_id_file_exists_on_s3', return_value=False, autospec=True ) mocker.patch.object( video_dashboard_item_finder, 'get_max_dashboard_item_id', return_value=get_max_dashboard_item_id_success_response, autospec=True ) mocker.patch.object( video_dashboard_item_finder, 'write_max_video_dashboard_item_id_to_s3', return_value=True, autospec=True ) video_dashboard_item_finder.handler(None, None) video_dashboard_item_finder.write_max_video_dashboard_item_id_to_s3.\ assert_called_once_with(123) def test_handler_read_s3_success(mocker): """Test handler when reading the max dashboard item id from S3.""" mocker.patch.object( video_dashboard_item_finder, 'check_dashboard_item_id_file_exists_on_s3', return_value=True, autospec=True ) mocker.patch.object( video_dashboard_item_finder, 'read_max_video_dashboard_item_id_from_s3', return_value=123, autospec=True ) mocker.patch.object( video_dashboard_item_finder, 'get_new_assets', return_value=get_new_assets_success_response, autospec=True ) mocker.patch.object( video_dashboard_item_finder, 'post_setup_workflow', return_value=post_setup_workflow_success_response, autospec=True ) mocker.patch.object( video_dashboard_item_finder, 'write_max_video_dashboard_item_id_to_s3', return_value=True, autospec=True ) video_dashboard_item_finder.handler(None, None) video_dashboard_item_finder.check_dashboard_item_id_file_exists_on_s3.\ assert_called_once() video_dashboard_item_finder.read_max_video_dashboard_item_id_from_s3.\ assert_called_once_with() video_dashboard_item_finder.get_new_assets.assert_called_once_with(123) video_dashboard_item_finder.post_setup_workflow.\ assert_called_once_with(54321) video_dashboard_item_finder.write_max_video_dashboard_item_id_to_s3.\ assert_called_once_with(124) @pytest.mark.parametrize(( 'test_description', 'product_id', 'expected_response', ), [ ( 'test successful post setup workflow', 54321, copy.deepcopy(post_setup_workflow_success_response), ), ]) def test_post_setup_workflow_success( test_description, product_id, expected_response, mocker ): """Test posting setup workflow for new videos.""" mock_process(expected_response, mocker) result = video_dashboard_item_finder.post_setup_workflow(product_id) assert result == expected_response def test_handler_exception(): """Test exceptions when calling the handler.""" with pytest.raises(Exception): video_dashboard_item_finder.handler(None, None) @pytest.mark.parametrize(( 'test_description', 'product_id', 'expected_response', ), [ ( 'test 404 exception when posting the setup workflow', 'invalid_input', copy.deepcopy(post_setup_workflow_success_response) ), ]) def test_post_setup_workflow_exception( test_description, product_id, expected_response, mocker, ): """Test exceptions when posting the setup workflow.""" Response = namedtuple('response', ['json', 'raise_for_status']) json_mock = MagicMock(return_value=expected_response) response = Response(json_mock, MagicMock()) mocker.patch.object(request, 'process', return_value=response) with pytest.raises(Exception): video_dashboard_item_finder.post_setup_workflow(product_id) def test_get_dashboard_item_id_success(mocker): """Test getting the max dashboard item id.""" mock_process(get_max_dashboard_item_id_success_response, mocker) result = video_dashboard_item_finder.get_max_dashboard_item_id() assert result == get_max_dashboard_item_id_success_response def test_get_dashboard_item_id_exception(): """Test exceptions when getting the max dashboard item id.""" with pytest.raises(Exception): video_dashboard_item_finder.get_max_dashboard_item_id()