1515class TestUploadImageAsset (unittest .TestCase ):
1616 """Tests for the upload_image_asset script."""
1717
18- @mock .patch ("examples.misc.upload_image_asset.get_image_bytes_from_url" )
1918 @mock .patch ("examples.misc.upload_image_asset.GoogleAdsClient" )
20- def setUp (self , mock_google_ads_client_class , mock_get_image_bytes_func ): # Corrected order and name
19+ def setUp (self , mock_google_ads_client_class ): # mock_get_image_bytes_func removed
2120 # Mock GoogleAdsClient and its methods
2221 self .mock_client = mock_google_ads_client_class .load_from_storage .return_value
2322 self .mock_asset_service = self .mock_client .get_service ("AssetService" )
@@ -29,10 +28,9 @@ def setUp(self, mock_google_ads_client_class, mock_get_image_bytes_func): # Corr
2928 # The test will then assert that the correct real enum values are used.
3029 # This also means the direct imports of AssetTypeEnum and MimeTypeEnum at the top are important.
3130
32- # Mock get_image_bytes_from_url
33- self .mock_get_image_bytes_from_url = mock_get_image_bytes_func # Corrected name
34- self .mock_image_bytes = b"test_image_data" # Changed from mock_image_data for clarity
35- self .mock_get_image_bytes_from_url .return_value = self .mock_image_bytes
31+ # self.mock_get_image_bytes_from_url removed
32+ self .mock_image_bytes = b"test_image_data" # This might be needed if other tests use it, or move to test_main_success
33+ # For now, keeping self.mock_image_bytes here, can be localized if only test_main_success uses it.
3634
3735 # Mock the mutate_assets response
3836 self .mock_mutate_response = mock .Mock ()
@@ -44,15 +42,18 @@ def setUp(self, mock_google_ads_client_class, mock_get_image_bytes_func): # Corr
4442 self .mock_asset_create = self .mock_asset_operation .return_value .create
4543 self .mock_asset_create .image_asset = mock .Mock ()
4644
47-
45+ @ mock . patch ( "examples.misc.upload_image_asset.get_image_bytes_from_url" )
4846 @mock .patch ("builtins.print" )
49- def test_main_success (self , mock_print ):
47+ def test_main_success (self , mock_get_image_bytes_in_success , mock_print_in_success ): # Signature updated
5048 """Tests a successful run of the main function."""
5149 customer_id = "1234567890"
5250
51+ # Configure the mock for get_image_bytes_from_url specifically for this test
52+ mock_get_image_bytes_in_success .return_value = self .mock_image_bytes # Using self.mock_image_bytes from setUp
53+
5354 upload_image_asset .main (self .mock_client , customer_id )
5455
55- self . mock_get_image_bytes_from_url . assert_called_once_with (
56+ mock_get_image_bytes_in_success . assert_called_once_with ( # Use the local mock from argument
5657 "https://gaagl.page.link/Eit5"
5758 )
5859 self .mock_asset_service .mutate_assets .assert_called_once ()
@@ -72,13 +73,13 @@ def test_main_success(self, mock_print):
7273 self .assertEqual (actual_operation .create .name , "Marketing Image" )
7374 self .assertEqual (actual_operation .create .image_asset .full_size .url , "https://gaagl.page.link/Eit5" )
7475
75- mock_print .assert_any_call (
76+ mock_print_in_success .assert_any_call ( # Use the local mock from argument
7677 f"Uploaded image asset with resource name: '{ self .mock_mutate_response .results [0 ].resource_name } '"
7778 )
7879
7980 @mock .patch ("sys.exit" )
80- @mock .patch ("builtins.print" ) # To check error messages if needed
81- def test_main_google_ads_exception (self , mock_print , mock_sys_exit ):
81+ @mock .patch ("builtins.print" )
82+ def test_main_google_ads_exception (self , mock_print_for_exception , mock_sys_exit_for_exception ): # Renamed for clarity
8283 """Tests handling of GoogleAdsException."""
8384 customer_id = "1234567890"
8485
@@ -96,10 +97,10 @@ def test_main_google_ads_exception(self, mock_print, mock_sys_exit):
9697 # The script's main() function itself doesn't call sys.exit, but the
9798 # if __name__ == "__main__" block does. This test is for main().
9899 # So, sys.exit should not be called by main().
99- mock_sys_exit .assert_not_called ()
100+ mock_sys_exit_for_exception .assert_not_called () # Use renamed mock
100101 # Optionally, check if specific error messages were printed
101102 # This depends on the exact error printing logic in the script
102- # For example: mock_print .assert_any_call(...)
103+ # For example: mock_print_for_exception .assert_any_call(...)
103104
104105
105106 def _simulate_script_main_block (
0 commit comments