diff --git a/tests/functional/adapter/conftest.py b/tests/functional/adapter/conftest.py index c5c980154..6cf4557e4 100644 --- a/tests/functional/adapter/conftest.py +++ b/tests/functional/adapter/conftest.py @@ -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 diff --git a/tests/functional/adapter/grants/test_model_grants.py b/tests/functional/adapter/grants/test_model_grants.py index 1e7ff5f9f..1841ff645 100644 --- a/tests/functional/adapter/grants/test_model_grants.py +++ b/tests/functional/adapter/grants/test_model_grants.py @@ -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)