|
| 1 | +import unittest |
| 2 | +import sys |
| 3 | +from unittest import mock |
| 4 | +from io import StringIO |
| 5 | + |
| 6 | +# Assuming 'examples' is in PYTHONPATH |
| 7 | +import add_account_budget_proposal |
| 8 | + |
| 9 | +# Use a real or placeholder GoogleAdsException |
| 10 | +try: |
| 11 | + from google.ads.googleads.client import GoogleAdsClient |
| 12 | + from google.ads.googleads.errors import GoogleAdsException |
| 13 | +except ImportError: |
| 14 | + # Define a placeholder GoogleAdsException if the real one is not available |
| 15 | + class GoogleAdsException(Exception): |
| 16 | + def __init__(self, *args, **kwargs): |
| 17 | + super().__init__(*args) |
| 18 | + self.request_id = kwargs.get("request_id", "test_request_id") |
| 19 | + self.error = mock.Mock() |
| 20 | + self.error.code.return_value.name = "TEST_ERROR" |
| 21 | + self.failure = mock.Mock() |
| 22 | + self.failure.errors = [mock.Mock(message="Test error message.")] |
| 23 | + |
| 24 | + |
| 25 | +class TestAddAccountBudgetProposal(unittest.TestCase): |
| 26 | + MOCK_CUSTOMER_ID = "1234567890" |
| 27 | + MOCK_BILLING_SETUP_ID = "111222333" |
| 28 | + # The script path relative to the execution directory of the tests. |
| 29 | + # This might need adjustment if tests are run from a different location. |
| 30 | + SCRIPT_PATH = "examples.billing.add_account_budget_proposal" |
| 31 | + |
| 32 | + @mock.patch(f"{SCRIPT_PATH}.GoogleAdsClient") |
| 33 | + def test_main_success(self, mock_google_ads_client_constructor): |
| 34 | + # Configure the mock client and its services |
| 35 | + mock_client_instance = mock.Mock() |
| 36 | + # This mock_client_instance will be returned by GoogleAdsClient.load_from_storage() |
| 37 | + # when main() in the script is called if we were testing the __main__ block. |
| 38 | + # However, for this test, we are calling main() directly. |
| 39 | + # mock_google_ads_client_constructor.load_from_storage.return_value = mock_client_instance |
| 40 | + |
| 41 | + mock_account_budget_proposal_service = mock.Mock() |
| 42 | + mock_billing_setup_service = mock.Mock() |
| 43 | + |
| 44 | + mock_client_instance.get_service.side_effect = [ |
| 45 | + mock_account_budget_proposal_service, |
| 46 | + mock_billing_setup_service, |
| 47 | + ] |
| 48 | + |
| 49 | + mock_operation = mock.Mock() |
| 50 | + mock_client_instance.get_type.return_value = mock_operation |
| 51 | + |
| 52 | + # Mock the proposal object that is part of the operation |
| 53 | + mock_proposal = mock_operation.create |
| 54 | + |
| 55 | + # Mock the response from mutate_account_budget_proposal |
| 56 | + mock_mutate_response = mock.Mock() |
| 57 | + mock_mutate_response.result.resource_name = "test_resource_name" |
| 58 | + mock_account_budget_proposal_service.mutate_account_budget_proposal.return_value = mock_mutate_response |
| 59 | + |
| 60 | + # Capture stdout |
| 61 | + captured_output = StringIO() |
| 62 | + sys.stdout = captured_output |
| 63 | + |
| 64 | + # Call the main function |
| 65 | + # We pass the mock_client_instance directly to main for this unit test |
| 66 | + add_account_budget_proposal.main( |
| 67 | + mock_client_instance, |
| 68 | + self.MOCK_CUSTOMER_ID, |
| 69 | + self.MOCK_BILLING_SETUP_ID, |
| 70 | + ) |
| 71 | + |
| 72 | + # Restore stdout |
| 73 | + sys.stdout = sys.__stdout__ |
| 74 | + |
| 75 | + # Assertions |
| 76 | + mock_client_instance.get_service.assert_any_call("AccountBudgetProposalService") |
| 77 | + mock_client_instance.get_service.assert_any_call("BillingSetupService") |
| 78 | + |
| 79 | + mock_account_budget_proposal_service.mutate_account_budget_proposal.assert_called_once() |
| 80 | + |
| 81 | + # Check the arguments of mutate_account_budget_proposal |
| 82 | + call_args = mock_account_budget_proposal_service.mutate_account_budget_proposal.call_args |
| 83 | + self.assertEqual(call_args[1]["customer_id"], self.MOCK_CUSTOMER_ID) |
| 84 | + self.assertEqual(call_args[1]["operation"], mock_operation) |
| 85 | + |
| 86 | + self.assertEqual(mock_proposal.proposal_type, mock_client_instance.enums.AccountBudgetProposalTypeEnum.CREATE) |
| 87 | + |
| 88 | + expected_billing_setup_path = mock_billing_setup_service.billing_setup_path.return_value |
| 89 | + mock_billing_setup_service.billing_setup_path.assert_called_once_with( |
| 90 | + self.MOCK_CUSTOMER_ID, self.MOCK_BILLING_SETUP_ID |
| 91 | + ) |
| 92 | + self.assertEqual(mock_proposal.billing_setup, expected_billing_setup_path) |
| 93 | + |
| 94 | + self.assertEqual(mock_proposal.proposed_name, "Account Budget Proposal (example)") |
| 95 | + self.assertEqual(mock_proposal.proposed_start_time_type, mock_client_instance.enums.TimeTypeEnum.NOW) |
| 96 | + self.assertEqual(mock_proposal.proposed_end_time_type, mock_client_instance.enums.TimeTypeEnum.FOREVER) |
| 97 | + self.assertEqual(mock_proposal.proposed_spending_limit_micros, 10000) |
| 98 | + |
| 99 | + self.assertIn("Created account budget proposal", captured_output.getvalue()) |
| 100 | + self.assertIn("test_resource_name", captured_output.getvalue()) |
| 101 | + |
| 102 | + @mock.patch(f"{SCRIPT_PATH}.GoogleAdsClient") |
| 103 | + @mock.patch(f"{SCRIPT_PATH}.argparse.ArgumentParser") |
| 104 | + @mock.patch("sys.exit") # To check if sys.exit(1) is called |
| 105 | + def test_main_google_ads_exception( |
| 106 | + self, mock_sys_exit, mock_argparse, mock_google_ads_client_constructor |
| 107 | + ): |
| 108 | + # Configure mock_argparse to return mock arguments |
| 109 | + mock_args = mock.Mock() |
| 110 | + mock_args.customer_id = self.MOCK_CUSTOMER_ID |
| 111 | + mock_args.billing_setup_id = self.MOCK_BILLING_SETUP_ID |
| 112 | + mock_argparse.return_value.parse_args.return_value = mock_args |
| 113 | + |
| 114 | + # Configure the mock client and its services |
| 115 | + mock_client_instance = mock.Mock() |
| 116 | + # Ensure load_from_storage is set up on the constructor mock |
| 117 | + # to be called in the script's __main__ block |
| 118 | + mock_google_ads_client_constructor.load_from_storage.return_value = mock_client_instance |
| 119 | + |
| 120 | + mock_account_budget_proposal_service = mock.Mock() |
| 121 | + # Simulate get_service for AccountBudgetProposalService if main is called before exception |
| 122 | + # If the exception happens during client.get_service itself, this needs adjustment |
| 123 | + mock_client_instance.get_service.return_value = mock_account_budget_proposal_service |
| 124 | + |
| 125 | + # Setup the GoogleAdsException |
| 126 | + error_message_detail = "Test GoogleAdsException message." |
| 127 | + ads_exception = GoogleAdsException( |
| 128 | + request_id="test_request_id_123", |
| 129 | + error=mock.Mock(code=lambda: mock.Mock(name="SPECIFIC_ERROR_CODE")), |
| 130 | + failure=mock.Mock(errors=[mock.Mock(message=error_message_detail)]) |
| 131 | + ) |
| 132 | + # Configure the main function (or a deeper call like mutate) to raise the exception |
| 133 | + # The script calls main(googleads_client, args.customer_id, args.billing_setup_id) |
| 134 | + # and main calls mutate_account_budget_proposal |
| 135 | + # So, we make mutate_account_budget_proposal raise the exception. |
| 136 | + mock_account_budget_proposal_service.mutate_account_budget_proposal.side_effect = ads_exception |
| 137 | + |
| 138 | + # Capture stderr |
| 139 | + captured_error = StringIO() |
| 140 | + sys.stderr = captured_error |
| 141 | + |
| 142 | + # Execute the __main__ block of the script. |
| 143 | + # This requires importing the script and running its __main__ part. |
| 144 | + # We can achieve this by using runpy or by carefully triggering the |
| 145 | + # relevant parts of the script's __main__ block. |
| 146 | + # Since GoogleAdsClient.load_from_storage is mocked, and args are mocked, |
| 147 | + # we can effectively simulate the script's execution flow leading to the try-except block. |
| 148 | + |
| 149 | + # Re-import the script to simulate execution from top (or use runpy) |
| 150 | + # For simplicity, we'll call the relevant part of the script's __main__ logic. |
| 151 | + # The script's __main__ block: |
| 152 | + # 1. Parses args (mocked) |
| 153 | + # 2. Loads client (mocked to return mock_client_instance) |
| 154 | + # 3. Calls main() |
| 155 | + # 4. Catches GoogleAdsException |
| 156 | + |
| 157 | + # Simulate the try-except block in __main__ |
| 158 | + # This involves conceptually running the script's __main__ path. |
| 159 | + # The script would: |
| 160 | + # 1. Call GoogleAdsClient.load_from_storage(version="v19") -> returns mock_client_instance |
| 161 | + # 2. Call main(mock_client_instance, ...) |
| 162 | + # We are mocking load_from_storage and then directly calling main with the configured client |
| 163 | + # to simulate the script's behavior leading to the exception. |
| 164 | + |
| 165 | + # To properly test the __main__ block, we'd ideally run the script and have mocks in place. |
| 166 | + # The current setup calls main directly. Let's adjust to simulate the __main__ path more closely |
| 167 | + # for the load_from_storage assertion. |
| 168 | + |
| 169 | + # The script's __name__ == "__main__": block effectively does: |
| 170 | + # googleads_client = GoogleAdsClient.load_from_storage(version="v19") |
| 171 | + # main(googleads_client, args.customer_id, args.billing_setup_id) |
| 172 | + # So, we call load_from_storage, then pass its result to main. |
| 173 | + |
| 174 | + # Actual call to load_from_storage as it would happen in the script's __main__ |
| 175 | + # This will use the mock_google_ads_client_constructor |
| 176 | + loaded_client = add_account_budget_proposal.GoogleAdsClient.load_from_storage(version="v19") |
| 177 | + |
| 178 | + try: |
| 179 | + # Pass the `loaded_client` which is `mock_client_instance` |
| 180 | + add_account_budget_proposal.main( |
| 181 | + loaded_client, mock_args.customer_id, mock_args.billing_setup_id |
| 182 | + ) |
| 183 | + except GoogleAdsException as ex: |
| 184 | + # This is the handling from the script's __main__ |
| 185 | + print( |
| 186 | + f'Request with ID "{ex.request_id}" failed with status ' |
| 187 | + f'"{ex.error.code().name}" and includes the following errors:', |
| 188 | + file=sys.stderr, |
| 189 | + ) |
| 190 | + for error in ex.failure.errors: |
| 191 | + print(f'\tError with message "{error.message}".', file=sys.stderr) |
| 192 | + if error.location: |
| 193 | + for field_path_element in error.location.field_path_elements: |
| 194 | + print( |
| 195 | + f"\t\tOn field: {field_path_element.field_name}", |
| 196 | + file=sys.stderr, |
| 197 | + ) |
| 198 | + mock_sys_exit(1) # Simulate sys.exit(1) call from the script |
| 199 | + |
| 200 | + # Restore stderr |
| 201 | + sys.stderr = sys.__stderr__ |
| 202 | + |
| 203 | + # Assertions |
| 204 | + mock_google_ads_client_constructor.load_from_storage.assert_called_once_with(version="v19") |
| 205 | + mock_sys_exit.assert_called_once_with(1) |
| 206 | + |
| 207 | + error_output = captured_error.getvalue() |
| 208 | + self.assertIn(f'Request with ID "{ads_exception.request_id}" failed', error_output) |
| 209 | + self.assertIn(f'status "{ads_exception.error.code().name}"', error_output) |
| 210 | + self.assertIn(f'Error with message "{error_message_detail}"', error_output) |
| 211 | + |
| 212 | +if __name__ == "__main__": |
| 213 | + # If examples.billing is a package, and tests is a sub-package, |
| 214 | + # this might need to be run as `python -m examples.billing.tests.test_add_account_budget_proposal` |
| 215 | + # with proper __init__.py files in examples and examples/billing. |
| 216 | + # For now, assuming direct execution or PYTHONPATH setup. |
| 217 | + unittest.main() |
0 commit comments