You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by cr...@apache.org on 2016/07/12 18:13:16 UTC

incubator-airflow git commit: [AIRFLOW-327] Add rename method to the FTPHook

Repository: incubator-airflow
Updated Branches:
  refs/heads/master 7f7f8bfdb -> 3c91bbb5d


[AIRFLOW-327] Add rename method to the FTPHook

Closes #1660 from skudriashev/airflow-327


Project: http://git-wip-us.apache.org/repos/asf/incubator-airflow/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-airflow/commit/3c91bbb5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-airflow/tree/3c91bbb5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-airflow/diff/3c91bbb5

Branch: refs/heads/master
Commit: 3c91bbb5d6ba4c5883f7972d74e235ed65750960
Parents: 7f7f8bf
Author: Stanislav Kudriashev <st...@gmail.com>
Authored: Tue Jul 12 11:13:09 2016 -0700
Committer: Chris Riccomini <ch...@wepay.com>
Committed: Tue Jul 12 11:13:09 2016 -0700

----------------------------------------------------------------------
 airflow/contrib/hooks/ftp_hook.py    | 10 ++++
 tests/contrib/hooks/test_ftp_hook.py | 83 +++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-airflow/blob/3c91bbb5/airflow/contrib/hooks/ftp_hook.py
----------------------------------------------------------------------
diff --git a/airflow/contrib/hooks/ftp_hook.py b/airflow/contrib/hooks/ftp_hook.py
index e14bfe2..2f2e4c2 100644
--- a/airflow/contrib/hooks/ftp_hook.py
+++ b/airflow/contrib/hooks/ftp_hook.py
@@ -213,6 +213,16 @@ class FTPHook(BaseHook):
         conn = self.get_conn()
         conn.delete(path)
 
+    def rename(self, from_name, to_name):
+        """
+        Rename a file.
+
+        :param from_name: rename file from name
+        :param to_name: rename file to name
+        """
+        conn = self.get_conn()
+        return conn.rename(from_name, to_name)
+
     def get_mod_time(self, path):
         conn = self.get_conn()
         ftp_mdtm = conn.sendcmd('MDTM ' + path)

http://git-wip-us.apache.org/repos/asf/incubator-airflow/blob/3c91bbb5/tests/contrib/hooks/test_ftp_hook.py
----------------------------------------------------------------------
diff --git a/tests/contrib/hooks/test_ftp_hook.py b/tests/contrib/hooks/test_ftp_hook.py
new file mode 100644
index 0000000..ab6f459
--- /dev/null
+++ b/tests/contrib/hooks/test_ftp_hook.py
@@ -0,0 +1,83 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed 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 mock
+import unittest
+
+from airflow.contrib.hooks import ftp_hook as fh
+
+
+class TestFTPHook(unittest.TestCase):
+
+    def setUp(self):
+        super(TestFTPHook, self).setUp()
+        self.path = '/some/path'
+        self.conn_mock = mock.MagicMock(name='conn')
+        self.get_conn_orig = fh.FTPHook.get_conn
+
+        def _get_conn_mock(hook):
+            hook.conn = self.conn_mock
+            return self.conn_mock
+
+        fh.FTPHook.get_conn = _get_conn_mock
+
+    def tearDown(self):
+        fh.FTPHook.get_conn = self.get_conn_orig
+        super(TestFTPHook, self).tearDown()
+
+    def test_close_conn(self):
+        ftp_hook = fh.FTPHook()
+        ftp_hook.get_conn()
+        ftp_hook.close_conn()
+
+        self.conn_mock.quit.assert_called_once_with()
+
+    def test_list_directory(self):
+        with fh.FTPHook() as ftp_hook:
+            ftp_hook.list_directory(self.path)
+
+        self.conn_mock.cwd.assert_called_once_with(self.path)
+        self.conn_mock.nlst.assert_called_once_with()
+
+    def test_create_directory(self):
+        with fh.FTPHook() as ftp_hook:
+            ftp_hook.create_directory(self.path)
+
+        self.conn_mock.mkd.assert_called_once_with(self.path)
+
+    def test_delete_directory(self):
+        with fh.FTPHook() as ftp_hook:
+            ftp_hook.delete_directory(self.path)
+
+        self.conn_mock.rmd.assert_called_once_with(self.path)
+
+    def test_delete_file(self):
+        with fh.FTPHook() as ftp_hook:
+            ftp_hook.delete_file(self.path)
+
+        self.conn_mock.delete.assert_called_once_with(self.path)
+
+    def test_rename(self):
+        from_path = '/path/from'
+        to_path = '/path/to'
+        with fh.FTPHook() as ftp_hook:
+            ftp_hook.rename(from_path, to_path)
+
+        self.conn_mock.rename.assert_called_once_with(from_path, to_path)
+        self.conn_mock.quit.assert_called_once_with()
+
+
+if __name__ == '__main__':
+    unittest.main()