"""Integration tests for lambda function.""" import json import boto3 import pytest from lambdacommon.common_config import logger from tests.consts import config as test_config # noqa from tests.integration.utils import neo4j_connector class TestSuccessfulCheck: """Test successful check.""" @pytest.fixture def upc(self): """Fixture for product UPC.""" return 4050486998229 @pytest.fixture def lambda_payload(self, upc): """Fixture for lambda payload.""" return json.dumps({'upc': upc}) @pytest.fixture def lambda_response(self, lambda_payload): """Fixture for lambda invocation response.""" client = boto3.client('lambda', region_name=test_config.AWS_REGION) response = client.invoke( FunctionName=test_config.LAMBDA_NAME, InvocationType='RequestResponse', LogType='None', Payload=lambda_payload, ) return json.loads(response['Payload'].read().decode('utf-8')) def test_successful_invocation(self, lambda_response): """Test successful invocation.""" assert lambda_response['status'] == 'OK' assert lambda_response['DDEX_CHECK_RESULT'] == 'DDEX_CHECK_SUCCESS' class TestFailure: """Test failure execution.""" @pytest.fixture def upc(self): """Fixture for product UPC.""" return 196006318888 @pytest.fixture def lambda_payload(self, upc): """Fixture for lambda payload.""" return json.dumps({'upc': upc}) @pytest.fixture def prepare_data(self): """Fixture for preparing test data.""" logger.info('Preparing test data for check-ddex-generation failure...') return neo4j_connector.execute_query( """ OPTIONAL MATCH (:Product:Orchard {id: $product_id})-[pr]- (:LabelParticipant {id: $label_participant_id}) OPTIONAL MATCH (:Track:Orchard {id: $track_id})-[tr]- (:LabelParticipant {id: $label_participant_id}) DELETE pr, tr; """, { 'product_id': 3284601, 'track_id': 34506529, 'label_participant_id': 367992272, } ) @pytest.fixture def lambda_response(self, lambda_payload, prepare_data): """Fixture for lambda invocation response.""" client = boto3.client('lambda', region_name=test_config.AWS_REGION) response = client.invoke( FunctionName=test_config.LAMBDA_NAME, InvocationType='RequestResponse', LogType='None', Payload=lambda_payload, ) logger.info(f'Lambda invocation response {response}') return json.loads(response['Payload'].read().decode('utf-8')) def test_ddex_failure_response(self, lambda_response): """Test failure response.""" assert lambda_response['errorType'] == 'DDEXCheckException'