Taragolis commented on code in PR #36953:
URL: https://github.com/apache/airflow/pull/36953#discussion_r1488506124


##########
airflow/providers/teradata/transfers/teradata_to_teradata.py:
##########
@@ -0,0 +1,91 @@
+#
+# 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.
+from __future__ import annotations
+
+from typing import TYPE_CHECKING, Sequence
+
+from airflow.models import BaseOperator
+from airflow.providers.teradata.hooks.teradata import TeradataHook
+
+if TYPE_CHECKING:
+    from airflow.utils.context import Context
+
+
+class TeradataToTeradataOperator(BaseOperator):
+    """
+    Moves data from Teradata source database to Teradata destination database.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the 
guide:
+        :ref:`howto/operator:TeradataToTeradataOperator`
+
+    :param teradata_destination_conn_id: destination Teradata connection.
+    :param destination_table: destination table to insert rows.
+    :param teradata_source_conn_id: :ref:`Source Teradata connection 
<howto/connection:Teradata>`.
+    :param source_sql: SQL query to execute against the source Teradata 
database
+    :param source_sql_params: Parameters to use in sql query.
+    :param rows_chunk: number of rows per chunk to commit.
+    """
+
+    template_fields: Sequence[str] = (
+        "source_sql",
+        "source_sql_params",
+    )
+    template_ext: Sequence[str] = (".sql",)
+    template_fields_renderers = {"source_sql": "sql", "source_sql_params": 
"py"}
+    ui_color = "#e07c24"
+
+    def __init__(
+        self,
+        *,
+        teradata_destination_conn_id: str,
+        destination_table: str,
+        teradata_source_conn_id: str,
+        source_sql: str,
+        source_sql_params: dict | None = None,
+        rows_chunk: int = 5000,
+        **kwargs,
+    ) -> None:
+        super().__init__(**kwargs)
+        if source_sql_params is None:
+            source_sql_params = {}
+        self.teradata_destination_conn_id = teradata_destination_conn_id
+        self.destination_table = destination_table
+        self.teradata_source_conn_id = teradata_source_conn_id
+        self.source_sql = source_sql
+        self.source_sql_params = source_sql_params
+        self.rows_chunk = rows_chunk
+
+    def _execute(self, src_hook, dest_hook, context) -> None:
+        with src_hook.get_conn() as src_conn:
+            cursor = src_conn.cursor()
+            cursor.execute(self.source_sql, self.source_sql_params)
+            target_fields = [field[0] for field in cursor.description]
+            rows_total = 0
+            for rows in iter(lambda: cursor.fetchmany(self.rows_chunk), []):
+                dest_hook.bulk_insert_rows(
+                    self.destination_table, rows, target_fields=target_fields, 
commit_every=self.rows_chunk
+                )
+                rows_total += len(rows)
+            self.log.info("Finished data transfer.")
+            cursor.close()
+
+    def execute(self, context: Context) -> None:
+        src_hook = TeradataHook(teradata_conn_id=self.teradata_source_conn_id)
+        dest_hook = 
TeradataHook(teradata_conn_id=self.teradata_destination_conn_id)
+        self._execute(src_hook, dest_hook, context)

Review Comment:
   You could move hooks into the properties and test it separately: initialise 
hook, operator execution, some other cases
   
   ```python
   from functools import cached_property
   
   from airflow.models import BaseOperator
   
   
   class FakeHook:
       def __init__(self, conn_id: str):
           self.conn_id = conn_id
   
       def method_1(self, param1, param2):
           # Should be covered into the Hook tests
           return "spam"
   
       def method_2(self, param1, param2):
           # Should be covered into the Hook tests
           return "egg"
   
   class SampleTransferOperator(BaseOperator):
       def __init__(self, *, src_conn_id: str, dest_conn_id: str, **kwargs):
           super().__init__(**kwargs)
           self.src_conn_id = src_conn_id
           self.dest_conn_id = dest_conn_id
   
       @cached_property
       def src_hook(self) -> FakeHook:
           return FakeHook(conn_id=self.src_conn_id)
   
       @cached_property
       def dest_hook(self) -> FakeHook:
           return FakeHook(conn_id=self.dest_conn_id)
   
       def execute(self, context):
           return f"{self.src_hook.method_1(1, 2)} {self.dest_hook.method_2(3, 
4)}"
   
   
   #### Test Cases for operator
   from unittest import mock
   import pytest
   
   @pytest.fixture
   def mocked_src_hook():
       with mock.patch.object(SampleTransferOperator, "src_hook", 
spec=FakeHook, name="FakeSourceHook") as m:
           yield m
   
   @pytest.fixture
   def mocked_dest_hook():
       with mock.patch.object(SampleTransferOperator, "dest_hook", 
spec=FakeHook, name="FakeDestHook") as m:
           yield m
   
   
   class TestSampleTransferOperator:
       def test_source_hook(self):
           op = SampleTransferOperator(task_id="test_source_hook", 
src_conn_id="foo", dest_conn_id="bar")
           hook = op.src_hook
           # If the assumption that hook already tested correct then it would 
be enough to test assignments
           assert hook
           assert hook is op.src_hook
           assert hook.conn_id == "foo"
   
       def test_destination_hook(self):
           op = SampleTransferOperator(task_id="test_source_hook", 
src_conn_id="foo", dest_conn_id="bar")
           hook = op.dest_hook
           # If the assumption that hook already tested correct then it would 
be enough to test assignments
           assert hook
           assert hook is op.dest_hook
           assert hook.conn_id == "bar"
   
       def test_execution(self, mocked_src_hook, mocked_dest_hook):
           mocked_src_hook.method_1.return_value = "baz"
           mocked_dest_hook.method_2.return_value = "qux"
   
           op = SampleTransferOperator(task_id="test_execution", 
src_conn_id="foo", dest_conn_id="bar")
           result = op.execute({})
           assert result == "baz qux"
           mocked_src_hook.method_1.assert_called_once_with(1, 2)
           mocked_dest_hook.method_2.assert_called_once_with(3, 4)
   ```



-- 
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.

To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to