Skip to content

Commit

Permalink
add very basic pytest that doesnt touch dbt
Browse files Browse the repository at this point in the history
  • Loading branch information
emmyoop committed Jun 9, 2024
1 parent 59bd935 commit 9684f6d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 103 deletions.
99 changes: 2 additions & 97 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,98 +1,3 @@
import pytest
import os
from pathlib import Path

pytest_plugins = ["dbt.tests.fixtures.project"]


def pytest_addoption(parser):
parser.addoption("--profile", action="store", default="postgres", type=str)


# Using @pytest.mark.skip_profile('postgres') uses the 'skip_by_profile_type'
# autouse fixture below
def pytest_configure(config):
config.addinivalue_line(
"markers",
"skip_profile(profile): skip test for the given profile",
)
config.addinivalue_line(
"markers",
"only_profile(profile): only test the given profile",
)


@pytest.fixture(scope="session")
def dbt_profile_target(request):
profile_type = request.config.getoption("--profile")
if profile_type == "postgres":
target = postgres_target()
elif profile_type == "redshift":
target = redshift_target()
elif profile_type == "snowflake":
target = snowflake_target()
elif profile_type == "bigquery":
target = bigquery_target()
else:
raise ValueError(f"Invalid profile type '{profile_type}'")
return target


def postgres_target():
return {
"type": "postgres",
"host": os.getenv('POSTGRES_TEST_HOST'),
"user": os.getenv('POSTGRES_TEST_USER'),
"pass": os.getenv('POSTGRES_TEST_PASS'),
"port": int(os.getenv('POSTGRES_TEST_PORT')),
"dbname": os.getenv('POSTGRES_TEST_DBNAME'),
}


def redshift_target():
return {
"type": "redshift",
"host": os.getenv('REDSHIFT_TEST_HOST'),
"user": os.getenv('REDSHIFT_TEST_USER'),
"pass": os.getenv('REDSHIFT_TEST_PASS'),
"port": int(os.getenv('REDSHIFT_TEST_PORT')),
"dbname": os.getenv('REDSHIFT_TEST_DBNAME'),
}


def bigquery_target():
return {
"type": "bigquery",
"method": "service-account",
"keyfile": os.getenv('BIGQUERY_SERVICE_KEY_PATH'),
"project": os.getenv('BIGQUERY_TEST_DATABASE'),
}


def snowflake_target():
return {
"type": "snowflake",
"account": os.getenv('SNOWFLAKE_TEST_ACCOUNT'),
"user": os.getenv('SNOWFLAKE_TEST_USER'),
"password": os.getenv('SNOWFLAKE_TEST_PASSWORD'),
"role": os.getenv('SNOWFLAKE_TEST_ROLE'),
"database": os.getenv('SNOWFLAKE_TEST_DATABASE'),
"warehouse": os.getenv('SNOWFLAKE_TEST_WAREHOUSE'),
}


@pytest.fixture(autouse=True)
def skip_by_profile_type(request):
profile_type = request.config.getoption("--profile")
if request.node.get_closest_marker("skip_profile"):
for skip_profile_type in request.node.get_closest_marker("skip_profile").args:
if skip_profile_type == profile_type:
pytest.skip("skipped on '{profile_type}' profile")


@pytest.fixture(autouse=True)
def only_profile_type(request):
profile_type = request.config.getoption("--profile")
if request.node.get_closest_marker("only_profile"):
for only_profile_type in request.node.get_closest_marker("only_profile").args:
if only_profile_type != profile_type:
pytest.skip("skipped on '{profile_type}' profile")
DBT_PROJECT_PATH = Path(__file__).parent.parent / "dbt_project"
7 changes: 7 additions & 0 deletions tests/functional/test_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


# def test_failure():
# assert 1 == 5

def test_pass():
assert 1 == 1
24 changes: 18 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,31 @@ commands =
dbt deps --target snowflake
dbt build --target snowflake --full-refresh


# Postgres integration tests for centralized dbt testing
# run dbt commands directly, assumes dbt is already installed in environment
# tox runs at the root of the repo, so we need to cd out of the integration_tests directory
[testenv:centralized_integration_postgres]
changedir = integration_tests
allowlist_externals = dbt
allowlist_externals =
dbt
bash
{[testenv:centralized_integration_postgres_pytest]allowlist_externals}
skip_install = true
deps = {[testenv:centralized_integration_postgres_pytest]deps}
commands =
dbt --version
dbt debug --target postgres
dbt deps --target postgres
dbt build --target postgres --full-refresh
bash -c "cd {toxinidir} && {[testenv:centralized_integration_postgres_pytest]commands}"

# Postgres integration tests for centralized dbt testing
# Uses pytest to run tests
; [testenv:dbt_integration_postgres]
; deps = pytest
; commands =
; pytest {posargs} tests/functional
# Uses pytest to run tests, assumes dbt is already installed in environment
[testenv:centralized_integration_postgres_pytest]
changedir = {toxinidir}
allowlist_externals = pytest
deps = pytest
skip_install = true
commands =
pytest {posargs} tests/functional

0 comments on commit 9684f6d

Please sign in to comment.