-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathconftest.py
181 lines (136 loc) · 4.99 KB
/
conftest.py
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
import io
import os
from contextlib import contextmanager, nullcontext
from http.client import HTTPMessage
from logging import getLogger
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import mock
import pytest
import yaml
from pytest import fixture
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from cumulusci.core.dependencies.utils import TaskContext
from cumulusci.core.github import get_github_api
from cumulusci.salesforce_api.org_schema_models import Base
from cumulusci.tasks.bulkdata.tests.integration_test_utils import (
ensure_accounts,
ensure_records,
)
from cumulusci.tasks.salesforce.tests.util import create_task_fixture
from cumulusci.tests.pytest_plugins.pytest_sf_vcr import salesforce_vcr, vcr_config
from cumulusci.tests.util import DummyKeychain, DummyOrgConfig, mock_env
ensure_accounts = ensure_accounts
ensure_records = ensure_records
@fixture(scope="session", autouse=True)
def mock_sleep():
"""Patch time.sleep to avoid delays in unit tests"""
with mock.patch("time.sleep"):
yield
class MockHttpResponse(mock.Mock):
def __init__(self, status):
super(MockHttpResponse, self).__init__()
self.status = status
self.strict = 0
self.version = 0
self.reason = None
self.msg = HTTPMessage(io.BytesIO())
self.closed = True
def read(self): # pragma: no cover
return b""
def isclosed(self):
return self.closed
@fixture
def gh_api():
return get_github_api("TestOwner", "TestRepo")
@fixture(scope="class", autouse=True)
def restore_cwd():
d = os.getcwd()
try:
yield
finally:
os.chdir(d)
@fixture
def mock_http_response():
def _make_response(status):
return MockHttpResponse(status)
return _make_response
@fixture(scope="session")
def fallback_org_config():
def fallback_org_config():
return DummyOrgConfig(
name="pytest_sf_orgconnect_dummy_org_config", keychain=DummyKeychain()
)
return fallback_org_config
vcr_config = fixture(vcr_config, scope="module")
vcr = fixture(salesforce_vcr, scope="module")
create_task_fixture = fixture(create_task_fixture, scope="function")
# TODO: This should also chdir to a temp directory which
# can represent the repo-root, but that will require
# test case changes.
@pytest.fixture(autouse=True)
def patch_home_and_env(request):
"Patch the default home directory and $HOME environment for all tests at once."
use_real_env = request.node.get_closest_marker("use_real_env")
with TemporaryDirectory(prefix="fake_home_") as home:
if use_real_env:
mock_env_cm = nullcontext()
else:
mock_env_cm = mock_env(home)
with mock_env_cm:
Path(home, ".cumulusci").mkdir()
Path(home, ".cumulusci/cumulusci.yml").touch()
yield
@pytest.fixture()
def temp_db():
with TemporaryDirectory() as t:
@contextmanager
def open_db():
engine = create_engine(f"sqlite:///{t}/tempfile.db")
with engine.connect() as connection:
Session = sessionmaker(bind=connection)
Base.metadata.bind = engine
Base.metadata.create_all()
session = Session()
yield connection, Base.metadata, session
yield open_db
@pytest.fixture()
def delete_data_from_org(create_task):
def delete_data_from_org(object_names):
from cumulusci.tasks.bulkdata.delete import DeleteData
t = create_task(DeleteData, {"objects": object_names})
assert t.org_config.scratch
t()
return delete_data_from_org
@pytest.fixture(scope="session")
def cumulusci_test_repo_root():
return Path(__file__).parent.parent
@pytest.fixture(scope="session")
def global_describe(cumulusci_test_repo_root):
global_describe_file = (
cumulusci_test_repo_root
/ "cumulusci/tests/shared_cassettes/GET_sobjects_Global_describe.yaml"
)
with global_describe_file.open() as f:
data = yaml.safe_load(yaml.safe_load(f)["response"]["body"]["string"])
def global_describe_specific_sobjects(sobjects: int = None):
if sobjects is None: # pragma: no cover
subset = data.copy()
elif isinstance(sobjects, int):
subset = data.copy()
subset["sobjects"] = subset["sobjects"][0:sobjects]
elif isinstance(sobjects, (list, tuple)): # pragma: no cover
raise NotImplementedError(
"We could implement a by-name subsetting here when we need it."
)
return subset
return global_describe_specific_sobjects
@pytest.fixture(scope="session")
def shared_vcr_cassettes(cumulusci_test_repo_root):
return Path(cumulusci_test_repo_root / "cumulusci/tests/shared_cassettes")
@pytest.fixture
def task_context(org_config, project_config):
return TaskContext(
org_config=org_config, project_config=project_config, logger=getLogger()
)