Skip to content

Commit dc0233f

Browse files
authored
Updating make_schema fixture to include location to create managed schema (#66)
<!-- REMOVE IRRELEVANT COMMENTS BEFORE CREATING A PULL REQUEST --> ## Changes Added option to pass location field in make_schema fixture to create a managed schema Resolves #65 ### Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [ ] added unit tests - [ ] added integration tests
1 parent 9b9a954 commit dc0233f

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ Create a schema and return its info. Remove it after the test. Returns instance
859859
Keyword Arguments:
860860
* `catalog_name` (str): The name of the catalog where the schema will be created. Default is `hive_metastore`.
861861
* `name` (str): The name of the schema. Default is a random string.
862+
* `location` (str): The path to the location if it should be a managed schema.
862863

863864
Usage:
864865
```python

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,15 @@ def remove(table_info: TableInfo):
241241

242242
@fixture
243243
def make_schema(
244-
sql_backend,
245-
make_random,
246-
log_workspace_link,
247-
watchdog_remove_after,
244+
sql_backend, make_random, log_workspace_link, watchdog_remove_after
248245
) -> Generator[Callable[..., SchemaInfo], None, None]:
249246
"""
250247
Create a schema and return its info. Remove it after the test. Returns instance of `databricks.sdk.service.catalog.SchemaInfo`.
251248
252249
Keyword Arguments:
253250
* `catalog_name` (str): The name of the catalog where the schema will be created. Default is `hive_metastore`.
254251
* `name` (str): The name of the schema. Default is a random string.
252+
* `location` (str): The path to the location if it should be a managed schema.
255253
256254
Usage:
257255
```python
@@ -263,11 +261,17 @@ def test_catalog_fixture(make_catalog, make_schema, make_table):
263261
```
264262
"""
265263

266-
def create(*, catalog_name: str = "hive_metastore", name: str | None = None) -> SchemaInfo:
264+
def create(
265+
*, catalog_name: str = "hive_metastore", name: str | None = None, location: str | None = None
266+
) -> SchemaInfo:
267267
name = name or f"dummy_s{make_random(4)}".lower()
268268
full_name = f"{catalog_name}.{name}".lower()
269-
sql_backend.execute(f"CREATE SCHEMA {full_name} WITH DBPROPERTIES (RemoveAfter={watchdog_remove_after})")
270-
schema_info = SchemaInfo(catalog_name=catalog_name, name=name, full_name=full_name)
269+
schema_ddl = f"CREATE SCHEMA {full_name}"
270+
if location:
271+
schema_ddl = f"{schema_ddl} LOCATION '{location}'"
272+
schema_ddl = f"{schema_ddl} WITH DBPROPERTIES (RemoveAfter={watchdog_remove_after})"
273+
sql_backend.execute(schema_ddl)
274+
schema_info = SchemaInfo(catalog_name=catalog_name, name=name, full_name=full_name, storage_location=location)
271275
path = f'explore/data/{schema_info.catalog_name}/{schema_info.name}'
272276
log_workspace_link(f'{schema_info.full_name} schema', path)
273277
return schema_info

tests/integration/fixtures/test_catalog.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ def test_schema_fixture(make_schema):
1717
logger.info(f"Created new schema: {make_schema()}")
1818

1919

20+
def test_managed_schema_fixture(make_schema, make_random, env_or_skip):
21+
schema_name = f"dummy_s{make_random(4)}".lower()
22+
schema_location = f"{env_or_skip('TEST_MOUNT_CONTAINER')}/a/{schema_name}"
23+
logger.info(f"Created new schema: {make_schema(location = schema_location)}")
24+
25+
2026
def test_new_managed_table_in_new_schema(make_table):
2127
logger.info(f"Created new managed table in new schema: {make_table()}")
2228

tests/unit/fixtures/test_catalog.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
from unittest.mock import ANY
22

3-
from databricks.sdk.service.catalog import TableInfo, TableType, DataSourceFormat, FunctionInfo
3+
from databricks.sdk.service.catalog import TableInfo, TableType, DataSourceFormat, FunctionInfo, SchemaInfo
44

55
from databricks.labs.pytester.fixtures.unwrap import call_stateful
6-
from databricks.labs.pytester.fixtures.catalog import make_table, make_udf, make_catalog, make_storage_credential
6+
from databricks.labs.pytester.fixtures.catalog import (
7+
make_table,
8+
make_udf,
9+
make_catalog,
10+
make_storage_credential,
11+
make_schema,
12+
)
713

814

915
def test_make_table_no_args():
@@ -137,3 +143,17 @@ def test_storage_credential():
137143
ctx, fn_info = call_stateful(make_storage_credential, credential_name='abc')
138144
assert ctx is not None
139145
assert fn_info is not None
146+
147+
148+
def test_make_schema() -> None:
149+
ctx, info = call_stateful(make_schema, name='abc', location='abfss://[email protected]')
150+
assert ctx['sql_backend'].queries == [
151+
"CREATE SCHEMA hive_metastore.abc LOCATION 'abfss://[email protected]' WITH DBPROPERTIES (RemoveAfter=2024091313)",
152+
"DROP SCHEMA IF EXISTS hive_metastore.abc CASCADE",
153+
]
154+
assert info == SchemaInfo(
155+
catalog_name='hive_metastore',
156+
name='abc',
157+
full_name='hive_metastore.abc',
158+
storage_location='abfss://[email protected]',
159+
)

0 commit comments

Comments
 (0)