Skip to content

Commit cdcfa5a

Browse files
Fix: Correct enum assertions in test_upload_image_asset.py
This commit resolves an AttributeError in the `test_main_success` method of `examples/misc/tests/test_upload_image_asset.py`. The error was `AttributeError: type object 'AssetTypeEnum' has no attribute 'IMAGE'`. The issue occurred because the test was asserting an enum value against the actual `AssetTypeEnum` class (e.g., `AssetTypeEnum.IMAGE`), whereas the script `upload_image_asset.py` sets these values using the client's enum instances (e.g., `client.enums.AssetTypeEnum.IMAGE`). The fix involves: 1. Updating the `setUp` method to ensure that `client.enums.AssetTypeEnum.IMAGE` and `client.enums.MimeTypeEnum.IMAGE_JPEG` are explicitly mocked to return specific, predictable string values. 2. Modifying the assertions in `test_main_success` to compare the operation's `type_` and `mime_type` attributes against these mocked string values obtained from `self.mock_client.enums.AssetTypeEnum.IMAGE` (and similarly for MimeTypeEnum), rather than the imported enum classes.
1 parent f946ebc commit cdcfa5a

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

examples/misc/tests/test_upload_image_asset.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ def setUp(self, mock_google_ads_client_class): # mock_get_image_bytes_func remov
2323
self.mock_asset_operation = self.mock_client.get_type("AssetOperation")
2424

2525
# Mock enums properly
26-
# Instead of replacing the whole enum type on mock_client.enums,
27-
# we let the script access the real enums (AssetTypeEnum, MimeTypeEnum are imported directly)
28-
# The test will then assert that the correct real enum values are used.
29-
# This also means the direct imports of AssetTypeEnum and MimeTypeEnum at the top are important.
26+
self.mock_asset_type_enum_instance = mock.Mock(name="AssetTypeEnumInstance")
27+
self.mock_asset_type_enum_instance.IMAGE = "mocked_asset_type_image_enum"
28+
self.mock_client.enums.AssetTypeEnum = self.mock_asset_type_enum_instance
29+
30+
self.mock_mime_type_enum_instance = mock.Mock(name="MimeTypeEnumInstance")
31+
self.mock_mime_type_enum_instance.IMAGE_JPEG = "mocked_mime_type_image_jpeg_enum"
32+
self.mock_client.enums.MimeTypeEnum = self.mock_mime_type_enum_instance
3033

3134
# 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
35+
self.mock_image_bytes = b"test_image_data"
3336
# For now, keeping self.mock_image_bytes here, can be localized if only test_main_success uses it.
3437

3538
# Mock the mutate_assets response
@@ -66,10 +69,10 @@ def test_main_success(self, mock_get_image_bytes_in_success, mock_print_in_succe
6669
actual_operation = call_args[1]["operations"][0]
6770

6871
# Assertions on the asset operation
69-
self.assertEqual(actual_operation.create.type_, AssetTypeEnum.IMAGE) # Uses imported AssetTypeEnum
72+
self.assertEqual(actual_operation.create.type_, self.mock_client.enums.AssetTypeEnum.IMAGE)
7073
self.assertEqual(actual_operation.create.image_asset.data, self.mock_image_bytes)
7174
self.assertEqual(actual_operation.create.image_asset.file_size, len(self.mock_image_bytes))
72-
self.assertEqual(actual_operation.create.image_asset.mime_type, MimeTypeEnum.IMAGE_JPEG) # Uses imported MimeTypeEnum
75+
self.assertEqual(actual_operation.create.image_asset.mime_type, self.mock_client.enums.MimeTypeEnum.IMAGE_JPEG)
7376
self.assertEqual(actual_operation.create.name, "Marketing Image")
7477
self.assertEqual(actual_operation.create.image_asset.full_size.url, "https://gaagl.page.link/Eit5")
7578

0 commit comments

Comments
 (0)