Skip to content

Commit

Permalink
Fix test collection for Connection and Variables for non-db tests (#4…
Browse files Browse the repository at this point in the history
…5249)

When Tests were collected for microsoft provider alone for non-db
tests, Connection were used in "parametrized" to perameterize the
tests. This was only working accidentally so far because all ORM
models were loaded before in other tests - including Triggers -and
then the connection objects did not trigger any DB operations. But
when microsoft provider tests were run first (and in separation),
the Triggers were not imported and Connection creation caused error:

> When initializing mapper mapped class AssetModel->asset, expression
  'Trigger' failed to locate a name ('Trigger') # Please enter the commit
  message for your changes. Lines starting

This can be mitigated (as already advised in unit tests documentation)
by replacing Connections with MagicMock.
  • Loading branch information
potiuk authored Dec 27, 2024
1 parent 7f2b8ef commit aab2b88
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 1 deletion.
7 changes: 7 additions & 0 deletions contributing-docs/testing/unit_tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@ For selected test types (example - the tests will run for Providers/API/CLI code
breeze testing providers-tests --skip-db-tests --parallel-test-types "Providers[google] Providers[amazon]"
You can also enter interactive shell with ``--skip-db-tests`` flag and run the tests iteratively

.. code-block:: bash
breeze shell --skip-db-tests
> pytest tests/your_test.py
How to make your test not depend on DB
......................................
Expand Down
8 changes: 8 additions & 0 deletions providers/tests/microsoft/azure/fs/test_adls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@

from __future__ import annotations

import os
from unittest import mock

import pytest

from airflow.models import Connection
from airflow.providers.microsoft.azure.fs.adls import get_fs

if os.environ.get("_AIRFLOW_SKIP_DB_TESTS") == "true":
# Handle collection of the test by non-db case
Connection = mock.MagicMock() # type: ignore[misc]


pytestmark = pytest.mark.db_test


@pytest.fixture
def mocked_blob_file_system():
Expand Down
5 changes: 5 additions & 0 deletions providers/tests/microsoft/azure/hooks/test_adx.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# under the License.
from __future__ import annotations

import os
from unittest import mock

import pytest
Expand All @@ -31,6 +32,10 @@

pytestmark = pytest.mark.db_test

if os.environ.get("_AIRFLOW_SKIP_DB_TESTS") == "true":
# Handle collection of the test by non-db case
Connection = mock.MagicMock() # type: ignore[misc]


class TestAzureDataExplorerHook:
@pytest.mark.parametrize(
Expand Down
7 changes: 6 additions & 1 deletion providers/tests/microsoft/azure/hooks/test_base_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
# under the License.
from __future__ import annotations

from unittest.mock import Mock, patch
import os
from unittest.mock import MagicMock, Mock, patch

import pytest

Expand All @@ -25,6 +26,10 @@

pytestmark = pytest.mark.db_test

if os.environ.get("_AIRFLOW_SKIP_DB_TESTS") == "true":
# Handle collection of the test by non-db case
Connection = MagicMock() # type: ignore[misc]

MODULE = "airflow.providers.microsoft.azure.hooks.base_azure"


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@
# under the License.
from __future__ import annotations

import os
from unittest import mock

import pytest

from airflow.models import Connection
from airflow.providers.microsoft.azure.hooks.container_registry import AzureContainerRegistryHook

pytestmark = pytest.mark.db_test

if os.environ.get("_AIRFLOW_SKIP_DB_TESTS") == "true":
# Handle collection of the test by non-db case
Connection = mock.MagicMock() # type: ignore[misc]


class TestAzureContainerRegistryHook:
@pytest.mark.parametrize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@
# under the License.
from __future__ import annotations

import os
from unittest import mock

import pytest

from airflow.models import Connection
from airflow.providers.microsoft.azure.hooks.container_volume import AzureContainerVolumeHook

pytestmark = pytest.mark.db_test

if os.environ.get("_AIRFLOW_SKIP_DB_TESTS") == "true":
# Handle collection of the test by non-db case
Connection = mock.MagicMock() # type: ignore[misc]


class TestAzureContainerVolumeHook:
@pytest.mark.parametrize(
Expand Down
7 changes: 7 additions & 0 deletions providers/tests/microsoft/azure/hooks/test_cosmos.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from __future__ import annotations

import logging
import os
import uuid
from unittest import mock
from unittest.mock import PropertyMock
Expand All @@ -30,8 +31,14 @@
from airflow.models import Connection
from airflow.providers.microsoft.azure.hooks.cosmos import AzureCosmosDBHook

pytestmark = pytest.mark.db_test

MODULE = "airflow.providers.microsoft.azure.hooks.cosmos"

if os.environ.get("_AIRFLOW_SKIP_DB_TESTS") == "true":
# Handle collection of the test by non-db case
Connection = mock.MagicMock() # type: ignore[misc]


class TestAzureCosmosDbHook:
# Set up an environment to test with
Expand Down
6 changes: 6 additions & 0 deletions providers/tests/microsoft/azure/hooks/test_data_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
# TODO: FIXME: the tests here have tricky issues with typing and need a bit more thought to fix them
# mypy: disable-error-code="union-attr,call-overload"

pytestmark = pytest.mark.db_test

if os.environ.get("_AIRFLOW_SKIP_DB_TESTS") == "true":
# Handle collection of the test by non-db case
Connection = mock.MagicMock() # type: ignore[misc]


@pytest.fixture(autouse=True)
def setup_connections(create_mock_connections):
Expand Down
4 changes: 4 additions & 0 deletions providers/tests/microsoft/azure/hooks/test_wasb.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# under the License.
from __future__ import annotations

import os
import re
from unittest import mock

Expand All @@ -31,6 +32,9 @@

pytestmark = pytest.mark.db_test

if os.environ.get("_AIRFLOW_SKIP_DB_TESTS") == "true":
# Handle collection of the test by non-db case
Connection = mock.MagicMock() # type: ignore[misc]

# connection_string has a format
CONN_STRING = (
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ testing = ["dev", "providers.tests", "task_sdk.tests", "tests_common", "tests"]

# Test compat imports banned imports to allow testing against older airflow versions
"tests_common/test_utils/compat.py" = ["TID251", "F401"]
"tests_common/pytest_plugin.py" = ["F811"]

[tool.ruff.lint.flake8-tidy-imports]
# Disallow all relative imports.
Expand Down

0 comments on commit aab2b88

Please sign in to comment.