|
12 | 12 | """ |
13 | 13 | Unit tests for db_utils module |
14 | 14 | """ |
| 15 | + |
15 | 16 | import uuid |
16 | 17 | from copy import deepcopy |
17 | 18 | from unittest import TestCase |
|
36 | 37 | from metadata.ingestion.lineage.models import Dialect |
37 | 38 | from metadata.ingestion.lineage.sql_lineage import search_cache |
38 | 39 | from metadata.ingestion.source.models import TableView |
39 | | -from metadata.utils.db_utils import get_host_from_host_port, get_view_lineage |
| 40 | +from metadata.utils.db_utils import ( |
| 41 | + clean_host_port, |
| 42 | + get_host_from_host_port, |
| 43 | + get_view_lineage, |
| 44 | +) |
40 | 45 |
|
41 | 46 |
|
42 | 47 | # Mock LineageTable class to simulate collate_sqllineage.core.models.Table |
@@ -118,6 +123,52 @@ def test_get_host_from_host_port(self): |
118 | 123 | self.assertEqual(get_host_from_host_port("localhost"), "localhost") |
119 | 124 | self.assertEqual(get_host_from_host_port("example.com"), "example.com") |
120 | 125 |
|
| 126 | + # Test with URL scheme prefixes |
| 127 | + self.assertEqual(get_host_from_host_port("http://localhost:3306"), "localhost") |
| 128 | + self.assertEqual( |
| 129 | + get_host_from_host_port("https://example.com:5432"), "example.com" |
| 130 | + ) |
| 131 | + self.assertEqual(get_host_from_host_port("http://localhost"), "localhost") |
| 132 | + |
| 133 | + def test_clean_host_port(self): |
| 134 | + """Test clean_host_port strips URL scheme prefixes""" |
| 135 | + # Already-clean values pass through unchanged |
| 136 | + self.assertEqual(clean_host_port("localhost:3306"), "localhost:3306") |
| 137 | + self.assertEqual(clean_host_port("127.0.0.1:5432"), "127.0.0.1:5432") |
| 138 | + self.assertEqual(clean_host_port("example.com"), "example.com") |
| 139 | + |
| 140 | + # HTTP prefix is stripped |
| 141 | + self.assertEqual(clean_host_port("http://localhost:3306"), "localhost:3306") |
| 142 | + self.assertEqual(clean_host_port("http://example.com:8080"), "example.com:8080") |
| 143 | + |
| 144 | + # HTTPS prefix is stripped |
| 145 | + self.assertEqual(clean_host_port("https://localhost:5432"), "localhost:5432") |
| 146 | + self.assertEqual( |
| 147 | + clean_host_port("https://mydb.example.com:3306"), "mydb.example.com:3306" |
| 148 | + ) |
| 149 | + |
| 150 | + # Trailing slash is stripped |
| 151 | + self.assertEqual(clean_host_port("http://localhost:3306/"), "localhost:3306") |
| 152 | + |
| 153 | + # Host only with scheme |
| 154 | + self.assertEqual(clean_host_port("http://localhost"), "localhost") |
| 155 | + self.assertEqual(clean_host_port("https://example.com"), "example.com") |
| 156 | + |
| 157 | + # URL with path is handled — path/query/fragment are discarded |
| 158 | + self.assertEqual(clean_host_port("http://localhost:3306/db"), "localhost:3306") |
| 159 | + self.assertEqual( |
| 160 | + clean_host_port("https://example.com:5432/mydb?ssl=true"), |
| 161 | + "example.com:5432", |
| 162 | + ) |
| 163 | + |
| 164 | + # Whitespace is stripped |
| 165 | + self.assertEqual(clean_host_port(" localhost:3306 "), "localhost:3306") |
| 166 | + self.assertEqual(clean_host_port(" http://localhost:3306 "), "localhost:3306") |
| 167 | + |
| 168 | + # JDBC-style URLs fall back to raw extraction |
| 169 | + self.assertEqual(clean_host_port("jdbc:postgresql://host:5432"), "host:5432") |
| 170 | + self.assertEqual(clean_host_port("jdbc:postgresql://host:5432/db"), "host:5432") |
| 171 | + |
121 | 172 | @patch("metadata.utils.db_utils.ConnectionTypeDialectMapper") |
122 | 173 | @patch("metadata.utils.db_utils.fqn") |
123 | 174 | def test_get_view_lineage_success_with_lineage_parser( |
|
0 commit comments