-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathconftest.py
More file actions
190 lines (151 loc) · 5.89 KB
/
conftest.py
File metadata and controls
190 lines (151 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import shutil
from pathlib import Path
from tempfile import mkdtemp
from typing import Optional
import pytest
import yaml
from dbt.version import __version__ as dbt_version
from dbt_project import PYTEST_XDIST_WORKER, SCHEMA_NAME_SUFFIX, DbtProject
from elementary.clients.dbt.factory import RunnerMethod
from env import Environment
from logger import get_logger
from packaging import version
DBT_PROJECT_PATH = Path(__file__).parent.parent / "dbt_project"
DBT_FUSION_SUPPORTED_TARGETS = [
"snowflake",
"bigquery",
"redshift",
"databricks_catalog",
]
logger = get_logger(__name__)
def pytest_addoption(parser):
parser.addoption("--target", action="store", default="postgres")
parser.addoption("--skip-init", action="store_true", default=False)
parser.addoption("--clear-on-end", action="store_true", default=False)
parser.addoption("--runner-method", action="store", default=None)
@pytest.fixture(scope="session")
def project_dir_copy(runner_method: Optional[RunnerMethod]):
dbt_project_copy_dir = mkdtemp(prefix="integration_tests_project_")
try:
shutil.copytree(
DBT_PROJECT_PATH,
dbt_project_copy_dir,
dirs_exist_ok=True,
symlinks=True,
)
_edit_packages_yml_to_include_absolute_elementary_package_path(
dbt_project_copy_dir
)
_remove_python_models_for_dbt_fusion(dbt_project_copy_dir, runner_method)
yield dbt_project_copy_dir
finally:
shutil.rmtree(dbt_project_copy_dir)
def _edit_packages_yml_to_include_absolute_elementary_package_path(
project_dir_copy: str,
):
logger.info(
f"Editing packages.yml to include absolute elementary package path for project {project_dir_copy}"
)
packages_yml_path = Path(project_dir_copy) / "packages.yml"
with packages_yml_path.open("r") as packages_yml_file:
packages_yml = yaml.safe_load(packages_yml_file)
packages_yml["packages"][0]["local"] = str(
(DBT_PROJECT_PATH / packages_yml["packages"][0]["local"]).resolve()
)
with packages_yml_path.open("w") as packages_yml_file:
yaml.dump(packages_yml, packages_yml_file)
def _remove_python_models_for_dbt_fusion(
project_dir_copy: str, runner_method: Optional[RunnerMethod]
):
if runner_method != RunnerMethod.FUSION:
return
logger.info(f"Removing python tests for project {project_dir_copy}")
# walk on the models dir and delete python files
for path in (Path(project_dir_copy) / "models").rglob("*.py"):
path.unlink()
@pytest.fixture(scope="session", autouse=True)
def init_tests_env(
target: str,
skip_init: bool,
clear_on_end: bool,
project_dir_copy: str,
runner_method: Optional[RunnerMethod],
):
env = Environment(target, project_dir_copy, runner_method)
if not skip_init:
logger.info(
"Initializing test environment (worker=%s, schema_suffix='%s')",
PYTEST_XDIST_WORKER or "main",
SCHEMA_NAME_SUFFIX,
)
env.clear()
env.init()
logger.info(
"Initialization complete (worker=%s)",
PYTEST_XDIST_WORKER or "main",
)
yield
if clear_on_end:
logger.info("Clearing tests environment")
env.clear()
logger.info("Clearing complete")
@pytest.fixture(autouse=True)
def skip_by_targets(request, target: str):
if request.node.get_closest_marker("skip_targets"):
skipped_targets = request.node.get_closest_marker("skip_targets").args[0]
if target in skipped_targets:
pytest.skip("Test unsupported for target: {}".format(target))
@pytest.fixture(autouse=True)
def only_on_targets(request, target: str):
if request.node.get_closest_marker("only_on_targets"):
requested_targets = request.node.get_closest_marker("only_on_targets").args[0]
if target not in requested_targets:
pytest.skip("Test unsupported for target: {}".format(target))
@pytest.fixture(autouse=True)
def skip_for_dbt_fusion(request, runner_method: Optional[RunnerMethod]):
if request.node.get_closest_marker("skip_for_dbt_fusion"):
if runner_method == RunnerMethod.FUSION:
pytest.skip("Test unsupported for dbt fusion")
@pytest.fixture(autouse=True)
def requires_dbt_version(request):
if request.node.get_closest_marker("requires_dbt_version"):
required_version = request.node.get_closest_marker("requires_dbt_version").args[
0
]
if version.parse(dbt_version) < version.parse(required_version):
pytest.skip(
"Test requires dbt version {} or above, but {} is installed.".format(
required_version, dbt_version
)
)
@pytest.fixture
def dbt_project(
target: str, project_dir_copy: str, runner_method: Optional[RunnerMethod]
) -> DbtProject:
return DbtProject(target, project_dir_copy, runner_method)
@pytest.fixture(scope="session")
def target(request) -> str:
return request.config.getoption("--target")
@pytest.fixture(scope="session")
def skip_init(request) -> bool:
return request.config.getoption("--skip-init")
@pytest.fixture(scope="session")
def clear_on_end(request) -> bool:
return request.config.getoption("--clear-on-end")
@pytest.fixture(scope="session")
def runner_method(request, target: str) -> Optional[RunnerMethod]:
runner_method_str = request.config.getoption("--runner-method")
if runner_method_str:
runner_method = RunnerMethod(runner_method_str)
if (
runner_method == RunnerMethod.FUSION
and target not in DBT_FUSION_SUPPORTED_TARGETS
):
raise ValueError(f"Fusion runner is not supported for target: {target}")
return runner_method
return None
@pytest.fixture
def test_id(request) -> str:
if request.cls:
return f"{request.cls.__name__}_{request.node.name}"
return request.node.name