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

Resolve deprecations in atlassian.jira provider tests #39517

Merged
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
4 changes: 0 additions & 4 deletions tests/deprecations_ignore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,6 @@
- tests/providers/amazon/aws/utils/test_connection_wrapper.py::TestAwsConnectionWrapper::test_get_endpoint_url_from_extra
- tests/providers/apache/livy/operators/test_livy.py::TestLivyOperator::test_execution_with_extra_options
- tests/providers/apache/spark/operators/test_spark_sql.py::TestSparkSqlOperator::test_execute
- tests/providers/atlassian/jira/operators/test_jira.py::TestJiraOperator::test_issue_search
- tests/providers/atlassian/jira/operators/test_jira.py::TestJiraOperator::test_project_issue_count
- tests/providers/atlassian/jira/operators/test_jira.py::TestJiraOperator::test_update_issue
- tests/providers/atlassian/jira/sensors/test_jira.py::TestJiraSensor::test_issue_label_set
- tests/providers/common/sql/hooks/test_dbapi.py::TestDbApiHook::test_insert_rows_executemany
- tests/providers/common/sql/hooks/test_dbapi.py::TestDbApiHook::test_insert_rows_replace_executemany_hana_dialect
- tests/providers/common/sql/hooks/test_dbapi.py::TestDbApiHook::test_instance_check_works_for_legacy_db_api_hook
Expand Down
66 changes: 33 additions & 33 deletions tests/providers/atlassian/jira/hooks/test_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,47 @@
# under the License.
from __future__ import annotations

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

import pytest

from airflow.exceptions import AirflowProviderDeprecationWarning
from airflow.models import Connection
from airflow.providers.atlassian.jira.hooks.jira import JiraHook
from airflow.utils import db

pytestmark = pytest.mark.db_test

jira_client_mock = Mock(name="jira_client")
@pytest.fixture
def mocked_jira_client():
with mock.patch("airflow.providers.atlassian.jira.hooks.jira.Jira", autospec=True) as m:
m.return_value = mock.Mock(name="jira_client")
yield m


class TestJiraHook:
depcrecation_message = (
"Extra parameter `verify` using str is deprecated and will be removed "
"in a future release. Please use `verify` using bool instead."
)
conn_id = "jira_default"
conn_id_with_str_verify = "jira_default_with_str"
conn_type = "jira"
host = "https://localhost/jira/"
port = 443
login = "user"
password = "password"
proxies = None
@pytest.fixture(autouse=True)
def setup_test_cases(self, monkeypatch):
self.conn_id = "jira_default"
self.conn_id_with_str_verify = "jira_default_with_str"
self.host = "https://localhost/jira/"
self.port = 443
self.login = "user"
self.password = "password"
self.proxies = None

def setup_method(self):
db.merge_conn(
monkeypatch.setenv(
f"AIRFLOW_CONN_{self.conn_id}".upper(),
Connection(
conn_id=self.conn_id,
conn_id="jira_default",
conn_type="jira",
host="https://localhost/jira/",
port=443,
login="user",
password="password",
extra='{"verify": false, "project": "AIRFLOW"}',
)
).as_json(),
)
db.merge_conn(
monkeypatch.setenv(
f"AIRFLOW_CONN_{self.conn_id_with_str_verify}".upper(),
Connection(
conn_id=self.conn_id_with_str_verify,
conn_type="jira",
Expand All @@ -66,34 +66,34 @@ def setup_method(self):
login="user",
password="password",
extra='{"verify": "False", "project": "AIRFLOW"}',
)
).as_json(),
)

@patch("airflow.providers.atlassian.jira.hooks.jira.Jira", autospec=True, return_value=jira_client_mock)
def test_jira_client_connection(self, jira_mock):
def test_jira_client_connection(self, mocked_jira_client):
jira_hook = JiraHook(proxies=self.proxies)

