"""Test integration of an app with SQS.""" from flexmock import flexmock from moto import mock_sqs from owslogger import logger from label_copy_export.connectors import sqs TEST_CORRELATION_ID = 'c305d169-455b-4bdd-ab69-0c49c49ae4fc' TEST_RECEIVE_COUNT = 5 sqs_attributes_correlation_id = { sqs.CORRELATION_ID_ATTRIBUTE: { 'string_value': TEST_CORRELATION_ID } } sqs_attributes_receive_count = { sqs.APPROXIMATE_RECEIVE_COUNT_ATTRIBUTE: TEST_RECEIVE_COUNT } def test_get_connection(): """Test if get_connection() returns a valid connection object.""" with mock_sqs(): connection = sqs.get_connection( access_key_id='test', secret_access_key='test-key') assert connection is not None def test_get_queue(): """Test if get_queue() returns a Queue with correct message_class.""" with mock_sqs(): connection = sqs.get_connection( access_key_id='test', secret_access_key='test-key') connection.create_queue('dev-test-queue') queue = sqs.get_queue('dev-test-queue', sqs_connection=connection) assert queue is not None assert queue.message_class == sqs.JSONMessageExt def test_json_message_ext_context(): """Test if JSONMessageExt context can store and return a test value.""" json_message = sqs.JSONMessageExt() json_message.context.test_value = 'test' assert json_message.context.test_value == 'test' def test_json_message_ext_correlation_id(): """Test if JSONMessageExt can return correlation_id.""" json_message = sqs.JSONMessageExt() flexmock(json_message).should_receive('message_attributes'). \ and_return(sqs_attributes_correlation_id) assert json_message.correlation_id == TEST_CORRELATION_ID def test_json_message_ext_no_correlation_id(): """Test JSONMessageExt. Test if JSONMessageExt can return None for message with empty correlation_id header. """ json_message = sqs.JSONMessageExt() assert json_message.correlation_id is None def test_json_message_ext_logger(): """Test if JSONMessageExt can return OwsLoggingAdapter.""" json_message = sqs.JSONMessageExt() flexmock(json_message).should_receive('message_attributes'). \ and_return(sqs_attributes_correlation_id) assert isinstance(json_message.logger, logger.OwsLoggingAdapter) def test_json_message_ext_receive_count(): """Test if JSONMessageExt can return approximate receive count.""" json_message = sqs.JSONMessageExt() flexmock(json_message).should_receive('attributes'). \ and_return(sqs_attributes_receive_count) assert json_message.receive_count == TEST_RECEIVE_COUNT