Skip to content

Commit

Permalink
Stop patching providers and retrieve current adapter instead
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Apr 5, 2022
1 parent 7e9cd5b commit ebff78f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 39 deletions.
12 changes: 8 additions & 4 deletions core/dbt/tests/fixtures/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import dbt.flags as flags
from dbt.config.runtime import RuntimeConfig
from dbt.adapters.factory import get_adapter, register_adapter, reset_adapters
from dbt.adapters.factory import get_adapter, register_adapter, reset_adapters, get_adapter_by_type
from dbt.events.functions import setup_event_logger
from dbt.tests.util import write_file, run_sql_with_adapter, TestProcessingException

Expand Down Expand Up @@ -269,7 +269,7 @@ def __init__(
self,
project_root,
profiles_dir,
adapter,
adapter_type,
test_dir,
shared_data_dir,
test_data_dir,
Expand All @@ -279,14 +279,18 @@ def __init__(
):
self.project_root = project_root
self.profiles_dir = profiles_dir
self.adapter = adapter
self.adapter_type = adapter_type
self.test_dir = test_dir
self.shared_data_dir = shared_data_dir
self.test_data_dir = test_data_dir
self.test_schema = test_schema
self.database = database
self.test_config = test_config

@property
def adapter(self):
return get_adapter_by_type(self.adapter_type)

# Run sql from a path
def run_sql_file(self, sql_path, fetch=None):
with open(sql_path, "r") as f:
Expand Down Expand Up @@ -349,7 +353,7 @@ def project(
project = TestProjInfo(
project_root=project_root,
profiles_dir=profiles_root,
adapter=adapter,
adapter_type=adapter.type(),
test_dir=request.fspath.dirname,
shared_data_dir=shared_data_dir,
test_data_dir=test_data_dir,
Expand Down
14 changes: 3 additions & 11 deletions core/dbt/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from dbt.contracts.graph.manifest import Manifest
from dbt.events.functions import fire_event, capture_stdout_logs, stop_capture_stdout_logs
from dbt.events.test_types import IntegrationTestDebug
from dbt.context import providers
from unittest.mock import patch

# =============================================================================
# Test utilities
Expand Down Expand Up @@ -391,15 +389,9 @@ def generate_update_clause(adapter, clause) -> str:

@contextmanager
def get_connection(adapter, name="_test"):
# Since the 'adapter' in dbt.adapters.factory may have been replaced by execution
# of dbt commands since the test 'adapter' was created, we patch the 'get_adapter' call in
# dbt.context.providers, so that macros that are called refer to this test adapter.
# This allows tests to run normal adapter macros as if reset_adapters() were not
# called by handle_and_check (for asserts, etc).
with patch.object(providers, "get_adapter", return_value=adapter):
with adapter.connection_named(name):
conn = adapter.connections.get_thread_connection()
yield conn
with adapter.connection_named(name):
conn = adapter.connections.get_thread_connection()
yield conn


# Uses:
Expand Down
24 changes: 0 additions & 24 deletions tests/functional/permission/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@
from tests.functional.permission.fixtures import models, project_files # noqa: F401


# postgres sometimes fails with an internal error if you run these tests too close together.
def postgres_error(err, *args):
msg = str(err)
if "tuple concurrently updated" in msg:
return True
return False


@mark.flaky(rerun_filter=postgres_error)
class TestPermissions:
@pytest.fixture(autouse=True)
def setUp(self, project):
Expand Down Expand Up @@ -58,18 +49,3 @@ def test_no_create_schema_permissions(
project.run_sql('drop schema if exists "{}" cascade'.format(project.test_schema))
with pytest.raises(RuntimeError):
run_dbt(["run", "--target", "noaccess"], expect_pass=False)

def test_create_schema_permissions(
self,
project,
):
# now it should work!
project.run_sql("grant create on database {} to noaccess".format(project.database))
project.run_sql(
'grant usage, create on schema "{}" to noaccess'.format(project.test_schema)
)
project.run_sql(
'grant select on all tables in schema "{}" to noaccess'.format(project.test_schema)
)
results = run_dbt(["run", "--target", "noaccess"], expect_pass=True)
assert len(results) == 1

0 comments on commit ebff78f

Please sign in to comment.