"""SQS connector tests.""" import json from flexmock import flexmock from moto import mock_aws from owslogger import logger import pytest from availability.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_aws(): connection = sqs.get_connection() assert connection is not None @pytest.mark.parametrize('explicit_connection', [True, False]) def test_get_queue_success(explicit_connection): """Test get_queue returns a Queue with correct message_class.""" dev_test_queue = 'dev-test-queue' with mock_aws(): connection = sqs.get_connection() connection.create_queue(QueueName=dev_test_queue) if explicit_connection: queue = sqs.get_queue(dev_test_queue, sqs_connection=connection) else: queue = sqs.get_queue(dev_test_queue) assert queue is not None assert queue.url def test_get_queue_none(): """Test get_queue returns None when there is no queue.""" mock_connection = flexmock() (flexmock(sqs) .should_receive('get_connection') .and_return(mock_connection)) (flexmock(mock_connection) .should_receive('get_queue_by_name') .and_return(None)) queue = sqs.get_queue('does-not-matter') assert queue is None def test_json_message_ext_correlation_id(): """Test if JSONMessageExt can return correlation_id.""" mock_msg = flexmock( message_attributes={ 'Correlation-Id': {'StringValue': TEST_CORRELATION_ID}}) json_message = sqs.JSONMessageExt(mock_msg) assert json_message.correlation_id == \ 'c305d169-455b-4bdd-ab69-0c49c49ae4fc' def test_get_queue_failure(): """Test get_queue returns None on unexpected exception.""" (flexmock(sqs) .should_receive('get_connection') .and_raise(ValueError())) queue = sqs.get_queue('does-not-matter') assert queue is None def test_json_message_ext_no_correlation_id(): """Test if JSONMessageExt return None for empty correlation_id header.""" mock_msg = flexmock( message_attributes={}) json_message = sqs.JSONMessageExt(mock_msg) assert json_message.correlation_id is None def test_json_message_ext_logger(): """Test if JSONMessageExt can return OwsLoggingAdapter.""" mock_msg = flexmock( message_attributes={'Correlation-Id': {'StringValue': '1234'}}) json_message = sqs.JSONMessageExt(mock_msg) # Accessing message logger again should return the same instance. assert isinstance(json_message.logger, logger.OwsLoggingAdapter) def test_json_message_ext_receive_count(): """Test if JSONMessageExt can return approximate receive count.""" mock_msg = flexmock(attributes={'ApproximateReceiveCount': 5}) json_message = sqs.JSONMessageExt(mock_msg) assert json_message.receive_count == TEST_RECEIVE_COUNT @mock_aws def test_sqs_message_body(): """Test if message body is received decoded.""" dev_test_queue = 'dev-test-queue' connection = sqs.get_connection() connection.create_queue(QueueName=dev_test_queue) queue = sqs.get_queue(dev_test_queue, sqs_connection=connection) message = { 'content_owner_id': 'theorchardmusic', 'isrc': 'US26V4410006', 'territories': ['US'] } message_body = json.dumps(message) queue.send_message(MessageBody=message_body) res_message = queue.receive_messages() json_message = sqs.JSONMessageExt(res_message[0]) assert json_message.get_body() == message