Skip to content

Commit

Permalink
Allow for customization of the environment in which test fixtures are…
Browse files Browse the repository at this point in the history
… initialized. (#9894)
  • Loading branch information
peterallenwebb authored Apr 11, 2024
1 parent a1f0057 commit c86cec3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
13 changes: 11 additions & 2 deletions core/dbt/tests/fixtures/project.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
from pathlib import Path
from typing import Mapping

import pytest # type: ignore
import random
from argparse import Namespace
Expand Down Expand Up @@ -493,11 +495,18 @@ def get_tables_in_schema(self):
return {model_name: materialization for (model_name, materialization) in result}


@pytest.fixture(scope="class")
def environment() -> Mapping[str, str]:
# By default, fixture initialization is done with the following environment
# from the os, but this fixture provides a way to customize the environment.
return os.environ


# Housekeeping that needs to be done before we start setting up any test fixtures.
@pytest.fixture(scope="class")
def initialization() -> None:
def initialization(environment) -> None:
# Create an "invocation context," which dbt application code relies on.
set_invocation_context(os.environ)
set_invocation_context(environment)

# Enable caches used between test runs, for better testing performance.
enable_test_caching()
Expand Down
19 changes: 13 additions & 6 deletions tests/functional/partial_parsing/test_pp_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,16 @@ def test_env_vars_models(self, project):


class TestProjectEnvVars:
@pytest.fixture(scope="class")
def environment(self):
custom_env = os.environ.copy()
custom_env["ENV_VAR_NAME"] = "Jane Smith"
return custom_env

@pytest.fixture(scope="class")
def project_config_update(self):
# Need to set the environment variable here initially because
# the project fixture loads the config.
os.environ["ENV_VAR_NAME"] = "Jane Smith"
return {"models": {"+meta": {"meta_name": "{{ env_var('ENV_VAR_NAME') }}"}}}

@pytest.fixture(scope="class")
Expand All @@ -279,6 +284,7 @@ def models(self):

def test_project_env_vars(self, project):
# Initial run
os.environ["ENV_VAR_NAME"] = "Jane Smith"
results = run_dbt(["run"])
assert len(results) == 1
manifest = get_manifest(project.project_root)
Expand Down Expand Up @@ -308,13 +314,14 @@ def models(self):
"model_one.sql": model_one_sql,
}

@pytest.fixture(scope="class")
def environment(self):
custom_env = os.environ.copy()
custom_env["ENV_VAR_HOST"] = "localhost"
return custom_env

@pytest.fixture(scope="class")
def dbt_profile_target(self):
# Need to set these here because the base integration test class
# calls 'load_config' before the tests are run.
# Note: only the specified profile is rendered, so there's no
# point it setting env_vars in non-used profiles.
os.environ["ENV_VAR_HOST"] = "localhost"
return {
"type": "postgres",
"threads": 4,
Expand Down

0 comments on commit c86cec3

Please sign in to comment.