-
Notifications
You must be signed in to change notification settings - Fork 296
test: add Airflow 3 DAG versioning tests for Cosmos #2177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1f1d4cc
test: add Airflow 3 DAG versioning tests for Cosmos
michal-mrazek 15e23c7
improv db handling
michal-mrazek 8089e8c
simple project and run the dag
michal-mrazek 502be07
Add Airflow 3.1+ DAG versioning integration tests
michal-mrazek 0612f36
Fix airflow versioning test
michal-mrazek 22b9536
Merge branch 'main' into main
michal-mrazek 13aba56
Merge branch 'main' into main
tatiana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import shutil | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| from packaging.version import Version | ||
|
|
||
| from cosmos.constants import AIRFLOW_VERSION | ||
| from tests.utils import run_dag | ||
|
|
||
| if AIRFLOW_VERSION < Version("3.1"): | ||
| pytest.skip("Skipping Airflow versioning tests on Airflow 2.x and 3.0", allow_module_level=True) | ||
| else: | ||
| from airflow.models import DagBag, DagRun, TaskInstance | ||
| from airflow.models.dag import DagModel | ||
| from airflow.models.dag_version import DagVersion | ||
| from airflow.models.dagbundle import DagBundleModel | ||
| from airflow.models.dagcode import DagCode | ||
| from airflow.models.serialized_dag import SerializedDagModel | ||
| from airflow.serialization.serialized_objects import LazyDeserializedDAG | ||
| from airflow.utils.session import provide_session | ||
|
|
||
|
|
||
| DBT_PROJECT_PATH = Path(__file__).parent.parent / "dev" / "dags" / "dbt" / "simple" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def dbt_project_test_dir(tmp_path): | ||
| """Create a test directory with the local simple dbt project and DAG file.""" | ||
| test_path = tmp_path / "simple" | ||
| shutil.copytree(DBT_PROJECT_PATH, test_path, dirs_exist_ok=True) | ||
|
|
||
| dag_file = test_path / "basic_cosmos_dag.py" | ||
| basic_dag_path = Path(__file__).parent.parent / "dev" / "dags" / "basic_cosmos_dag.py" | ||
| dag_content = ( | ||
| basic_dag_path.read_text() | ||
| .replace("DBT_PROJECT_PATH = DBT_ROOT_PATH / DBT_PROJECT_NAME", f'DBT_PROJECT_PATH = Path("{test_path}")') | ||
| .replace( | ||
| 'DBT_PROJECT_NAME = os.getenv("DBT_PROJECT_NAME", "jaffle_shop")', | ||
| 'DBT_PROJECT_NAME = "simple"', | ||
| ) | ||
| ) | ||
| dag_file.write_text(dag_content) | ||
|
|
||
| return test_path | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def test_dag_id(): | ||
| """DAG ID used in versioning tests.""" | ||
| return "basic_cosmos_dag" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def dag_version_cleaner(test_dag_id): | ||
| """Fixture to clean up DAG versioning data before and after tests.""" | ||
|
|
||
| @provide_session | ||
| def _cleanup(dag_id, session=None): | ||
| """Clean up all Airflow 3 versioning-related data for a DAG.""" | ||
| try: | ||
| for table in [TaskInstance, DagRun, SerializedDagModel, DagVersion, DagCode, DagModel]: | ||
| session.query(table).filter(table.dag_id == dag_id).delete(synchronize_session="fetch") | ||
| session.commit() | ||
| except Exception: | ||
| session.rollback() | ||
| raise | ||
|
|
||
| _cleanup(test_dag_id) | ||
| yield | ||
| _cleanup(test_dag_id) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def serialize_dag(): | ||
| """Factory to serialize DAG.""" | ||
|
|
||
| @provide_session | ||
| def _serialize(dag, bundle_name="test_bundle", bundle_version="1.0.0", session=None): | ||
| """Serialize DAG.""" | ||
| # Ensure bundle exists atomically | ||
| session.merge(DagBundleModel(name=bundle_name)) | ||
| # Ensure DagModel exists atomically | ||
| session.merge(DagModel(dag_id=dag.dag_id, bundle_name=bundle_name)) | ||
|
|
||
| # Serialize DAG (uses scheduler's hash-based versioning) | ||
| return SerializedDagModel.write_dag( | ||
| dag=LazyDeserializedDAG.from_dag(dag), | ||
| bundle_name=bundle_name, | ||
| bundle_version=bundle_version, | ||
| session=session, | ||
| ) | ||
|
|
||
| return _serialize | ||
|
|
||
|
|
||
| @pytest.mark.integration | ||
| def test_cosmos_dag_version_tracking_with_added_model( | ||
| dbt_project_test_dir, test_dag_id, dag_version_cleaner, serialize_dag | ||
| ): | ||
| """Test that DAG versions increment when dbt models are added. | ||
|
|
||
| This test verifies that Airflow 3's DAG versioning system correctly tracks | ||
| structural changes to Cosmos DAGs when dbt models are added. | ||
| """ | ||
| # Parse DAG and run | ||
| dagbag = DagBag(dag_folder=dbt_project_test_dir) | ||
| dag_v1 = dagbag.dags[test_dag_id] | ||
| run_dag(dag_v1) | ||
|
|
||
| version_1 = DagVersion.get_latest_version(test_dag_id) | ||
|
|
||
| # Add new dbt model (cosmos dag should change) | ||
| (dbt_project_test_dir / "models" / "dummy_model.sql").write_text("SELECT 2 AS id, 'example' AS name") | ||
|
|
||
| # Re-parse DAG, serialize and run | ||
| dagbag = DagBag(dag_folder=dbt_project_test_dir) | ||
| dag_v2 = dagbag.dags[test_dag_id] | ||
| serialize_dag(dag_v2) | ||
| run_dag(dag_v2) | ||
|
|
||
| version_2 = DagVersion.get_latest_version(test_dag_id) | ||
|
|
||
| # Verify version incremented | ||
| assert version_1.version_number == 1 | ||
| assert version_2.version_number == 2 | ||
|
|
||
| # Verify structural change (new task added) | ||
| assert len(dag_v2.tasks) == len(dag_v1.tasks) + 1 | ||
|
|
||
|
|
||
| @pytest.mark.integration | ||
| def test_cosmos_dag_version_unchanged_without_modifications( | ||
| dbt_project_test_dir, | ||
| test_dag_id, | ||
| dag_version_cleaner, | ||
| serialize_dag, | ||
| ): | ||
| """Test that DAG version does not change when DAG structure is unchanged. | ||
|
|
||
| This test verifies that Airflow 3's DAG versioning system recognizes | ||
| when there are no structural changes to the DAG. | ||
| """ | ||
| # Parse DAG and run | ||
| dagbag = DagBag(dag_folder=dbt_project_test_dir) | ||
| dag_v1 = dagbag.dags[test_dag_id] | ||
| run_dag(dag_v1) | ||
|
|
||
| version_1 = DagVersion.get_latest_version(test_dag_id) | ||
|
|
||
| # Re-parse DAG, serialize and run | ||
| dagbag = DagBag(dag_folder=dbt_project_test_dir) | ||
| dag_v2 = dagbag.dags[test_dag_id] | ||
| serialize_dag(dag_v2) | ||
| run_dag(dag_v2) | ||
|
|
||
| version_2 = DagVersion.get_latest_version(test_dag_id) | ||
|
|
||
| # Verify version did not increment (same version reused) | ||
| assert version_1.id == version_2.id | ||
| assert version_1.version_number == version_2.version_number | ||
|
|
||
| # Verify same number of tasks | ||
| assert len(dag_v2.tasks) == len(dag_v1.tasks) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.