|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, MagicMock, call |
| 3 | + |
| 4 | +from examples.basic_operations import get_responsive_search_ads |
| 5 | + |
| 6 | +class TestGetResponsiveSearchAds(unittest.TestCase): |
| 7 | + |
| 8 | + @patch("examples.basic_operations.get_responsive_search_ads.argparse.ArgumentParser") |
| 9 | + @patch("examples.basic_operations.get_responsive_search_ads.GoogleAdsClient.load_from_storage") |
| 10 | + def test_main(self, mock_load_from_storage, mock_argument_parser): |
| 11 | + # Mock the GoogleAdsClient |
| 12 | + mock_google_ads_client = MagicMock() |
| 13 | + mock_load_from_storage.return_value = mock_google_ads_client |
| 14 | + |
| 15 | + # Mock the GoogleAdsService |
| 16 | + mock_ga_service = MagicMock() |
| 17 | + mock_google_ads_client.get_service.return_value = mock_ga_service |
| 18 | + |
| 19 | + # Mock command line arguments |
| 20 | + mock_args = MagicMock() |
| 21 | + mock_args.customer_id = "1234567890" |
| 22 | + mock_args.ad_group_id = "ADGROUPID_01" # Optional, so test with it present |
| 23 | + mock_argument_parser.return_value.parse_args.return_value = mock_args |
| 24 | + |
| 25 | + # Mock the response from search |
| 26 | + # This is a row from the SearchGoogleAdsResponse, not AdGroupAd directly |
| 27 | + mock_row1 = MagicMock() |
| 28 | + mock_ad_group_ad = mock_row1.ad_group_ad # This is the AdGroupAd object |
| 29 | + |
| 30 | + mock_ad_group_ad.ad.resource_name = "customers/1234567890/ads/AD_ID_1" |
| 31 | + mock_ad_group_ad.status.name = "ENABLED" # Example status |
| 32 | + |
| 33 | + # RSA has headlines and descriptions (AdTextAsset) |
| 34 | + mock_headline1 = MagicMock() |
| 35 | + mock_headline1.text = "Cruise to Mars" |
| 36 | + mock_headline1.pinned_field.name = "HEADLINE_1" # Script uses asset.pinned_field.name |
| 37 | + |
| 38 | + mock_description1 = MagicMock() |
| 39 | + mock_description1.text = "Visit the Red Planet in style." |
| 40 | + mock_description1.pinned_field.name = "DESCRIPTION_1" |
| 41 | + |
| 42 | + mock_ad_group_ad.ad.responsive_search_ad.headlines = [mock_headline1] |
| 43 | + mock_ad_group_ad.ad.responsive_search_ad.descriptions = [mock_description1] |
| 44 | + # The script doesn't print final_urls for RSA, it prints headlines and descriptions |
| 45 | + |
| 46 | + # search returns an iterable of these rows |
| 47 | + mock_ga_service.search.return_value = [mock_row1] |
| 48 | + |
| 49 | + # Enums are compared by their .name attribute by the script, so no need to mock them as strings here |
| 50 | + # if the attributes being accessed on them (like .name) are already strings or MagicMocks. |
| 51 | + # The script uses ad.responsive_search_ad.headlines which are AdTextAssets. |
| 52 | + # ad_text_assets_to_strs accesses asset.text and asset.pinned_field.name. |
| 53 | + |
| 54 | + |
| 55 | + # Call the main function of the example script |
| 56 | + with patch("builtins.print") as mock_print: |
| 57 | + get_responsive_search_ads.main(mock_google_ads_client, mock_args.customer_id, mock_args.ad_group_id) |
| 58 | + |
| 59 | + # Assertions |
| 60 | + mock_google_ads_client.get_service.assert_called_once_with("GoogleAdsService") |
| 61 | + |
| 62 | + # Check that search was called |
| 63 | + mock_ga_service.search.assert_called_once() |
| 64 | + call_args = mock_ga_service.search.call_args |
| 65 | + |
| 66 | + # The first argument to search is the request object |
| 67 | + request_arg = call_args[1]['request'] # request is a keyword argument |
| 68 | + self.assertEqual(request_arg.customer_id, mock_args.customer_id) |
| 69 | + # Verify query construction based on ad_group_id presence |
| 70 | + self.assertIn(f"AND ad_group.id = {mock_args.ad_group_id}", request_arg.query) |
| 71 | + |
| 72 | + |
| 73 | + # Verify print output - this will be complex due to loops and formatting |
| 74 | + # We'll check for key pieces of information. |
| 75 | + # Note: The exact formatting of pinned fields might require more specific mocking if it's complex. |
| 76 | + # This example assumes simple text output for headlines and descriptions. |
| 77 | + |
| 78 | + # Expected calls based on the mocked ad structure |
| 79 | + # The script's print format is: |
| 80 | + # print(f"Responsive search ad with resource name \"{ad.resource_name}\", status {row.ad_group_ad.status.name} was found.") |
| 81 | + # print(f"Headlines:\n{headlines_str}\nDescriptions:\n{descriptions_str}\n") |
| 82 | + |
| 83 | + expected_ad_info_print = call( |
| 84 | + f"Responsive search ad with resource name \"{mock_ad_group_ad.ad.resource_name}\", status {mock_ad_group_ad.status.name} was found." |
| 85 | + ) |
| 86 | + |
| 87 | + # Output from ad_text_assets_to_strs |
| 88 | + expected_headline_str = f"\t {mock_headline1.text} pinned to {mock_headline1.pinned_field.name}" |
| 89 | + expected_description_str = f"\t {mock_description1.text} pinned to {mock_description1.pinned_field.name}" |
| 90 | + |
| 91 | + expected_details_print = call(f"Headlines:\n{expected_headline_str}\nDescriptions:\n{expected_description_str}\n") |
| 92 | + |
| 93 | + expected_calls = [expected_ad_info_print, expected_details_print] |
| 94 | + |
| 95 | + mock_print.assert_has_calls(expected_calls, any_order=False) |
| 96 | + |
| 97 | + |
| 98 | + @patch("examples.basic_operations.get_responsive_search_ads.argparse.ArgumentParser") |
| 99 | + @patch("examples.basic_operations.get_responsive_search_ads.GoogleAdsClient.load_from_storage") |
| 100 | + def test_main_no_ad_group_id(self, mock_load_from_storage, mock_argument_parser): |
| 101 | + # Test the case where ad_group_id is None |
| 102 | + mock_google_ads_client = MagicMock() |
| 103 | + mock_load_from_storage.return_value = mock_google_ads_client |
| 104 | + mock_ga_service = MagicMock() |
| 105 | + mock_google_ads_client.get_service.return_value = mock_ga_service |
| 106 | + |
| 107 | + mock_args = MagicMock() |
| 108 | + mock_args.customer_id = "1234567890" |
| 109 | + mock_args.ad_group_id = None # Test this path |
| 110 | + mock_argument_parser.return_value.parse_args.return_value = mock_args |
| 111 | + |
| 112 | + mock_ga_service.search.return_value = [] # No results needed for this query check path |
| 113 | + |
| 114 | + with patch("builtins.print"): # Suppress print for this test |
| 115 | + get_responsive_search_ads.main(mock_google_ads_client, mock_args.customer_id, mock_args.ad_group_id) |
| 116 | + |
| 117 | + mock_ga_service.search.assert_called_once() |
| 118 | + request_arg = mock_ga_service.search.call_args[1]['request'] |
| 119 | + self.assertNotIn("AND ad_group.id", request_arg.query) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + unittest.main() |
0 commit comments