Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for customization of the environment in which test fixtures are… #9894

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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