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 2018/12/31 06:02:58 UTC

[GitHub] feng-tao closed pull request #4400: [AIRFLOW-3595] Add tests for Hive2SambaOperator

feng-tao closed pull request #4400: [AIRFLOW-3595] Add tests for Hive2SambaOperator
URL: https://github.com/apache/incubator-airflow/pull/4400
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/airflow/operators/hive_to_samba_operator.py b/airflow/operators/hive_to_samba_operator.py
index 7963524a10..0da1017967 100644
--- a/airflow/operators/hive_to_samba_operator.py
+++ b/airflow/operators/hive_to_samba_operator.py
@@ -17,7 +17,7 @@
 # specific language governing permissions and limitations
 # under the License.
 
-import tempfile
+from tempfile import NamedTemporaryFile
 
 from airflow.hooks.hive_hooks import HiveServer2Hook
 from airflow.hooks.samba_hook import SambaHook
@@ -33,35 +33,35 @@ class Hive2SambaOperator(BaseOperator):
 
     :param hql: the hql to be exported. (templated)
     :type hql: str
-    :param hiveserver2_conn_id: reference to the hiveserver2 service
-    :type hiveserver2_conn_id: str
+    :param destination_filepath: the file path to where the file will be pushed onto samba
+    :type destination_filepath: str
     :param samba_conn_id: reference to the samba destination
     :type samba_conn_id: str
+    :param hiveserver2_conn_id: reference to the hiveserver2 service
+    :type hiveserver2_conn_id: str
     """
 
     template_fields = ('hql', 'destination_filepath')
     template_ext = ('.hql', '.sql',)
 
     @apply_defaults
-    def __init__(
-            self, hql,
-            destination_filepath,
-            samba_conn_id='samba_default',
-            hiveserver2_conn_id='hiveserver2_default',
-            *args, **kwargs):
+    def __init__(self,
+                 hql,
+                 destination_filepath,
+                 samba_conn_id='samba_default',
+                 hiveserver2_conn_id='hiveserver2_default',
+                 *args, **kwargs):
         super(Hive2SambaOperator, self).__init__(*args, **kwargs)
-
         self.hiveserver2_conn_id = hiveserver2_conn_id
         self.samba_conn_id = samba_conn_id
         self.destination_filepath = destination_filepath
         self.hql = hql.strip().rstrip(';')
 
     def execute(self, context):
-        samba = SambaHook(samba_conn_id=self.samba_conn_id)
-        hive = HiveServer2Hook(hiveserver2_conn_id=self.hiveserver2_conn_id)
-        tmpfile = tempfile.NamedTemporaryFile()
-        self.log.info("Fetching file from Hive")
-        hive.to_csv(hql=self.hql, csv_filepath=tmpfile.name,
-                    hive_conf=context_to_airflow_vars(context))
-        self.log.info("Pushing to samba")
-        samba.push_from_local(self.destination_filepath, tmpfile.name)
+        with NamedTemporaryFile() as tmp_file:
+            self.log.info("Fetching file from Hive")
+            hive = HiveServer2Hook(hiveserver2_conn_id=self.hiveserver2_conn_id)
+            hive.to_csv(hql=self.hql, csv_filepath=tmp_file.name, hive_conf=context_to_airflow_vars(context))
+            self.log.info("Pushing to samba")
+            samba = SambaHook(samba_conn_id=self.samba_conn_id)
+            samba.push_from_local(self.destination_filepath, tmp_file.name)
diff --git a/tests/operators/test_hive_to_samba_operator.py b/tests/operators/test_hive_to_samba_operator.py
new file mode 100644
index 0000000000..5a9a1b5087
--- /dev/null
+++ b/tests/operators/test_hive_to_samba_operator.py
@@ -0,0 +1,57 @@
+# -*- 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 mock import patch, PropertyMock, Mock
+
+from airflow.operators.hive_to_samba_operator import Hive2SambaOperator
+from airflow.utils.operator_helpers import context_to_airflow_vars
+
+
+class TestHive2SambaOperator(unittest.TestCase):
+
+    def setUp(self):
+        self.kwargs = dict(
+            hql='hql',
+            destination_filepath='destination_filepath',
+            samba_conn_id='samba_default',
+            hiveserver2_conn_id='hiveserver2_default',
+            task_id='test_hive_to_samba_operator',
+            dag=None
+        )
+
+    @patch('airflow.operators.hive_to_samba_operator.SambaHook')
+    @patch('airflow.operators.hive_to_samba_operator.HiveServer2Hook')
+    @patch('airflow.operators.hive_to_samba_operator.NamedTemporaryFile')
+    def test_execute(self, mock_tmp_file, mock_hive_hook, mock_samba_hook):
+        type(mock_tmp_file).name = PropertyMock(return_value='tmp_file')
+        mock_tmp_file.return_value.__enter__ = Mock(return_value=mock_tmp_file)
+        context = {}
+
+        Hive2SambaOperator(**self.kwargs).execute(context)
+
+        mock_hive_hook.assert_called_once_with(hiveserver2_conn_id=self.kwargs['hiveserver2_conn_id'])
+        mock_hive_hook.return_value.to_csv.assert_called_once_with(
+            hql=self.kwargs['hql'],
+            csv_filepath=mock_tmp_file.name,
+            hive_conf=context_to_airflow_vars(context))
+        mock_samba_hook.assert_called_once_with(samba_conn_id=self.kwargs['samba_conn_id'])
+        mock_samba_hook.return_value.push_from_local.assert_called_once_with(
+            self.kwargs['destination_filepath'], mock_tmp_file.name)


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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