forked from ban-archive/api-gestion-poc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
97 lines (68 loc) · 2.17 KB
/
Copy pathconftest.py
File metadata and controls
97 lines (68 loc) · 2.17 KB
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
from urllib.parse import urlencode
import pytest
from ban.tests.factories import UserFactory, TokenFactory, SessionFactory
from ban import db
from ban.commands.db import models, create as createdb, truncate as truncatedb
from ban.core import context
from ban.http import application, reverse
def pytest_configure(config):
assert db.test.database.startswith('test_')
db.test.connect()
for model in models:
model._meta.database = db.test
createdb(fail_silently=True)
verbose = config.getoption('verbose')
if verbose:
import logging
logging.basicConfig(level=logging.DEBUG)
def pytest_unconfigure(config):
db.test.drop_tables(models)
db.test.close()
def pytest_runtest_setup(item):
truncatedb(force=True)
context.set('session', None)
@pytest.fixture()
def user():
return UserFactory()
@pytest.fixture()
def staff():
return UserFactory(is_staff=True)
@pytest.fixture()
def session():
session = SessionFactory()
context.set('session', session)
return session
@pytest.fixture()
def token():
return TokenFactory()
@pytest.fixture
def app():
return application
@pytest.fixture
def get(client):
return client.get
@pytest.fixture()
def url():
def _(class_, query_string=None, **kwargs):
url = reverse(class_, **kwargs)
if query_string:
url = '{}?{}'.format(url, urlencode(query_string))
return url
return _
class MonkeyPatchWrapper(object):
def __init__(self, monkeypatch, wrapped_object):
super().__setattr__('monkeypatch', monkeypatch)
super().__setattr__('wrapped_object', wrapped_object)
def __getattr__(self, attr):
return getattr(self.wrapped_object, attr)
def __setattr__(self, attr, value):
self.monkeypatch.setattr(self.wrapped_object, attr, value,
raising=False)
def __delattr__(self, attr):
self.monkeypatch.delattr(self.wrapped_object, attr)
@pytest.fixture()
def config(request, monkeypatch):
from ban.core import config as ban_config
# Make sure config cache is empty.
ban_config.cache.clear()
return MonkeyPatchWrapper(monkeypatch, ban_config)