dabla commented on code in PR #56324:
URL: https://github.com/apache/airflow/pull/56324#discussion_r3033192267
##########
providers/google/tests/unit/google/common/hooks/test_base_google.py:
##########
@@ -541,6 +541,65 @@ def
test_get_credentials_and_project_id_with_default_auth_and_overridden_project
)
assert result == ("CREDENTIALS", "SECOND_PROJECT_ID")
+ def test_quota_project_id_init(self):
+ """Test that quota project ID is properly initialized."""
+ hook = GoogleBaseHook(gcp_conn_id="google_cloud_default",
quota_project_id="test-quota-project")
+ assert hook.quota_project_id == "test-quota-project"
+
+ @mock.patch("google.auth._default._load_credentials_from_file")
+ @mock.patch("os.environ", {CREDENTIALS: "/test/key-path"})
+ def test_quota_project_id_from_connection(self, mock_load_creds):
+ """Test that quota project ID from connection is applied to
credentials."""
+ mock_creds = mock.MagicMock()
+ mock_creds.with_quota_project.return_value = mock_creds
+ mock_load_creds.return_value = (mock_creds, "test-project")
+
+ # Mock connection with quota_project_id in extras
+ uri =
f"google-cloud-platform://?extras={json.dumps({'quota_project_id':
'test-quota-project'})}"
+ with mock.patch.dict("os.environ",
{"AIRFLOW_CONN_GOOGLE_CLOUD_DEFAULT": uri}):
+ hook = GoogleBaseHook(gcp_conn_id="google_cloud_default")
+ creds, _ = hook.get_credentials_and_project_id()
+
mock_creds.with_quota_project.assert_called_once_with("test-quota-project")
+
+ @mock.patch("google.auth._default._load_credentials_from_file")
+ @mock.patch("os.environ", {CREDENTIALS: "/test/key-path"})
+ def test_quota_project_id_param_overrides_connection(self,
mock_load_creds):
+ """Test that quota project ID from param overrides connection value."""
+ mock_creds = mock.MagicMock()
+ mock_creds.with_quota_project.return_value = mock_creds
+ mock_load_creds.return_value = (mock_creds, "test-project")
+
+ # Connection with quota_project_id in extras
+ conn_quota = "connection-quota-project"
+ uri =
f"google-cloud-platform://?extras={json.dumps({'quota_project_id':
conn_quota})}"
+
+ with mock.patch.dict("os.environ",
{"AIRFLOW_CONN_GOOGLE_CLOUD_DEFAULT": uri}):
+ hook = GoogleBaseHook(gcp_conn_id="google_cloud_default",
quota_project_id="test-quota-project")
+ creds, _ = hook.get_credentials_and_project_id()
+
+ # Should use param quota project, not connection quota project
+
mock_creds.with_quota_project.assert_called_once_with("test-quota-project")
+
+ def test_quota_project_invalid_format(self):
+ """Test validation of quota project ID format."""
Review Comment:
Please refactor the for-loop for invalid id's as an parametrized test like
example below, there are enough examples in existing tests on how to do this:
```
@pytest.mark.parametrize(
"invalid_id",
[
"UPPERCASE", # Must be lowercase
"special@chars", # Invalid characters
"ab-cd", # Too short
"a" + "b" * 30, # Too long
"1starts-with-number", # Must start with letter
"", # Empty string
],
)
def test_quota_project_invalid_format(invalid_id):
"""Test validation of quota project ID format."""
mock_creds = mock.MagicMock()
mock_creds.with_quota_project.return_value = mock_creds
with mock.patch(
MODULE_NAME + ".GoogleBaseHook.get_credentials_and_project_id",
return_value=(mock_creds, None),
):
hook = GoogleBaseHook(quota_project_id=invalid_id)
with pytest.raises((TypeError, ValueError)):
hook.get_credentials()
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]