-
Notifications
You must be signed in to change notification settings - Fork 5
/
conftest.py
124 lines (107 loc) · 3.39 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
import pytest
from mock import patch
from hypothesis import settings, Verbosity
import os
import crm.models
from django.test import override_settings
from rest_framework.test import APITestCase, APIClient
from django.contrib.auth.models import User
import subprocess
from geocodable.api import DummyAdaptor
from birdisle.redis import StrictRedis
settings.register_profile("ci", max_examples=300)
settings.register_profile("dev", max_examples=10)
settings.register_profile("debug", max_examples=10, verbosity=Verbosity.verbose)
settings.load_profile(os.getenv('HYPOTHESIS_PROFILE', 'default'))
DUMMY_CACHE_CONFIG = {
'CACHES': {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache'
}
},
}
SYNC_QUEUE_CONFIG = {
'RQ_QUEUES': {
'default': {
'USE_REDIS_CACHE': 'default',
'ASYNC': False
}
}
}
DUMMY_SERVER_CONFIG = {
'CACHES': {
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': 'redis://localhost:6379/0'
}
},
}
@pytest.fixture
def default_personstate():
return crm.models.PersonState.objects.get_or_create(name='Default')[0]
@pytest.fixture(scope="session")
def redis_server(request):
proc = subprocess.Popen('redis-server')
override = override_settings(**DUMMY_SERVER_CONFIG)
override.enable()
request.addfinalizer(proc.kill)
request.addfinalizer(override.disable)
@pytest.fixture
def sync_rq(request):
override = override_settings(**SYNC_QUEUE_CONFIG)
override.enable()
request.addfinalizer(override.stop)
return None
@pytest.fixture
def mock_redis(request):
patched = patch('django_rq.queues.get_redis_connection')
override = override_settings(**DUMMY_CACHE_CONFIG)
override.enable()
mock = patched.start()
mock.reset_mock()
mock.return_value = StrictRedis()
request.addfinalizer(patched.stop)
request.addfinalizer(override.disable)
return mock.return_value
@pytest.fixture
def test_user():
return User.objects.create(email='[email protected]')
@pytest.fixture
def api_client():
return APIClient()
@pytest.fixture(autouse=True)
def _mock_skipped_auth(request):
marker = request.node.get_closest_marker('skip_auth')
if marker:
patched = patch('rest_framework.views.APIView.check_permissions')
patched.start()
request.addfinalizer(patched.stop)
@pytest.fixture(autouse=True)
def _disable_ssl(request):
override = override_settings(SECURE_SSL_REDIRECT=False)
override.enable()
request.addfinalizer(override.disable)
return None
@pytest.fixture(autouse=True)
def _mock_redis_markers(request):
marker = request.node.get_closest_marker('mock_redis')
if marker:
request.getfixturevalue("mock_redis")
marker = request.node.get_closest_marker('redis_server')
if marker:
request.getfixturevalue("redis_server")
marker = request.node.get_closest_marker('sync_rq')
if marker:
request.getfixturevalue("sync_rq")
@pytest.fixture
def dummy_geocoder(request):
patched = patch('geocodable.api.get_adaptor')
mock = patched.start()
mock.return_value = DummyAdaptor()
request.addfinalizer(patched.stop)
return mock.return_value
@pytest.fixture(autouse=True)
def _mock_geocoder(request):
marker = request.node.get_closest_marker('mock_geocoder')
if marker:
request.getfixturevalue('dummy_geocoder')