Skip to content

Commit

Permalink
Resolve deprecations in the tests for Google Analytics operators & …
Browse files Browse the repository at this point in the history
…hooks (apache#40328)
  • Loading branch information
boraberke authored Jun 20, 2024
1 parent 4fbdd07 commit 3b6fba9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 65 deletions.
15 changes: 0 additions & 15 deletions tests/deprecations_ignore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -239,21 +239,6 @@
- tests/providers/google/cloud/transfers/test_gcs_to_gcs.py::TestGoogleCloudStorageToCloudStorageOperator::test_wc_with_last_modified_time_with_all_true_cond
- tests/providers/google/cloud/transfers/test_gcs_to_gcs.py::TestGoogleCloudStorageToCloudStorageOperator::test_wc_with_last_modified_time_with_one_true_cond
- tests/providers/google/cloud/transfers/test_gcs_to_gcs.py::TestGoogleCloudStorageToCloudStorageOperator::test_wc_with_no_last_modified_time
- tests/providers/google/marketing_platform/hooks/test_analytics.py::TestGoogleAnalyticsHook::test_delete_upload_data
- tests/providers/google/marketing_platform/hooks/test_analytics.py::TestGoogleAnalyticsHook::test_gen_conn
- tests/providers/google/marketing_platform/hooks/test_analytics.py::TestGoogleAnalyticsHook::test_get_ad_words_links_call
- tests/providers/google/marketing_platform/hooks/test_analytics.py::TestGoogleAnalyticsHook::test_init
- tests/providers/google/marketing_platform/hooks/test_analytics.py::TestGoogleAnalyticsHook::test_list_accounts
- tests/providers/google/marketing_platform/hooks/test_analytics.py::TestGoogleAnalyticsHook::test_list_accounts_for_multiple_pages
- tests/providers/google/marketing_platform/hooks/test_analytics.py::TestGoogleAnalyticsHook::test_list_ad_words_links
- tests/providers/google/marketing_platform/hooks/test_analytics.py::TestGoogleAnalyticsHook::test_list_ad_words_links_for_multiple_pages
- tests/providers/google/marketing_platform/hooks/test_analytics.py::TestGoogleAnalyticsHook::test_list_upload
- tests/providers/google/marketing_platform/hooks/test_analytics.py::TestGoogleAnalyticsHook::test_upload_data
- tests/providers/google/marketing_platform/operators/test_analytics.py::TestGoogleAnalyticsDataImportUploadOperator::test_execute
- tests/providers/google/marketing_platform/operators/test_analytics.py::TestGoogleAnalyticsDeletePreviousDataUploadsOperator::test_execute
- tests/providers/google/marketing_platform/operators/test_analytics.py::TestGoogleAnalyticsGetAdsLinkOperator::test_execute
- tests/providers/google/marketing_platform/operators/test_analytics.py::TestGoogleAnalyticsListAccountsOperator::test_execute
- tests/providers/google/marketing_platform/operators/test_analytics.py::TestGoogleAnalyticsRetrieveAdsLinksListOperator::test_execute
- tests/providers/jdbc/operators/test_jdbc.py::TestJdbcOperator::test_execute_do_push
- tests/providers/jdbc/operators/test_jdbc.py::TestJdbcOperator::test_execute_dont_push
- tests/providers/microsoft/azure/hooks/test_adx.py::TestAzureDataExplorerHook::test_backcompat_prefix_works
Expand Down
25 changes: 18 additions & 7 deletions tests/providers/google/marketing_platform/hooks/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
# under the License.
from __future__ import annotations

import warnings
from unittest import mock

import pytest

from airflow.exceptions import AirflowProviderDeprecationWarning
from airflow.providers.google.marketing_platform.hooks.analytics import GoogleAnalyticsHook
from tests.providers.google.cloud.utils.base_gcp_mock import mock_base_gcp_hook_default_project_id

Expand All @@ -30,24 +34,31 @@
GCP_CONN_ID = "test_gcp_conn_id"
DELEGATE_TO = "TEST_DELEGATE_TO"
IMPERSONATION_CHAIN = ["ACCOUNT_1", "ACCOUNT_2", "ACCOUNT_3"]
DEPRECATION_MESSAGE = (
r"Call to deprecated class GoogleAnalyticsHook\."
r" \(The `GoogleAnalyticsHook` class is deprecated,"
r" please use `GoogleAnalyticsAdminHook` instead\.\)"
)


class TestGoogleAnalyticsHook:
def setup_method(self):
with mock.patch(
"airflow.providers.google.common.hooks.base_google.GoogleBaseHook.__init__",
new=mock_base_gcp_hook_default_project_id,
):
), warnings.catch_warnings():
warnings.simplefilter("ignore", AirflowProviderDeprecationWarning)
self.hook = GoogleAnalyticsHook(API_VERSION, GCP_CONN_ID)

