Skip to content

Commit

Permalink
Fix test.
Browse files Browse the repository at this point in the history
  • Loading branch information
VersusFacit committed May 26, 2024
1 parent cea0294 commit 80a9141
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions tests/unit/test_lazy_load_agate.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 80a9141

Please sign in to comment.