From 760e2055df6b509853393b6711edad6c285a8cfa Mon Sep 17 00:00:00 2001 From: Peter Allen Webb Date: Thu, 11 Apr 2024 13:20:22 -0400 Subject: [PATCH] Allow for customization of the environment in which test fixtures are initialized. --- core/dbt/tests/fixtures/project.py | 13 +++++++++++-- tests/functional/partial_parsing/test_pp_vars.py | 9 +++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/core/dbt/tests/fixtures/project.py b/core/dbt/tests/fixtures/project.py index 3ef25c55789..252f11116c1 100644 --- a/core/dbt/tests/fixtures/project.py +++ b/core/dbt/tests/fixtures/project.py @@ -1,5 +1,7 @@ import os from pathlib import Path +from typing import Mapping + import pytest # type: ignore import random from argparse import Namespace @@ -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() diff --git a/tests/functional/partial_parsing/test_pp_vars.py b/tests/functional/partial_parsing/test_pp_vars.py index a01e78c6458..f92655fb6e9 100644 --- a/tests/functional/partial_parsing/test_pp_vars.py +++ b/tests/functional/partial_parsing/test_pp_vars.py @@ -308,14 +308,19 @@ 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_USER"] = "root" + custom_env["ENV_VAR_PASS"] = "password" + 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_USER"] = "root" - os.environ["ENV_VAR_PASS"] = "password" return { "type": "postgres", "threads": 4,