-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MINOR: add test for sqla compiler (#15275)
* add test for sqla compiler
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from contextlib import contextmanager | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from sqlalchemy import create_engine | ||
|
||
from metadata.ingestion.connections.test_connections import ( | ||
test_connection_engine_step as fn_test_connection_engine_step, # renamed to avoid getting picked up as test by pytest | ||
) | ||
|
||
|
||
def assert_execute(engine, expected_query): | ||
def inner(query, *_, **__): | ||
assert str(query.compile(dialect=engine.dialect)) == expected_query | ||
|
||
return inner | ||
|
||
|
||
def create_mock_connect(mock): | ||
@contextmanager | ||
def mock_connect(*_, **__): | ||
yield mock | ||
|
||
return mock_connect | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="function") | ||
def bigquery(monkeypatch): | ||
monkeypatch.setattr( | ||
"sqlalchemy_bigquery._helpers.create_bigquery_client", | ||
Mock(), | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"dialect,expected_test_fn", | ||
( | ||
[ | ||
("sqlite", "SELECT 42"), | ||
("mysql", "SELECT 42"), | ||
("bigquery", "SELECT SESSION_USER()"), | ||
# TODO this is skipped because installing ibm_db_sa requires going through hoops | ||
# ("ibmi", "SELECT 42 FROM SYSIBM.SYSDUMMY1;"), | ||
] | ||
), | ||
) | ||
def test_test_connection_engine_step(dialect, expected_test_fn): | ||
engine = create_engine(dialect + "://") | ||
mock_connect = Mock() | ||
mock_connect.execute = assert_execute(engine, expected_test_fn) | ||
if dialect != "sqlite": | ||
engine.connect = create_mock_connect(mock_connect) | ||
fn_test_connection_engine_step(engine) |