-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathconftest.py
171 lines (130 loc) · 4.19 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
from dataclasses import dataclass
from test.factory import models as model_factories
from test.factory.models.media import CREATED_BY_FIXTURE_MARKER, MediaFactory
from unittest.mock import MagicMock
from rest_framework.test import APIClient, APIRequestFactory
import pook
import pytest
import pytest_asyncio
from elasticsearch import Elasticsearch
from fakeredis import FakeRedis
from api.serializers.audio_serializers import (
AudioSearchRequestSerializer,
AudioSerializer,
)
from api.serializers.image_serializers import (
ImageSearchRequestSerializer,
ImageSerializer,
)
from api.serializers.media_serializers import (
MediaSearchRequestSerializer,
MediaSerializer,
)
from api.utils import aiohttp
@pytest.fixture()
def redis(monkeypatch) -> FakeRedis:
fake_redis = FakeRedis()
def get_redis_connection(*args, **kwargs):
return fake_redis
monkeypatch.setattr("django_redis.get_redis_connection", get_redis_connection)
yield fake_redis
fake_redis.client().close()
@pytest.fixture
def api_client():
return APIClient()
@pytest.fixture(autouse=True)
def capture_exception(monkeypatch):
mock = MagicMock()
monkeypatch.setattr("sentry_sdk.capture_exception", mock)
yield mock
@pytest.fixture
def request_factory() -> APIRequestFactory():
request_factory = APIRequestFactory(defaults={"REMOTE_ADDR": "192.0.2.1"})
return request_factory
@dataclass
class MediaTypeConfig:
media_type: str
url_prefix: str
origin_index: str
filtered_index: str
model_factory: MediaFactory
mature_factory: MediaFactory
search_request_serializer: MediaSearchRequestSerializer
model_serializer: MediaSerializer
MEDIA_TYPE_CONFIGS = {
"image": MediaTypeConfig(
media_type="image",
url_prefix="images",
origin_index="image",
filtered_index="image-filtered",
model_factory=model_factories.ImageFactory,
mature_factory=model_factories.MatureImageFactory,
search_request_serializer=ImageSearchRequestSerializer,
model_serializer=ImageSerializer,
),
"audio": MediaTypeConfig(
media_type="audio",
url_prefix="audio",
origin_index="audio",
filtered_index="audio-filtered",
model_factory=model_factories.AudioFactory,
mature_factory=model_factories.MatureAudioFactory,
search_request_serializer=AudioSearchRequestSerializer,
model_serializer=AudioSerializer,
),
}
@pytest.fixture
def image_media_type_config():
return MEDIA_TYPE_CONFIGS["image"]
@pytest.fixture
def audio_media_type_config():
return MEDIA_TYPE_CONFIGS["audio"]
@pytest.fixture(
params=MEDIA_TYPE_CONFIGS.values(),
ids=lambda x: f"{x.media_type}_media_type_config",
)
def media_type_config(request: pytest.FixtureRequest) -> MediaTypeConfig:
return request.param
@pytest.fixture(autouse=True)
def cleanup_elasticsearch_test_documents(request, settings):
yield None
# This fixture only matters after tests are finished
if not request.node.get_closest_marker("django_db"):
# If the test isn't configured to access the database
# then it couldn't have created any new documents,
# so we can skip cleanup
return
es: Elasticsearch = settings.ES
es.delete_by_query(
index="*",
body={"query": {"match": {"tags.name": CREATED_BY_FIXTURE_MARKER}}},
refresh=True,
)
@pytest_asyncio.fixture(autouse=True)
async def cleanup_aiohttp_session():
yield None
if aiohttp._SESSION and not aiohttp._SESSION.closed:
await aiohttp._SESSION.close()
@pytest.fixture
def pook_on():
"""
Safely turn pook on and off for a test.
pytest-asyncio marks mess with the `pook.on`
decorator, so this is a workaround that prevents
individual tests needing to safely handle clean up.
"""
pook.on()
yield
pook.off()
@pytest.fixture
def pook_off():
"""
Turn pook off after a test.
Similar to ``pook_on`` above.
Useful to ensure pook is turned off after a test
if you need to manually turn pook on (for example,
to avoid it capturing requests you actually do
want to send, e.g., to Elasticsearch).
"""
yield
pook.off()