Skip to content

Commit

Permalink
Add schema teardown method where necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
damian3031 committed Aug 1, 2023
1 parent b29236f commit b7752fe
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Under the Hood-20230801-125319.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Under the Hood
body: Add schema teardown method where necessary
time: 2023-08-01T12:53:19.864514+02:00
custom:
Author: damian3031
Issue: "267"
PR: "335"
38 changes: 37 additions & 1 deletion tests/functional/adapter/dbt_clone/test_dbt_clone.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
import pytest
from dbt.tests.adapter.dbt_clone.fixtures import (
custom_can_clone_tables_false_macros_sql,
get_schema_name_sql,
infinite_macros_sql,
macros_sql,
)
from dbt.tests.adapter.dbt_clone.test_dbt_clone import BaseCloneNotPossible

iceberg_macro_override_sql = """
{% macro trino__current_timestamp() -%}
current_timestamp(6)
{%- endmacro %}
"""


class TestTrinoCloneNotPossible(BaseCloneNotPossible):
pass
@pytest.fixture(scope="class")
def macros(self):
return {
"macros.sql": macros_sql,
"my_can_clone_tables.sql": custom_can_clone_tables_false_macros_sql,
"infinite_macros.sql": infinite_macros_sql,
"get_schema_name.sql": get_schema_name_sql,
"iceberg.sql": iceberg_macro_override_sql,
}

# TODO: below method probably should be implemented in base class (on dbt-core side)
@pytest.fixture(autouse=True)
def clean_up(self, project):
yield
with project.adapter.connection_named("__test"):
relation = project.adapter.Relation.create(
database=project.database, schema=f"{project.test_schema}_seeds"
)
project.adapter.drop_schema(relation)

relation = project.adapter.Relation.create(
database=project.database, schema=project.test_schema
)
project.adapter.drop_schema(relation)
13 changes: 13 additions & 0 deletions tests/functional/adapter/dbt_debug/test_dbt_debug.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from dbt.tests.adapter.dbt_debug.test_dbt_debug import (
BaseDebug,
BaseDebugProfileVariable,
Expand All @@ -6,12 +7,24 @@


class TestDebugTrino(BaseDebug):
# TODO: below teardown method probably should be implemented in base class (on dbt-core side)
@pytest.fixture(scope="function", autouse=True)
def teardown_method(self, project):
yield
project.run_sql(f"drop schema if exists {project.test_schema}")

def test_ok_trino(self, project):
run_dbt(["debug"])
assert "ERROR" not in self.capsys.readouterr().out


class TestDebugProfileVariableTrino(BaseDebugProfileVariable):
# TODO: below teardown method probably should be implemented in base class (on dbt-core side)
@pytest.fixture(scope="function", autouse=True)
def teardown_method(self, project):
yield
project.run_sql(f"drop schema if exists {project.test_schema}")

def test_ok_trino(self, project):
run_dbt(["debug"])
assert "ERROR" not in self.capsys.readouterr().out
29 changes: 24 additions & 5 deletions tests/functional/adapter/materialization/test_materialized_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,27 @@
)


# TODO: teardown_method is needed to properly remove relations and schemas after tests.
# It could be refactored and simplified when CASCADE will be supported in Iceberg, delta, hive connectors
@pytest.mark.iceberg
class TestIcebergMaterializedViewExists:
class TestIcebergMaterializedViewBase:
@pytest.fixture(scope="function", autouse=True)
def teardown_method(self, project):
yield
# Drop materialized views first, then drop schema
sql = "select * from system.metadata.materialized_views"
results = run_sql_with_adapter(project.adapter, sql, fetch="all")
for mv in results:
project.run_sql(f"drop materialized view {mv[0]}.{mv[1]}.{mv[2]}")

relation = project.adapter.Relation.create(
database=project.database, schema=project.test_schema
)
project.adapter.drop_schema(relation)


@pytest.mark.iceberg
class TestIcebergMaterializedViewExists(TestIcebergMaterializedViewBase):
@pytest.fixture(scope="class")
def project_config_update(self):
return {
Expand Down Expand Up @@ -54,7 +73,7 @@ def test_mv_is_dropped_when_model_runs_view(self, project):


@pytest.mark.iceberg
class TestIcebergMaterializedViewWithCTE:
class TestIcebergMaterializedViewWithCTE(TestIcebergMaterializedViewBase):
# Configuration in dbt_project.yml
@pytest.fixture(scope="class")
def project_config_update(self):
Expand Down Expand Up @@ -89,7 +108,7 @@ def test_mv_with_cte_is_created(self, project):


@pytest.mark.iceberg
class TestIcebergMaterializedViewCreate:
class TestIcebergMaterializedViewCreate(TestIcebergMaterializedViewBase):
# Configuration in dbt_project.yml
@pytest.fixture(scope="class")
def project_config_update(self):
Expand Down Expand Up @@ -148,7 +167,7 @@ def test_mv_is_created_and_refreshed(self, project):


@pytest.mark.iceberg
class TestIcebergMaterializedViewDropAndCreate:
class TestIcebergMaterializedViewDropAndCreate(TestIcebergMaterializedViewBase):
# Configuration in dbt_project.yml
@pytest.fixture(scope="class")
def project_config_update(self):
Expand Down Expand Up @@ -216,7 +235,7 @@ def test_mv_overrides_relation(self, project):

@pytest.mark.iceberg
@pytest.mark.skip_profile("starburst_galaxy")
class TestIcebergMaterializedViewProperties:
class TestIcebergMaterializedViewProperties(TestIcebergMaterializedViewBase):
# Configuration in dbt_project.yml
@pytest.fixture(scope="class")
def project_config_update(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ def models(self):
"table_store_failures.yml": table_profile_yml,
}

@pytest.fixture(autouse=True)
def teardown_method(self, project):
yield
with project.adapter.connection_named("__test"):
relation = project.adapter.Relation.create(
database=project.database, schema=f"{project.test_schema}_dbt_test__audit"
)
project.adapter.drop_schema(relation)

def test_run_seed_test(self, project):
# seed seeds
results = run_dbt(["seed"], expect_pass=True)
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/adapter/test_custom_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ def models(self):
)
}

@pytest.fixture(scope="function", autouse=True)
def teardown_method(self, project):
yield
relation = project.adapter.Relation.create(
database=project.database, schema=f"{project.test_schema}_{self.custom_schema_name}"
)
project.adapter.drop_schema(relation)

def test_custom_schema_trino(self, project):
# Seed seeds, run models.
results = run_dbt(["seed"], expect_pass=True)
Expand Down

0 comments on commit b7752fe

Please sign in to comment.