Skip to content

Commit 260b2c9

Browse files
authored
Support providing name in make_catalog fixture (#52)
## Changes Add name parameter to `make_catalog` fixture. ### Tests - [x] added unit tests
1 parent c229e99 commit 260b2c9

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,9 @@ See also [`ws`](#ws-fixture), [`env_or_skip`](#env_or_skip-fixture), [`sql_backe
806806
Create a catalog and return its info. Remove it after the test.
807807
Returns instance of [`CatalogInfo`](https://databricks-sdk-py.readthedocs.io/en/latest/dbdataclasses/catalog.html#databricks.sdk.service.catalog.CatalogInfo).
808808

809+
Keyword Arguments:
810+
* `name` (str): The name of the catalog. Default is a random string.
811+
809812
Usage:
810813
```python
811814
def test_catalog_fixture(make_catalog, make_schema, make_table):

src/databricks/labs/pytester/fixtures/catalog.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ def make_catalog(
287287
Create a catalog and return its info. Remove it after the test.
288288
Returns instance of `databricks.sdk.service.catalog.CatalogInfo`.
289289
290+
Keyword Arguments:
291+
* `name` (str): The name of the catalog. Default is a random string.
292+
290293
Usage:
291294
```python
292295
def test_catalog_fixture(make_catalog, make_schema, make_table):
@@ -297,8 +300,8 @@ def test_catalog_fixture(make_catalog, make_schema, make_table):
297300
```
298301
"""
299302

300-
def create() -> CatalogInfo:
301-
name = f"dummy_C{make_random(4)}".lower()
303+
def create(*, name: str | None = None) -> CatalogInfo:
304+
name = name or f"dummy_C{make_random(4)}".lower()
302305
catalog_info = ws.catalogs.create(name=name, properties={"RemoveAfter": watchdog_remove_after})
303306
if isinstance(catalog_info, Mock):
304307
catalog_info.name = name

src/databricks/labs/pytester/fixtures/unwrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __str__(self):
6565
_GENERATORS = set[str]()
6666

6767

68-
def call_stateful(some: Callable[..., T], **kwargs) -> tuple[CallContext, T]:
68+
def call_stateful(some: Callable[..., Generator[Callable[..., T]]], **kwargs) -> tuple[CallContext, T]:
6969
# pylint: disable=too-complex
7070
ctx = CallContext()
7171
drains = []

tests/unit/fixtures/test_catalog.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from unittest.mock import ANY
2+
13
from databricks.sdk.service.catalog import TableInfo, TableType, DataSourceFormat, FunctionInfo
24

35
from databricks.labs.pytester.fixtures.unwrap import call_stateful
@@ -101,12 +103,17 @@ def test_make_table_custom_schema():
101103
]
102104

103105

104-
def test_make_catalog():
106+
def test_make_catalog() -> None:
105107
ctx, info = call_stateful(make_catalog)
106-
ctx['ws'].catalogs.create.assert_called() # can't specify call params accurately
108+
ctx['ws'].catalogs.create.assert_called_once() # can't specify call params accurately
107109
assert info.properties and info.properties.get("RemoveAfter", None)
108110

109111

112+
def test_make_catalog_creates_catalog_with_name() -> None:
113+
ctx, _ = call_stateful(make_catalog, name="test")
114+
ctx['ws'].catalogs.create.assert_called_once_with(name="test", properties=ANY)
115+
116+
110117
def test_make_udf():
111118
ctx, fn_info = call_stateful(make_udf)
112119

0 commit comments

Comments
 (0)