ashb commented on a change in pull request #16889: URL: https://github.com/apache/airflow/pull/16889#discussion_r666348163
########## File path: tests/jobs/conftest.py ########## @@ -0,0 +1,83 @@ +# 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 os + +import pytest + +from airflow.models.dag import DAG +from airflow.models.dagbag import DagBag +from airflow.operators.dummy import DummyOperator +from airflow.operators.python import PythonOperator +from airflow.utils import timezone +from airflow.utils.state import State +from tests.test_utils import db + +TEST_DAG_FOLDER = os.environ['AIRFLOW__CORE__DAGS_FOLDER'] + + [email protected]() +def clear_db(): + def _clean_up(): + db.clear_db_dags() + db.clear_db_jobs() + db.clear_db_runs() + db.clear_db_task_fail() + + _clean_up() + yield + + [email protected] +def dag_maker(request): + + DEFAULT_DATE = timezone.datetime(2016, 1, 1) + + def create_dag_ti_dr( + dag_id='test_dag', task_id='op1', python_callable=None, py_kwargs=None, with_dagbag=False, **kwargs + ): + # If python_callable we create a PythonOperator task + # For ease of use, set start date to "DEFAULT_DATE" module variable from request + if "start_date" not in kwargs and hasattr(request.module, 'DEFAULT_DATE'): + kwargs['start_date'] = getattr(request.module, 'DEFAULT_DATE') + else: + kwargs['start_date'] = DEFAULT_DATE + if with_dagbag: + dagbag = DagBag( + dag_folder=TEST_DAG_FOLDER, + include_examples=False, + ) + dag = dagbag.get_dag(dag_id) + task = dag.get_task(task_id) + else: + dag = DAG(dag_id, start_date=kwargs['start_date']) + if python_callable: + task = PythonOperator(task_id=task_id, python_callable=python_callable, dag=dag, **py_kwargs) + else: + task = DummyOperator(task_id=task_id, run_as_user=kwargs.get("run_as_user", None), dag=dag) Review comment: Perhaps this structure: ```python @pytest.fixture def dag_maker(request): DEFAULT_DATE = timezone.datetime(2016, 1, 1) class DagFactory(): def __enter__(self): self.dag.__enter__() return self.dag def __exit__(self, type, value, traceback): dag = self.dag dag.__exit__(type, value, traceback) if type is None: dag.sync_to_db() dag.clear() self.dag_run = dag.create_dagrun( run_id="test", state=self.kwargs.get('state', State.SUCCESS), execution_date=self.kwargs.get('execution_date', self.kwargs['start_date']), start_date=self.kwargs['start_date'], ) def __call__(self, dag_id='test_dag', **kwargs): self.kwargs = kwargs if "start_date" not in kwargs: if hasattr(request.module, 'DEFAULT_DATE'): kwargs['start_date'] = getattr(request.module, 'DEFAULT_DATE') else: kwargs['start_date'] = DEFAULT_DATE # TODO: filter out of kwargs things we use to control the dag-run we create. self.dag = DAG(dag_id, **kwargs) return self return DagFactory() ``` We could then use it like this in tests ```python with dag_maker(): task = PythonOperator(task_id=task_id, python_callable=python_callable) dag_run = dag_maker.dag_run ``` Thoughts? -- 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]
