Skip to content

Commit

Permalink
Add fixture to create grants/roles for test purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-rogers-dbt committed Oct 9, 2023
1 parent 2bb40b8 commit f464d44
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions tests/functional/adapter/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
import pytest
import os

from dbt.exceptions import DbtDatabaseError

# This is a hack to prevent the fixture from running more than once
GRANTS_AND_ROLES_SETUP = False


@pytest.fixture(scope="class", autouse=True)
def setup_grants_and_roles(project):
global GRANTS_AND_ROLES_SETUP
groups = [
os.environ[env_var] for env_var in os.environ if env_var.startswith("DBT_TEST_GROUP_")
]
roles = [os.environ[env_var] for env_var in os.environ if env_var.startswith("DBT_TEST_ROLE_")]
if not GRANTS_AND_ROLES_SETUP:
with project.adapter.connection_named("__test"):
for group in groups:
try:
project.adapter.execute(f"CREATE GROUP {group}")
except DbtDatabaseError:
# This is expected if the group already exists
pass

for role in roles:
try:
project.adapter.execute(f"CREATE ROLE {role}")
except DbtDatabaseError:
# This is expected if the group already exists
pass

GRANTS_AND_ROLES_SETUP = True


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/adapter/grants/test_model_grants.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"""


class BaseModelGrantsRedshift(BaseGrantsRedshift):
class TestModelGrantsRedshift(BaseGrantsRedshift):
@pytest.fixture(scope="class")
def models(self):
updated_schema = self.interpolate_name_overrides(model_schema_yml)
Expand Down

0 comments on commit f464d44

Please sign in to comment.