|
| 1 | +# Copyright 2025 Collate |
| 2 | +# Licensed under the Collate Community License, Version 1.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# https://github.com/open-metadata/OpenMetadata/blob/main/ingestion/LICENSE |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +""" |
| 13 | +Tests for unitycatalog.connection.get_sqlalchemy_connection. |
| 14 | +""" |
| 15 | + |
| 16 | +from sqlalchemy.engine import Engine |
| 17 | + |
| 18 | +from metadata.generated.schema.entity.services.connections.database.databricks.personalAccessToken import ( |
| 19 | + PersonalAccessToken, |
| 20 | +) |
| 21 | +from metadata.generated.schema.entity.services.connections.database.unityCatalogConnection import ( |
| 22 | + UnityCatalogConnection, |
| 23 | +) |
| 24 | +from metadata.ingestion.source.database.unitycatalog.connection import ( |
| 25 | + get_sqlalchemy_connection, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +def _connection(**overrides) -> UnityCatalogConnection: |
| 30 | + defaults = { |
| 31 | + "hostPort": "test-host:443", |
| 32 | + "authType": PersonalAccessToken(token="test-token"), |
| 33 | + } |
| 34 | + defaults.update(overrides) |
| 35 | + return UnityCatalogConnection(**defaults) |
| 36 | + |
| 37 | + |
| 38 | +def test_returns_engine_when_http_path_and_connection_args_are_unset(): |
| 39 | + """ |
| 40 | + Regression for the AttributeError raised on |
| 41 | + `connection.connectionArguments.root.update(auth_args)` when both |
| 42 | + httpPath and connectionArguments are omitted from the service config. |
| 43 | + """ |
| 44 | + connection = _connection() |
| 45 | + assert connection.httpPath is None |
| 46 | + assert connection.connectionArguments is None |
| 47 | + |
| 48 | + engine = get_sqlalchemy_connection(connection) |
| 49 | + |
| 50 | + assert isinstance(engine, Engine) |
| 51 | + |
| 52 | + |
| 53 | +def test_returns_engine_when_http_path_is_set(): |
| 54 | + """Engine is created and http_path is accepted as a connect arg.""" |
| 55 | + connection = _connection(httpPath="/sql/1.0/warehouses/abc") |
| 56 | + |
| 57 | + engine = get_sqlalchemy_connection(connection) |
| 58 | + |
| 59 | + assert isinstance(engine, Engine) |
| 60 | + assert engine.url.host == "test-host" |
0 commit comments