|
| 1 | +import pytest |
| 2 | +import os |
| 3 | + |
| 4 | +pytest_plugins = ["dbt.tests.fixtures.project"] |
| 5 | + |
| 6 | + |
| 7 | +def pytest_addoption(parser): |
| 8 | + parser.addoption("--profile", action="store", default="postgres", type=str) |
| 9 | + |
| 10 | + |
| 11 | +# Using @pytest.mark.skip_profile('postgres') uses the 'skip_by_profile_type' |
| 12 | +# autouse fixture below |
| 13 | +def pytest_configure(config): |
| 14 | + config.addinivalue_line( |
| 15 | + "markers", |
| 16 | + "skip_profile(profile): skip test for the given profile", |
| 17 | + ) |
| 18 | + config.addinivalue_line( |
| 19 | + "markers", |
| 20 | + "only_profile(profile): only test the given profile", |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="session") |
| 25 | +def dbt_profile_target(request): |
| 26 | + profile_type = request.config.getoption("--profile") |
| 27 | + if profile_type == "postgres": |
| 28 | + target = postgres_target() |
| 29 | + elif profile_type == "redshift": |
| 30 | + target = redshift_target() |
| 31 | + elif profile_type == "snowflake": |
| 32 | + target = snowflake_target() |
| 33 | + elif profile_type == "bigquery": |
| 34 | + target = bigquery_target() |
| 35 | + else: |
| 36 | + raise ValueError(f"Invalid profile type '{profile_type}'") |
| 37 | + return target |
| 38 | + |
| 39 | + |
| 40 | +def postgres_target(): |
| 41 | + return { |
| 42 | + "type": "postgres", |
| 43 | + "host": os.getenv('POSTGRES_TEST_HOST'), |
| 44 | + "user": os.getenv('POSTGRES_TEST_USER'), |
| 45 | + "pass": os.getenv('POSTGRES_TEST_PASS'), |
| 46 | + "port": int(os.getenv('POSTGRES_TEST_PORT')), |
| 47 | + "dbname": os.getenv('POSTGRES_TEST_DBNAME'), |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | +def redshift_target(): |
| 52 | + return { |
| 53 | + "type": "redshift", |
| 54 | + "host": os.getenv('REDSHIFT_TEST_HOST'), |
| 55 | + "user": os.getenv('REDSHIFT_TEST_USER'), |
| 56 | + "pass": os.getenv('REDSHIFT_TEST_PASS'), |
| 57 | + "port": int(os.getenv('REDSHIFT_TEST_PORT')), |
| 58 | + "dbname": os.getenv('REDSHIFT_TEST_DBNAME'), |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | +def bigquery_target(): |
| 63 | + return { |
| 64 | + "type": "bigquery", |
| 65 | + "method": "service-account", |
| 66 | + "keyfile": os.getenv('BIGQUERY_SERVICE_KEY_PATH'), |
| 67 | + "project": os.getenv('BIGQUERY_TEST_DATABASE'), |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | +def snowflake_target(): |
| 72 | + return { |
| 73 | + "type": "snowflake", |
| 74 | + "account": os.getenv('SNOWFLAKE_TEST_ACCOUNT'), |
| 75 | + "user": os.getenv('SNOWFLAKE_TEST_USER'), |
| 76 | + "password": os.getenv('SNOWFLAKE_TEST_PASSWORD'), |
| 77 | + "role": os.getenv('SNOWFLAKE_TEST_ROLE'), |
| 78 | + "database": os.getenv('SNOWFLAKE_TEST_DATABASE'), |
| 79 | + "warehouse": os.getenv('SNOWFLAKE_TEST_WAREHOUSE'), |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +@pytest.fixture(autouse=True) |
| 84 | +def skip_by_profile_type(request): |
| 85 | + profile_type = request.config.getoption("--profile") |
| 86 | + if request.node.get_closest_marker("skip_profile"): |
| 87 | + for skip_profile_type in request.node.get_closest_marker("skip_profile").args: |
| 88 | + if skip_profile_type == profile_type: |
| 89 | + pytest.skip("skipped on '{profile_type}' profile") |
| 90 | + |
| 91 | + |
| 92 | +@pytest.fixture(autouse=True) |
| 93 | +def only_profile_type(request): |
| 94 | + profile_type = request.config.getoption("--profile") |
| 95 | + if request.node.get_closest_marker("only_profile"): |
| 96 | + for only_profile_type in request.node.get_closest_marker("only_profile").args: |
| 97 | + if only_profile_type != profile_type: |
| 98 | + pytest.skip("skipped on '{profile_type}' profile") |
0 commit comments