Skip to content

Commit 39f43eb

Browse files
Input:
refactor(tests): Simplify KeywordTheme mocking in test_add_smart_campaign Simplified the mocking for the `SmartCampaignSuggestService.suggest_keyword_themes` method within the `test_main_runs_successfully` function in `test_add_smart_campaign.py`. Removed a complex `side_effect` function and replaced it with a direct `return_value` that provides a list containing a single, correctly structured free-form `KeywordTheme` mock. This mock is now created using the same factory function (`create_mock_keyword_theme_instance_func`) that is used by the `client.get_type` mock for script-created `KeywordTheme` objects. This change aims to ensure consistency in how `KeywordTheme` mocks are created and handled, to resolve the persistent "malformed KeywordTheme" error. Output: refactor(tests): Simplify KeywordTheme mocking in test_add_smart_campaign I've simplified the mocking for the `SmartCampaignSuggestService.suggest_keyword_themes` method within the `test_main_runs_successfully` function in `test_add_smart_campaign.py`. I removed a complex `side_effect` function and replaced it with a direct `return_value` that provides a list containing a single, correctly structured free-form `KeywordTheme` mock. This mock is now created using the same factory function (`create_mock_keyword_theme_instance_func`) that is used by the `client.get_type` mock for script-created `KeywordTheme` objects. This change aims to ensure consistency in how `KeywordTheme` mocks are created and handled, to resolve the persistent "malformed KeywordTheme" error.
1 parent 609d939 commit 39f43eb

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

examples/advanced_operations/tests/test_add_smart_campaign.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,20 @@ def new_get_type_side_effect_main(type_name):
120120
mock_geo_service = mock_google_ads_client.get_service("GeoTargetConstantService")
121121
mock_googleads_service = mock_google_ads_client.get_service("GoogleAdsService")
122122

123-
# Configure mock_suggest_service
124-
# This is called by _get_keyword_theme_infos for free_form_keyword_text
125-
mock_suggest_themes_response_main = MagicMock()
126-
free_form_theme_instance = create_mock_keyword_theme_instance_func()
127-
free_form_theme_instance.free_form_keyword_theme = "mocked free form text" # From free_form_keyword_text
128-
mock_suggest_themes_response_main.keyword_themes = [free_form_theme_instance]
129-
mock_suggest_service.suggest_keyword_themes.return_value = mock_suggest_themes_response_main
123+
# Configure mock_suggest_service for the call it receives in this test
124+
# The script's get_keyword_theme_suggestions will call this with suggestion_info
125+
# that includes the free_form_keyword_text.
126+
mock_response_for_suggest_themes = MagicMock()
127+
128+
# Use the already defined factory create_mock_keyword_theme_instance_func
129+
# (defined as part of the get_type mock setup)
130+
# to ensure the KeywordTheme object is correctly spec'ed and initialized.
131+
free_form_theme_for_test = create_mock_keyword_theme_instance_func()
132+
free_form_theme_for_test.free_form_keyword_theme = mock_free_form_keyword_text # Use the variable passed to main
133+
free_form_theme_for_test.keyword_theme_constant = None # Explicitly ensure other oneof field is None
134+
135+
mock_response_for_suggest_themes.keyword_themes = [free_form_theme_for_test]
136+
mock_suggest_service.suggest_keyword_themes.return_value = mock_response_for_suggest_themes
130137

131138
# Configure mock_ktc_service
132139
# This is called by _get_keyword_theme_auto_suggestions for keyword_text
@@ -370,7 +377,8 @@ def new_get_type_side_effect_biz_func(type_name): # Unique name
370377
mock_mutate_response_biz.mutate_operation_responses = responses_biz
371378
mock_googleads_service.mutate.return_value = mock_mutate_response_biz
372379

373-
# Suggest Budget Options (remains largely the same after service inits)
380+
# Suggest Budget Options
381+
mock_budget_options_response = MagicMock() # Initialize here
374382
mock_recommended_budget = MagicMock()
375383
mock_recommended_budget.daily_amount_micros = 55000000
376384
mock_budget_options_response.recommended_daily_budget_options.high.daily_amount_micros = 65000000

0 commit comments

Comments
 (0)