|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +import argparse |
| 4 | +import sys |
| 5 | +from datetime import datetime |
| 6 | +import io |
| 7 | +import runpy |
| 8 | + |
| 9 | +from google.ads.googleads.errors import GoogleAdsException |
| 10 | + |
| 11 | +# Assuming create_customer.py is in examples.account_management |
| 12 | +from examples.account_management.create_customer import main |
| 13 | + |
| 14 | + |
| 15 | +class TestCreateCustomer(unittest.TestCase): |
| 16 | + @patch('examples.account_management.create_customer.datetime') |
| 17 | + @patch('examples.account_management.create_customer.GoogleAdsClient.load_from_storage') |
| 18 | + def test_main_success(self, mock_load_from_storage, mock_datetime): |
| 19 | + # Mock the GoogleAdsClient instance and its methods |
| 20 | + mock_google_ads_client = MagicMock() |
| 21 | + mock_load_from_storage.return_value = mock_google_ads_client # load_from_storage returns the client instance |
| 22 | + mock_customer_service = MagicMock() |
| 23 | + mock_google_ads_client.get_service.return_value = mock_customer_service |
| 24 | + mock_create_customer_response = MagicMock() |
| 25 | + mock_create_customer_response.resource_name = "customers/1234567890/conversionActions/test_client_customer_id" |
| 26 | + mock_customer_service.create_customer_client.return_value = mock_create_customer_response |
| 27 | + |
| 28 | + # Mock datetime |
| 29 | + mock_datetime.today.return_value.strftime.return_value = "2023-10-27" |
| 30 | + |
| 31 | + manager_customer_id = "test_manager_id" |
| 32 | + |
| 33 | + # Capture stdout |
| 34 | + captured_output = io.StringIO() |
| 35 | + sys.stdout = captured_output |
| 36 | + |
| 37 | + # Call main with the mocked client instance and manager_customer_id |
| 38 | + main(mock_google_ads_client, manager_customer_id) |
| 39 | + |
| 40 | + # Reset stdout |
| 41 | + sys.stdout = sys.__stdout__ |
| 42 | + |
| 43 | + mock_load_from_storage.assert_called_once() # Assert that load_from_storage was called to get the client |
| 44 | + mock_google_ads_client.get_service.assert_called_once_with("CustomerService") |
| 45 | + |
| 46 | + # Assert create_customer_client was called with the correct arguments |
| 47 | + call_args = mock_customer_service.create_customer_client.call_args |
| 48 | + self.assertIsNotNone(call_args) # Ensure it was called |
| 49 | + args, kwargs = call_args |
| 50 | + self.assertEqual(kwargs['customer_id'], manager_customer_id) |
| 51 | + |
| 52 | + customer_client_arg = args[0] |
| 53 | + self.assertEqual(customer_client_arg.descriptive_name, "Account created with CustomerService on 2023-10-27") |
| 54 | + self.assertEqual(customer_client_arg.currency_code, "USD") |
| 55 | + self.assertEqual(customer_client_arg.time_zone, "America/New_York") |
| 56 | + self.assertEqual(customer_client_arg.tracking_url_template, None) |
| 57 | + self.assertEqual(customer_client_arg.final_url_suffix, None) |
| 58 | + |
| 59 | + self.assertIn( |
| 60 | + "New customer account with resource name 'customers/1234567890/conversionActions/test_client_customer_id' under manager account 'test_manager_id' was created.", |
| 61 | + captured_output.getvalue() |
| 62 | + ) |
| 63 | + |
| 64 | + @patch('examples.account_management.create_customer.GoogleAdsClient.load_from_storage') |
| 65 | + @patch('sys.exit') |
| 66 | + def test_main_google_ads_exception(self, mock_sys_exit, mock_load_from_storage): |
| 67 | + # Mock the GoogleAdsClient instance and its methods |
| 68 | + mock_google_ads_client = MagicMock() |
| 69 | + mock_load_from_storage.return_value = mock_google_ads_client |
| 70 | + mock_customer_service = MagicMock() |
| 71 | + mock_google_ads_client.get_service.return_value = mock_customer_service |
| 72 | + |
| 73 | + # Configure create_customer_client to raise GoogleAdsException |
| 74 | + mock_error = MagicMock() |
| 75 | + mock_error.message = "Test GoogleAdsException message" |
| 76 | + mock_failure = MagicMock() |
| 77 | + mock_failure.errors = [mock_error] |
| 78 | + google_ads_exception = GoogleAdsException( |
| 79 | + error=None, # This should be the original gRPC error object if available |
| 80 | + call=None, # This should be the gRPC call object if available |
| 81 | + failure=mock_failure, |
| 82 | + error_code=None, # This could be a more specific error code if available |
| 83 | + message="Test GoogleAdsException" |
| 84 | + ) |
| 85 | + mock_customer_service.create_customer_client.side_effect = google_ads_exception |
| 86 | + |
| 87 | + manager_customer_id = "test_manager_id" |
| 88 | + |
| 89 | + # Capture stdout |
| 90 | + captured_output = io.StringIO() |
| 91 | + sys.stdout = captured_output |
| 92 | + |
| 93 | + # Call main with the mocked client instance and manager_customer_id |
| 94 | + main(mock_google_ads_client, manager_customer_id) |
| 95 | + |
| 96 | + # Reset stdout |
| 97 | + sys.stdout = sys.__stdout__ |
| 98 | + |
| 99 | + mock_load_from_storage.assert_called_once() |
| 100 | + mock_google_ads_client.get_service.assert_called_once_with("CustomerService") |
| 101 | + mock_customer_service.create_customer_client.assert_called_once() |
| 102 | + mock_sys_exit.assert_called_with(1) |
| 103 | + |
| 104 | + output = captured_output.getvalue() |
| 105 | + self.assertIn(f"Request with ID", output) |
| 106 | + self.assertIn(f"Test GoogleAdsException message", output) |
| 107 | + |
| 108 | + @patch('argparse.ArgumentParser.parse_args') |
| 109 | + @patch('examples.account_management.create_customer.main') |
| 110 | + @patch('examples.account_management.create_customer.GoogleAdsClient.load_from_storage') # Also mock client loading for the script's main execution |
| 111 | + def test_argument_parser(self, mock_load_from_storage, mock_main_function, mock_parse_args): |
| 112 | + # Simulate command line arguments |
| 113 | + test_manager_id = "12345" |
| 114 | + sys.argv = ["create_customer.py", "-m", test_manager_id] |
| 115 | + |
| 116 | + # Mock parse_args to return the manager_customer_id |
| 117 | + mock_parse_args.return_value = argparse.Namespace(manager_customer_id=test_manager_id) |
| 118 | + |
| 119 | + # Mock the client returned by load_from_storage |
| 120 | + mock_google_ads_client_instance = MagicMock() |
| 121 | + mock_load_from_storage.return_value = mock_google_ads_client_instance |
| 122 | + |
| 123 | + # Execute the script's main block using runpy |
| 124 | + # This will trigger the argument parsing and the call to main() within the script |
| 125 | + runpy.run_module("examples.account_management.create_customer", run_name="__main__") |
| 126 | + |
| 127 | + mock_parse_args.assert_called_once() |
| 128 | + # The main function in create_customer.py is called with the client and manager_id |
| 129 | + mock_main_function.assert_called_once_with(mock_google_ads_client_instance, test_manager_id) |
0 commit comments