ashb commented on code in PR #25780: URL: https://github.com/apache/airflow/pull/25780#discussion_r962837415
########## tests/decorators/test_external_python.py: ########## @@ -0,0 +1,101 @@ +# +# 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 datetime +import sys +from datetime import timedelta +from subprocess import CalledProcessError + +import pytest + +from airflow.decorators import task +from airflow.utils import timezone + +DEFAULT_DATE = timezone.datetime(2016, 1, 1) +END_DATE = timezone.datetime(2016, 1, 2) +INTERVAL = timedelta(hours=12) +FROZEN_NOW = timezone.datetime(2016, 1, 2, 12, 1, 1) + +TI_CONTEXT_ENV_VARS = [ + 'AIRFLOW_CTX_DAG_ID', + 'AIRFLOW_CTX_TASK_ID', + 'AIRFLOW_CTX_EXECUTION_DATE', + 'AIRFLOW_CTX_DAG_RUN_ID', +] + + +PYTHON_VERSION = sys.version_info[0] + +# Technically Not a separate virtualenv but should be good enough for unit tests +PYTHON = sys.executable + + +class TestExternalPythonDecorator: + def test_add_dill(self, dag_maker): + @task.external_python(python=PYTHON, use_dill=True) + def f(): + """Ensure dill is correctly installed.""" + import dill # noqa: F401 + + with dag_maker(): + ret = f() + + ret.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE) + + def test_fail(self, dag_maker): + @task.external_python(python=PYTHON) + def f(): + raise Exception + + with dag_maker(): + ret = f() + + with pytest.raises(CalledProcessError): + ret.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE) + + def test_with_args(self, dag_maker): + @task.external_python(python=PYTHON) + def f(a, b, c=False, d=False): + if a == 0 and b == 1 and c and not d: + return True + else: + raise Exception + + with dag_maker(): + ret = f(0, 1, c=True) + + ret.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE) + + def test_return_none(self, dag_maker): + @task.external_python(python=PYTHON) + def f(): + return None + + with dag_maker(): + ret = f() + + ret.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE) + + def test_nonimported_as_arg(self, dag_maker): Review Comment: Don't think this case is needed either. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
