"""Integration tests for lambda function.""" import json import pytest from lambdacommon.aws.sfn import StateMachineExecutionTest from lambdacommon.aws.sfn import StateType from lambdacommon.common_config import logger from tests.integration.utils import neo4j_connector STEP_FUNCTIONS_ARN = \ 'arn:aws:states:us-east-1:437795906767:stateMachine:qa-metadata-consistency-checks-sfn' class TestNoInconsistencies: """Test successful execution.""" test_name = 'test-no-inconsistencies' @pytest.fixture(scope='class') def execution_input(self): """Fixture execution input.""" return json.dumps({'product_id': 2994687}) @pytest.fixture(scope='class') def execution(self, execution_input): """Fixture execution.""" test_instance = StateMachineExecutionTest( sfn_arn=STEP_FUNCTIONS_ARN, test_name=self.test_name, execution_input=execution_input, ) test_instance.run() test_instance.wait_execution_end() return test_instance def test_successful_execution(self, execution): """Test successful execution.""" assert execution.was_successful is True def test_ddex_task_was_called(self, execution): """Test DDEX Check task was called.""" assert execution.task_was_called('DDEX Check') is True def test_data_sync_was_not_called(self, execution): """Test data sync task was not called.""" assert execution.task_was_called('kinesis-to-neo4j') is False def test_output(self, execution): """Test successful execution output.""" output = json.loads(execution.final_output) assert output['DDEX_check']['success']['DDEX_CHECK_RESULT'] == 'DDEX_CHECK_SUCCESS' class TestProductParticipationInconsistencies: """Test product participation inconsistencies.""" test_name = 'test-product-participation-inconsistencies' @pytest.fixture(scope='class') def product_id(self): """Fixture for product id.""" return 2237375 @pytest.fixture(scope='class') def label_participant_id(self): """Fixture for label participant id.""" return 98702 @pytest.fixture(scope='class') def execution_input(self, product_id): """Fixture execution input.""" return json.dumps({'product_id': product_id}) @pytest.fixture(scope='class') def state_machine(self, execution_input): """Fixture execution.""" return StateMachineExecutionTest( sfn_arn=STEP_FUNCTIONS_ARN, test_name=self.test_name, execution_input=execution_input, ) @pytest.fixture(scope='class') def prepare_data(self, state_machine, product_id, label_participant_id): """Fixture for preparing test data.""" if not state_machine.was_started_parallel: logger.info(f'Preparing data for {self.test_name}...') return neo4j_connector.execute_query( 'MATCH (:Product {id: $product_id})<-[deleted_relation:PARTICIPATED_IN]-' '(:LabelParticipant {id: $label_participant_id}) ' 'DETACH DELETE deleted_relation ' 'RETURN deleted_relation', { 'product_id': product_id, 'label_participant_id': label_participant_id, } ) return None @pytest.fixture(scope='class') def execution(self, state_machine, prepare_data): """Fixture execution.""" if not state_machine.was_started_parallel: state_machine.run() state_machine.wait_execution_end() return state_machine def test_product_partitipations_check_output(self, execution): """Test Product Participations Check output.""" task = execution.get_task('Product Participations Check', StateType.EXITED) output = json.loads(task.details['output']) missing_data = output['report']['missing_data'] assert len(missing_data) == 1 record = missing_data[0] assert record['record_id'] == 4738064 assert record['table_name'] == 'release_artist' assert record['primary_key_name'] == 'release_artist_id' def test_fetch_product_participations_output(self, execution): """Test Fetch Product Participations output.""" task = execution.get_task('fetch-product-participations', StateType.EXITED) output = json.loads(task.details['output']) records_to_sync = output['recordToSync']['Records'] assert len(records_to_sync) == 1 kinesis_payload = records_to_sync[0]['kinesis']['data'] assert kinesis_payload != '' def test_data_sync_was_called(self, execution): """Test data sync task was called.""" assert execution.task_was_called('kinesis-to-neo4j') is True def test_ddex_task_was_called(self, execution): """Test DDEX Check task was called.""" assert execution.task_was_called('DDEX Check') is True def test_ddex_check_output(self, execution): """Test Product Participations Check output.""" task = execution.get_task('DDEX Check', StateType.EXITED) output = json.loads(task.details['output']) assert output['DDEX_check']['success']['DDEX_CHECK_RESULT'] == 'DDEX_CHECK_SUCCESS' def test_successful_execution(self, execution): """Test successful execution.""" assert execution.was_successful is True def test_data_prepared_properly(self, prepare_data, state_machine): """Test data was prepared properly.""" if state_machine.was_started_parallel: pytest.skip('Test was started in parallel, skipping data preparation check!') else: assert len(prepare_data.records) == 1, ( f'Test data for "{self.test_name}" was not prepared properly!!!\n' f'Please try to rerun the test!!!')