Skip to content

Commit

Permalink
Set up adapter testing framework for use by adapter test repos (#4846)
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank authored Mar 17, 2022
1 parent 0aeb997 commit 5e0a765
Show file tree
Hide file tree
Showing 31 changed files with 1,001 additions and 865 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Features-20220309-142645.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Features
body: Testing framework for dbt adapter testing
time: 2022-03-09T14:26:45.828295-05:00
custom:
Author: gshank
Issue: "4730"
PR: "4846"
26 changes: 26 additions & 0 deletions core/dbt/adapters/sql/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,29 @@ def check_schema_exists(self, database: str, schema: str) -> bool:
kwargs = {"information_schema": information_schema, "schema": schema}
results = self.execute_macro(CHECK_SCHEMA_EXISTS_MACRO_NAME, kwargs=kwargs)
return results[0][0] > 0

# This is for use in the test suite
def run_sql_for_tests(self, sql, fetch, conn):
cursor = conn.handle.cursor()
try:
cursor.execute(sql)
if hasattr(conn.handle, "commit"):
conn.handle.commit()
if fetch == "one":
if hasattr(cursor, "fetchone"): # for spark
return cursor.fetchone()
else:
# for spark
return cursor.fetchall()[0]
elif fetch == "all":
return cursor.fetchall()
else:
return
except BaseException as e:
if conn.handle and not getattr(conn.handle, "closed", True):
conn.handle.rollback()
print(sql)
print(e)
raise
finally:
conn.transaction_open = False
1 change: 0 additions & 1 deletion core/dbt/tests/__init__.py

This file was deleted.

212 changes: 0 additions & 212 deletions core/dbt/tests/adapter.py

This file was deleted.

57 changes: 33 additions & 24 deletions core/dbt/tests/fixtures/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from dbt.config.runtime import RuntimeConfig
from dbt.adapters.factory import get_adapter, register_adapter, reset_adapters
from dbt.events.functions import setup_event_logger
from dbt.tests.util import write_file, run_sql_with_adapter
from dbt.tests.util import write_file, run_sql_with_adapter, TestProcessingException


# These are the fixtures that are used in dbt core functional tests
Expand Down Expand Up @@ -63,6 +63,19 @@ def test_data_dir(request):
return os.path.join(request.fspath.dirname, "data")


@pytest.fixture(scope="class")
def dbt_profile_target():
return {
"type": "postgres",
"threads": 4,
"host": "localhost",
"port": int(os.getenv("POSTGRES_TEST_PORT", 5432)),
"user": os.getenv("POSTGRES_TEST_USER", "root"),
"pass": os.getenv("POSTGRES_TEST_PASS", "password"),
"dbname": os.getenv("POSTGRES_TEST_DATABASE", "dbt"),
}


# This fixture can be overridden in a project
@pytest.fixture(scope="class")
def profiles_config_update():
Expand All @@ -71,35 +84,19 @@ def profiles_config_update():

# The profile dictionary, used to write out profiles.yml
@pytest.fixture(scope="class")
def dbt_profile_data(unique_schema, profiles_config_update):
def dbt_profile_data(unique_schema, dbt_profile_target, profiles_config_update):
profile = {
"config": {"send_anonymous_usage_stats": False},
"test": {
"outputs": {
"default": {
"type": "postgres",
"threads": 4,
"host": "localhost",
"port": int(os.getenv("POSTGRES_TEST_PORT", 5432)),
"user": os.getenv("POSTGRES_TEST_USER", "root"),
"pass": os.getenv("POSTGRES_TEST_PASS", "password"),
"dbname": os.getenv("POSTGRES_TEST_DATABASE", "dbt"),
"schema": unique_schema,
},
"other_schema": {
"type": "postgres",
"threads": 4,
"host": "localhost",
"port": int(os.getenv("POSTGRES_TEST_PORT", 5432)),
"user": "noaccess",
"pass": "password",
"dbname": os.getenv("POSTGRES_TEST_DATABASE", "dbt"),
"schema": unique_schema + "_alt", # Should this be the same unique_schema?
},
"default": {},
},
"target": "default",
},
}
target = dbt_profile_target
target["schema"] = unique_schema
profile["test"]["outputs"]["default"] = target

if profiles_config_update:
profile.update(profiles_config_update)
Expand Down Expand Up @@ -199,6 +196,8 @@ def write_project_files(project_root, dir_name, file_dict):

# Write files out from file_dict. Can be nested directories...
def write_project_files_recursively(path, file_dict):
if type(file_dict) is not dict:
raise TestProcessingException(f"Error creating {path}. Did you forget the file extension?")
for name, value in file_dict.items():
if name.endswith(".sql") or name.endswith(".csv") or name.endswith(".md"):
write_file(value, path, name)
Expand Down Expand Up @@ -276,6 +275,7 @@ def __init__(
test_data_dir,
test_schema,
database,
test_config,
):
self.project_root = project_root
self.profiles_dir = profiles_dir
Expand All @@ -285,6 +285,7 @@ def __init__(
self.test_data_dir = test_data_dir
self.test_schema = test_schema
self.database = database
self.test_config = test_config

# Run sql from a path
def run_sql_file(self, sql_path, fetch=None):
Expand Down Expand Up @@ -313,6 +314,13 @@ def get_tables_in_schema(self):
return {model_name: materialization for (model_name, materialization) in result}


# This fixture is for customizing tests that need overrides in adapter
# repos. Example in dbt.tests.adapter.basic.test_base.
@pytest.fixture(scope="class")
def test_config():
return {}


@pytest.fixture(scope="class")
def project(
project_root,
Expand All @@ -328,6 +336,7 @@ def project(
shared_data_dir,
test_data_dir,
logs_dir,
test_config,
):
# Logbook warnings are ignored so we don't have to fork logbook to support python 3.10.
# This _only_ works for tests in `tests/` that use the project fixture.
Expand All @@ -345,8 +354,8 @@ def project(
shared_data_dir=shared_data_dir,
test_data_dir=test_data_dir,
test_schema=unique_schema,
# the following feels kind of fragile. TODO: better way of getting database
database=profiles_yml["test"]["outputs"]["default"]["dbname"],
database=adapter.config.credentials.database,
test_config=test_config,
)
project.run_sql("drop schema if exists {schema} cascade")
project.run_sql("create schema {schema}")
Expand Down
Loading

0 comments on commit 5e0a765

Please sign in to comment.