From e405e3dde5af33c736d19c576410313370dcae91 Mon Sep 17 00:00:00 2001 From: Serge Smertin Date: Wed, 18 Sep 2024 11:12:15 +0200 Subject: [PATCH 1/2] Fix nightly CI builds --- pyproject.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 0933f14..15b726b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,9 +48,9 @@ classifiers = [ ] dependencies = [ - "databricks-sdk~=0.30", - "databricks-labs-lsql~=0.10", - "pytest~=8.3.3", + "databricks-sdk>=0.30", + "databricks-labs-lsql>=0.10", + "pytest>=8.3", ] [project.entry-points.pytest11] From 88c62d9ae9e0ba6a3468fd9a2a7d613219c438ae Mon Sep 17 00:00:00 2001 From: Serge Smertin Date: Wed, 18 Sep 2024 11:37:55 +0200 Subject: [PATCH 2/2] .. --- .github/workflows/no-cheat.yml | 32 ----------- CHANGELOG.md | 4 -- README.md | 15 +++++ .../labs/pytester/fixtures/permissions.py | 57 ++++++++++--------- tests/unit/fixtures/test_permissions.py | 3 +- 5 files changed, 47 insertions(+), 64 deletions(-) delete mode 100644 .github/workflows/no-cheat.yml diff --git a/.github/workflows/no-cheat.yml b/.github/workflows/no-cheat.yml deleted file mode 100644 index bd5edca..0000000 --- a/.github/workflows/no-cheat.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: no-cheat - -on: - pull_request: - types: [opened, synchronize] - merge_group: - types: [checks_requested] - push: - # required for merge queue to work. jobs.integration.if will mark it as skipped - branches: - - main - -jobs: - no-pylint-disable: - runs-on: ubuntu-latest - if: github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'synchronize') - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Verify no additional disabled lint in the new code - run: | - git fetch origin $GITHUB_BASE_REF:$GITHUB_BASE_REF - git diff $GITHUB_BASE_REF...$(git branch --show-current) >> diff_data.txt - python tests/unit/no_cheat.py diff_data.txt >> cheats.txt - COUNT=$(cat cheats.txt | wc -c) - if [ ${COUNT} -gt 1 ]; then - cat cheats.txt - exit 1 - fi diff --git a/CHANGELOG.md b/CHANGELOG.md index eef300f..fff45e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,7 +39,3 @@ Dependency updates: * Updated pytest requirement from ~=8.1.0 to ~=8.3.3 ([#31](https://github.com/databrickslabs/pytester/pull/31)). - -## 0.0.0 - -Initial release \ No newline at end of file diff --git a/README.md b/README.md index f31fd16..ea17cd3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # Python Testing for Databricks +[![python](https://img.shields.io/badge/python-3.10,%203.11,%203.12-green)](https://github.com/databrickslabs/pytester/actions/workflows/push.yml) +[![lines of code](https://tokei.rs/b1/github/databrickslabs/pytester)]([https://github.com/databrickslabs/pytester](https://github.com/databrickslabs/pytester)) + + * [Python Testing for Databricks](#python-testing-for-databricks) * [Ecosystem](#ecosystem) @@ -61,6 +65,17 @@ * [Project Support](#project-support) +## Installation + +Add a `databricks-labs-pytester` dependency to your `pyproject.toml` file (or legacy `requirements.txt` file). You can +also install it directly from the command line: + +```shell +pip install databricks-labs-pytester +``` + +[[back to top](#python-testing-for-databricks)] + ## Ecosystem Built on top of [Databricks SDK for Python](https://github.com/databricks/databricks-sdk-py), this library is part of the Databricks Labs Python ecosystem, which includes the following projects: diff --git a/src/databricks/labs/pytester/fixtures/permissions.py b/src/databricks/labs/pytester/fixtures/permissions.py index 7240a5e..a10cc6e 100644 --- a/src/databricks/labs/pytester/fixtures/permissions.py +++ b/src/databricks/labs/pytester/fixtures/permissions.py @@ -1,8 +1,9 @@ import pytest from databricks.sdk.errors import InvalidParameterValue -from databricks.sdk.service import sql, iam +from databricks.sdk.service import iam from databricks.sdk.service.iam import PermissionLevel -from databricks.sdk.service.sql import GetResponse +from databricks.sdk.service.sql import GetResponse, ObjectTypePlural, AccessControl +from databricks.sdk.service.sql import PermissionLevel as RedashPermissionLevel from databricks.labs.pytester.fixtures.baseline import factory @@ -31,18 +32,18 @@ def __repr__(self): class _RedashPermissionsChange: - def __init__(self, object_id: str, before: list[sql.AccessControl], after: list[sql.AccessControl]): + def __init__(self, object_id: str, before: list[AccessControl], after: list[AccessControl]): self.object_id = object_id self.before = before self.after = after @staticmethod - def _principal(acr: sql.AccessControl) -> str: + def _principal(acr: AccessControl) -> str: if acr.user_name is not None: return f"user_name {acr.user_name}" return f"group_name {acr.group_name}" - def _list(self, acl: list[sql.AccessControl]): + def _list(self, acl: list[AccessControl]): return ", ".join(f"{self._principal(_)} {_.permission_level.value}" for _ in acl) def __repr__(self): @@ -132,11 +133,11 @@ def remove(change: _PermissionsChange): def _make_redash_permissions_factory(name, resource_type, levels, id_retriever): def _non_inherited(acl: GetResponse): - out: list[sql.AccessControl] = [] + out: list[AccessControl] = [] assert acl.access_control_list is not None for access_control in acl.access_control_list: out.append( - sql.AccessControl( + AccessControl( permission_level=access_control.permission_level, group_name=access_control.group_name, user_name=access_control.user_name, @@ -148,10 +149,10 @@ def _make_permissions(ws): def create( *, object_id: str, - permission_level: sql.PermissionLevel | None = None, + permission_level: RedashPermissionLevel | None = None, group_name: str | None = None, user_name: str | None = None, - access_control_list: list[sql.AccessControl] | None = None, + access_control_list: list[AccessControl] | None = None, ): nothing_specified = permission_level is None and access_control_list is None both_specified = permission_level is not None and access_control_list is not None @@ -172,14 +173,14 @@ def create( access_control_list = [] if group_name is not None: access_control_list.append( - sql.AccessControl( + AccessControl( group_name=group_name, permission_level=permission_level, ) ) if user_name is not None: access_control_list.append( - sql.AccessControl( + AccessControl( user_name=user_name, permission_level=permission_level, ) @@ -190,7 +191,9 @@ def create( def remove(change: _RedashPermissionsChange): ws.dbsql_permissions.set( - sql.ObjectTypePlural(resource_type), change.object_id, access_control_list=change.before + ObjectTypePlural(resource_type), + change.object_id, + access_control_list=change.before, ) yield from factory(f"{name} permissions", create, remove) @@ -371,12 +374,12 @@ def _path(ws, path): make_dashboard_permissions = pytest.fixture( _make_redash_permissions_factory( "dashboard", - "dashboards", + ObjectTypePlural.DASHBOARDS, [ - PermissionLevel.CAN_EDIT, - PermissionLevel.CAN_RUN, - PermissionLevel.CAN_MANAGE, - PermissionLevel.CAN_VIEW, + RedashPermissionLevel.CAN_EDIT, + RedashPermissionLevel.CAN_RUN, + RedashPermissionLevel.CAN_MANAGE, + RedashPermissionLevel.CAN_VIEW, ], _simple, ) @@ -384,12 +387,12 @@ def _path(ws, path): make_alert_permissions = pytest.fixture( _make_redash_permissions_factory( "alert", - "alerts", + ObjectTypePlural.ALERTS, [ - PermissionLevel.CAN_EDIT, - PermissionLevel.CAN_RUN, - PermissionLevel.CAN_MANAGE, - PermissionLevel.CAN_VIEW, + RedashPermissionLevel.CAN_EDIT, + RedashPermissionLevel.CAN_RUN, + RedashPermissionLevel.CAN_MANAGE, + RedashPermissionLevel.CAN_VIEW, ], _simple, ) @@ -397,12 +400,12 @@ def _path(ws, path): make_query_permissions = pytest.fixture( _make_redash_permissions_factory( "query", - "queries", + ObjectTypePlural.QUERIES, [ - PermissionLevel.CAN_EDIT, - PermissionLevel.CAN_RUN, - PermissionLevel.CAN_MANAGE, - PermissionLevel.CAN_VIEW, + RedashPermissionLevel.CAN_EDIT, + RedashPermissionLevel.CAN_RUN, + RedashPermissionLevel.CAN_MANAGE, + RedashPermissionLevel.CAN_VIEW, ], _simple, ) diff --git a/tests/unit/fixtures/test_permissions.py b/tests/unit/fixtures/test_permissions.py index 7a73c93..a9602de 100644 --- a/tests/unit/fixtures/test_permissions.py +++ b/tests/unit/fixtures/test_permissions.py @@ -1,4 +1,5 @@ from databricks.sdk.service.iam import PermissionLevel +from databricks.sdk.service.sql import PermissionLevel as SqlPermissionLevel from databricks.labs.pytester.fixtures.permissions import make_cluster_permissions, make_query_permissions from databricks.labs.pytester.fixtures.unwrap import call_stateful @@ -18,7 +19,7 @@ def test_make_query_permissions_no_args(): ctx, query_permissions = call_stateful( make_query_permissions, object_id="dummy", - permission_level=PermissionLevel.CAN_MANAGE, + permission_level=SqlPermissionLevel.CAN_MANAGE, ) assert ctx is not None assert query_permissions is not None