dbt-core icon indicating copy to clipboard operation
dbt-core copied to clipboard

[CT-3111] [Tech debt] Create setup fixture for setting flags in unit tests

Open MichelleArk opened this issue 1 year ago • 1 comments

Housekeeping

  • [X] I am a maintainer of dbt-core

Short description

Introduce a common mechanism for updating the tests and ensure that all of the unit tests can be run individually via pytest fixtures.

Acceptance criteria

  • Introduce a pytest set fixture that calls set_from_args appropriately in unit tests. This fixture should reset the global flags object as part of fixture teardown
  • Refactor existing unit test classes or methods to use this fixture instead of calling set_from_args in various patterns

Impact to Other Teams

No

Will backports be required?

No

Context

  • Split out as part of https://github.com/dbt-labs/dbt-core/issues/8091
  • Reference documentation: https://docs.pytest.org/en/latest/how-to/fixtures.html#teardown-cleanup-aka-fixture-finalization
  • For inspiration, an example of how something similar is done for env-vars in cloud:
EMP_ENV_VARS = {}
ENV_VARS_TO_SUSPEND = ["DBT_CLOUD_PUBLICATIONS_DIR", "DBT_CLOUD_PUBLICATION_FILE_PATH"]

@pytest.fixture(scope="session", autouse=True)
def tests_setup_and_teardown():
    # Will be executed before the first test
    old_environ = dict(os.environ)
    os.environ.update(TEMP_ENV_VARS)
    for env_var in ENV_VARS_TO_SUSPEND:
        os.environ.pop(env_var, default=None)

    yield
    # Will be executed after the last test
    os.environ.clear()
    os.environ.update(old_environ)

MichelleArk avatar Sep 12 '23 15:09 MichelleArk

Lowering priority of this given manual fixes to several unit tests affected by this issue: https://github.com/dbt-labs/dbt-core/pull/9897

MichelleArk avatar Apr 16 '24 18:04 MichelleArk

@QMalcolm If you're open to it, I'd like to explore how we can combine acheive this by moving the global flags collection into InvocationContext. This seems like an even better place to start than EventManager.

peterallenwebb avatar May 16 '24 21:05 peterallenwebb