jira_mock.assert_called_once_with(
mocked_jira_client.assert_called_once_with(
url=self.host,
username=self.login,
password=self.password,
verify_ssl=False,
proxies=self.proxies,
)
assert isinstance(jira_hook.client, Mock)
assert jira_hook.client.name == jira_mock.return_value.name
assert isinstance(jira_hook.client, mock.Mock)
assert jira_hook.client.name == mocked_jira_client.return_value.name

@patch("airflow.providers.atlassian.jira.hooks.jira.Jira", autospec=True, return_value=jira_client_mock)
def test_jira_client_connection_with_str(self, jira_mock):
with pytest.warns(AirflowProviderDeprecationWarning, match=self.depcrecation_message):
def test_jira_client_connection_with_str(self, mocked_jira_client):
warning_message = "Extra parameter `verify` using str is deprecated and will be removed"

with pytest.warns(AirflowProviderDeprecationWarning, match=warning_message):
jira_hook = JiraHook(jira_conn_id=self.conn_id_with_str_verify, proxies=self.proxies)

jira_mock.assert_called_once_with(
mocked_jira_client.assert_called_once_with(
url=self.host,
username=self.login,
password=self.password,
verify_ssl=False,
proxies=self.proxies,
)
assert isinstance(jira_hook.client, Mock)
assert jira_hook.client.name == jira_mock.return_value.name
assert isinstance(jira_hook.client, mock.Mock)
assert jira_hook.client.name == mocked_jira_client.return_value.name
84 changes: 41 additions & 43 deletions tests/providers/atlassian/jira/operators/test_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,16 @@
# under the License.
from __future__ import annotations

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

import pytest

from airflow.models import Connection
from airflow.models.dag import DAG
from airflow.providers.atlassian.jira.operators.jira import JiraOperator
from airflow.utils import db, timezone

pytestmark = pytest.mark.db_test

from airflow.utils import timezone

DEFAULT_DATE = timezone.datetime(2017, 1, 1)
jira_client_mock = Mock(name="jira_client_for_test")

minimal_test_ticket = {
MINIMAL_TEST_TICKET = {
"id": "911539",
"self": "https://sandbox.localhost/jira/rest/api/2/issue/911539",
"key": "TEST-1226",
Expand All @@ -43,20 +37,29 @@
}


@pytest.fixture
def mocked_jira_client():
with mock.patch("airflow.providers.atlassian.jira.hooks.jira.Jira", autospec=True) as m:
m.return_value = mock.Mock(name="jira_client_for_test")
yield m


class TestJiraOperator:
def setup_method(self):
args = {"owner": "airflow", "start_date": DEFAULT_DATE}
dag = DAG("test_dag_id", default_args=args)
self.dag = dag
db.merge_conn(
@pytest.fixture(autouse=True)
def setup_test_cases(self, monkeypatch):
monkeypatch.setenv(
"AIRFLOW_CONN_JIRA_DEFAULT",
Connection(
conn_id="jira_default",
conn_type="jira",
host="https://localhost/jira/",
port=443,
extra='{"verify": "False", "project": "AIRFLOW"}',
)
extra='{"verify": false, "project": "AIRFLOW"}',
).as_json(),
)
with mock.patch("airflow.models.baseoperator.BaseOperator.xcom_push", return_value=None) as m:
self.mocked_xcom_push = m
yield

def test_operator_init_with_optional_args(self):
jira_operator = JiraOperator(task_id="jira_list_issue_types", jira_method="issue_types")
Expand All @@ -65,51 +68,46 @@ def test_operator_init_with_optional_args(self):
assert jira_operator.result_processor is None
assert jira_operator.get_jira_resource_method is None

@patch("airflow.providers.atlassian.jira.hooks.jira.Jira", autospec=True, return_value=jira_client_mock)
def test_project_issue_count(self, jira_mock):
jira_mock.return_value.get_project_issues_count.return_value = 10

jira_ticket_search_operator = JiraOperator(
def test_project_issue_count(self, mocked_jira_client):
mocked_jira_client.return_value.get_project_issues_count.return_value = 10
op = JiraOperator(
task_id="get-issue-count",
jira_method="get_project_issues_count",
jira_method_args={"project": "ABC"},
dag=self.dag,
)

jira_ticket_search_operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)
op.execute({})

assert jira_mock.called
assert jira_mock.return_value.get_project_issues_count.called
assert mocked_jira_client.called
assert mocked_jira_client.return_value.get_project_issues_count.called
self.mocked_xcom_push.assert_called_once_with(mock.ANY, key="id", value=None)

@patch("airflow.providers.atlassian.jira.hooks.jira.Jira", autospec=True, return_value=jira_client_mock)
def test_issue_search(self, jira_mock):
def test_issue_search(self, mocked_jira_client):
jql_str = "issuekey=TEST-1226"
jira_mock.return_value.jql_get_list_of_tickets.return_value = minimal_test_ticket

jira_ticket_search_operator = JiraOperator(
mocked_jira_client.return_value.jql_get_list_of_tickets.return_value = MINIMAL_TEST_TICKET
op = JiraOperator(
task_id="search-ticket-test",
jira_method="jql_get_list_of_tickets",
jira_method_args={"jql": jql_str, "limit": "1"},
dag=self.dag,
)

jira_ticket_search_operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)
op.execute({})

assert jira_mock.called
assert jira_mock.return_value.jql_get_list_of_tickets.called
assert mocked_jira_client.called
assert mocked_jira_client.return_value.jql_get_list_of_tickets.called
self.mocked_xcom_push.assert_called_once_with(mock.ANY, key="id", value="911539")

@patch("airflow.providers.atlassian.jira.hooks.jira.Jira", autospec=True, return_value=jira_client_mock)
def test_update_issue(self, jira_mock):
jira_mock.return_value.issue_add_comment.return_value = minimal_test_ticket
def test_update_issue(self, mocked_jira_client):
mocked_jira_client.return_value.issue_add_comment.return_value = MINIMAL_TEST_TICKET

add_comment_operator = JiraOperator(
op = JiraOperator(
task_id="add_comment_test",
jira_method="issue_add_comment",
jira_method_args={"issue_key": minimal_test_ticket.get("key"), "comment": "this is test comment"},
dag=self.dag,
jira_method_args={"issue_key": MINIMAL_TEST_TICKET.get("key"), "comment": "this is test comment"},
)

add_comment_operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)
op.execute({})

assert jira_mock.called
assert jira_mock.return_value.issue_add_comment.called
assert mocked_jira_client.called
assert mocked_jira_client.return_value.issue_add_comment.called
self.mocked_xcom_push.assert_called_once_with(mock.ANY, key="id", value="911539")
48 changes: 24 additions & 24 deletions tests/providers/atlassian/jira/sensors/test_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,16 @@
# under the License.
from __future__ import annotations

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

import pytest

from airflow.models import Connection
from airflow.models.dag import DAG
from airflow.providers.atlassian.jira.sensors.jira import JiraTicketSensor
from airflow.utils import db, timezone

pytestmark = pytest.mark.db_test
from airflow.utils import timezone

DEFAULT_DATE = timezone.datetime(2017, 1, 1)
jira_client_mock = Mock(name="jira_client_for_test")

minimal_test_ticket = {
MINIMAL_TEST_TICKET = {
"id": "911539",
"self": "https://sandbox.localhost/jira/rest/api/2/issue/911539",
"key": "TEST-1226",
Expand All @@ -42,36 +37,41 @@
}


@pytest.fixture
def mocked_jira_client():
with mock.patch("airflow.providers.atlassian.jira.hooks.jira.Jira", autospec=True) as m:
m.return_value = mock.Mock(name="jira_client_for_test")
yield m


class TestJiraSensor:
def setup_method(self):
args = {"owner": "airflow", "start_date": DEFAULT_DATE}
dag = DAG("test_dag_id", default_args=args)
self.dag = dag
db.merge_conn(
@pytest.fixture(autouse=True)
def setup_test_cases(self, monkeypatch):
monkeypatch.setenv(
"AIRFLOW_CONN_JIRA_DEFAULT".upper(),
Connection(
conn_id="jira_default",
conn_type="jira",
host="https://localhost/jira/",
port=443,
extra='{"verify": "False", "project": "AIRFLOW"}',
)
login="user",
password="password",
extra='{"verify": false, "project": "AIRFLOW"}',
).as_json(),
)

@patch("airflow.providers.atlassian.jira.hooks.jira.Jira", autospec=True, return_value=jira_client_mock)
def test_issue_label_set(self, jira_mock):
jira_mock.return_value.issue.return_value = minimal_test_ticket

ticket_label_sensor = JiraTicketSensor(
def test_issue_label_set(self, mocked_jira_client):
mocked_jira_client.return_value.issue.return_value = MINIMAL_TEST_TICKET
sensor = JiraTicketSensor(
task_id="search-ticket-test",
ticket_id="TEST-1226",
field="labels",
expected_value="test-label-1",
timeout=518400,
poke_interval=10,
dag=self.dag,
)

ticket_label_sensor.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)
assert sensor.poke({})

assert jira_mock.called
assert jira_mock.return_value.issue.called
assert mocked_jira_client.called
assert mocked_jira_client.return_value.issue.called