mans2singh commented on a change in pull request #4887: [AIRFLOW-4055] Add AWS 
SQS Sensor
URL: https://github.com/apache/airflow/pull/4887#discussion_r274982989
 
 

 ##########
 File path: tests/contrib/sensors/test_sqs_sensor.py
 ##########
 @@ -0,0 +1,109 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+
+import unittest
+from airflow import DAG, configuration
+from airflow.contrib.sensors.aws_sqs_sensor import SQSSensor
+from airflow.utils import timezone
+from mock import patch, MagicMock
+from airflow.exceptions import AirflowException
+from moto import mock_sqs
+from airflow.contrib.hooks.aws_sqs_hook import SQSHook
+
+DEFAULT_DATE = timezone.datetime(2017, 1, 1)
+
+
+class TestSQSSensor(unittest.TestCase):
+
+    def setUp(self):
+        configuration.load_test_config()
+
+        args = {
+            'owner': 'airflow',
+            'start_date': DEFAULT_DATE
+        }
+
+        self.dag = DAG('test_dag_id', default_args=args)
+        self.sensor = SQSSensor(
+            task_id='test_task',
+            dag=self.dag,
+            sqs_queue='test',
+            aws_conn_id='aws_default'
+        )
+
+        self.mock_context = MagicMock()
+        self.sqs_hook = SQSHook()
+
+    @mock_sqs
+    def test_poke_success(self):
+        self.sqs_hook.create_queue('test')
+        self.sqs_hook.send_message(queue_url='test', message_body='hello')
+
+        result = self.sensor.poke(self.mock_context)
+        self.assertTrue(result)
+
+        self.assertTrue("'Body': 'hello'" in 
str(self.mock_context['ti'].method_calls),
+                        "context call should contain message hello")
+
+    @mock_sqs
+    def test_poke_no_messsage_failed(self):
+
+        self.sqs_hook.create_queue('test')
+        result = self.sensor.poke(self.mock_context)
+        self.assertFalse(result)
+
+        context_calls = []
+
+        self.assertTrue(self.mock_context['ti'].method_calls == context_calls, 
"context call  should be same")
+
+    @patch('airflow.contrib.sensors.aws_sqs_sensor.SQSHook')
 
 Review comment:
   @Fokko - I've added moto tests cases for scenarios where there is a message 
in the mock queue or not.  However, I am using mock hook here to simulate cases 
where an exception is to be thrown based on return value of the delete request. 
 Please let me know if there is a better way to using moto.  
   Thanks again for your recommendations.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to