Skip to content

Commit 4500d7d

Browse files
google-labs-jules[bot]BenRKarl
authored andcommitted
Fix: Adjust patch scope for get_image_bytes_from_url in test_upload_image_asset.py
This commit refactors `examples/misc/tests/test_upload_image_asset.py` to address an `AssertionError: Expected 'get_image_bytes_from_url' to be called once. Called 0 times.` in the `test_main_success` method. The `@mock.patch` for `examples.misc.upload_image_asset.get_image_bytes_from_url` was moved from the `setUp` method directly onto the `test_main_success` method. This ensures that the patch is active in the most specific scope required, which can help resolve issues where the patch might not be effectively applied to the system under test due to import timing or other complex interactions when applied more broadly in `setUp`. The `test_main_success` method's signature and mock handling were updated accordingly.
1 parent 91f08d2 commit 4500d7d

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

examples/misc/tests/test_upload_image_asset.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
class 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

Comments
 (0)