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/10/25 14:08:15 UTC

[GitHub] [airflow] turbaszek commented on a change in pull request #11839: Adding ChainBetwenDAGAndOperatorNotAllowedRule for easing upgrade to 2.0

turbaszek commented on a change in pull request #11839:
URL: https://github.com/apache/airflow/pull/11839#discussion_r511601761



##########
File path: tests/upgrade/rules/test_chain_between_dag_and_operator_not_allowed_rule.py
##########
@@ -0,0 +1,167 @@
+# 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 contextlib import contextmanager
+from unittest import TestCase
+
+from tempfile import NamedTemporaryFile
+from tests.compat import mock
+
+from airflow.upgrade.rules.chain_between_dag_and_operator_not_allowed_rule import \
+    ChainBetweenDAGAndOperatorNotAllowedRule
+
+
+@contextmanager
+def create_temp_file(mock_list_files, lines):
+    with NamedTemporaryFile("w+") as temp_file:
+        mock_list_files.return_value = [temp_file.name]
+        temp_file.writelines("\n".join(lines))
+        temp_file.flush()
+        yield temp_file
+
+
+@mock.patch("airflow.upgrade.rules.chain_between_dag_and_operator_not_allowed_rule.list_py_file_paths")
+class TestChainBetweenDAGAndOperatorNotAllowedRule(TestCase):
+    def test_valid_check(self, mock_list_files):
+        lines = ["with DAG('my_dag') as dag:",
+                 "    dummy1 = DummyOperator(task_id='dummy1')",
+                 "    dummy2 = DummyOperator(task_id='dummy2')",
+                 "    dummy1 >> dummy2"]
+        with create_temp_file(mock_list_files, lines):
+            rule = ChainBetweenDAGAndOperatorNotAllowedRule()
+            assert isinstance(rule.description, str)
+            assert isinstance(rule.title, str)
+
+            msgs = rule.check()
+            assert 0 == len(msgs)
+
+    def test_invalid_check(self, mock_list_files):
+        lines = ["dag = DAG('my_dag')",
+                 "dummy = DummyOperator(task_id='dummy')",
+                 "dag >> dummy"]
+        with create_temp_file(mock_list_files, lines) as temp_file:
+
+            rule = ChainBetweenDAGAndOperatorNotAllowedRule()
+
+            assert isinstance(rule.description, str)
+            assert isinstance(rule.title, str)

Review comment:
       How about adding separate test for this instead of duplicating this in every test? 😉 




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