Skip to content

Commit

Permalink
Create a schema for each Grant Access Test run (#842)
Browse files Browse the repository at this point in the history
* use dynamic schema in test_grant_access_to.py

* use dynamic schema in test_grant_access_to.py

* have test grant access dynamically create schema

* have test grant access dynamically create schema
  • Loading branch information
colin-rogers-dbt authored Jul 24, 2023
1 parent 1f80a20 commit da9f2da
Showing 1 changed file with 56 additions and 22 deletions.
78 changes: 56 additions & 22 deletions tests/functional/adapter/test_grant_access_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,79 @@
from dbt.tests.util import run_dbt


SELECT_1 = """
{{ config(
materialized='view',
grant_access_to=[
{'project': 'dbt-test-env-alt', 'dataset': 'GrantAccessTest'},
]
) }}
SELECT 1 as one
"""
def select_1(dataset: str, materialized: str):
config = f"""config(
materialized='{materialized}',
grant_access_to=[
{{'project': 'dbt-test-env', 'dataset': '{dataset}'}},
]
)"""
return (
"{{"
+ config
+ "}}"
+ """
SELECT 1 as one"""
)


SELECT_1_TABLE = """
{{ config(
materialized='table',
grant_access_to=[
{'project': 'dbt-test-env-alt', 'dataset': 'GrantAccessTest'},
]
) }}
SELECT 1 as one
"""
BAD_CONFIG_TABLE_NAME = "bad_view"
BAD_CONFIG_TABLE = """
{{ config(
materialized='view',
grant_access_to=[
{'project': 'dbt-test-env-alt', 'dataset': 'NonExistentDataset'},
{'project': 'dbt-test-env', 'dataset': 'NonExistentDataset'},
]
) }}
SELECT 1 as one
"""

BAD_CONFIG_CHILD_TABLE = "SELECT 1 as one FROM {{ref('" + BAD_CONFIG_TABLE_NAME + "')}}"


def get_schema_name(base_schema_name: str) -> str:
return f"{base_schema_name}_grant_access"


class TestAccessGrantSucceeds:
@pytest.fixture(scope="class")
def models(self):
return {"select_1.sql": SELECT_1, "select_1_table.sql": SELECT_1_TABLE}
def setup_grant_schema(
self,
project,
unique_schema,
):
with project.adapter.connection_named("__test_grants"):
relation = project.adapter.Relation.create(
database=project.database,
schema=get_schema_name(unique_schema),
identifier="grant_access",
)
project.adapter.create_schema(relation)
yield relation

@pytest.fixture(scope="class")
def teardown_grant_schema(
self,
project,
unique_schema,
):
yield
with project.adapter.connection_named("__test_grants"):
relation = project.adapter.Relation.create(
database=project.database, schema=get_schema_name(unique_schema)
)
project.adapter.drop_schema(relation)

@pytest.fixture(scope="class")
def models(self, unique_schema):
dataset = get_schema_name(unique_schema)
return {
"select_1.sql": select_1(dataset=dataset, materialized="view"),
"select_1_table.sql": select_1(dataset=dataset, materialized="table"),
}

def test_grant_access_succeeds(self, project):
def test_grant_access_succeeds(self, project, setup_grant_schema, teardown_grant_schema):
# Need to run twice to validate idempotency
results = run_dbt(["run"])
assert len(results) == 2
Expand Down

0 comments on commit da9f2da

Please sign in to comment.