-
Notifications
You must be signed in to change notification settings - Fork 13.7k
/
test_analytics.py
109 lines (99 loc) · 4.6 KB
/
test_analytics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import unittest
from unittest import mock
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
API_VERSION = "v3"
GCP_CONN_ID = "google_cloud_default"
class TestGoogleAnalyticsHook(unittest.TestCase):
NUM_RETRIES = 5
def setUp(self):
with mock.patch(
"airflow.providers.google.cloud.hooks.base.CloudBaseHook.__init__",
new=mock_base_gcp_hook_default_project_id,
):
self.hook = GoogleAnalyticsHook(API_VERSION, GCP_CONN_ID)
@mock.patch(
"airflow.providers.google.marketing_platform.hooks."
"analytics.GoogleAnalyticsHook._authorize"
)
@mock.patch("airflow.providers.google.marketing_platform.hooks.analytics.build")
def test_gen_conn(self, mock_build, mock_authorize):
result = self.hook.get_conn()
mock_build.assert_called_once_with(
"analytics",
API_VERSION,
http=mock_authorize.return_value,
cache_discovery=False,
)
self.assertEqual(mock_build.return_value, result)
@mock.patch(
"airflow.providers.google.marketing_platform.hooks."
"analytics.GoogleAnalyticsHook.get_conn"
)
def test_list_accounts(self, get_conn_mock):
mock_accounts = get_conn_mock.return_value.management.return_value.accounts
mock_list = mock_accounts.return_value.list
mock_execute = mock_list.return_value.execute
mock_execute.return_value = {"items": ["a", "b"], "totalResults": 2}
list_accounts = self.hook.list_accounts()
self.assertEqual(list_accounts, ["a", "b"])
@mock.patch(
"airflow.providers.google.marketing_platform.hooks."
"analytics.GoogleAnalyticsHook.get_conn"
)
def test_list_accounts_for_multiple_pages(self, get_conn_mock):
mock_accounts = get_conn_mock.return_value.management.return_value.accounts
mock_list = mock_accounts.return_value.list
mock_execute = mock_list.return_value.execute
mock_execute.side_effect = [
{"items": ["a"], "totalResults": 2},
{"items": ["b"], "totalResults": 2},
]
list_accounts = self.hook.list_accounts()
self.assertEqual(list_accounts, ["a", "b"])
@mock.patch(
"airflow.providers.google.marketing_platform.hooks."
"analytics.GoogleAnalyticsHook.get_conn"
)
def test_list_ad_words_links(self, get_conn_mock):
account_id = "the_knight_who_says_ni!"
web_property_id = "web_property_id"
mock_ads_links = get_conn_mock.return_value.management.return_value.webPropertyAdWordsLinks
mock_list = mock_ads_links.return_value.list
mock_execute = mock_list.return_value.execute
mock_execute.return_value = {"items": ["a", "b"], "totalResults": 2}
list_ads_links = self.hook.list_ad_words_links(account_id=account_id, web_property_id=web_property_id)
self.assertEqual(list_ads_links, ["a", "b"])
@mock.patch(
"airflow.providers.google.marketing_platform.hooks."
"analytics.GoogleAnalyticsHook.get_conn"
)
def test_list_ad_words_links_for_multiple_pages(self, get_conn_mock):
account_id = "the_knight_who_says_ni!"
web_property_id = "web_property_id"
mock_ads_links = get_conn_mock.return_value.management.return_value.webPropertyAdWordsLinks
mock_list = mock_ads_links.return_value.list
mock_execute = mock_list.return_value.execute
mock_execute.side_effect = [
{"items": ["a"], "totalResults": 2},
{"items": ["b"], "totalResults": 2},
]
list_ads_links = self.hook.list_ad_words_links(account_id=account_id, web_property_id=web_property_id)
self.assertEqual(list_ads_links, ["a", "b"])