-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
101 lines (74 loc) · 3.48 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
import builtins
import hashlib
from unittest.mock import patch
import pytest
from harp.utils.testing.databases import TEST_DATABASES
builtins.__pytest__ = True
DISABLED_APPLICATIONS_FOR_TESTS = ("telemetry", "harp_apps.telemetry")
@pytest.fixture(scope="session", autouse=True)
def default_session_fixture():
from harp.config import Config
DEFAULT_APPLICATIONS_FOR_TESTS = list(Config.DEFAULT_APPLICATIONS)
for app in DISABLED_APPLICATIONS_FOR_TESTS:
if app in DEFAULT_APPLICATIONS_FOR_TESTS:
DEFAULT_APPLICATIONS_FOR_TESTS.remove(app)
with patch("harp.config.Config.DEFAULT_APPLICATIONS", DEFAULT_APPLICATIONS_FOR_TESTS):
yield
# see https://github.com/GrahamDumpleton/wrapt/issues/257
@pytest.fixture(autouse=True, scope="session")
def patch_wrapt_for_pycharm():
from wrapt import FunctionWrapper, decorators
from wrapt.decorators import AdapterWrapper, _AdapterFunctionSurrogate
class _PatchedAdapterFunctionSurrogate(_AdapterFunctionSurrogate):
@property
def __class__(self):
try:
return super().__class__
except ValueError:
return type(self)
class PatchedAdapterWrapper(AdapterWrapper):
def __init__(self, *args, **kwargs):
adapter = kwargs.pop("adapter")
FunctionWrapper.__init__(self, *args, **kwargs)
self._self_surrogate = _PatchedAdapterFunctionSurrogate(self.__wrapped__, adapter)
self._self_adapter = adapter
@property
def __class__(self):
try:
return super().__class__
except ValueError:
return type(self)
with pytest.MonkeyPatch.context() as patch:
patch.setattr(decorators, "AdapterWrapper", PatchedAdapterWrapper)
yield
@pytest.fixture(scope="session", params=TEST_DATABASES)
def database_url(request):
dialect, image, driver = request.param.split("|")
yield from create_database_container_for(dialect, image, driver)
def create_database_container_for(dialect, image, driver):
if dialect == "sqlite":
yield f"{dialect}+{driver}:///:memory:"
elif dialect == "postgresql":
from testcontainers.postgres import PostgresContainer
with PostgresContainer(image) as container:
yield container.get_connection_url().replace("postgresql+psycopg2://", f"{dialect}+{driver}://")
elif dialect == "mysql":
from testcontainers.mysql import MySqlContainer
with MySqlContainer(image) as container:
yield container.get_connection_url().replace("mysql+pymysql://", f"{dialect}+{driver}://")
elif dialect == "mssql":
from testcontainers.mssql import SqlServerContainer
with SqlServerContainer() as container:
yield container.get_connection_url().replace("mssql+pymssql://", f"{dialect}+{driver}://")
else:
raise ValueError(f"Unsupported or invalid dialect: {dialect!r}")
@pytest.fixture(scope="session")
def httpbin():
from testcontainers.core.container import DockerContainer
from harp.utils.network import wait_for_port
with DockerContainer("mccutchen/go-httpbin:v2.13.2").with_exposed_ports(8080) as container:
wait_for_port(int(container.get_exposed_port(8080)), container.get_container_host_ip())
yield f"http://{container.get_container_host_ip()}:{container.get_exposed_port(8080)}"
@pytest.fixture
def test_id(request):
return hashlib.md5(str(request.node.nodeid).encode("utf-8")).hexdigest()