This repository has been archived by the owner on Oct 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconftest.py
157 lines (113 loc) · 3.83 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
import json
import pytest
from flask import url_for
from flask.testing import FlaskClient
from ban import db
from ban.commands.db import create as createdb
from ban.commands.db import truncate as truncatedb
from ban.commands.db import models
from ban.commands.reporter import Reporter
from ban.core import context
from ban.http.api import app as application
from ban.tests.factories import SessionFactory, TokenFactory, UserFactory
def pytest_configure(config):
db.database.prefix = 'test_'
db.database.connect()
createdb(fail_silently=True)
verbose = config.getoption('verbose')
if verbose:
import logging
logging.basicConfig(level=logging.DEBUG)
def pytest_unconfigure(config):
db.database.drop_tables(models)
db.database.close()
db.cache.clear()
def pytest_runtest_setup(item):
assert db.database.database.startswith('test_')
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(user__is_staff=True)
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 post(client):
return client.post
@pytest.fixture
def patch(client):
return client.patch
@pytest.fixture
def put(client):
return client.put
class Client(FlaskClient):
def open(self, *args, **kwargs):
if len(args) == 2:
# Be smart, allow to pass data as arg, even if EnvironBuilder want
# it as kwarg only.
kwargs['data'] = args[1]
args = args[0],
# Allow to define headers and content_type before opening the request.
kwargs.setdefault('headers', {})
kwargs['headers'].update(getattr(self, 'extra_headers', {}))
if hasattr(self, 'content_type') and not kwargs.get('content_type'):
kwargs['content_type'] = self.content_type
if kwargs.get('content_type') == 'application/json':
if 'data' in kwargs:
kwargs['data'] = json.dumps(kwargs['data'])
return super().open(*args, **kwargs)
application.test_client_class = Client
@pytest.fixture()
def url():
def _(endpoint, **kwargs):
if 'id' in kwargs:
# Allow to create "id:value" identifiers from kwargs.
kwargs['identifier'] = '{identifier}:{id}'.format(**kwargs)
del kwargs['id']
if not isinstance(endpoint, str): # Passing the resource.
endpoint = endpoint.endpoint
uri = url_for(endpoint, **kwargs)
uri = 'http://localhost{}'.format(uri)
return uri
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.clear()
return MonkeyPatchWrapper(monkeypatch, ban_config)
@pytest.fixture
def reporter():
reporter_ = Reporter(2)
context.set('reporter', reporter_)
return reporter_
@pytest.fixture
def sql_spy(mocker):
from playhouse.postgres_ext import PostgresqlExtDatabase
return mocker.spy(PostgresqlExtDatabase, 'execute_sql')