-
Notifications
You must be signed in to change notification settings - Fork 4
/
conftest.py
68 lines (53 loc) · 1.89 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
import pytest
from extra_checks.checks.self_checks import CheckConfig
from extra_checks.registry import Registry
@pytest.fixture
def registry() -> Registry:
registry = Registry()
registry._register(["extra_checks_selfcheck"], CheckConfig)
return registry
class TestCase:
TEST_TAG = "__test_case_check__"
def __init__(self, settings, monkeypatch, registry):
self._settings = settings
self._monkeypatch = monkeypatch
self._registry = registry
def settings(self, settings):
self._settings.EXTRA_CHECKS = settings
return self
def check(self, *checks):
for check in checks:
self._registry._register([self.TEST_TAG], check)
return self
def handler(self, handler):
self._registry._add_handler(self.TEST_TAG, handler)
return self
def run(self):
self._registry.enabled_checks = {}
handlers = self._registry.bind()
assert (
self._registry.is_healthy
), f"Settings has errors: {self._registry._config.errors.as_text()}"
return list(handlers[self.TEST_TAG]())
def models(self, *models):
self._monkeypatch.setattr(
"extra_checks.checks.model_checks._get_models_to_check",
lambda *a, **kw: (m for m in models),
)
return self
def serializers(self, *serializers):
import rest_framework.serializers
n, m = [], []
for s in serializers:
if issubclass(s, rest_framework.serializers.ModelSerializer):
m.append(s)
else:
n.append(s)
self._monkeypatch.setattr(
"extra_checks.checks.drf_serializer_checks._get_serializers_to_check",
lambda *a, **kw: (n, m),
)
return self
@pytest.fixture
def test_case(settings, monkeypatch, registry):
return TestCase(settings, monkeypatch, registry)