Skip to content

Commit

Permalink
MINOR: add test for sqla compiler (#15275)
Browse files Browse the repository at this point in the history
* add test for sqla compiler
  • Loading branch information
sushi30 authored Feb 22, 2024
1 parent 3adc1e5 commit 4967e09
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Empty file.
53 changes: 53 additions & 0 deletions ingestion/tests/unit/connections/test_test_connections.py
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)

0 comments on commit 4967e09

Please sign in to comment.