You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/02/14 14:27:10 UTC

[GitHub] [airflow] feluelle commented on a change in pull request #7422: [AIRFLOW-6809] Test for presto operators

feluelle commented on a change in pull request #7422: [AIRFLOW-6809] Test for presto operators
URL: https://github.com/apache/airflow/pull/7422#discussion_r379456567
 
 

 ##########
 File path: tests/providers/presto/operators/test_presto_check.py
 ##########
 @@ -0,0 +1,224 @@
+#
+# 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 datetime import datetime
+
+import mock
+
+from airflow.exceptions import AirflowException
+from airflow.models import DAG
+from airflow.providers.presto.operators.presto_check import (
+    PrestoCheckOperator,
+    PrestoIntervalCheckOperator,
+    PrestoValueCheckOperator)
+
+
+class TestPrestoCheckOperator(unittest.TestCase):
+
+    @mock.patch.object(PrestoCheckOperator, 'get_db_hook')
+    def test_execute_no_records(self, mock_get_db_hook):
+        mock_get_db_hook.return_value.get_first.return_value = []
+
+        with self.assertRaises(AirflowException):
+            PrestoCheckOperator(sql='sql').execute()
+
+    @mock.patch.object(PrestoCheckOperator, 'get_db_hook')
+    def test_execute_not_all_records_are_true(self, mock_get_db_hook):
+        mock_get_db_hook.return_value.get_first.return_value = ["data", ""]
+
+        with self.assertRaises(AirflowException):
+            PrestoCheckOperator(sql='sql').execute()
+
+
+class TestValuePrestoCheckOperator(unittest.TestCase):
+
+    def setUp(self):
+        self.task_id = 'test_task'
+        self.conn_id = 'default_conn'
+
+    def _construct_operator(self, sql, pass_value, tolerance=None):
+        dag = DAG('test_dag', start_date=datetime(2017, 1, 1))
+
+        return PrestoValueCheckOperator(
+            dag=dag,
+            task_id=self.task_id,
+            conn_id=self.conn_id,
+            sql=sql,
+            pass_value=pass_value,
+            tolerance=tolerance)
+
+    def test_pass_value_template_string(self):
+        pass_value_str = "2018-03-22"
+        operator = self._construct_operator('select date from tab1;', "{{ ds }}")
+
+        operator.render_template_fields({'ds': pass_value_str})
+
+        self.assertEqual(operator.task_id, self.task_id)
+        self.assertEqual(operator.pass_value, pass_value_str)
+
+    def test_pass_value_template_string_float(self):
+        pass_value_float = 4.0
+        operator = self._construct_operator('select date from tab1;', pass_value_float)
+
+        operator.render_template_fields({})
+
+        self.assertEqual(operator.task_id, self.task_id)
+        self.assertEqual(operator.pass_value, str(pass_value_float))
 
 Review comment:
   I don't think these test cases are necessary but good to have?! Because the logic you are testing `operator.render_template_fields({})` is implemented in the `BaseOperator` not in the operator you are testing.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services