Skip to content

Commit 235354b

Browse files
committed
Update misc/ example tests
1 parent 178e44b commit 235354b

File tree

7 files changed

+86
-48
lines changed

7 files changed

+86
-48
lines changed

examples/misc/campaign_report_to_csv.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@
3636

3737

3838
_DEFAULT_FILE_NAME = "campaign_report_to_csv_results.csv"
39+
_QUERY: str = """
40+
SELECT
41+
customer.descriptive_name,
42+
segments.date,
43+
campaign.name,
44+
metrics.impressions,
45+
metrics.clicks,
46+
metrics.cost_micros
47+
FROM campaign
48+
WHERE
49+
segments.date DURING LAST_7_DAYS
50+
ORDER BY metrics.impressions DESC
51+
LIMIT 25"""
3952

4053

4154
def main(
@@ -55,24 +68,10 @@ def main(
5568
file_path: str = os.path.join(file_dir, output_file)
5669
ga_service = client.get_service("GoogleAdsService")
5770

58-
query: str = """
59-
SELECT
60-
customer.descriptive_name,
61-
segments.date,
62-
campaign.name,
63-
metrics.impressions,
64-
metrics.clicks,
65-
metrics.cost_micros
66-
FROM campaign
67-
WHERE
68-
segments.date DURING LAST_7_DAYS
69-
ORDER BY metrics.impressions DESC
70-
LIMIT 25"""
71-
7271
# Issues a search request using streaming.
7372
search_request = client.get_type("SearchGoogleAdsStreamRequest")
7473
search_request.customer_id = customer_id
75-
search_request.query = query
74+
search_request.query = _QUERY
7675
stream = ga_service.search_stream(search_request)
7776

7877
with open(file_path, "w", newline="") as f:

examples/misc/tests/test_add_ad_group_image_asset.py renamed to tests/examples/misc/add_ad_group_image_asset_test.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
114
import unittest
215
from unittest import mock
3-
import sys
416
import argparse # Ensure argparse is imported
517

618
from google.ads.googleads.errors import GoogleAdsException
@@ -203,7 +215,4 @@ def test_argument_parsing(
203215
expected_cust_id,
204216
expected_ag_id,
205217
expected_asset_id_val
206-
)
207-
208-
if __name__ == "__main__":
209-
unittest.main()
218+
)

examples/misc/tests/test_campaign_report_to_csv.py renamed to tests/examples/misc/campaign_report_to_csv_test.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
114
import unittest
215
from unittest import mock
316
import csv
@@ -11,14 +24,6 @@
1124
from examples.misc import campaign_report_to_csv
1225
from google.ads.googleads.errors import GoogleAdsException
1326
from .test_utils import create_mock_google_ads_exception
14-
from google.ads.googleads.v19.services.services.google_ads_service import GoogleAdsServiceClient
15-
from google.ads.googleads.v19.services.types.google_ads_service import SearchGoogleAdsStreamResponse
16-
# Campaign, AdGroup, Customer, GoogleAdsRow types are not strictly needed for these tests
17-
# if we use the custom mock row/batch classes, but keeping them doesn't hurt.
18-
from google.ads.googleads.v19.resources.types.campaign import Campaign
19-
from google.ads.googleads.v19.resources.types.ad_group import AdGroup
20-
from google.ads.googleads.v19.resources.types.customer import Customer
21-
from google.ads.googleads.v19.services.types.google_ads_service import GoogleAdsRow
2227

2328

2429
# Helper classes for mocking Google Ads API CSV Row/Batch structures
@@ -332,7 +337,7 @@ def _simulate_script_main_block(
332337
# Script's GoogleAdsClient.load_from_storage call
333338
mock_client_instance = mock.Mock(name="GoogleAdsClientInstance")
334339
mock_gads_client_class_from_decorator.load_from_storage.return_value = mock_client_instance
335-
googleads_client = mock_gads_client_class_from_decorator.load_from_storage(version="v19")
340+
googleads_client = mock_gads_client_class_from_decorator.load_from_storage(version="v20")
336341

337342
# Script's main function call
338343
mock_main_func_from_decorator(
@@ -384,7 +389,7 @@ def test_argument_parsing(self, mock_script_main, mock_gads_client_class,
384389

385390
mock_parser_inst_for_assert.parse_args.assert_called_once_with()
386391

387-
mock_gads_client_class.load_from_storage.assert_called_once_with(version="v19")
392+
mock_gads_client_class.load_from_storage.assert_called_once_with(version="v20")
388393

389394
client_inst_for_assert = mock_gads_client_class.load_from_storage.return_value
390395
mock_script_main.assert_called_once_with(
@@ -425,15 +430,12 @@ def test_argument_parsing_no_headers_flag(self, mock_script_main, mock_gads_clie
425430
# For brevity, only checking parse_args and main call here, assuming add_argument calls are covered.
426431
mock_parser_inst_for_assert.parse_args.assert_called_once_with()
427432

428-
mock_gads_client_class.load_from_storage.assert_called_once_with(version="v19")
433+
mock_gads_client_class.load_from_storage.assert_called_once_with(version="v20")
429434

430435
client_inst_for_assert = mock_gads_client_class.load_from_storage.return_value
431436
mock_script_main.assert_called_once_with(
432437
client_inst_for_assert,
433438
expected_cust_id,
434439
expected_filepath,
435440
expected_headers_bool
436-
)
437-
438-
if __name__ == "__main__":
439-
unittest.main()
441+
)

examples/misc/tests/test_set_custom_client_timeouts.py renamed to tests/examples/misc/set_custom_client_timeouts_test.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
114
import unittest
215
from unittest import mock
316
import argparse
4-
import sys
517

618
# Assuming the script to be tested is in the parent directory.
719
# Adjust the import path as necessary.
820
from examples.misc import set_custom_client_timeouts
9-
from google.ads.googleads.errors import GoogleAdsException
1021
from google.api_core.exceptions import DeadlineExceeded
1122
from google.api_core.retry import Retry # Added import for Retry
1223
from .test_utils import create_mock_google_ads_exception
@@ -306,7 +317,4 @@ def test_argument_parsing_and_script_execution(
306317
mock_script_main_function.assert_called_once_with(
307318
client_instance_for_assert,
308319
expected_customer_id
309-
)
310-
311-
if __name__ == "__main__":
312-
unittest.main()
320+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
114
from unittest import mock
215
from google.ads.googleads.errors import GoogleAdsException
316

examples/misc/tests/test_upload_image_asset.py renamed to tests/examples/misc/upload_image_asset_test.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
114
import unittest
215
from unittest import mock
316
import argparse
4-
import sys
517

618
# Assuming the script to be tested is in the parent directory.
719
# Adjust the import path as necessary.
820
from examples.misc import upload_image_asset
921
from google.ads.googleads.errors import GoogleAdsException
1022
from .test_utils import create_mock_google_ads_exception
11-
from google.ads.googleads.v19.enums.types.asset_type import AssetTypeEnum
12-
from google.ads.googleads.v19.enums.types.mime_type import MimeTypeEnum
1323

1424

1525
class TestUploadImageAsset(unittest.TestCase):
@@ -181,7 +191,4 @@ def test_argument_parsing_and_script_execution(
181191
mock_script_main.assert_called_once_with(
182192
client_instance_for_assert,
183193
expected_cust_id
184-
)
185-
186-
if __name__ == "__main__":
187-
unittest.main()
194+
)

0 commit comments

Comments
 (0)