-
Notifications
You must be signed in to change notification settings - Fork 73
/
conftest.py
177 lines (143 loc) · 4.35 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
172
173
174
175
176
177
import os
from datetime import datetime, timedelta
import flask_migrate
import pytest
from sqlalchemy.orm import scoped_session, sessionmaker
from newdle.core.app import create_app
from newdle.core.db import db
from newdle.models import Newdle, Participant
TEST_DATABASE_URI = os.environ.get(
'NEWDLE_TEST_DATABASE_URI', 'postgresql:///newdle_tests'
)
@pytest.fixture(scope='session')
def app():
"""Session-wide test `Flask` application."""
config_override = {
'TESTING': True,
'SQLALCHEMY_DATABASE_URI': TEST_DATABASE_URI,
'SERVER_NAME': 'flask.test',
'SECRET_KEY': 'test',
'EMAIL_BACKEND': 'newdle.vendor.django_mail.backends.locmem.EmailBackend',
}
app = create_app(config_override, use_env_config=False)
ctx = app.app_context()
ctx.push()
yield app
ctx.pop()
@pytest.fixture(scope='session')
def database(app):
flask_migrate.upgrade(revision='head')
yield
flask_migrate.downgrade(revision='base')
@pytest.fixture
def db_session(app, database):
"""Create a new database session."""
connection = db.engine.connect()
transaction = connection.begin()
options = {'bind': connection, 'binds': {}}
session_factory = sessionmaker(**options)
session = scoped_session(session_factory)
db.session = session
db.app = app
yield session
transaction.rollback()
connection.close()
session.remove()
@pytest.fixture
def mail_queue():
from newdle.vendor import django_mail
django_mail.outbox = []
return django_mail.outbox
@pytest.fixture
def flask_client(app):
return app.test_client()
@pytest.fixture
def cli_runner(app):
return app.test_cli_runner()
@pytest.fixture
def dummy_uid():
return 'user123'
@pytest.fixture
def dummy_participant_uid():
return 'pig'
@pytest.fixture
def create_newdle(dummy_uid, db_session):
"""Return a callable which lets you create dummy newdles."""
def _create_newdle(id=None, **kwargs):
kwargs.setdefault(
'participants',
{
Participant(
code=f'part1{id}' if id is not None else 'part1',
name='Guinea Pig',
email='[email protected]',
auth_uid='pig',
),
},
)
kwargs.setdefault(
'timeslots',
[
datetime(2019, 9, 11, 13, 0),
datetime(2019, 9, 11, 14, 0),
datetime(2019, 9, 12, 13, 0),
datetime(2019, 9, 12, 13, 30),
],
)
kwargs.setdefault('code', f'dummy#{id}' if id is not None else 'dummy')
kwargs.setdefault(
'title', f'Test event {id}' if id is not None else 'Test event'
)
kwargs.setdefault('creator_name', 'Dummy')
kwargs.setdefault('duration', timedelta(minutes=60))
kwargs.setdefault('private', True)
kwargs.setdefault('timezone', 'Europe/Zurich')
newdle = Newdle(
id=id,
creator_uid=dummy_uid,
**kwargs,
)
db_session.add(newdle)
db_session.flush()
return newdle
return _create_newdle
@pytest.fixture
def override_config(app):
"""Return a callable which lets you override app config variables."""
orig_config = dict(app.config)
def _override_config(**kwargs):
app.config.update(**kwargs)
try:
yield _override_config
finally:
app.config.update(orig_config)
@pytest.fixture
def dummy_newdle(db_session, dummy_uid):
newdle = Newdle(
code='dummy',
title='Test event',
creator_uid=dummy_uid,
creator_name='Dummy',
duration=timedelta(minutes=60),
private=True,
timezone='Europe/Zurich',
timeslots=[
datetime(2019, 9, 11, 13, 0),
datetime(2019, 9, 11, 14, 0),
datetime(2019, 9, 12, 13, 0),
datetime(2019, 9, 12, 13, 30),
],
participants={
Participant(code='part1', name='Tony Stark'),
Participant(code='part2', name='Albert Einstein'),
Participant(
code='part3',
name='Guinea Pig',
email='[email protected]',
auth_uid='pig',
),
},
)
db_session.add(newdle)
db_session.flush()
return newdle