Skip to content

Commit bae9265

Browse files
committed
improve pytest setup
1 parent bf6b0a1 commit bae9265

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

tests/conftest.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
import pytest
2-
from flask import Flask, g
2+
from flask import Flask, current_app, g
33
from flask.testing import FlaskClient
44

55
from teufa import db as dbm
66
from teufa.app import create_app
77
from teufa.ext import db
88

99

10-
@pytest.fixture
11-
def app():
10+
def pytest_configure():
1211
app = create_app()
13-
with app.app_context():
14-
dbm.Base.metadata.create_all(db.engine)
12+
app.app_context().push()
13+
14+
15+
def create_default_tenant():
16+
tenant = dbm.Tenant(
17+
name="Default",
18+
hostname="localhost",
19+
)
20+
db.session.add(tenant)
21+
db.session.commit()
1522

16-
tenant = dbm.Tenant(
17-
name="Default",
18-
hostname="localhost",
19-
)
20-
db.session.add(tenant)
21-
db.session.commit()
22-
g.tenant = tenant
23+
g.tenant = tenant
24+
25+
26+
@pytest.fixture
27+
def app():
28+
dbm.Base.metadata.create_all(db.engine)
2329

24-
yield app
30+
with current_app.app_context():
31+
create_default_tenant()
32+
yield current_app
2533

26-
dbm.Base.metadata.drop_all(db.engine)
34+
dbm.Base.metadata.drop_all(db.engine)
2735

2836

2937
@pytest.fixture

teufa/app.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ def create_app():
1515

1616
@app.before_request
1717
def before_request():
18-
if not hasattr(g, "tenant"):
19-
hostname = request.host.split(":")[0]
20-
g.tenant = db.session.scalars(
21-
select(dbm.Tenant).filter_by(hostname=hostname).limit(1)
22-
).first()
18+
hostname = request.host.split(":")[0]
19+
g.tenant = db.session.scalars(
20+
select(dbm.Tenant).filter_by(hostname=hostname).limit(1)
21+
).first()
2322

2423
@app.teardown_request
2524
def teardown_request(exc):

0 commit comments

Comments
 (0)