Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace unittests in providers-microsoft tests by pure pytest [Wave-4] #27735

Merged
merged 1 commit into from
Nov 18, 2022
Merged
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
2 changes: 1 addition & 1 deletion tests/providers/microsoft/azure/hooks/test_adx.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@


class TestAzureDataExplorerHook:
def teardown(self):
def teardown_method(self):
with create_session() as session:
session.query(Connection).filter(Connection.conn_id == ADX_TEST_CONN_ID).delete()

Expand Down
5 changes: 2 additions & 3 deletions tests/providers/microsoft/azure/hooks/test_azure_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from __future__ import annotations

import json
import unittest
from unittest import mock
from unittest.mock import PropertyMock

Expand All @@ -29,9 +28,9 @@
from airflow.utils import db


class TestAzureBatchHook(unittest.TestCase):
class TestAzureBatchHook:
# set up the test environment
def setUp(self):
def setup_method(self):
# set up the test variable
self.test_vm_conn_id = "test_azure_batch_vm"
self.test_cloud_conn_id = "test_azure_batch_cloud"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from __future__ import annotations

import json
import unittest
from unittest.mock import patch

from azure.mgmt.containerinstance.models import (
Expand All @@ -34,8 +33,8 @@
from airflow.utils import db


class TestAzureContainerInstanceHook(unittest.TestCase):
def setUp(self):
class TestAzureContainerInstanceHook:
def setup_method(self):
db.merge_conn(
Connection(
conn_id="azure_container_instance_test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
# under the License.
from __future__ import annotations

import unittest

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


class TestAzureContainerRegistryHook(unittest.TestCase):
class TestAzureContainerRegistryHook:
def test_get_conn(self):
db.merge_conn(
Connection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
from __future__ import annotations

import json
import unittest

from airflow.models import Connection
from airflow.providers.microsoft.azure.hooks.container_volume import AzureContainerVolumeHook
from airflow.utils import db
from tests.test_utils.providers import get_provider_min_airflow_version


class TestAzureContainerVolumeHook(unittest.TestCase):
class TestAzureContainerVolumeHook:
def test_get_file_volume(self):
db.merge_conn(
Connection(
Expand Down
5 changes: 2 additions & 3 deletions tests/providers/microsoft/azure/hooks/test_azure_cosmos.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import json
import logging
import unittest
import uuid
from unittest import mock
from unittest.mock import PropertyMock
Expand All @@ -34,10 +33,10 @@
from tests.test_utils.providers import get_provider_min_airflow_version


class TestAzureCosmosDbHook(unittest.TestCase):
class TestAzureCosmosDbHook:

# Set up an environment to test with
def setUp(self):
def setup_method(self):
# set up some test variables
self.test_end_point = "https://test_endpoint:443"
self.test_master_key = "magic_test_key"
Expand Down
5 changes: 2 additions & 3 deletions tests/providers/microsoft/azure/hooks/test_azure_data_lake.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from __future__ import annotations

import json
import unittest
from unittest import mock

from airflow.models import Connection
Expand All @@ -27,8 +26,8 @@
from tests.test_utils.providers import get_provider_min_airflow_version


class TestAzureDataLakeHook(unittest.TestCase):
def setUp(self):
class TestAzureDataLakeHook:
def setup_method(self):
db.merge_conn(
Connection(
conn_id="adl_test_key",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@


class TestAzureFileshareHook:
def setup(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also find this typo, just reminder for myself: "check other tests"

Copy link
Contributor Author

@Taragolis Taragolis Nov 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea but seems like setup method works as setup_method even if it not listed in https://docs.pytest.org/en/6.2.x/xunit_setup.html ... or I miss something

def setup_method(self):
db.merge_conn(
Connection(
conn_id="azure_fileshare_test_key",
Expand Down
3 changes: 1 addition & 2 deletions tests/providers/microsoft/azure/hooks/test_base_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
# under the License.
from __future__ import annotations

import unittest
from unittest.mock import Mock, patch

from airflow.models import Connection
from airflow.providers.microsoft.azure.hooks.base_azure import AzureBaseHook


class TestBaseAzureHook(unittest.TestCase):
class TestBaseAzureHook:
@patch("airflow.providers.microsoft.azure.hooks.base_azure.get_client_from_auth_file")
@patch(
"airflow.providers.microsoft.azure.hooks.base_azure.AzureBaseHook.get_connection",
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/microsoft/azure/hooks/test_wasb.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@


class TestWasbHook:
def setup(self):
def setup_method(self):
db.merge_conn(Connection(conn_id="wasb_test_key", conn_type="wasb", login="login", password="key"))
self.connection_type = "wasb"
self.connection_string_id = "azure_test_connection_string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# under the License.
from __future__ import annotations

import unittest
from unittest import mock

from airflow.providers.microsoft.azure.operators.adls import ADLSDeleteOperator
Expand All @@ -26,7 +25,7 @@
TEST_PATH = "test"


class TestAzureDataLakeStorageDeleteOperator(unittest.TestCase):
class TestAzureDataLakeStorageDeleteOperator:
@mock.patch("airflow.providers.microsoft.azure.operators.adls.AzureDataLakeHook")
def test_execute(self, mock_hook):

Expand Down
3 changes: 1 addition & 2 deletions tests/providers/microsoft/azure/operators/test_adls_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# under the License.
from __future__ import annotations

import unittest
from unittest import mock

from airflow.providers.microsoft.azure.operators.adls import ADLSListOperator
Expand All @@ -33,7 +32,7 @@
]


class TestAzureDataLakeStorageListOperator(unittest.TestCase):
class TestAzureDataLakeStorageListOperator:
@mock.patch("airflow.providers.microsoft.azure.operators.adls.AzureDataLakeHook")
def test_execute(self, mock_hook):
mock_hook.return_value.list.return_value = MOCK_FILES
Expand Down
5 changes: 2 additions & 3 deletions tests/providers/microsoft/azure/operators/test_adx.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# under the License.
from __future__ import annotations

import unittest
from unittest import mock

from azure.kusto.data._models import KustoResultTable
Expand Down Expand Up @@ -60,8 +59,8 @@ class MockResponse:
primary_results = [MOCK_RESULT]


class TestAzureDataExplorerQueryOperator(unittest.TestCase):
def setUp(self):
class TestAzureDataExplorerQueryOperator:
def setup_method(self):
args = {"owner": "airflow", "start_date": DEFAULT_DATE, "provide_context": True}

self.dag = DAG(TEST_DAG_ID + "test_schedule_dag_once", default_args=args, schedule="@once")
Expand Down
5 changes: 2 additions & 3 deletions tests/providers/microsoft/azure/operators/test_azure_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from __future__ import annotations

import json
import unittest
from unittest import mock

import pytest
Expand All @@ -41,11 +40,11 @@
$TargetDedicated = $isWorkingWeekdayHour ? 20:10;"""


class TestAzureBatchOperator(unittest.TestCase):
class TestAzureBatchOperator:
# set up the test environment
@mock.patch("airflow.providers.microsoft.azure.hooks.batch.AzureBatchHook")
@mock.patch("airflow.providers.microsoft.azure.hooks.batch.BatchServiceClient")
def setUp(self, mock_batch, mock_hook):
def setup_method(self, method, mock_batch, mock_hook):
# set up the test variable
self.test_vm_conn_id = "test_azure_batch_vm2"
self.test_cloud_conn_id = "test_azure_batch_cloud2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# under the License.
from __future__ import annotations

import unittest
from collections import namedtuple
from unittest import mock
from unittest.mock import MagicMock
Expand Down Expand Up @@ -64,7 +63,7 @@ def make_mock_cg_with_missing_events(container_state):
return container_g


class TestACIOperator(unittest.TestCase):
class TestACIOperator:
@mock.patch("airflow.providers.microsoft.azure.operators.container_instances.AzureContainerInstanceHook")
def test_execute(self, aci_mock):
expected_c_state = ContainerState(state="Terminated", exit_code=0, detail_status="test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from __future__ import annotations

import json
import unittest
import uuid
from unittest import mock

Expand All @@ -27,10 +26,10 @@
from airflow.utils import db


class TestAzureCosmosDbHook(unittest.TestCase):
class TestAzureCosmosDbHook:

# Set up an environment to test with
def setUp(self):
def setup_method(self):
# set up some test variables
self.test_end_point = "https://test_endpoint:443"
self.test_master_key = "magic_test_key"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from __future__ import annotations

import json
import unittest
from unittest import mock
from unittest.mock import MagicMock

Expand All @@ -39,8 +38,8 @@
JOB_RUN_RESPONSE = {"id": 123}


class TestAzureSynapseRunSparkBatchOperator(unittest.TestCase):
def setUp(self):
class TestAzureSynapseRunSparkBatchOperator:
def setup_method(self):
self.mock_ti = MagicMock()
self.mock_context = {"ti": self.mock_ti}
self.config = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@
from __future__ import annotations

import datetime
import unittest
from unittest import mock

from airflow.models.dag import DAG
from airflow.providers.microsoft.azure.operators.wasb_delete_blob import WasbDeleteBlobOperator


class TestWasbDeleteBlobOperator(unittest.TestCase):
class TestWasbDeleteBlobOperator:

_config = {
"container_name": "container",
"blob_name": "blob",
}

def setUp(self):
def setup_method(self):
args = {"owner": "airflow", "start_date": datetime.datetime(2017, 1, 1)}
self.dag = DAG("test_dag_id", default_args=args)

Expand Down
18 changes: 13 additions & 5 deletions tests/providers/microsoft/azure/secrets/test_azure_key_vault.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
# under the License.
from __future__ import annotations

from unittest import TestCase, mock
from unittest import mock

import pytest
from azure.core.exceptions import ResourceNotFoundError

from airflow.providers.microsoft.azure.secrets.key_vault import AzureKeyVaultBackend


class TestAzureKeyVaultBackend(TestCase):
class TestAzureKeyVaultBackend:
@mock.patch("airflow.providers.microsoft.azure.secrets.key_vault.AzureKeyVaultBackend.get_conn_value")
def test_get_connection(self, mock_get_value):
mock_get_value.return_value = "scheme://user:pass@host:100"
Expand All @@ -44,7 +45,8 @@ def test_get_conn_uri(self, mock_secret_client, mock_azure_cred):
)

backend = AzureKeyVaultBackend(vault_url="https://example-akv-resource-name.vault.azure.net/")
returned_uri = backend.get_conn_uri(conn_id="hi")
with pytest.warns(DeprecationWarning, match="Method `.*get_conn_uri` is deprecated"):
returned_uri = backend.get_conn_uri(conn_id="hi")
mock_secret_client.assert_called_once_with(
credential=mock_cred, vault_url="https://example-akv-resource-name.vault.azure.net/"
)
Expand All @@ -60,7 +62,8 @@ def test_get_conn_uri_non_existent_key(self, mock_client):
mock_client.get_secret.side_effect = ResourceNotFoundError
backend = AzureKeyVaultBackend(vault_url="https://example-akv-resource-name.vault.azure.net/")

assert backend.get_conn_uri(conn_id=conn_id) is None
with pytest.warns(DeprecationWarning, match="Method `.*get_conn_uri` is deprecated"):
assert backend.get_conn_uri(conn_id=conn_id) is None
assert backend.get_connection(conn_id=conn_id) is None

@mock.patch("airflow.providers.microsoft.azure.secrets.key_vault.AzureKeyVaultBackend.client")
Expand Down Expand Up @@ -113,9 +116,14 @@ def test_connection_prefix_none_value(self, mock_get_secret):
kwargs = {"connections_prefix": None}

backend = AzureKeyVaultBackend(**kwargs)
assert backend.get_conn_uri("test_mysql") is None
assert backend.get_connection("test_mysql") is None
mock_get_secret.assert_not_called()

mock_get_secret.reset_mock()
with pytest.warns(DeprecationWarning, match="Method `.*get_conn_uri` is deprecated"):
assert backend.get_conn_uri("test_mysql") is None
mock_get_secret.assert_not_called()

@mock.patch("airflow.providers.microsoft.azure.secrets.key_vault.AzureKeyVaultBackend._get_secret")
def test_variable_prefix_none_value(self, mock_get_secret):
"""
Expand Down
3 changes: 1 addition & 2 deletions tests/providers/microsoft/azure/sensors/test_azure_cosmos.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# under the License.
from __future__ import annotations

import unittest
from unittest import mock

from airflow.providers.microsoft.azure.sensors.cosmos import AzureCosmosDocumentSensor
Expand All @@ -26,7 +25,7 @@
DOCUMENT_ID = "test-document-id"


class TestAzureCosmosSensor(unittest.TestCase):
class TestAzureCosmosSensor:
@mock.patch("airflow.providers.microsoft.azure.sensors.cosmos.AzureCosmosDBHook")
def test_should_call_hook_with_args(self, mock_hook):
mock_instance = mock_hook.return_value
Expand Down
Loading