This is an automated email from the ASF dual-hosted git repository. potiuk pushed a commit to branch v2-8-test in repository https://gitbox.apache.org/repos/asf/airflow.git
commit 0dc8c285537abf8057b94cb38f3cac8886832c9a Author: Ash Berlin-Taylor <[email protected]> AuthorDate: Fri Feb 2 13:51:47 2024 +0000 Allow running airflow against sqlite in-memory DB for tests (#37144) This makes no sense for production workloads, but it is useful for writing tests of Airflow operators etc. I also moved the warning from configure_vars to convigure_orm so that it is checked after local settings have been applied in case that changes anything. (cherry picked from commit 7324400c6b379d22030a6bd54fb14912c1cb291f) --- airflow/settings.py | 21 ++++++++++++++------- tests/core/test_settings.py | 24 +++++++++++++++++------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/airflow/settings.py b/airflow/settings.py index 88cd8e5276..9b2815efea 100644 --- a/airflow/settings.py +++ b/airflow/settings.py @@ -192,13 +192,6 @@ def configure_vars(): global PLUGINS_FOLDER global DONOT_MODIFY_HANDLERS SQL_ALCHEMY_CONN = conf.get("database", "SQL_ALCHEMY_CONN") - if SQL_ALCHEMY_CONN.startswith("sqlite") and not SQL_ALCHEMY_CONN.startswith("sqlite:////"): - from airflow.exceptions import AirflowConfigException - - raise AirflowConfigException( - f"Cannot use relative path: `{SQL_ALCHEMY_CONN}` to connect to sqlite. " - "Please use absolute path such as `sqlite:////tmp/airflow.db`." - ) DAGS_FOLDER = os.path.expanduser(conf.get("core", "DAGS_FOLDER")) @@ -232,6 +225,20 @@ def configure_orm(disable_connection_pool=False, pool_class=None): """Configure ORM using SQLAlchemy.""" from airflow.utils.log.secrets_masker import mask_secret + if ( + SQL_ALCHEMY_CONN + and SQL_ALCHEMY_CONN.startswith("sqlite") + and not SQL_ALCHEMY_CONN.startswith("sqlite:////") + # In memory is not useful for production, but useful for writing tests against Airflow for extensions + and SQL_ALCHEMY_CONN != "sqlite://" + ): + from airflow.exceptions import AirflowConfigException + + raise AirflowConfigException( + f"Cannot use relative path: `{SQL_ALCHEMY_CONN}` to connect to sqlite. " + "Please use absolute path such as `sqlite:////tmp/airflow.db`." + ) + global Session global engine if os.environ.get("_AIRFLOW_SKIP_DB_TESTS") == "true": diff --git a/tests/core/test_settings.py b/tests/core/test_settings.py index 79f49b4ef2..55d46cac15 100644 --- a/tests/core/test_settings.py +++ b/tests/core/test_settings.py @@ -17,11 +17,12 @@ # under the License. from __future__ import annotations +import contextlib import os import sys import tempfile from unittest import mock -from unittest.mock import MagicMock, call, patch +from unittest.mock import MagicMock, call import pytest @@ -216,11 +217,20 @@ class TestUpdatedConfigNames: assert session_lifetime_config == default_timeout_minutes -def test_sqlite_relative_path(): [email protected]( + ["value", "expectation"], + [ + ( + "sqlite:///./relative_path.db", + pytest.raises(AirflowConfigException, match=r"Cannot use relative path:"), + ), + # Should not raise an exception + ("sqlite://", contextlib.nullcontext()), + ], +) +def test_sqlite_relative_path(monkeypatch, value, expectation): from airflow import settings - with patch("airflow.settings.conf.get") as conf_get_mock: - conf_get_mock.return_value = "sqlite:///./relative_path.db" - with pytest.raises(AirflowConfigException) as exc: - settings.configure_vars() - assert "Cannot use relative path:" in str(exc.value) + monkeypatch.setattr(settings, "SQL_ALCHEMY_CONN", value) + with expectation: + settings.configure_orm()
