"""Lambda test module.""" import json import boto3 import moto import pytest from constants import common_fields from constants import const from constants import fields import index @pytest.fixture def ddb_stream_dict(): """Return typical ddb stream new value part.""" return { 's3_key': {'S': '2000'}, 'created_at': {'S': '2017-08-28T10:00:02'}, 'encoder_id': {'N': '18'}, 'priority': {'N': '1'}, 'meta_update': {'S': 'Y'}, 'order_id': {'S': '2000'}, 's3_bucket_name': {'S': 'dev-vector-order'} } @pytest.fixture def original_order_dict(): """Return typical S3 VO content.""" return { common_fields.VO_ORDER_ID: '2000', common_fields.S3_VO_STORES: [1], common_fields.S3_VO_PRODUCTS: [9999999], common_fields.VO_META_UPDATE: 'Y', common_fields.VO_ENCODER_ID: 18, common_fields.VO_CREATED_AT: '2017-08-28T10:00:02', common_fields.S3_VO_VALIDATED: {'9999999': [1]}, common_fields.VO_PRIORITY: 1 } @pytest.fixture def received_order_dict(): """Return processed order data that we originally got from S3.""" result = original_order_dict() result[common_fields.S3_VO_VALIDATED] = { int(k): v for k, v in result[common_fields.S3_VO_VALIDATED].items()} return result @pytest.fixture def details_list(): """Return list of data for saving details in dd.""" return [ {fields.EQ_DETAIL_ENCODING_QUEUE_ID: 100, fields.EQ_DETAIL_STORE_ID: 2, fields.EQ_DETAIL_UPC: 10}, {fields.EQ_DETAIL_ENCODING_QUEUE_ID: 100, fields.EQ_DETAIL_STORE_ID: 1, fields.EQ_DETAIL_UPC: 11}, {fields.EQ_DETAIL_ENCODING_QUEUE_ID: 100, fields.EQ_DETAIL_STORE_ID: 3, fields.EQ_DETAIL_UPC: 11}] @pytest.fixture def details_dict(): """Return typical new validated dict.""" return {10: [2], 11: [1, 3]} def test_get_order_type_by_encoder_id(): """Test index.get_order_type_by_encoder_id function.""" assert (index.get_order_type_by_encoder_id( const.VO_ENCODER_VIDEO) == const.VO_TYPE_RELEASE) assert (index.get_order_type_by_encoder_id( const.VO_ENCODER_VECTOR_2_5) == const.VO_TYPE_RELEASE) assert (index.get_order_type_by_encoder_id( const.VO_ENCODER_VECTOR_BULK_FEEDS) == const.VO_TYPE_RELEASE) assert (index.get_order_type_by_encoder_id( const.VO_ENCODER_VECTOR_2_5_HD) == const.VO_TYPE_HARDDRIVE) with pytest.raises(Exception): index.get_order_type_by_encoder_id(-1) @moto.mock_s3 def test_get_dict_from_s3(original_order_dict, received_order_dict): """Test index.get_json_from_s3 function.""" s3_bucket = 'bucket' s3_key = 'key' # get s3 mock s3 = boto3.resource('s3') # create bucket mock s3.create_bucket(Bucket=s3_bucket) # generate key and put to s3 mock s3.Object(s3_bucket, s3_key).put(Body=json.dumps(original_order_dict)) s3_data = index.get_dict_from_s3(s3_bucket, s3_key) assert s3_data == received_order_dict def patch_session_scope(mocker, return_value): """Patch session scope function.""" # session mock mocked_session = mocker.Mock() mocked_session.execute.return_value = return_value # context mock mocked_context = mocker.Mock() mocked_context.__enter__ = mocker.Mock( return_value=mocked_session) mocked_context.__exit__ = mocker.Mock(return_value=None) # session scope function mock mocked_session_scope = mocker.patch( 'connectors.direct_delivery.session_scope') mocked_session_scope.return_value = mocked_context return mocked_session def test_save_order_to_dd_db_not_exists(mocker, received_order_dict): """Test index.save_order_to_dd_db function when record does not exist.""" # patch session scope return_val = mocker.Mock() return_val.lastrowid = 100 mocked_session = patch_session_scope(mocker, return_val) result = index.save_order_to_dd_db(received_order_dict) assert result == 100 assert mocked_session.execute.call_count == 1 def test_save_order_to_dd_db_exists(mocker, received_order_dict): """Test index.save_order_to_dd_db function when record exists in db.""" # patch session scope return_val = mocker.Mock() return_val.lastrowid = 0 return_val.fetchone = mocker.Mock() return_val.fetchone.return_value = [200] mocked_session = patch_session_scope(mocker, return_val) result = index.save_order_to_dd_db(received_order_dict) assert result == 200 assert mocked_session.execute.call_count == 2 def test_get_all_order_details(mocker, details_dict, details_list): """Test index.get_all_order_details function.""" # patch session scope patch_session_scope(mocker, details_list) result = index.get_all_order_details(100) assert result == details_dict def test_get_new_order_details(details_dict): """Test index.get_new_order_details function.""" result = index.get_new_order_details( {10: [1]}, {10: [1, 2], 11: [1, 3]}) assert result == details_dict def test_convert_details_to_list(details_dict, details_list): """Test index.convert_details_to_list function.""" result = index.convert_details_to_list(100, details_dict) assert result == details_list def test_save_details_to_dd_db(mocker, details_list): """Test index.save_details_to_dd_db function.""" # patch session scope mocked_session = patch_session_scope(mocker, None) sql_text = 'test' mocked_sqlalchemy_text = mocker.patch('index.sqlalchemy.text') mocked_sqlalchemy_text.return_value = sql_text index.save_details_to_dd_db(details_list) assert mocked_session.execute.call_count == 1 assert mocked_session.execute.call_args[0] == ( sql_text, details_list) def test_handler( mocker, ddb_stream_dict, received_order_dict, details_dict, details_list): """Test main handler method.""" mocked_get_dict_from_s3 = mocker.patch('index.get_dict_from_s3') mocked_get_dict_from_s3.return_value = received_order_dict mocked_save_order_to_dd_db = mocker.patch('index.save_order_to_dd_db') mocked_save_order_to_dd_db.return_value = 100 mocked_get_all_order_details = mocker.patch('index.get_all_order_details') mocked_get_all_order_details.return_value = details_dict mocked_get_new_order_details = mocker.patch('index.get_new_order_details') mocked_get_new_order_details.return_value = ( received_order_dict[common_fields.S3_VO_VALIDATED]) mocked_convert_details_to_list = ( mocker.patch('index.convert_details_to_list')) mocked_convert_details_to_list.return_value = details_list mocked_save_details_to_dd_db = mocker.patch('index.save_details_to_dd_db') index.handler(ddb_stream_dict, None) assert mocked_get_dict_from_s3.call_count == 1 assert mocked_get_dict_from_s3.call_args[0] == ( ddb_stream_dict[common_fields.DDB_VO_BUCKET_NAME]['S'], ddb_stream_dict[common_fields.DDB_VO_S3_KEY]['S']) assert mocked_save_order_to_dd_db.call_count == 1 assert mocked_save_order_to_dd_db.call_args[0] == (received_order_dict,) assert mocked_get_all_order_details.call_count == 1 assert mocked_get_all_order_details.call_args[0] == (100,) assert mocked_get_new_order_details.call_count == 1 assert mocked_get_new_order_details.call_args[0] == ( details_dict, received_order_dict[common_fields.S3_VO_VALIDATED]) assert mocked_convert_details_to_list.call_count == 1 assert mocked_convert_details_to_list.call_args[0] == ( 100, received_order_dict[common_fields.S3_VO_VALIDATED]) assert mocked_save_details_to_dd_db.call_count == 1 assert mocked_save_details_to_dd_db.call_args[0] == (details_list,)