-
Notifications
You must be signed in to change notification settings - Fork 8
/
conftest.py
309 lines (236 loc) · 9.12 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import datetime
import os
import uuid
from typing import Any, AsyncGenerator, Callable, Generator, cast
import nest_asyncio
import pytest
import taskiq_fastapi
from alembic import command
from alembic.config import Config
from fastapi import FastAPI
from pytest import Parser
from pytest_asyncio import is_async_test
from pytest_mock import MockerFixture
from sqlalchemy import event
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine, AsyncSession
from sqlalchemy.orm import Session, SessionTransaction
from apps.answers.deps.preprocess_arbitrary import get_answer_session, get_answer_session_by_subject
from apps.mailing.services import TestMail
from apps.shared.test.client import TestClient
from broker import broker
from config import settings
from infrastructure.app import create_app
from infrastructure.database.core import build_engine
from infrastructure.database.deps import get_session
from infrastructure.utility import FCMNotificationTest, RedisCacheTest
pytest_plugins = [
"apps.activities.tests.fixtures.configs",
"apps.activities.tests.fixtures.response_values",
"apps.activities.tests.fixtures.items",
"apps.activities.tests.fixtures.conditional_logic",
"apps.activities.tests.fixtures.scores_reports",
"apps.activities.tests.fixtures.activities",
"apps.users.tests.fixtures.users",
"apps.applets.tests.fixtures.applets",
"apps.users.tests.fixtures.user_devices",
]
# Fix for issue https://github.com/pytest-dev/pytest-asyncio/issues/112
nest_asyncio.apply()
@pytest.fixture(scope="session")
async def global_engine():
engine = build_engine(settings.database.url)
yield engine
await engine.dispose()
@pytest.fixture(scope="session")
async def global_session(global_engine: AsyncEngine):
"""
Global session is used to create pre-defined objects in database for ALL pytest session.
Inside tests and for local/intermediate fixtures please use session fixture.
"""
async with AsyncSession(bind=global_engine) as session:
yield session
# TODO: Instead of custom faketime for tests add function wrapper `now`
# to use it instead of builtin datetime.datetime.utcnow
class FakeTime(datetime.datetime):
current_utc = datetime.datetime(2024, 1, 1, 0, 0, 0)
@classmethod
def utcnow(cls):
return cls.current_utc
alembic_configs = [Config("alembic.ini"), Config("alembic_arbitrary.ini")]
def pytest_addoption(parser: Parser) -> None:
parser.addoption(
"--keepdb",
action="store_true",
default=False,
help="If keepdb is true, then migrations wont be downgraded after tests", # noqa: E501
)
def before():
os.environ["PYTEST_APP_TESTING"] = "1"
for alembic_cfg in alembic_configs:
command.upgrade(alembic_cfg, "head")
def after():
for alembic_cfg in alembic_configs[::-1]:
command.downgrade(alembic_cfg, "base")
os.environ.pop("PYTEST_APP_TESTING", None)
def pytest_sessionstart(session) -> None:
before()
@pytest.hookimpl(trylast=True)
def pytest_sessionfinish(session, exitstatus) -> None:
# Don't run downgrade migrations
keepdb = session.config.getvalue("keepdb")
if not keepdb:
after()
@pytest.fixture(scope="session")
def app() -> FastAPI:
return create_app()
@pytest.fixture(scope="session")
def arbitrary_db_url() -> str:
host = settings.database.host
return f"postgresql+asyncpg://postgres:postgres@{host}:5432/test_arbitrary"
@pytest.fixture()
async def engine() -> AsyncGenerator[AsyncEngine, Any]:
engine = build_engine(settings.database.url)
yield engine
await engine.dispose()
@pytest.fixture()
async def arbitrary_engine(
arbitrary_db_url: str,
) -> AsyncGenerator[AsyncEngine, Any]:
engine = build_engine(arbitrary_db_url)
yield engine
await engine.dispose()
@pytest.fixture
async def session(engine: AsyncEngine) -> AsyncGenerator:
async with engine.begin() as conn:
conn = cast(AsyncConnection, conn)
await conn.begin_nested()
async with AsyncSession(bind=conn) as async_session:
@event.listens_for(async_session.sync_session, "after_transaction_end")
def end_savepoint(session: Session, transaction: SessionTransaction) -> None:
nonlocal conn
conn = cast(AsyncConnection, conn)
if conn.closed:
return
if not conn.in_nested_transaction():
if conn.sync_connection:
conn.sync_connection.begin_nested()
yield async_session
await conn.rollback()
@pytest.fixture
async def arbitrary_session(arbitrary_engine: AsyncEngine) -> AsyncGenerator:
async with arbitrary_engine.begin() as conn:
conn = cast(AsyncConnection, conn)
await conn.begin_nested()
async with AsyncSession(bind=conn) as async_session:
@event.listens_for(async_session.sync_session, "after_transaction_end")
def end_savepoint(session: Session, transaction: SessionTransaction) -> None:
if conn.closed:
return
if not conn.in_nested_transaction():
if conn.sync_connection:
conn.sync_connection.begin_nested()
yield async_session
await conn.rollback()
@pytest.fixture
def client(session: AsyncSession, app: FastAPI) -> TestClient:
app.dependency_overrides[get_session] = lambda: session
taskiq_fastapi.populate_dependency_context(broker, app)
client = TestClient(app)
return client
@pytest.fixture
def arbitrary_client(
app: FastAPI, session: AsyncSession, arbitrary_session: AsyncSession
) -> Generator[TestClient, None, None]:
"""Use only for tests which interact with arbitrary servers, because
arbitrary (answers) session has higher prioritet then general session.
"""
app.dependency_overrides[get_session] = lambda: session
app.dependency_overrides[get_answer_session] = lambda: arbitrary_session
app.dependency_overrides[get_answer_session_by_subject] = lambda: arbitrary_session
taskiq_fastapi.populate_dependency_context(broker, app)
client = TestClient(app)
yield client
app.dependency_overrides.pop(get_answer_session_by_subject)
app.dependency_overrides.pop(get_answer_session)
def pytest_collection_modifyitems(items) -> None:
pytest_asyncio_tests = (item for item in items if is_async_test(item))
session_scope_marker = pytest.mark.asyncio(scope="session")
for async_test in pytest_asyncio_tests:
async_test.add_marker(session_scope_marker)
@pytest.fixture
def local_image_name() -> str:
return "test.jpg"
@pytest.fixture
def remote_image(local_image_name: str) -> str:
# TODO: add support for localimages for tests
return f"http://localhost/{local_image_name}"
@pytest.fixture
async def mock_kiq_report(mocker) -> AsyncGenerator[Any, Any]:
mock = mocker.patch("apps.answers.service.create_report.kiq")
yield mock
@pytest.fixture
async def mock_report_server_response(mocker) -> AsyncGenerator[Any, Any]:
Recipients = list[str]
FakeBody = dict[str, str | dict[str, str | Recipients]]
def json_() -> FakeBody:
return dict(
pdf="cGRmIGJvZHk=",
email=dict(
body="Body",
subject="Subject",
attachment="Attachment name",
emailRecipients=["[email protected]"],
),
)
mock = mocker.patch("aiohttp.ClientSession.post")
mock.return_value.__aenter__.return_value.status = 200
mock.return_value.__aenter__.return_value.json.side_effect = json_
yield mock
@pytest.fixture
async def mock_reencrypt_kiq(mocker) -> AsyncGenerator[Any, Any]:
mock = mocker.patch("apps.users.api.password.reencrypt_answers.kiq")
yield mock
@pytest.fixture(scope="session")
def uuid_zero() -> uuid.UUID:
return uuid.UUID("00000000-0000-0000-0000-000000000000")
@pytest.fixture
def faketime(mocker: MockerFixture) -> type[FakeTime]:
mock = mocker.patch("datetime.datetime", new=FakeTime)
return mock
@pytest.fixture
def mock_get_session(
session: AsyncSession,
arbitrary_session: AsyncSession,
mocker: MockerFixture,
arbitrary_db_url: str,
) -> Callable[..., Callable[[], AsyncSession]]:
# Add stub for first argument, because orig get_session takes instanace as first argument after mock
def get_session(_, url: str = settings.database.url) -> Callable[[], AsyncSession]:
def f() -> AsyncSession:
if url == arbitrary_db_url:
return arbitrary_session
return session
return f
mock = mocker.patch(
"infrastructure.database.core.SessionManager.get_session",
new=get_session,
)
return mock
@pytest.fixture
def fcm_client() -> FCMNotificationTest:
client = FCMNotificationTest()
client.notifications.clear()
return client
@pytest.fixture
def redis() -> RedisCacheTest:
redis = RedisCacheTest()
redis._storage.clear()
return redis
@pytest.fixture
def mailbox() -> TestMail:
class Connection:
pass
connection = Connection()
box = TestMail(connection)
box.clear_mails()
return box