Exemple #1
0
    def setUp(self):
        args = {'owner': 'airflow', 'start_date': DEFAULT_DATE}

        self.dag = DAG('test_dag_id', default_args=args)
        self.operator = SQSPublishOperator(task_id='test_task',
                                           dag=self.dag,
                                           sqs_queue='test',
                                           message_content='hello',
                                           aws_conn_id='aws_default')

        self.mock_context = MagicMock()
        self.sqs_hook = SQSHook()
class TestSQSPublishOperator(unittest.TestCase):
    def setUp(self):
        args = {'owner': 'airflow', 'start_date': DEFAULT_DATE}

        self.dag = DAG('test_dag_id', default_args=args)
        self.operator = SQSPublishOperator(
            task_id='test_task',
            dag=self.dag,
            sqs_queue='test',
            message_content='hello',
            aws_conn_id='aws_default',
        )

        self.mock_context = MagicMock()
        self.sqs_hook = SQSHook()

    @mock_sqs
    def test_execute_success(self):
        self.sqs_hook.create_queue('test')

        result = self.operator.execute(self.mock_context)
        self.assertTrue('MD5OfMessageBody' in result)
        self.assertTrue('MessageId' in result)

        message = self.sqs_hook.get_conn().receive_message(QueueUrl='test')

        self.assertEqual(len(message['Messages']), 1)
        self.assertEqual(message['Messages'][0]['MessageId'],
                         result['MessageId'])
        self.assertEqual(message['Messages'][0]['Body'], 'hello')

        context_calls = []

        self.assertTrue(self.mock_context['ti'].method_calls == context_calls,
                        "context call  should be same")
Exemple #3
0
        dag_id='example_sqs',
        schedule_interval=None,
        start_date=datetime(2021, 1, 1),
        dagrun_timeout=timedelta(minutes=60),
        tags=['example'],
        catchup=False,
) as dag:
    # [START howto_sqs_operator_and_sensor]

    # Using a task-decorated function to create an SQS queue
    create_queue = create_queue_fn()

    publish_to_queue = SQSPublishOperator(
        task_id='publish_to_queue',
        sqs_queue=create_queue,
        message_content="{{ task_instance }}-{{ execution_date }}",
        message_attributes=None,
        delay_seconds=0,
        aws_conn_id=AWS_CONN_ID,
    )

    read_from_queue = SQSSensor(
        task_id='read_from_queue',
        sqs_queue=create_queue,
        max_messages=5,
        wait_time_seconds=1,
        visibility_timeout=None,
        message_filtering=None,
        message_filtering_match_values=None,
        message_filtering_config=None,
        aws_conn_id=AWS_CONN_ID,
    )