amoghrajesh commented on code in PR #31465:
URL: https://github.com/apache/airflow/pull/31465#discussion_r1213436532
##########
airflow/models/connection.py:
##########
@@ -187,10 +187,22 @@ def _normalize_conn_type(conn_type):
return conn_type
def _parse_from_uri(self, uri: str):
+ schemes_count_in_uri = uri.count("://")
+ if schemes_count_in_uri > 2:
+ raise AirflowException(f"Invalid connection string: {uri}.")
+ scheme_in_uri = schemes_count_in_uri == 2
Review Comment:
This variable name is rather confusing to me, can we have something else
here?
##########
airflow/models/connection.py:
##########
@@ -187,10 +187,22 @@ def _normalize_conn_type(conn_type):
return conn_type
def _parse_from_uri(self, uri: str):
+ schemes_count_in_uri = uri.count("://")
+ if schemes_count_in_uri > 2:
+ raise AirflowException(f"Invalid connection string: {uri}.")
+ scheme_in_uri = schemes_count_in_uri == 2
uri_parts = urlsplit(uri)
conn_type = uri_parts.scheme
self.conn_type = self._normalize_conn_type(conn_type)
- self.host = _parse_netloc_to_hostname(uri_parts)
+ reset_of_the_url = uri.replace(f"{conn_type}://", ("" if scheme_in_uri
else "//"))
Review Comment:
nit: typo: `rest_of_the_url`
##########
tests/models/test_connection.py:
##########
@@ -0,0 +1,188 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+import re
+
+import pytest
+
+from airflow import AirflowException
+from airflow.models import Connection
+
+
+class TestConnection:
+ @pytest.mark.parametrize(
+ "uri, expected_conn_type, expected_host, expected_login,
expected_password,"
+ " expected_port, expected_schema, expected_extra_dict,
expected_exception_message",
+ [
+ (
+ "type://user:pass@host:100/schema",
+ "type",
+ "host",
+ "user",
+ "pass",
+ 100,
+ "schema",
+ {},
+ None,
+ ),
+ (
+ "type://user:pass@host/schema",
+ "type",
+ "host",
+ "user",
+ "pass",
+ None,
+ "schema",
+ {},
+ None,
+ ),
+ (
+ "type://user:pass@host/schema?param1=val1¶m2=val2",
+ "type",
+ "host",
+ "user",
+ "pass",
+ None,
+ "schema",
+ {"param1": "val1", "param2": "val2"},
+ None,
+ ),
+ (
+ "type://host",
+ "type",
+ "host",
+ None,
+ None,
+ None,
+ "",
+ {},
+ None,
+ ),
+ (
+
"spark://mysparkcluster.com:80?deploy-mode=cluster&spark_binary=command&namespace=kube+namespace",
+ "spark",
+ "mysparkcluster.com",
+ None,
+ None,
+ 80,
+ "",
+ {"deploy-mode": "cluster", "spark_binary": "command",
"namespace": "kube namespace"},
+ None,
+ ),
+ (
+ "spark://k8s://100.68.0.1:443?deploy-mode=cluster",
+ "spark",
+ "k8s://100.68.0.1",
+ None,
+ None,
+ 443,
+ "",
+ {"deploy-mode": "cluster"},
+ None,
+ ),
Review Comment:
Good coverage on the test cases here :)
--
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]