From 176af19652e2f3c41fc621ee01d301fa09bd1d9b Mon Sep 17 00:00:00 2001 From: Jarek Potiuk Date: Fri, 27 Dec 2024 18:24:31 +0100 Subject: [PATCH] Fix test collection for Connection and Variables for non-db tests 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. --- contributing-docs/testing/unit_tests.rst | 7 +++++++ providers/tests/microsoft/azure/fs/test_adls.py | 8 ++++++++ providers/tests/microsoft/azure/hooks/test_adx.py | 5 +++++ providers/tests/microsoft/azure/hooks/test_base_azure.py | 7 ++++++- .../microsoft/azure/hooks/test_container_registry.py | 7 +++++++ .../tests/microsoft/azure/hooks/test_container_volume.py | 7 +++++++ providers/tests/microsoft/azure/hooks/test_cosmos.py | 7 +++++++ .../tests/microsoft/azure/hooks/test_data_factory.py | 6 ++++++ providers/tests/microsoft/azure/hooks/test_wasb.py | 4 ++++ pyproject.toml | 1 + 10 files changed, 58 insertions(+), 1 deletion(-) diff --git a/contributing-docs/testing/unit_tests.rst b/contributing-docs/testing/unit_tests.rst index e15af7cc70e59..5a4cf4cac6cf2 100644 --- a/contributing-docs/testing/unit_tests.rst +++ b/contributing-docs/testing/unit_tests.rst @@ -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 ...................................... diff --git a/providers/tests/microsoft/azure/fs/test_adls.py b/providers/tests/microsoft/azure/fs/test_adls.py index 623ef37b6f4b8..24d6b862c4166 100644 --- a/providers/tests/microsoft/azure/fs/test_adls.py +++ b/providers/tests/microsoft/azure/fs/test_adls.py @@ -17,6 +17,7 @@ from __future__ import annotations +import os from unittest import mock import pytest @@ -24,6 +25,13 @@ 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(): diff --git a/providers/tests/microsoft/azure/hooks/test_adx.py b/providers/tests/microsoft/azure/hooks/test_adx.py index e2001c93848d3..c2741c3b8dfbe 100644 --- a/providers/tests/microsoft/azure/hooks/test_adx.py +++ b/providers/tests/microsoft/azure/hooks/test_adx.py @@ -17,6 +17,7 @@ # under the License. from __future__ import annotations +import os from unittest import mock import pytest @@ -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( diff --git a/providers/tests/microsoft/azure/hooks/test_base_azure.py b/providers/tests/microsoft/azure/hooks/test_base_azure.py index 8e666d2524c7b..89881eae16513 100644 --- a/providers/tests/microsoft/azure/hooks/test_base_azure.py +++ b/providers/tests/microsoft/azure/hooks/test_base_azure.py @@ -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 @@ -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" diff --git a/providers/tests/microsoft/azure/hooks/test_container_registry.py b/providers/tests/microsoft/azure/hooks/test_container_registry.py index bf1ee6ffe7263..166ad961915f1 100644 --- a/providers/tests/microsoft/azure/hooks/test_container_registry.py +++ b/providers/tests/microsoft/azure/hooks/test_container_registry.py @@ -17,6 +17,7 @@ # under the License. from __future__ import annotations +import os from unittest import mock import pytest @@ -24,6 +25,12 @@ 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( diff --git a/providers/tests/microsoft/azure/hooks/test_container_volume.py b/providers/tests/microsoft/azure/hooks/test_container_volume.py index 02b9ad5081c1f..5ab87e9cee8e8 100644 --- a/providers/tests/microsoft/azure/hooks/test_container_volume.py +++ b/providers/tests/microsoft/azure/hooks/test_container_volume.py @@ -17,6 +17,7 @@ # under the License. from __future__ import annotations +import os from unittest import mock import pytest @@ -24,6 +25,12 @@ 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( diff --git a/providers/tests/microsoft/azure/hooks/test_cosmos.py b/providers/tests/microsoft/azure/hooks/test_cosmos.py index bc6b4f4277a98..671ea7a34a41c 100644 --- a/providers/tests/microsoft/azure/hooks/test_cosmos.py +++ b/providers/tests/microsoft/azure/hooks/test_cosmos.py @@ -18,6 +18,7 @@ from __future__ import annotations import logging +import os import uuid from unittest import mock from unittest.mock import PropertyMock @@ -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 diff --git a/providers/tests/microsoft/azure/hooks/test_data_factory.py b/providers/tests/microsoft/azure/hooks/test_data_factory.py index a7d8786fd88c3..52fbca929da35 100644 --- a/providers/tests/microsoft/azure/hooks/test_data_factory.py +++ b/providers/tests/microsoft/azure/hooks/test_data_factory.py @@ -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): diff --git a/providers/tests/microsoft/azure/hooks/test_wasb.py b/providers/tests/microsoft/azure/hooks/test_wasb.py index b696b38485e39..26996d9d90fc5 100644 --- a/providers/tests/microsoft/azure/hooks/test_wasb.py +++ b/providers/tests/microsoft/azure/hooks/test_wasb.py @@ -17,6 +17,7 @@ # under the License. from __future__ import annotations +import os import re from unittest import mock @@ -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 = ( diff --git a/pyproject.toml b/pyproject.toml index 73238f6e0949e..3462c8625d6b2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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.