Skip to content

Commit

Permalink
Added ability to specify custom SQL in make_query
Browse files Browse the repository at this point in the history
  • Loading branch information
nfx committed Sep 16, 2024
1 parent 53034fd commit 5d993da
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,9 @@ See also [`ws`](#ws-fixture).
### `make_query` fixture
Create a query and remove it after the test is done. Returns the [`LegacyQuery`](https://databricks-sdk-py.readthedocs.io/en/latest/dbdataclasses/sql.html#databricks.sdk.service.sql.LegacyQuery) object.

Keyword Arguments:
- `query`: The query to be stored. Default is `SELECT * FROM <newly created random table>`.

Usage:
```python
from databricks.sdk.service.sql import PermissionLevel
Expand Down
23 changes: 14 additions & 9 deletions src/databricks/labs/pytester/fixtures/redash.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
from collections.abc import Generator

from pytest import fixture
from databricks.sdk.errors import DatabricksError
from databricks.sdk.service.sql import LegacyQuery

from databricks.labs.pytester.fixtures.baseline import factory


@fixture
def make_query(
ws, make_table, make_random, log_workspace_link, watchdog_purge_suffix
ws,
make_table,
make_random,
log_workspace_link,
watchdog_purge_suffix,
) -> Generator[LegacyQuery, None, None]:
"""
Create a query and remove it after the test is done. Returns the `databricks.sdk.service.sql.LegacyQuery` object.
Keyword Arguments:
- `query`: The query to be stored. Default is `SELECT * FROM <newly created random table>`.
Usage:
```python
from databricks.sdk.service.sql import PermissionLevel
Expand All @@ -33,22 +39,21 @@ def test_permissions_for_redash(
```
"""

def create() -> LegacyQuery:
table = make_table()
def create(sql_query: str | None = None) -> LegacyQuery:
if sql_query is None:
table = make_table()
sql_query = f"SELECT * FROM {table.catalog_name}.{table.schema_name}.{table.name}"
query_name = f"dummy_query_Q{make_random(4)}_{watchdog_purge_suffix}"
query = ws.queries_legacy.create(
name=query_name,
description="TEST QUERY FOR UCX",
query=f"SELECT * FROM {table.schema_name}.{table.name}",
query=sql_query,
tags=["original_query_tag"],
)
log_workspace_link(f"{query_name} query", f'sql/editor/{query.id}')
return query

def remove(query: LegacyQuery):
try:
ws.queries_legacy.delete(query_id=query.id)
except DatabricksError:
pass
ws.queries_legacy.delete(query_id=query.id)

yield from factory("query", create, remove)

0 comments on commit 5d993da

Please sign in to comment.