amoghrajesh commented on code in PR #65759: URL: https://github.com/apache/airflow/pull/65759#discussion_r3154253985
########## airflow-core/tests/unit/state/test_metastore.py: ########## @@ -0,0 +1,363 @@ +# +# 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 + +import pytest +from sqlalchemy import select + +from airflow._shared.timezones import timezone +from airflow.models.asset import AssetModel +from airflow.models.dagrun import DagRun, DagRunType +from airflow.models.task_state import TaskStateModel +from airflow.state import AssetScope, TaskScope, resolve_state_backend +from airflow.state.metastore import MetastoreStateBackend +from airflow.utils.session import create_session + +from tests_common.test_utils.config import conf_vars +from tests_common.test_utils.db import clear_db_assets, clear_db_dags, clear_db_runs + +if TYPE_CHECKING: + from sqlalchemy.orm import Session + +pytestmark = pytest.mark.db_test + +DAG_ID = "test_dag" +TASK_ID = "test_task" +RUN_ID = "scheduled__2026-04-24" + + [email protected](autouse=True) +def clean_tables(): + clear_db_dags() + clear_db_runs() + clear_db_assets() + yield + clear_db_dags() + clear_db_runs() + clear_db_assets() + + [email protected] +def backend() -> MetastoreStateBackend: + return MetastoreStateBackend() + + [email protected] +def dag_run(session: Session) -> DagRun: + run = DagRun( + dag_id=DAG_ID, + run_id=RUN_ID, + run_type=DagRunType.SCHEDULED, + logical_date=timezone.datetime(2026, 4, 24), + run_after=timezone.datetime(2026, 4, 24), + ) + session.add(run) + session.flush() + return run + + [email protected] +def dag_run_committed() -> DagRun: + """DagRun committed to DB so async sessions (which open their own connections) can see it.""" + with create_session() as session: + run = DagRun( + dag_id=DAG_ID, + run_id=RUN_ID, + run_type=DagRunType.SCHEDULED, + logical_date=timezone.datetime(2026, 4, 24), + run_after=timezone.datetime(2026, 4, 24), + ) + session.add(run) + session.flush() + session.expunge(run) + return run + + [email protected] +def asset(session: Session) -> AssetModel: + a = AssetModel(uri="s3://bucket/prefix", name="test_asset", group="test") + session.add(a) + session.flush() + return a + + [email protected] +def asset_committed() -> AssetModel: + """AssetModel committed to DB so async sessions (which open their own connections) can see it.""" + with create_session() as session: + a = AssetModel(uri="s3://bucket/prefix", name="test_asset", group="test") + session.add(a) + session.flush() + session.expunge(a) + return a + + +class TestMetastoreStateBackendTaskScope: + def test_get_returns_none_for_missing_key( + self, session: Session, backend: MetastoreStateBackend, dag_run: DagRun + ): + scope = TaskScope(dag_id=DAG_ID, run_id=RUN_ID, task_id=TASK_ID) + assert backend.get(scope, "missing", session=session) is None + + def test_set_and_get_roundtrip(self, session: Session, backend: MetastoreStateBackend, dag_run: DagRun): + scope = TaskScope(dag_id=DAG_ID, run_id=RUN_ID, task_id=TASK_ID) + backend.set(scope, "job_id", "app_1234", session=session) + session.flush() + assert backend.get(scope, "job_id", session=session) == "app_1234" + + def test_set_twice_overrides_existing_value( Review Comment: Very good catch. I was under the impression that if `pytestmark = pytest.mark.db_test` was added, it would run on all backends. The conftest skips tests without a matching backend marker when `--backend postgres/mysql` is passed, so the upsert tests were only ever running on sqlite, and I was mistaken. Added `@pytest.mark.backend("postgres", "mysql", "sqlite")` to both -- 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]
