Skip to content

Commit 001e6ae

Browse files
authored
Fix config so that login_customer_id=None doesn't raise an error. (#1064)
1 parent 3cdf8f9 commit 001e6ae

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

google/ads/googleads/config.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,16 @@ def validate_dict(config_data: dict[str, Any]) -> None:
214214
"found. The required fields are: {}".format(str(_REQUIRED_KEYS))
215215
)
216216

217-
if "login_customer_id" in config_data:
217+
if (
218+
"login_customer_id" in config_data
219+
and config_data["login_customer_id"] is not None
220+
):
218221
validate_login_customer_id(str(config_data["login_customer_id"]))
219222

220-
if "linked_customer_id" in config_data:
223+
if (
224+
"linked_customer_id" in config_data
225+
and config_data["linked_customer_id"] is not None
226+
):
221227
validate_linked_customer_id(str(config_data["linked_customer_id"]))
222228

223229

tests/client_test.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,50 @@ def test_load_from_dict_versioned(self):
439439
ads_assistant=None,
440440
)
441441

442+
def test_load_from_dict_login_customer_id_explicit_none(self):
443+
config = {
444+
**self.default_config,
445+
**{
446+
"client_id": self.client_id,
447+
"client_secret": self.client_secret,
448+
"refresh_token": self.refresh_token,
449+
"login_customer_id": None,
450+
},
451+
}
452+
mock_credentials_instance = mock.Mock()
453+
454+
with (
455+
mock.patch.object(
456+
Client.GoogleAdsClient, "__init__", return_value=None
457+
) as mock_client_init,
458+
mock.patch.object(
459+
Client.oauth2,
460+
"get_installed_app_credentials",
461+
return_value=mock_credentials_instance,
462+
) as mock_credentials,
463+
):
464+
try:
465+
Client.GoogleAdsClient.load_from_dict(config)
466+
except ValueError as ex:
467+
self.fail(
468+
"GoogleAdsClient.load_from_dict raised ValueError "
469+
f"unexpectedly when login_customer_id is None: {ex}"
470+
)
471+
472+
mock_client_init.assert_called_once_with(
473+
credentials=mock_credentials_instance,
474+
developer_token=self.developer_token,
475+
use_proto_plus=self.use_proto_plus,
476+
endpoint=None,
477+
login_customer_id=None,
478+
logging_config=None,
479+
linked_customer_id=None,
480+
version=None,
481+
http_proxy=None,
482+
use_cloud_org_for_api_access=None,
483+
ads_assistant=None,
484+
)
485+
442486
def test_load_from_string(self):
443487
config = {
444488
**self.default_config,

tests/config_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,17 @@ def test_load_from_dict(self):
289289
}
290290
self.assertEqual(config.load_from_dict(config_data), config_data)
291291

292+
def test_load_from_dict_login_customer_id_explicit_none(self):
293+
"""Should not raise ValueError when login_customer_id is explicitly None."""
294+
config_data = {
295+
**self.default_dict_config,
296+
"login_customer_id": None,
297+
}
298+
try:
299+
config.load_from_dict(config_data)
300+
except ValueError as ex:
301+
self.fail(f"load_from_dict raised ValueError unexpectedly: {ex}")
302+
292303
def test_load_from_dict_logging(self):
293304
"""Should load logging config from dict."""
294305
config_data = {

0 commit comments

Comments
 (0)