Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions contributing-docs/testing/unit_tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ from Airflow DB using Airflow's core code and it's crucial to run the tests agai
that Airflow supports in order to check if the SQLAlchemy queries are correct and if the database
schema is correct.

Those tests should be marked with ``@pytest.mark.db`` decorator on one of the levels:
Those tests should be marked with ``@pytest.mark.db_test`` decorator on one of the levels:

* test method can be marked with ``@pytest.mark.db`` decorator
* test class can be marked with ``@pytest.mark.db`` decorator
* test module can be marked with ``pytestmark = pytest.mark.db`` at the top level of the module
* test method can be marked with ``@pytest.mark.db_test`` decorator
* test class can be marked with ``@pytest.mark.db_test`` decorator
* test module can be marked with ``pytestmark = pytest.mark.db_test`` at the top level of the module

For the DB tests, they are run against the multiple databases Airflow support, multiple versions of those
and multiple Python versions it supports. In order to save time for testing not all combinations are
Expand Down
48 changes: 31 additions & 17 deletions tests/core/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import pytest

from airflow import settings
from airflow.exceptions import AirflowClusterPolicyViolation, AirflowConfigException
from tests.test_utils.config import conf_vars

Expand Down Expand Up @@ -217,22 +218,35 @@ def test_uses_updated_session_timeout_config_by_default(self):
assert session_lifetime_config == default_timeout_minutes


@pytest.mark.parametrize(
["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(value, expectation):
from airflow import settings

with patch("os.environ", {"_AIRFLOW_SKIP_DB_TESTS": "true"}), patch(
"airflow.settings.SQL_ALCHEMY_CONN", value
), patch("airflow.settings.Session"), patch("airflow.settings.engine"):
@pytest.mark.db_test
class TestSqliteRelativePath:
def setup_method(self):
self.old_session = settings.Session
self.old_engine = settings.engine
self.old_conn = settings.SQL_ALCHEMY_CONN

def teardown_method(self):
settings.Session = self.old_session
settings.engine = self.old_engine
settings.SQL_ALCHEMY_CONN = self.old_conn

@pytest.mark.parametrize(
["value", "expectation"],
[
(
"sqlite:///./relative_path.db",
pytest.raises(AirflowConfigException, match=r"Cannot use relative path:"),
),
# Should not raise an exception
("sqlite://", contextlib.nullcontext()),
],
)
@patch("airflow.settings.setup_event_handlers")
@patch("airflow.settings.scoped_session")
@patch("airflow.settings.sessionmaker")
@patch("airflow.settings.create_engine")
def test_sqlite_relative_path(self, mock_create_engine, _a, _b, _c, value, expectation, monkeypatch):
monkeypatch.setattr(settings, "SQL_ALCHEMY_CONN", value)
with expectation:
settings.configure_orm()
mock_create_engine.assert_called_once_with(value, connect_args={}, encoding="utf-8", future=True)