@mock.patch("airflow.providers.google.common.hooks.base_google.GoogleBaseHook.__init__")
def test_init(self, mock_base_init):
hook = GoogleAnalyticsHook(
API_VERSION,
GCP_CONN_ID,
delegate_to=DELEGATE_TO,
impersonation_chain=IMPERSONATION_CHAIN,
)
with pytest.warns(AirflowProviderDeprecationWarning, match=DEPRECATION_MESSAGE):
hook = GoogleAnalyticsHook(
API_VERSION,
GCP_CONN_ID,
delegate_to=DELEGATE_TO,
impersonation_chain=IMPERSONATION_CHAIN,
)
mock_base_init.assert_called_once_with(
GCP_CONN_ID,
delegate_to=DELEGATE_TO,
Expand Down
113 changes: 70 additions & 43 deletions tests/providers/google/marketing_platform/operators/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from tempfile import NamedTemporaryFile
from unittest import mock

import pytest

from airflow.exceptions import AirflowProviderDeprecationWarning
from airflow.providers.google.marketing_platform.operators.analytics import (
GoogleAnalyticsDataImportUploadOperator,
GoogleAnalyticsDeletePreviousDataUploadsOperator,
Expand All @@ -42,12 +45,17 @@
class TestGoogleAnalyticsListAccountsOperator:
@mock.patch("airflow.providers.google.marketing_platform.operators.analytics.GoogleAnalyticsHook")
def test_execute(self, hook_mock):
op = GoogleAnalyticsListAccountsOperator(
api_version=API_VERSION,
gcp_conn_id=GCP_CONN_ID,
task_id="test_task",
impersonation_chain=IMPERSONATION_CHAIN,
)
with pytest.warns(
AirflowProviderDeprecationWarning,
match="The `GoogleAnalyticsListAccountsOperator` class is deprecated, please use "
"`GoogleAnalyticsAdminListAccountsOperator` instead.",
):
op = GoogleAnalyticsListAccountsOperator(
api_version=API_VERSION,
gcp_conn_id=GCP_CONN_ID,
task_id="test_task",
impersonation_chain=IMPERSONATION_CHAIN,
)
op.execute(context=None)
hook_mock.assert_called_once()
hook_mock.return_value.list_accounts.assert_called_once()
Expand All @@ -56,14 +64,19 @@ def test_execute(self, hook_mock):
class TestGoogleAnalyticsRetrieveAdsLinksListOperator:
@mock.patch("airflow.providers.google.marketing_platform.operators.analytics.GoogleAnalyticsHook")
def test_execute(self, hook_mock):
op = GoogleAnalyticsRetrieveAdsLinksListOperator(
account_id=ACCOUNT_ID,
web_property_id=WEB_PROPERTY_ID,
api_version=API_VERSION,
gcp_conn_id=GCP_CONN_ID,
task_id="test_task",
impersonation_chain=IMPERSONATION_CHAIN,
)
with pytest.warns(
AirflowProviderDeprecationWarning,
match="The `GoogleAnalyticsRetrieveAdsLinksListOperator` class is deprecated, please use "
"`GoogleAnalyticsAdminListGoogleAdsLinksOperator` instead.",
):
op = GoogleAnalyticsRetrieveAdsLinksListOperator(
account_id=ACCOUNT_ID,
web_property_id=WEB_PROPERTY_ID,
api_version=API_VERSION,
gcp_conn_id=GCP_CONN_ID,
task_id="test_task",
impersonation_chain=IMPERSONATION_CHAIN,
)
op.execute(context=None)
hook_mock.assert_called_once()
hook_mock.return_value.list_ad_words_links.assert_called_once()
Expand All @@ -80,15 +93,20 @@ def test_execute(self, hook_mock):
class TestGoogleAnalyticsGetAdsLinkOperator:
@mock.patch("airflow.providers.google.marketing_platform.operators.analytics.GoogleAnalyticsHook")
def test_execute(self, hook_mock):
op = GoogleAnalyticsGetAdsLinkOperator(
account_id=ACCOUNT_ID,
web_property_id=WEB_PROPERTY_ID,
web_property_ad_words_link_id=WEB_PROPERTY_AD_WORDS_LINK_ID,
api_version=API_VERSION,
gcp_conn_id=GCP_CONN_ID,
task_id="test_task",
impersonation_chain=IMPERSONATION_CHAIN,
)
with pytest.warns(
AirflowProviderDeprecationWarning,
match="The `GoogleAnalyticsGetAdsLinkOperator` class is deprecated, please use "
"`GoogleAnalyticsAdminGetGoogleAdsLinkOperator` instead.",
):
op = GoogleAnalyticsGetAdsLinkOperator(
account_id=ACCOUNT_ID,
web_property_id=WEB_PROPERTY_ID,
web_property_ad_words_link_id=WEB_PROPERTY_AD_WORDS_LINK_ID,
api_version=API_VERSION,
gcp_conn_id=GCP_CONN_ID,
task_id="test_task",
impersonation_chain=IMPERSONATION_CHAIN,
)
op.execute(context=None)
hook_mock.assert_called_once()
hook_mock.return_value.get_ad_words_link.assert_called_once()
Expand All @@ -112,17 +130,22 @@ def test_execute(self, mock_tempfile, gcs_hook_mock, ga_hook_mock):
filename = "file/"
mock_tempfile.return_value.__enter__.return_value.name = filename

op = GoogleAnalyticsDataImportUploadOperator(
account_id=ACCOUNT_ID,
web_property_id=WEB_PROPERTY_ID,
storage_bucket=BUCKET,
storage_name_object=BUCKET_OBJECT_NAME,
custom_data_source_id=DATA_SOURCE,
api_version=API_VERSION,
gcp_conn_id=GCP_CONN_ID,
task_id="test_task",
impersonation_chain=IMPERSONATION_CHAIN,
)
with pytest.warns(
AirflowProviderDeprecationWarning,
match="The `GoogleAnalyticsDataImportUploadOperator` class is deprecated, "
"please use `GoogleAnalyticsAdminCreateDataStreamOperator` instead.",
):
op = GoogleAnalyticsDataImportUploadOperator(
account_id=ACCOUNT_ID,
web_property_id=WEB_PROPERTY_ID,
storage_bucket=BUCKET,
storage_name_object=BUCKET_OBJECT_NAME,
custom_data_source_id=DATA_SOURCE,
api_version=API_VERSION,
gcp_conn_id=GCP_CONN_ID,
task_id="test_task",
impersonation_chain=IMPERSONATION_CHAIN,
)
op.execute(context=None)

gcs_hook_mock.assert_called_once_with(
Expand Down Expand Up @@ -154,15 +177,19 @@ def test_execute(self, mock_hook):
{"id": 2},
{"id": 3},
]

op = GoogleAnalyticsDeletePreviousDataUploadsOperator(
account_id=ACCOUNT_ID,
web_property_id=WEB_PROPERTY_ID,
custom_data_source_id=DATA_SOURCE,
api_version=API_VERSION,
gcp_conn_id=GCP_CONN_ID,
task_id="test_task",
)
with pytest.warns(
AirflowProviderDeprecationWarning,
match="The `GoogleAnalyticsDeletePreviousDataUploadsOperator` class is deprecated, please use "
"`GoogleAnalyticsAdminDeleteDataStreamOperator` instead.",
):
op = GoogleAnalyticsDeletePreviousDataUploadsOperator(
account_id=ACCOUNT_ID,
web_property_id=WEB_PROPERTY_ID,
custom_data_source_id=DATA_SOURCE,
api_version=API_VERSION,
gcp_conn_id=GCP_CONN_ID,
task_id="test_task",
)
op.execute(context=None)
mock_hook.assert_called_once_with(
gcp_conn_id=GCP_CONN_ID,
Expand Down

0 comments on commit 3b6fba9

Please sign in to comment.