diff --git a/tests/unit/test_lazy_load_agate.py b/tests/unit/test_lazy_load_agate.py index 98f26ee2a..0d9822f38 100644 --- a/tests/unit/test_lazy_load_agate.py +++ b/tests/unit/test_lazy_load_agate.py @@ -1,37 +1,31 @@ import pytest import sys import importlib +import dbt +import copy -@pytest.fixture -def remove_agate_from_path(): - """conftest and other envs load agate modules upon initilization so we need - to remove their presence from module tracking to assess correctness of direct imports""" +def test_lazy_loading_agate(): + """If agate is imported directly here or in any of the subsequent files, this test will fail. Also test that our assumptions about imports affecting sys modules hold. - original_sys_path = sys.modules.copy() + Because other tests use Agate, we must be extra careful with state changes. + """ # import ahead of time to avoid reimporting agate upon package initialization - import dbt.adapters.snowflake.__init__ - modules_to_remove = [m for m in sys.modules if "agate" in m] - for m in modules_to_remove: - del sys.modules[m] + original_modules = copy.copy(sys.modules) + try: + agate_modules = [m for m in sys.modules if "agate" in m] + for m in agate_modules: + del sys.modules[m] - yield - sys.path = original_sys_path + import dbt.adapters.snowflake.__init__ + importlib.reload(dbt.adapters.snowflake.connections) + importlib.reload(dbt.adapters.snowflake.impl) + importlib.reload(dbt.adapters.snowflake.relation_configs.base) + importlib.reload(dbt.adapters.snowflake.relation_configs.dynamic_table) + assert not any([module_name for module_name in sys.modules if "agate" in module_name]) -def test_lazy_loading_agate(remove_agate_from_path): - """If agate is imported directly here or in any of the subsequent files, this test will fail. Also test that our assumptions about imports affecting sys modules hold.""" - - import dbt - - importlib.reload(dbt.adapters.snowflake.connections) - importlib.reload(dbt.adapters.snowflake.impl) - importlib.reload(dbt.adapters.snowflake.relation_configs.base) - importlib.reload(dbt.adapters.snowflake.relation_configs.dynamic_table) - assert not any([module_name for module_name in sys.modules if "agate" in module_name]) - - import agate - - assert any([module_name for module_name in sys.modules if "agate" in module_name]) + finally: + sys.modules = original_modules